{"iq":[{"id":"1",
"q":"What is the most important feature of Java?",
"answer":"Java is a platform independent language."
},
{"id":"2",
"q":"What do you mean by platform independence? ",
"answer":"Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc). "
},
{"id":"3",
"q":"What is a JVM?",
"answer":"JVM is Java Virtual Machine which is a run time environment for the compiled java class files."
},
{"id":"4",
"q":"What is the difference between a JDK and a JVM?",
"answer":"JDK is Java Development Kit which is for development purpose and it includes execution environment also.But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM"
},
{"id":"5",
"q":"What is the base class of all classes? ",
"answer":"java.lang.Object"
},
{"id":"6",
"q":"Is Java a pure object oriented language? ",
"answer":"Java uses primitive data types and hence is not a pure object oriented language "
},
{"id":"7",
"q":"what is oops ?",
"answer":"oops approach is a programming methodology to design computer programs using classes and objects "
},
{"id":"8",
"q":"what are the main principles of oops .?",
"answer":"1. Inheritance\n2.polymorphism\n3.Encapusulation\n4. Abstraction"
},
{"id":"9",
"q":"What are memory areas allocated in JVM?",
"answer":"Memory areas allocated in JVM are:\nHeap area\nMethod area\nJVM language stacks\nProgram counter (PC) register\nNative method stacks"
},
{"id":"10",
"q":"What is Abstraction?",
"answer":"Abstraction is achieved using interface and abstract class in Java."
},
{"id":"11",
"q":"What is encapsulation?",
"answer":"Encapsulation in java is the process of binding related data(variables) and functionality(methods) into a single unit called class.\nEncapsulation can be achieved by using access modifier such as public, private, protected or default."
},
{"id":"12",
"q":"What is Polymorphism in java?",
"answer":"Polymorphism means one name many forms. In Java, polymorphism can be achieved by method overloading and method overriding.\nThere are two types of polymorphism in java.\nCompile time polymorphism.\nRun time polymorphism."
},
{"id":"13",
"q":"What is Compile time polymorphism .?",
"answer":"Compile time Polymorphism is nothing but method overloading in java. You can define various methods with same name but different method arguments"
},
{"id":"14",
"q":"What is run time polymorphism.?",
"answer":"Runtime Polymorphism is nothing but method overriding in java.\nIf subclass is having same method as base class then it is known as method overriding Or in another word,\nIf subclass provides specific implementation to any methodwhich is present in its one of parents classes then it is known as method overriding."
},
{"id":"15",
"q":"What is inheritance in java? ",
"answer":"Inheritance allows to inherit properties and methods of parent class, so you can reuse all methods and properties. "
},
{"id":"16",
"q":"What is constructor in java? ",
"answer":"Constructor can be considered a special code which is used to initiaze objects.\nIt has two main points\n\nClass and Constuctor name should match\nConstructor should not have any return type else it will be same as method. "
},
{"id":"17",
"q":"Can we declare constructor as final?",
"answer":"No, Constructor can not be declared as final. If you do so, you will get compile time error. "
},
{"id":"18",
"q":"What is immutable object in java? ",
"answer":"Immutable object is object whose state can not be changed once created. You can take String object as example for immutable object."
},
{"id":"19",
"q":"Why String is declared final or immutable in java? ",
"answer":"There are various reasons to make String immutable.\nString pool\nThread Safe\nSecurity\nClass Loading\nCache hash value "
},
{"id":"20",
"q":"What are access modifier available in java?",
"answer":"It Specifies accessibility of variables, methods , constructor of class.\nThere are four access modifier in java\nPrivate : Accessible only to the class.\nDefault : Accessible in the package.\nProtected : Accessible in the packages and its subclasses.\nPublic : Accessible everywhere "
},
{"id":"21",
"q":"What are local variables?",
"answer":" Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them. "
},
{"id":"22",
"q":"What are instance variables? ",
"answer":" Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values. "
},
{"id":"23",
"q":"How to define a constant variable in Java?",
"answer":" The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also.\nstatic final int MAX_LENGTH = 50; is an example for constant."
},{"id":"24",
"q":"Should a main() method be compulsorily declared in all java classes?",
"answer":"No not required. main() method should be defined only if the source class is a java application."
},{"id":"25",
"q":"What is the return type of the main() method? ",
"answer":"Main() method doesn't return anything hence declared void."
},{"id":"26",
"q":"Why is the main() method declared static?",
"answer":"main() method is called by the JVM even before the instantiation of the class hence it is declared as static."
},{"id":"27",
"q":"What is the arguement of main() method?",
"answer":"main() method accepts an array of String object as arguement."
},{"id":"28",
"q":"Can we make array volatile in Java? ",
"answer":"This is one of the tricky Java multi-threading questions you will see in senior Java developer Interview.\nYes, you can make an array volatile in Java but only the reference which is pointing to an array, not the whole array. What I mean, if one thread changes the reference variable to points to another array,that will provide a volatile guarantee, but if multiple threads are changing individual array elementsthey won't be having happens before guarantee provided by the volatile modifier."
},{"id":"29",
"q":"What is the use of synchronized keyword? ",
"answer":"synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method other threads have to wait until current thread finishes the execution and release lock. Synchronized keyword provides a lock on the object and thus prevents race condition."
},{"id":"30",
"q":"What is the difference between an Inner Class and a Sub-Class?",
"answer":"An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.\nA sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class."
},{"id":"31",
"q":"What are the various access specifiers for Java classes? ",
"answer":"In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:\n1. Public : Class,Method,Field is accessible from anywhere.\n2. Protected:Method,Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package,but not from outside.\n3. Default: Method,Field,class can be accessed only from the same package and not from outside of it’s native package.\n4. Private: Method,Field can be accessed from the same class to which they belong.\n"
},{"id":"32",
"q":"What’s the purpose of Static methods and static variables?",
"answer":"When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects. "
},{"id":"33",
"q":"What are Loops in Java? What are three types of loops?",
"answer":"Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:\n1) For Loops\nFor loops are used in java to execute statements repeatedly for a given number of times. For loops are used when number of times to execute the statements is known to programmer.\n2) While Loops\nWhile loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, condition is checked first before execution of statements.\n3) Do While Loops\nDo While Loop is same as While loop with only difference that condition is checked after execution of block of statements. Hence in case of do while loop, statements are executed at least once. "
},{"id":"34",
"q":"What is a singleton class? Give a practical example of its usage.",
"answer":"A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for a class.\nThe best example of singleton usage scenario is when there is a limit of having only one connection to a database due to some driver limitations or because of any licensing issues."},
{"id":"35",
"q":"What is an infinite Loop? How infinite loop is declared?",
"answer":"An infinite loop runs without any condition and runs infinitely. An infinite loop can be broken by defining any breaking logic in the body of the statement blocks.\nInfinite loop is declared as follows:\nJava\nfor (;;)\n{\n}\nfor (;;\n{\n}"
},{"id":"36",
"q":"What is the difference between continue and break statement?",
"answer":"break and continue are two important keywords used in Loops. When a break keyword is used in a loop, loop is broken instantly while when continue keyword is used, current iteration is broken and loop continues with next iteration.\nIn below example, Loop is broken when counter reaches.\nfor (counter=0;counter<10;counter++)\nsystem.out.println(counter);\nif (counter==4) {\nbreak;}\n}\n\nfor (counter=0;counter<10;counter++)\nsystem.out.println(counter);\nif (counter==4) {\nbreak;}}"
},{"id":"37",
"q":"What is the difference between double and float variables in Java?",
"answer":"In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number."
},{"id":"38",
"q":"What is Final Keyword in Java? Give an example.",
"answer":"In java, a constant is declared using the keyword Final. Value can be assigned only once and after assignment, value of a constant can’t be changed.\nIn below example, a constant with the name const_val is declared and assigned avalue:\nPrivate Final int const_val=100\nWhen a method is declared as final,it can NOT be overridden by the subclasses.This method are faster than any other method,because they are resolved at complied time.\nWhen a class is declares as final,it cannot be subclassed. Example String,Integer and other wrapper classes."
},{"id":"39",
"q":"What is ternary operator? Give an example. ",
"answer":"Ternary operator , also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It’s denoted as ?\n\nIn the below example, if rank is 1, status is assigned a value of “Done” else “Pending”.\n\nJava\npublic class conditionTest {\npublic static void main(String args[]) {\nString status;\nint rank = 3;\nstatus = (rank == 1) ? \"Done\" : \"Pending\";\nSystem.out.println(status);\n}\n}\n\npublic class conditionTest {\npublic static void main(String args[]) {\nString status;\nint rank = 3;\nstatus = (rank == 1) ? \"Done\" : \"Pending\";\nSystem.out.println(status);\n}\n}"
},{"id":"40",
"q":"How can you generate random numbers in Java? ",
"answer":"Using Math.random() you can generate random numbers in the range 0.1 to 1.0\nUsing Random class in package java.util"
},{"id":"41",
"q":"What are Java Packages? What’s the significance of packages? ",
"answer":"In Java, package is a collection of classes and interfaces which are bundled together as they are related to each other. Use of packages helps developers to modularize the code and group the code for proper re-use. Once code has been packaged in Packages, it can be imported in other classes and used."
},{"id":"42",
"q":"Can we declare a class as Abstract without having any abstract method? ",
"answer":"Yes we can create an abstract class by using abstract keyword before class name even if it doesn’t have any abstract method. However, if a class has even one abstract method, it must be declared as abstract otherwise it will give an error."
},{"id":"43",
"q":"Can we declare the main method of our class as private?",
"answer":"In java, main method must be public static in order to run any application correctly. If main method is declared as private, developer won’t get any compilation error however, it will not get executed and will give a runtime error"
},{"id":"44",
"q":"How can we pass argument to a function by reference instead of pass by value?",
"answer":"In java, we can pass argument to a function only by value and not by reference."
},{"id":"45",
"q":"How an object is serialized in java? ",
"answer":"In java, to convert an object into byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing serializable interface get serialized and their state is saved in byte stream."
},{"id":"46",
"q":"When we should use serialization?",
"answer":"Serialization is used when data needs to be transmitted over the network. Using serialization, object’s state is saved and converted into byte stream .The byte stream is transferred over the network and the object is re-created at destination. "
},{"id":"47",
"q":"Is it compulsory for a Try Block to be followed by a Catch Block in Java for Exception handling?",
"answer":"Try block needs to be followed by either Catch block or Finally block or both. Any exception thrown from try block needs to be either caught in the catch block or else any specific tasks to be performed before code abortion are put in the Finally block. "
},{"id":"48",
"q":"When the constructor of a class is invoked?",
"answer":"The constructor of a class is invoked every time an object is created with new keyword.\n\nFor example, in the following class two objects are created using new keyword and hence, constructor is invoked two times.\n\nJava\npublic class const_example {\nconst_example() {\nsystem.out.println(\"Inside constructor\");\n}\n\npublic static void main(String args[]) {\nconst_example c1=new const_example();\nconst_example c2=new const_example();\n}\n}\npublic class const_example {\nconst_example() {\nsystem.out.println(\"Inside constructor\");\n}\npublic static void main(String args[]) {\nconst_example c1=new const_example();\nconst_example c2=new const_example();\n}\n} "
},{"id":"49",
"q":"Can a class have multiple constructors?",
"answer":"Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects. "
},{"id":"50",
"q":"Can we override static methods of a class? ",
"answer":"We cannot override static methods. Static methods belong to a class and not to individual objects and are resolved at the time of compilation (not at runtime).Even if we try to override static method,we will not get an complitaion error,nor the impact of overriding when running the code."
},{"id":"51",
"q":"Is String a data type in java? ",
"answer":"String is not a primitive data type in java. When a string is created in java, it’s actually an object of Java.Lang.String class that gets created. After creation of this string object, all built-in methods of String class can be used on the string object. "
},
{"id":"52",
"q":"What’s the difference between an array and Vector?",
"answer":"An array groups data of same primitive type and is static in nature while vectors are dynamic in nature and can hold data of different data types. "
},{"id":"53",
"q":"What is multi-threading? ",
"answer":"Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share same process stack and running in parallel. It helps in performance improvement of any program. "
},{"id":"54",
"q":"When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer?",
"answer":"Since StringBuffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, it’s always a good choice to use StringBuffer when data is being changed too much. If we use String in such a case, for every data change a new String object will be created which will be an extra overhead."
},{"id":"55",
"q":"What’s the purpose of using Break in each case of Switch Statement? ",
"answer":"Break is used after each case (except the last one) in a switch so that code breaks after the valid case and doesn’t flow in the proceeding cases too.\nIf break isn’t used after each case, all cases after the valid case also get executed resulting in wrong results."
},{"id":"56",
"q":"How garbage collection is done in Java?",
"answer":"A In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method."
},{"id":"57",
"q":"How objects of a class are created if no constructor is defined in the class? ",
"answer":"Even if no explicit constructor is defined in a java class, objects get created successfully as a default constructor is implicitly used for object creation. This constructor has no parameters."
},{"id":"58",
"q":"Can we call the constructor of a class more than once for an object? ",
"answer":"Constructor is called automatically when we create an object using new keyword. It’s called only once for an object at the time of object creation and hence, we can’t invoke the constructor again for an object after its creation."
},{"id":"59",
"q":"How can we make copy of a java object? ",
"answer":"We can use the concept of cloning to create copy of an object. Using clone, we create copies with the actual state of an object.\nClone() is a method of Cloneable interface and hence, Cloneable interface needs to be implemented for making object copies."
},{"id":"60",
"q":"What’s the default access specifier for variables and methods of a class? ",
"answer":"Default access specifier for variables and method is package protected i.e variables and class is available to any other class but in the same package,not outside the package. "
},{"id":"61",
"q":"Give an example of use of Pointers in Java class.",
"answer":"There are no pointers in Java. So we can’t use concept of pointers in Java."
},
{"id":"62",
"q":"What’s difference between Stack and Queue? ",
"answer":"Stack and Queue both are used as placeholder for a collection of data. The primary difference between a stack and a queue is that stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle."
},
{"id":"63",
"q":"Can we call a non-static method from inside a static method? ",
"answer":"Non-Static methods are owned by objects of a class and have object level scope and in order to call the non-Static methods from a static block (like from a static main method), an object of the class needs to be created first. Then using object reference, these methods can be invoked"
},
{"id":"64",
"q":"Why wait, notify and nofiyAll method belong to object class ?",
"answer":"In java, we put locks on shared objects not on thread, so these methods are present in Object class. As every object have mutex(lock), it make sense to put these methods in object class."
},
{"id":"65",
"q":"Can you call wait, notify and notifyAll from non synchronized context?",
"answer":"No, you can not call wait, notify and notifyAll from non synchronized context. If you do so, it will throw IllegalMonitorStateException."
},
{"id":"66",
"q":"What is Covariant return type in java?",
"answer":"Covariant return type means if subclass overrides any method, return type of this overriding method can be subclass of return type of base class method."
},
{"id":"67",
"q":"What are order of precedence and associativity, and how are they used?",
"answer":"Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left."
},
{"id":"68",
"q":"Is null a keyword?",
"answer":"The null value is not a keyword"
},
{"id":"69",
"q":"How is rounding performed under integer division?",
"answer":" The fractional part of the result is truncated. This is known as rounding toward zero."
},
{"id":"70",
"q":"Does a class inherit the constructors of its superclass?",
"answer":"A class does not inherit constructors from any of its superclasses."
},
{"id":"71",
"q":"What is numeric promotion?",
"answer":"Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required."
},
{"id":"72",
"q":"What is a Java package and how is it used?",
"answer":"A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces."
},
{"id":"73",
"q":"How many static initializers can you have ?",
"answer":"As many as you want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope."
},
{"id":"74",
"q":"What is the difference between method overriding and overloading?",
"answer":"Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments"
},
{"id":"75",
"q":"What is constructor chaining and how is it achieved in Java ?",
"answer":"A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement."
},
{"id":"76",
"q":"What is a transient variable?",
"answer":"Transient variable is a variable that may not be serialized."
},
{"id":"77",
"q":"What is the difference between the >> and >>> operators?",
"answer":"The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out."
},
{"id":"78",
"q":"How does Java handle integer overflows and underflows?",
"answer":"It uses those low order bytes of the result that can fit into the size of the type allowed by the operation."
},
{"id":"79",
"q":"Is sizeof a keyword?",
"answer":"The sizeof operator is not a keyword."
},
{"id":"80",
"q":"What’s meant by anonymous class?",
"answer":"An anonymous class is a class defined without any name in a single line of code using new keyword.\nFor example, in below code we have defined an anonymous class in one line of code:\n\nJava\n public java.util.Enumeration testMethod()\n{\nreturn new java.util.Enumeration()\n{\n@Override\npublic boolean hasMoreElements()\n{\nreturn false;\n}\n@Override\npublic Object nextElement()\n{\nreturn null;\n}\n}\npublic java.util.Enumeration testMethod()\n{\nreturn new java.util.Enumeration()\n{\n@Override\npublic boolean hasMoreElements()\n{\nreturn false;\n}\n\n@Override\n public Object nextElement()\n{\nreturn null;\n}\n}\n"
},{"id":"81",
"q":"Is there a way to increase the size of an array after its declaration? ",
"answer":"Arrays are static and once we have specified its size, we can’t change it. If we want to use such collections where we may require a change of size ( no of items), we should prefer vector over array."
},{"id":"82",
"q":"What is a Local class in Java?",
"answer":"In Java, if we define a new class inside a particular block, it’s called a local class. Such a class has local scope and isn’t usable outside the block where its defined."
},{"id":"83",
"q":"String and StringBuffer both represent String objects. Can we compare String and StringBuffer in Java?",
"answer":"Although String and StringBuffer both represent String objects, we can’t compare them with each other and if we try to compare them, we get an error."
},{"id":"84",
"q":"Which API is provided by Java for operations on set of objects?",
"answer":"Java provides a Collection API which provides many useful methods which can be applied on a set of objects. Some of the important classes provided by Collection API include ArrayList, HashMap, TreeSet and TreeMap."
},{"id":"85",
"q":"What’s the base class of all exception classes?",
"answer":"In Java, Java.lang.Throwable is the super class of all exception classes and all exception classes are derived from this base class."
},{"id":"86",
"q":"What’s the order of call of constructors in inheritiance?",
"answer":"In case of inheritance, when a new object of a derived class is created, first the constructor of the super class is invoked and then the constructor of the derived class is invoked."
},
{"id":"87",
"q":"What is garbage Collection?",
"answer":"Garbage Collection is a process of looking at heap memory and deleting unused object present in heap memory. Garbage Collection frees unused memory. Garbage Collection is done by JVM."
},
{"id":"88",
"q":"What is System.gc()?",
"answer":"This method is used to invoke garbage collection for clean up unreachable object but it is not guaranteed that when you invoke System.gc(), garbage collection will definitely trigger."
},
{"id":"89",
"q":"What is use of finalize() method in object class? ",
"answer":"Finalize method get called when object is being collected by Garbage Collector. This method can be used to write clean code before object is collected by Garbage Collector."
},
{"id":"90",
"q":"Define states of thread in java?",
"answer":"There are 5 states of thread in java\n\nNew : When you create a thread object and it is not alive yet.\n\nRunnable: When you call start method of thread, it goes into Runnable state. Whether it will execute immediately or execute after some times , depends on thread scheduler.\n\nRunning : When thread is being executed, it goes to running state.\n\nBlocked : When thread waits for some resources or some other thread to complete (due to thread’s join), it goes to blocked state.\n\nDead: When thread’s run method returns, thread goes to dead state. "
},
{"id":"91",
"q":"Can we start a thread twice in java?",
"answer":"No, Once you have started a thread, it can not be started again. If you try to start thread again , it will throw IllegalThreadStateException."
},
{"id":"92",
"q":"What is CountDownLatch in java? ",
"answer":"As per java docs, CountDownLatch is synchronisation aid that allow one or more threads to wait until set of operations being performed in other threads completes. So in other words, CountDownLatch waits for other threads to complete set of operations.\nCountDownLatch is initialized with count. Any thread generally main threads calls latch.awaits() method, so it will wait for either count becomes zero or it’s interrupted by another thread and all other thread need to call latch.countDown() once they complete some operation.\nSo count is reduced by 1 whenever latch.countDown() method get called, so if count is n that means count can be used as n threads have to complete some action or some action have to be completed n times."
},
{"id":"93",
"q":"How do you create custom exception in java?",
"answer":"You just need to extend Exception class to create custom exception. If you want to create Unchecked exception, then you need extend Runtime Exception."
},
{"id":"94",
"q":"What is difference between Checked Exception and Unchecked Exception?",
"answer":"Checked Exception: Checked exceptions are those exceptions which are checked at compile. If you do not handle them , you will get compilation error.\n\nFor example: IOException\n\nUnchecked Exception : Unchecked exceptions are those exceptions which are not checked at compile time. Java won’t complain if you do not handle the exception. "
},
{"id":"95",
"q":"Can we have try without catch block in java ?",
"answer":"Yes, we can have try without catch block by using finally block. You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.exit()."
},
{"id":"96",
"q":"Do we have lock while getting value from ConcurrentHashMap?",
"answer":"There is no lock while getting values from ConcurrentHashMap.Segments are only for write operation.In case of read operation, it allows full concurrency and provides most recently updated value using volatile variables."
},{"id":"97",
"q":"Why java uses another hash function to calculate hash value apart from hashcode method which have implemented?",
"answer":"It is due to avoid large number of collisions due to bad hashcode method written by developers."
},{"id":"98",
"q":"What if you don’t override hashcode method while putting custom objects as key in HashMap?",
"answer":"As we did not implement hashcode method, each object will have different hashcode(memory address) by default, so even if we have implemented equals method correctly, it won’t work as expected."
},{"id":"99",
"q":"Can you explain internal working of HashSet in java?",
"answer":"HashSet internally uses HashMap to store elements in HashSet. It uses PRESENT as dummy object as value in that HashMap. HashSet uses HashMap to check duplicates in the HashSet."
},
{"id":"100",
"q":"Can we declare constructor as final?",
"answer":" No, Constructor can not be declared as final. If you do so, you will get compile time error. "
}
]
}
"q":"What is the most important feature of Java?",
"answer":"Java is a platform independent language."
},
{"id":"2",
"q":"What do you mean by platform independence? ",
"answer":"Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc). "
},
{"id":"3",
"q":"What is a JVM?",
"answer":"JVM is Java Virtual Machine which is a run time environment for the compiled java class files."
},
{"id":"4",
"q":"What is the difference between a JDK and a JVM?",
"answer":"JDK is Java Development Kit which is for development purpose and it includes execution environment also.But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM"
},
{"id":"5",
"q":"What is the base class of all classes? ",
"answer":"java.lang.Object"
},
{"id":"6",
"q":"Is Java a pure object oriented language? ",
"answer":"Java uses primitive data types and hence is not a pure object oriented language "
},
{"id":"7",
"q":"what is oops ?",
"answer":"oops approach is a programming methodology to design computer programs using classes and objects "
},
{"id":"8",
"q":"what are the main principles of oops .?",
"answer":"1. Inheritance\n2.polymorphism\n3.Encapusulation\n4. Abstraction"
},
{"id":"9",
"q":"What are memory areas allocated in JVM?",
"answer":"Memory areas allocated in JVM are:\nHeap area\nMethod area\nJVM language stacks\nProgram counter (PC) register\nNative method stacks"
},
{"id":"10",
"q":"What is Abstraction?",
"answer":"Abstraction is achieved using interface and abstract class in Java."
},
{"id":"11",
"q":"What is encapsulation?",
"answer":"Encapsulation in java is the process of binding related data(variables) and functionality(methods) into a single unit called class.\nEncapsulation can be achieved by using access modifier such as public, private, protected or default."
},
{"id":"12",
"q":"What is Polymorphism in java?",
"answer":"Polymorphism means one name many forms. In Java, polymorphism can be achieved by method overloading and method overriding.\nThere are two types of polymorphism in java.\nCompile time polymorphism.\nRun time polymorphism."
},
{"id":"13",
"q":"What is Compile time polymorphism .?",
"answer":"Compile time Polymorphism is nothing but method overloading in java. You can define various methods with same name but different method arguments"
},
{"id":"14",
"q":"What is run time polymorphism.?",
"answer":"Runtime Polymorphism is nothing but method overriding in java.\nIf subclass is having same method as base class then it is known as method overriding Or in another word,\nIf subclass provides specific implementation to any methodwhich is present in its one of parents classes then it is known as method overriding."
},
{"id":"15",
"q":"What is inheritance in java? ",
"answer":"Inheritance allows to inherit properties and methods of parent class, so you can reuse all methods and properties. "
},
{"id":"16",
"q":"What is constructor in java? ",
"answer":"Constructor can be considered a special code which is used to initiaze objects.\nIt has two main points\n\nClass and Constuctor name should match\nConstructor should not have any return type else it will be same as method. "
},
{"id":"17",
"q":"Can we declare constructor as final?",
"answer":"No, Constructor can not be declared as final. If you do so, you will get compile time error. "
},
{"id":"18",
"q":"What is immutable object in java? ",
"answer":"Immutable object is object whose state can not be changed once created. You can take String object as example for immutable object."
},
{"id":"19",
"q":"Why String is declared final or immutable in java? ",
"answer":"There are various reasons to make String immutable.\nString pool\nThread Safe\nSecurity\nClass Loading\nCache hash value "
},
{"id":"20",
"q":"What are access modifier available in java?",
"answer":"It Specifies accessibility of variables, methods , constructor of class.\nThere are four access modifier in java\nPrivate : Accessible only to the class.\nDefault : Accessible in the package.\nProtected : Accessible in the packages and its subclasses.\nPublic : Accessible everywhere "
},
{"id":"21",
"q":"What are local variables?",
"answer":" Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them. "
},
{"id":"22",
"q":"What are instance variables? ",
"answer":" Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values. "
},
{"id":"23",
"q":"How to define a constant variable in Java?",
"answer":" The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also.\nstatic final int MAX_LENGTH = 50; is an example for constant."
},{"id":"24",
"q":"Should a main() method be compulsorily declared in all java classes?",
"answer":"No not required. main() method should be defined only if the source class is a java application."
},{"id":"25",
"q":"What is the return type of the main() method? ",
"answer":"Main() method doesn't return anything hence declared void."
},{"id":"26",
"q":"Why is the main() method declared static?",
"answer":"main() method is called by the JVM even before the instantiation of the class hence it is declared as static."
},{"id":"27",
"q":"What is the arguement of main() method?",
"answer":"main() method accepts an array of String object as arguement."
},{"id":"28",
"q":"Can we make array volatile in Java? ",
"answer":"This is one of the tricky Java multi-threading questions you will see in senior Java developer Interview.\nYes, you can make an array volatile in Java but only the reference which is pointing to an array, not the whole array. What I mean, if one thread changes the reference variable to points to another array,that will provide a volatile guarantee, but if multiple threads are changing individual array elementsthey won't be having happens before guarantee provided by the volatile modifier."
},{"id":"29",
"q":"What is the use of synchronized keyword? ",
"answer":"synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method other threads have to wait until current thread finishes the execution and release lock. Synchronized keyword provides a lock on the object and thus prevents race condition."
},{"id":"30",
"q":"What is the difference between an Inner Class and a Sub-Class?",
"answer":"An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.\nA sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class."
},{"id":"31",
"q":"What are the various access specifiers for Java classes? ",
"answer":"In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:\n1. Public : Class,Method,Field is accessible from anywhere.\n2. Protected:Method,Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package,but not from outside.\n3. Default: Method,Field,class can be accessed only from the same package and not from outside of it’s native package.\n4. Private: Method,Field can be accessed from the same class to which they belong.\n"
},{"id":"32",
"q":"What’s the purpose of Static methods and static variables?",
"answer":"When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects. "
},{"id":"33",
"q":"What are Loops in Java? What are three types of loops?",
"answer":"Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:\n1) For Loops\nFor loops are used in java to execute statements repeatedly for a given number of times. For loops are used when number of times to execute the statements is known to programmer.\n2) While Loops\nWhile loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, condition is checked first before execution of statements.\n3) Do While Loops\nDo While Loop is same as While loop with only difference that condition is checked after execution of block of statements. Hence in case of do while loop, statements are executed at least once. "
},{"id":"34",
"q":"What is a singleton class? Give a practical example of its usage.",
"answer":"A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for a class.\nThe best example of singleton usage scenario is when there is a limit of having only one connection to a database due to some driver limitations or because of any licensing issues."},
{"id":"35",
"q":"What is an infinite Loop? How infinite loop is declared?",
"answer":"An infinite loop runs without any condition and runs infinitely. An infinite loop can be broken by defining any breaking logic in the body of the statement blocks.\nInfinite loop is declared as follows:\nJava\nfor (;;)\n{\n}\nfor (;;\n{\n}"
},{"id":"36",
"q":"What is the difference between continue and break statement?",
"answer":"break and continue are two important keywords used in Loops. When a break keyword is used in a loop, loop is broken instantly while when continue keyword is used, current iteration is broken and loop continues with next iteration.\nIn below example, Loop is broken when counter reaches.\nfor (counter=0;counter<10;counter++)\nsystem.out.println(counter);\nif (counter==4) {\nbreak;}\n}\n\nfor (counter=0;counter<10;counter++)\nsystem.out.println(counter);\nif (counter==4) {\nbreak;}}"
},{"id":"37",
"q":"What is the difference between double and float variables in Java?",
"answer":"In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number."
},{"id":"38",
"q":"What is Final Keyword in Java? Give an example.",
"answer":"In java, a constant is declared using the keyword Final. Value can be assigned only once and after assignment, value of a constant can’t be changed.\nIn below example, a constant with the name const_val is declared and assigned avalue:\nPrivate Final int const_val=100\nWhen a method is declared as final,it can NOT be overridden by the subclasses.This method are faster than any other method,because they are resolved at complied time.\nWhen a class is declares as final,it cannot be subclassed. Example String,Integer and other wrapper classes."
},{"id":"39",
"q":"What is ternary operator? Give an example. ",
"answer":"Ternary operator , also called conditional operator is used to decide which value to assign to a variable based on a Boolean value evaluation. It’s denoted as ?\n\nIn the below example, if rank is 1, status is assigned a value of “Done” else “Pending”.\n\nJava\npublic class conditionTest {\npublic static void main(String args[]) {\nString status;\nint rank = 3;\nstatus = (rank == 1) ? \"Done\" : \"Pending\";\nSystem.out.println(status);\n}\n}\n\npublic class conditionTest {\npublic static void main(String args[]) {\nString status;\nint rank = 3;\nstatus = (rank == 1) ? \"Done\" : \"Pending\";\nSystem.out.println(status);\n}\n}"
},{"id":"40",
"q":"How can you generate random numbers in Java? ",
"answer":"Using Math.random() you can generate random numbers in the range 0.1 to 1.0\nUsing Random class in package java.util"
},{"id":"41",
"q":"What are Java Packages? What’s the significance of packages? ",
"answer":"In Java, package is a collection of classes and interfaces which are bundled together as they are related to each other. Use of packages helps developers to modularize the code and group the code for proper re-use. Once code has been packaged in Packages, it can be imported in other classes and used."
},{"id":"42",
"q":"Can we declare a class as Abstract without having any abstract method? ",
"answer":"Yes we can create an abstract class by using abstract keyword before class name even if it doesn’t have any abstract method. However, if a class has even one abstract method, it must be declared as abstract otherwise it will give an error."
},{"id":"43",
"q":"Can we declare the main method of our class as private?",
"answer":"In java, main method must be public static in order to run any application correctly. If main method is declared as private, developer won’t get any compilation error however, it will not get executed and will give a runtime error"
},{"id":"44",
"q":"How can we pass argument to a function by reference instead of pass by value?",
"answer":"In java, we can pass argument to a function only by value and not by reference."
},{"id":"45",
"q":"How an object is serialized in java? ",
"answer":"In java, to convert an object into byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing serializable interface get serialized and their state is saved in byte stream."
},{"id":"46",
"q":"When we should use serialization?",
"answer":"Serialization is used when data needs to be transmitted over the network. Using serialization, object’s state is saved and converted into byte stream .The byte stream is transferred over the network and the object is re-created at destination. "
},{"id":"47",
"q":"Is it compulsory for a Try Block to be followed by a Catch Block in Java for Exception handling?",
"answer":"Try block needs to be followed by either Catch block or Finally block or both. Any exception thrown from try block needs to be either caught in the catch block or else any specific tasks to be performed before code abortion are put in the Finally block. "
},{"id":"48",
"q":"When the constructor of a class is invoked?",
"answer":"The constructor of a class is invoked every time an object is created with new keyword.\n\nFor example, in the following class two objects are created using new keyword and hence, constructor is invoked two times.\n\nJava\npublic class const_example {\nconst_example() {\nsystem.out.println(\"Inside constructor\");\n}\n\npublic static void main(String args[]) {\nconst_example c1=new const_example();\nconst_example c2=new const_example();\n}\n}\npublic class const_example {\nconst_example() {\nsystem.out.println(\"Inside constructor\");\n}\npublic static void main(String args[]) {\nconst_example c1=new const_example();\nconst_example c2=new const_example();\n}\n} "
},{"id":"49",
"q":"Can a class have multiple constructors?",
"answer":"Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects. "
},{"id":"50",
"q":"Can we override static methods of a class? ",
"answer":"We cannot override static methods. Static methods belong to a class and not to individual objects and are resolved at the time of compilation (not at runtime).Even if we try to override static method,we will not get an complitaion error,nor the impact of overriding when running the code."
},{"id":"51",
"q":"Is String a data type in java? ",
"answer":"String is not a primitive data type in java. When a string is created in java, it’s actually an object of Java.Lang.String class that gets created. After creation of this string object, all built-in methods of String class can be used on the string object. "
},
{"id":"52",
"q":"What’s the difference between an array and Vector?",
"answer":"An array groups data of same primitive type and is static in nature while vectors are dynamic in nature and can hold data of different data types. "
},{"id":"53",
"q":"What is multi-threading? ",
"answer":"Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share same process stack and running in parallel. It helps in performance improvement of any program. "
},{"id":"54",
"q":"When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer?",
"answer":"Since StringBuffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, it’s always a good choice to use StringBuffer when data is being changed too much. If we use String in such a case, for every data change a new String object will be created which will be an extra overhead."
},{"id":"55",
"q":"What’s the purpose of using Break in each case of Switch Statement? ",
"answer":"Break is used after each case (except the last one) in a switch so that code breaks after the valid case and doesn’t flow in the proceeding cases too.\nIf break isn’t used after each case, all cases after the valid case also get executed resulting in wrong results."
},{"id":"56",
"q":"How garbage collection is done in Java?",
"answer":"A In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method."
},{"id":"57",
"q":"How objects of a class are created if no constructor is defined in the class? ",
"answer":"Even if no explicit constructor is defined in a java class, objects get created successfully as a default constructor is implicitly used for object creation. This constructor has no parameters."
},{"id":"58",
"q":"Can we call the constructor of a class more than once for an object? ",
"answer":"Constructor is called automatically when we create an object using new keyword. It’s called only once for an object at the time of object creation and hence, we can’t invoke the constructor again for an object after its creation."
},{"id":"59",
"q":"How can we make copy of a java object? ",
"answer":"We can use the concept of cloning to create copy of an object. Using clone, we create copies with the actual state of an object.\nClone() is a method of Cloneable interface and hence, Cloneable interface needs to be implemented for making object copies."
},{"id":"60",
"q":"What’s the default access specifier for variables and methods of a class? ",
"answer":"Default access specifier for variables and method is package protected i.e variables and class is available to any other class but in the same package,not outside the package. "
},{"id":"61",
"q":"Give an example of use of Pointers in Java class.",
"answer":"There are no pointers in Java. So we can’t use concept of pointers in Java."
},
{"id":"62",
"q":"What’s difference between Stack and Queue? ",
"answer":"Stack and Queue both are used as placeholder for a collection of data. The primary difference between a stack and a queue is that stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle."
},
{"id":"63",
"q":"Can we call a non-static method from inside a static method? ",
"answer":"Non-Static methods are owned by objects of a class and have object level scope and in order to call the non-Static methods from a static block (like from a static main method), an object of the class needs to be created first. Then using object reference, these methods can be invoked"
},
{"id":"64",
"q":"Why wait, notify and nofiyAll method belong to object class ?",
"answer":"In java, we put locks on shared objects not on thread, so these methods are present in Object class. As every object have mutex(lock), it make sense to put these methods in object class."
},
{"id":"65",
"q":"Can you call wait, notify and notifyAll from non synchronized context?",
"answer":"No, you can not call wait, notify and notifyAll from non synchronized context. If you do so, it will throw IllegalMonitorStateException."
},
{"id":"66",
"q":"What is Covariant return type in java?",
"answer":"Covariant return type means if subclass overrides any method, return type of this overriding method can be subclass of return type of base class method."
},
{"id":"67",
"q":"What are order of precedence and associativity, and how are they used?",
"answer":"Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left."
},
{"id":"68",
"q":"Is null a keyword?",
"answer":"The null value is not a keyword"
},
{"id":"69",
"q":"How is rounding performed under integer division?",
"answer":" The fractional part of the result is truncated. This is known as rounding toward zero."
},
{"id":"70",
"q":"Does a class inherit the constructors of its superclass?",
"answer":"A class does not inherit constructors from any of its superclasses."
},
{"id":"71",
"q":"What is numeric promotion?",
"answer":"Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required."
},
{"id":"72",
"q":"What is a Java package and how is it used?",
"answer":"A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces."
},
{"id":"73",
"q":"How many static initializers can you have ?",
"answer":"As many as you want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope."
},
{"id":"74",
"q":"What is the difference between method overriding and overloading?",
"answer":"Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments"
},
{"id":"75",
"q":"What is constructor chaining and how is it achieved in Java ?",
"answer":"A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement."
},
{"id":"76",
"q":"What is a transient variable?",
"answer":"Transient variable is a variable that may not be serialized."
},
{"id":"77",
"q":"What is the difference between the >> and >>> operators?",
"answer":"The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out."
},
{"id":"78",
"q":"How does Java handle integer overflows and underflows?",
"answer":"It uses those low order bytes of the result that can fit into the size of the type allowed by the operation."
},
{"id":"79",
"q":"Is sizeof a keyword?",
"answer":"The sizeof operator is not a keyword."
},
{"id":"80",
"q":"What’s meant by anonymous class?",
"answer":"An anonymous class is a class defined without any name in a single line of code using new keyword.\nFor example, in below code we have defined an anonymous class in one line of code:\n\nJava\n public java.util.Enumeration testMethod()\n{\nreturn new java.util.Enumeration()\n{\n@Override\npublic boolean hasMoreElements()\n{\nreturn false;\n}\n@Override\npublic Object nextElement()\n{\nreturn null;\n}\n}\npublic java.util.Enumeration testMethod()\n{\nreturn new java.util.Enumeration()\n{\n@Override\npublic boolean hasMoreElements()\n{\nreturn false;\n}\n\n@Override\n public Object nextElement()\n{\nreturn null;\n}\n}\n"
},{"id":"81",
"q":"Is there a way to increase the size of an array after its declaration? ",
"answer":"Arrays are static and once we have specified its size, we can’t change it. If we want to use such collections where we may require a change of size ( no of items), we should prefer vector over array."
},{"id":"82",
"q":"What is a Local class in Java?",
"answer":"In Java, if we define a new class inside a particular block, it’s called a local class. Such a class has local scope and isn’t usable outside the block where its defined."
},{"id":"83",
"q":"String and StringBuffer both represent String objects. Can we compare String and StringBuffer in Java?",
"answer":"Although String and StringBuffer both represent String objects, we can’t compare them with each other and if we try to compare them, we get an error."
},{"id":"84",
"q":"Which API is provided by Java for operations on set of objects?",
"answer":"Java provides a Collection API which provides many useful methods which can be applied on a set of objects. Some of the important classes provided by Collection API include ArrayList, HashMap, TreeSet and TreeMap."
},{"id":"85",
"q":"What’s the base class of all exception classes?",
"answer":"In Java, Java.lang.Throwable is the super class of all exception classes and all exception classes are derived from this base class."
},{"id":"86",
"q":"What’s the order of call of constructors in inheritiance?",
"answer":"In case of inheritance, when a new object of a derived class is created, first the constructor of the super class is invoked and then the constructor of the derived class is invoked."
},
{"id":"87",
"q":"What is garbage Collection?",
"answer":"Garbage Collection is a process of looking at heap memory and deleting unused object present in heap memory. Garbage Collection frees unused memory. Garbage Collection is done by JVM."
},
{"id":"88",
"q":"What is System.gc()?",
"answer":"This method is used to invoke garbage collection for clean up unreachable object but it is not guaranteed that when you invoke System.gc(), garbage collection will definitely trigger."
},
{"id":"89",
"q":"What is use of finalize() method in object class? ",
"answer":"Finalize method get called when object is being collected by Garbage Collector. This method can be used to write clean code before object is collected by Garbage Collector."
},
{"id":"90",
"q":"Define states of thread in java?",
"answer":"There are 5 states of thread in java\n\nNew : When you create a thread object and it is not alive yet.\n\nRunnable: When you call start method of thread, it goes into Runnable state. Whether it will execute immediately or execute after some times , depends on thread scheduler.\n\nRunning : When thread is being executed, it goes to running state.\n\nBlocked : When thread waits for some resources or some other thread to complete (due to thread’s join), it goes to blocked state.\n\nDead: When thread’s run method returns, thread goes to dead state. "
},
{"id":"91",
"q":"Can we start a thread twice in java?",
"answer":"No, Once you have started a thread, it can not be started again. If you try to start thread again , it will throw IllegalThreadStateException."
},
{"id":"92",
"q":"What is CountDownLatch in java? ",
"answer":"As per java docs, CountDownLatch is synchronisation aid that allow one or more threads to wait until set of operations being performed in other threads completes. So in other words, CountDownLatch waits for other threads to complete set of operations.\nCountDownLatch is initialized with count. Any thread generally main threads calls latch.awaits() method, so it will wait for either count becomes zero or it’s interrupted by another thread and all other thread need to call latch.countDown() once they complete some operation.\nSo count is reduced by 1 whenever latch.countDown() method get called, so if count is n that means count can be used as n threads have to complete some action or some action have to be completed n times."
},
{"id":"93",
"q":"How do you create custom exception in java?",
"answer":"You just need to extend Exception class to create custom exception. If you want to create Unchecked exception, then you need extend Runtime Exception."
},
{"id":"94",
"q":"What is difference between Checked Exception and Unchecked Exception?",
"answer":"Checked Exception: Checked exceptions are those exceptions which are checked at compile. If you do not handle them , you will get compilation error.\n\nFor example: IOException\n\nUnchecked Exception : Unchecked exceptions are those exceptions which are not checked at compile time. Java won’t complain if you do not handle the exception. "
},
{"id":"95",
"q":"Can we have try without catch block in java ?",
"answer":"Yes, we can have try without catch block by using finally block. You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.exit()."
},
{"id":"96",
"q":"Do we have lock while getting value from ConcurrentHashMap?",
"answer":"There is no lock while getting values from ConcurrentHashMap.Segments are only for write operation.In case of read operation, it allows full concurrency and provides most recently updated value using volatile variables."
},{"id":"97",
"q":"Why java uses another hash function to calculate hash value apart from hashcode method which have implemented?",
"answer":"It is due to avoid large number of collisions due to bad hashcode method written by developers."
},{"id":"98",
"q":"What if you don’t override hashcode method while putting custom objects as key in HashMap?",
"answer":"As we did not implement hashcode method, each object will have different hashcode(memory address) by default, so even if we have implemented equals method correctly, it won’t work as expected."
},{"id":"99",
"q":"Can you explain internal working of HashSet in java?",
"answer":"HashSet internally uses HashMap to store elements in HashSet. It uses PRESENT as dummy object as value in that HashMap. HashSet uses HashMap to check duplicates in the HashSet."
},
{"id":"100",
"q":"Can we declare constructor as final?",
"answer":" No, Constructor can not be declared as final. If you do so, you will get compile time error. "
}
]
}