Home Login

Basic Java Interview Questions

Categories: Java

Q.1. How does HashMap work in Java?

Ans. A HashMap in Java stores key-value pairs. The HashMap requires a hash function and uses hashCode and equals methods in order to put and retrieve elements to and from the collection. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection. If the key exists then its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor and the threshold resizing.

 

Q.2. What are the differences between wait() and sleep()?

Ans.

Wait() is a method of Object class. Sleep() is a method of Thread class.

Sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock.

Wait() allows the thread to release the lock and go to suspended state. The thread is only active when a notify() or notifAll() method is called for the same object.

 

Q.3. What are the uses of synchronized keywords?

Ans. 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 then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object and thus prevents race condition.

 

Q.4. What does the ‘static’ keyword mean? Is it possible to override private or static method in Java?

Ans. The static keyword denotes that a member variable or method can be accessed, without requiring an instantiation of the class to which it belongs. You cannot override static methods in Java, because method overriding is based upon dynamic binding at runtime and static methods are statically binded at compile time. A static method is not associated with any instance of a class, so the concept is not applicable.

 

Q.5. What is the default value of the local variables?

Ans. The local variables are not initialized to any default value; neither primitives nor object references.

 

Q.6. What is the importance of reflection in Java?

Ans. Reflection is a runtime API for inspecting and changing the behavior of methods, classes, and interfaces. Java Reflection is a powerful tool that can be really beneficial. Java Reflection allows you to analyze classes, interfaces, fields, and methods during runtime without knowing what they are called at compile time. Reflection can also be used to create new objects, call methods, and get/set field values. External, user-defined classes can be used by creating instances of extensibility objects with their fully-qualified names. Debuggers can also use reflection to examine private members of classes.

 

Q.7. How to not allow serialization of attributes of a class in Java?

Ans. The Non-Serialized attribute can be used to prevent member variables from being serialized.

You should also make an object that potentially contains security-sensitive data non-serializable if possible. Apply the Non-Serialized attribute to certain fields that store sensitive data if the object must be serialized. If you don’t exclude these fields from serialization, the data they store will be visible to any programs with serialization permission.

 

Q.8. Can you call a constructor of a class inside another constructor?

Ans. Yes, we can call a constructor of a class inside another constructor. This is also called as constructor chaining. Constructor chaining can be done in 2 ways-

 

(i) Within the same class: For constructors in the same class, the this() keyword can be used.

 

(ii) From the base class: The super() keyword is used to call the constructor from the base class. The constructor chaining follows the process of inheritance. The constructor of the sub class first calls the constructor of the super class. Due to this, the creation of sub class’s object starts with the initialization of the data members of the super class. The constructor chaining works similarly with any number of classes. Every constructor keeps calling the chain till the top of the chain.

 

Q.9. Contiguous memory locations are usually used for storing actual values in an array but not in ArrayList. Explain.

Ans. An array generally contains elements of the primitive data types such as int, float, etc. In such cases, the array directly stores these elements at contiguous memory locations. While an ArrayList does not contain primitive data types. An arrayList contains the reference of the objects at different memory locations instead of the object itself. That is why the objects are not stored at contiguous memory locations.

 

Q.10. How is the creation of a String using new() different from that of a literal?

Ans. When we create a string using new(), a new object is created. Whereas, if we create a string using the string literal syntax, it may return an already existing object with the same name.

Top articles
Basic Java Interview Questions Published at:- Simple Tips for Learning Java Faster Published at:- Java Multiple Choice Question MCQ Published at:- Java Multiple Choice Question MCQ Published at:-
Main Category
|| Java || Technology || US Education News || Others || Spring || PROGRAMMING LANGUAGE || NEW SCHEMES || Education || UK EDUCATION NEWS || US EDUCATION GRADE 9 || WORLD EDUATION NEWS || UNITED STATE COMPUTER EDUCATION || PEBC Exam Canada || Software Engineering || science education || GRE QUESTION PAPER || History || Health ||
closeConn($conn); ?>