In this Article I am writing popular Java Interview Questions in Technical Round for Beginners and Experienced Techies.So, Before attend the Technical Round brush up this Article to clear the round.
1. Can you access non static variable in static context?
Ans: A static variable in java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. if your code tries to access a non-static variable without any instance,the compiler will complain, because those variables are not created yet and they not associated with any instance.
2. What are pass by reference and pass by value?
Ans: When an Object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object,it doesn't affect the original value. When an object is passed by reference,this means that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by external method are also reflected in all places.
3.What is benefit of Generic in Collections Framework?
Ans: Java 1.5 came with Generics and all collection interfaces and implementations use it heavily. Generics allow us to provide the type of object that a collection can contain,so if you try to add any element of other type it throws compile time error. This avoids ClassCastException at runtime because you get the error at compilation. Also Generics make code clean since we don't need to use casting and instanceof operator. I would highly recommend to go through java Generic Topic to understand Generics in a better way.
4. Why Collection does not extend Cloneable and Serializable interfaces?
Ans: Collection interface specifies group of Objects known as elements. How the elements are maintained is left up to the concrete implementations of collection. For example,some collection implementations list List allow duplicate elements whereas other implementations like Set don't. A lot of the collection implementations have a public clone method. However, it doesn't really make sense to include it in all implementations of collection.This is because Collection is an abstract representation.The specific implementation should make the decision as to whether it can be cloned or serialized.
5. Why there is not method like iterator.add() to add elements to the collections?
Ans: The semantics are unclear, given that the contract for iterator makes no guarantees about the order of iteration. Note, however,that Listiterator does provide an add operation,as it does guarantee the order of the iteration.
6. Explain the available thread states in a high-level?
Ans: During its execution, a thread can reside in one of the following states:
NEW: The thread becomes ready to run,but does not necessarily start running immediately.
Runnable: The java virtual machine(JVM) is actively executing the thread's code.
Blocked: The thread is in a blocked state while waiting for a monitor lock.
Waiting: The thread waits for another thread to perform a particular action.
Timed_waiting: The thread waits for another thread to perform a particular action up to a specified waiting time.
Terminated: The Thread has finished its execution
7. What if main method is declared is private?
Ans: The program compiles properly but at runtime it will give Main method not public message
8. If you are overriding the method equals() of an object,which other method you might also consider?
Ans: hashcode()
9. What do you understand by iterator fail-fast property?
Ans: iterator fail-fast property checks for any modification in the structure of the underlying collection every time we try to get the next element. If there are any modifications found,it throws ConcurrentModificationException. All the implementations of iterator in Collection classes are fail-fast by design except the concurrent collection classes like ConcurrentHashMap and CopyOnWriteArrayList.
10. How to avoid ConcurrentModificationException while iterating a collection?
Ans: we can use concurrent collection classes to avoid ConcurrentModificationException while iterating over a collection, for example CopyOnWriteArrayList instead of ArrayList.
11) How to create a String?
Ans: In java a String can be created in two ways: They are
1) By String literal
2)Using new keyword
String literal: In this method we creates a String type object and assign a sequence of characters to it enclose with " "
Example: String s="learnprogramingbyluckysir";
Using new keyword: An String object can be obtained using 'new' keyword as shown below
String str=new String("learnprogramingbyluckysir");
12) What is Wrapper class?
Ans: wrapper classes are classes that allow primitive types to be accessed as objects.
13) What is the use of ValueOf() method?
Ans: ValueOf() method converts data from its internal format into a human readable form.
14) What is the importance of hashcode() and equals() methods?
Ans: In java, a HashMap uses hashcode and equals methods to determine the index of the key-value pair and to detect duplicates. The hashcode method is used in order to determine where the specified key will be stored.Since different keys may produce the same hash value,the equals method is used,in order to determine whether the specified key actually exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and efefficiency of the HashMap.
15) How to decide between HashMap and TreeMap()?
Ans: For inserting,deleting,and locating elements in a Map,the HashMap offers the best alternative. If, however,you need to traverse the keys in a sorted order,then TreeMap is your better alternative. Depending upon the size of your collection,it may be faster to add elements to a HashMap,then convert the map to a TreeMap for sorted key traversal.
16) What is the primary benefit of Encapsulation?
Ans: The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this encapsulation gives maintainability,flexibility and extensible to our code.
17) What are the ways in which a thread can enter the waiting state?
Ans: A thread can enter the waiting state by invoking it's sleep() method or by invoking an object's wait() method. It can also enter the waiting state by invoking its suspend() method.
18) How does multi-threading take place on a computer with a single CPU?
Ans: The OS task Scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks,it creates the impression that tasks execute sequentially.
19) What is the use of subclass in a java program?
Ans: The Sub class inherits all the public and protected methods and the implementation. It also inherits all the default modifier methods and their implementation.
20) Is there any limitation of using Inheritance?
Ans: YES, since inheritance inherits everything from the super class and interface,it may take the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation
21) When is the ArrayStoreException thrown?
Ans: When copying elements between different arrays,if the source or destination arguments are not arrays or their types are not compatible,an ArrayStoreException will be thrown
22) Why do we need wrapper classes?
Ans: We can pass them around as method parameters whee a method expects an Object. It also provides utility methods.
23) Why deletion in LinkedList is fast than ArrayList?
Ans: Deletion in linked list is fast because it involves only updating the next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.
24) What is difference between path and classpath?
Ans: Path and ClassPath are OS level environment variables. Path is defines where the system can find executable(.exe) files and classpath is used to specify the location of .class files.
25) What is the diff b/w inner class and nested class?
Ans: When a class is defined within a scope of another class,then it becomes inner class. If the access modifier of the inner class is static,then it becomes nested class.
Recommended to Read The following Interview Questions:
JSP INTERVIEW QUESTIONS
Struts Interview Questions
MVC Architecture in Java
Basic Java Interview Questions
TOP 10 JAVA PROGRAM INTERVIEW QUESTIONS
1. Can you access non static variable in static context?
Ans: A static variable in java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. if your code tries to access a non-static variable without any instance,the compiler will complain, because those variables are not created yet and they not associated with any instance.
2. What are pass by reference and pass by value?
Ans: When an Object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object,it doesn't affect the original value. When an object is passed by reference,this means that the actual object is not passed, rather a reference of the object is passed. Thus, any changes made by external method are also reflected in all places.
3.What is benefit of Generic in Collections Framework?
Ans: Java 1.5 came with Generics and all collection interfaces and implementations use it heavily. Generics allow us to provide the type of object that a collection can contain,so if you try to add any element of other type it throws compile time error. This avoids ClassCastException at runtime because you get the error at compilation. Also Generics make code clean since we don't need to use casting and instanceof operator. I would highly recommend to go through java Generic Topic to understand Generics in a better way.
4. Why Collection does not extend Cloneable and Serializable interfaces?
Ans: Collection interface specifies group of Objects known as elements. How the elements are maintained is left up to the concrete implementations of collection. For example,some collection implementations list List allow duplicate elements whereas other implementations like Set don't. A lot of the collection implementations have a public clone method. However, it doesn't really make sense to include it in all implementations of collection.This is because Collection is an abstract representation.The specific implementation should make the decision as to whether it can be cloned or serialized.
5. Why there is not method like iterator.add() to add elements to the collections?
Ans: The semantics are unclear, given that the contract for iterator makes no guarantees about the order of iteration. Note, however,that Listiterator does provide an add operation,as it does guarantee the order of the iteration.
6. Explain the available thread states in a high-level?
Ans: During its execution, a thread can reside in one of the following states:
NEW: The thread becomes ready to run,but does not necessarily start running immediately.
Runnable: The java virtual machine(JVM) is actively executing the thread's code.
Blocked: The thread is in a blocked state while waiting for a monitor lock.
Waiting: The thread waits for another thread to perform a particular action.
Timed_waiting: The thread waits for another thread to perform a particular action up to a specified waiting time.
Terminated: The Thread has finished its execution
7. What if main method is declared is private?
Ans: The program compiles properly but at runtime it will give Main method not public message
8. If you are overriding the method equals() of an object,which other method you might also consider?
Ans: hashcode()
9. What do you understand by iterator fail-fast property?
Ans: iterator fail-fast property checks for any modification in the structure of the underlying collection every time we try to get the next element. If there are any modifications found,it throws ConcurrentModificationException. All the implementations of iterator in Collection classes are fail-fast by design except the concurrent collection classes like ConcurrentHashMap and CopyOnWriteArrayList.
10. How to avoid ConcurrentModificationException while iterating a collection?
Ans: we can use concurrent collection classes to avoid ConcurrentModificationException while iterating over a collection, for example CopyOnWriteArrayList instead of ArrayList.
11) How to create a String?
Ans: In java a String can be created in two ways: They are
1) By String literal
2)Using new keyword
String literal: In this method we creates a String type object and assign a sequence of characters to it enclose with " "
Example: String s="learnprogramingbyluckysir";
Using new keyword: An String object can be obtained using 'new' keyword as shown below
String str=new String("learnprogramingbyluckysir");
12) What is Wrapper class?
Ans: wrapper classes are classes that allow primitive types to be accessed as objects.
13) What is the use of ValueOf() method?
Ans: ValueOf() method converts data from its internal format into a human readable form.
14) What is the importance of hashcode() and equals() methods?
Ans: In java, a HashMap uses hashcode and equals methods to determine the index of the key-value pair and to detect duplicates. The hashcode method is used in order to determine where the specified key will be stored.Since different keys may produce the same hash value,the equals method is used,in order to determine whether the specified key actually exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and efefficiency of the HashMap.
15) How to decide between HashMap and TreeMap()?
Ans: For inserting,deleting,and locating elements in a Map,the HashMap offers the best alternative. If, however,you need to traverse the keys in a sorted order,then TreeMap is your better alternative. Depending upon the size of your collection,it may be faster to add elements to a HashMap,then convert the map to a TreeMap for sorted key traversal.
16) What is the primary benefit of Encapsulation?
Ans: The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this encapsulation gives maintainability,flexibility and extensible to our code.
17) What are the ways in which a thread can enter the waiting state?
Ans: A thread can enter the waiting state by invoking it's sleep() method or by invoking an object's wait() method. It can also enter the waiting state by invoking its suspend() method.
18) How does multi-threading take place on a computer with a single CPU?
Ans: The OS task Scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks,it creates the impression that tasks execute sequentially.
19) What is the use of subclass in a java program?
Ans: The Sub class inherits all the public and protected methods and the implementation. It also inherits all the default modifier methods and their implementation.
20) Is there any limitation of using Inheritance?
Ans: YES, since inheritance inherits everything from the super class and interface,it may take the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation
21) When is the ArrayStoreException thrown?
Ans: When copying elements between different arrays,if the source or destination arguments are not arrays or their types are not compatible,an ArrayStoreException will be thrown
22) Why do we need wrapper classes?
Ans: We can pass them around as method parameters whee a method expects an Object. It also provides utility methods.
23) Why deletion in LinkedList is fast than ArrayList?
Ans: Deletion in linked list is fast because it involves only updating the next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.
24) What is difference between path and classpath?
Ans: Path and ClassPath are OS level environment variables. Path is defines where the system can find executable(.exe) files and classpath is used to specify the location of .class files.
25) What is the diff b/w inner class and nested class?
Ans: When a class is defined within a scope of another class,then it becomes inner class. If the access modifier of the inner class is static,then it becomes nested class.
Recommended to Read The following Interview Questions:
JSP INTERVIEW QUESTIONS
Struts Interview Questions
MVC Architecture in Java
Basic Java Interview Questions
TOP 10 JAVA PROGRAM INTERVIEW QUESTIONS
No comments:
Post a Comment