03-March 2023
Training

Core Java Interview Questions with Correct Answers and Explanation

..
Core Java Interview Questions with Correct Answers and Explanation

Core Java Interview Questions

1. What is the difference between static and non-static methods in Java?

Answer: Static methods belong to the class and can be called without creating an instance of the class, while non-static methods belong to the object and require an instance of the class to be called.

 

2. What is the difference between abstract classes and interfaces in Java?

Answer: Abstract classes can have implemented methods, while interfaces cannot. Also, a class can implement multiple interfaces, but it can only inherit from one abstract class.

 

3. What is the purpose of the transient keyword in Java?

Answer: The transient keyword is used to indicate that a variable should not be serialized when an object is written to a file or over a network. It is used to protect sensitive information.

 

4. What is the difference between a final variable and a constant variable in Java?

Answer: A final variable can be assigned a value only once and cannot be changed afterwards, while a constant variable is a variable that is never intended to be changed, and its value is set at compile time using the final keyword.

 

5. What is the difference between the throw and throws keywords in Java?

Answer: The throw keyword is used to manually throw an exception, while the throws keyword is used to declare that a method can throw an exception and specifies the type of exception that can be thrown.

 

6. What is the difference between the == operator and the equals() method in Java?

Answer: The == operator checks if two objects have the same memory reference, while the equals() method checks if two objects have the same value.

 

7. What is the purpose of the synchronized keyword in Java?

Answer: The synchronized keyword is used to provide mutual exclusion and ensure that only one thread at a time can access a block of code.

 

8. What is the purpose of the finalize() method in Java?

Answer: The finalize() method is called by the garbage collector before an object is garbage collected. It is used to perform final cleanup operations before the object is destroyed.

 

9. What is the purpose of the clone() method in Java?

Answer: The clone() method is used to create a copy of an object. It is used to avoid the overhead of creating a new object and initializing it with the values of an existing object.

 

10. What is the difference between the super() and this() keywords in Java?

Answer: The super() keyword is used to call the constructor of the parent class, while the this() keyword is used to call the constructor of the current class.

 

11. What is the purpose of the volatile keyword in Java?

Answer: The volatile keyword is used to indicate that a variable's value may be modified by different threads. It ensures that the value of the variable is always read from the main memory, rather than from a thread's cache.

 

12. What is the difference between an ArrayList and a LinkedList in Java?

Answer: An ArrayList is backed by an array, while a LinkedList is implemented as a doubly linked list. ArrayList provides constant time access to elements, while LinkedList provides constant time insertion and deletion of elements.

 

13. What is the purpose of the @Override annotation in Java?

Answer: The @Override annotation is used to indicate that a method is intended to override a method in the parent class. It ensures that the method signature matches that of the parent class, and helps to avoid errors due to typos or incorrect method signatures.

 

14. What is the difference between the Comparable and Comparator interfaces in Java?

Answer: The Comparable interface is used to define a natural ordering for a class, while the Comparator interface is used to define a custom ordering for a class. The Comparable interface is implemented by the class being sorted, while the Comparator interface is implemented by a separate class.

 

15. What is the purpose of the getClass() method in Java?

Answer: The getClass() method is used to get the class object of an object. It can be used to obtain information about the object's class, such as its name, superclass, and interfaces.

 

16. What is the difference between checked and unchecked exceptions in Java?

Answer: Checked exceptions are checked at compile time and must be handled by the calling method, either by catching the exception or declaring it in the method's throws clause. Unchecked exceptions, on the other hand, are not checked at compile time and do not need to be handled by the calling method.

 

17. What is the purpose of the finalize() method in Java?

Answer: The finalize() method is called by the garbage collector before an object is garbage collected. It is used to perform final cleanup operations before the object is destroyed.

 

18. What is the difference between a HashSet and a TreeSet in Java?

Answer: A HashSet is an unordered collection that does not allow duplicate elements, while a TreeSet is a sorted collection that does not allow duplicate elements. TreeSet elements are ordered according to their natural ordering or a custom Comparator.

 

19. What is the purpose of the assert keyword in Java?

Answer: The assert keyword is used to check a condition that should be true. It is used for debugging and can be enabled or disabled at runtime.

 

20. What is the difference between the FileInputStream and the FileReader classes in Java?

Answer: The FileInputStream class is used to read binary data from a file, while the FileReader class is used to read character data from a file. The FileReader class automatically handles character encoding, while the FileInputStream class does not.

 

21. What is the purpose of the System class in Java?

Answer: The System class provides access to system resources, such as standard input and output, and allows for the manipulation of system properties.

 

22. What is the difference between the finalize() method and the finally block in Java?

Answer: The finalize() method is called by the garbage collector before an object is garbage collected, while the finally block is used to ensure that a block of code is always executed, regardless of whether an exception is thrown or not.

 

23. What is the purpose of the Collections class in Java?

Answer: The Collections class provides static methods for working with collections, such as sorting, searching, and filling.

 

24. What is the difference between a private and a protected method in Java?

Answer: A private method is only accessible within the class in which it is declared, while a protected method is accessible within the class in which it is declared and within its subclasses.

 

25. What is the purpose of the Thread class in Java?

Answer: The Thread class is used to create and control threads in a Java program.

 

26. What is the difference between a shallow copy and a deep copy in Java?

Answer: A shallow copy creates a new object with a reference to the original object, while a deep copy creates a new object with a copy of the original object's fields.

 

27. What is the purpose of the finalize() method in Java?

Answer: The finalize() method is called by the garbage collector before an object is garbage collected. It is used to perform final cleanup operations before the object is destroyed.

 

28. What is the difference between a StringBuilder and a StringBuffer in Java?

Answer: A StringBuilder is not synchronized, while a StringBuffer is synchronized. StringBuilder is faster than StringBuffer, but not thread-safe.

 

29. What is the purpose of the static keyword in Java?

Answer: The static keyword is used to indicate that a variable or method belongs to the class, rather than to an instance of the class.

 

30. What is the difference between a break and a continue statement in Java?

Answer: A break statement terminates the innermost loop or switch statement, while a continue statement skips the current iteration of the loop and continues with the next iteration.

 

We hope that you must have found this exercise quite useful. If you wish to join Core Java, Advance Java, Spring Boot Framework, Struts Framework training, feel free to contact us at +91-9936804420 or email us at aditya.inspiron@gmail.com. 

Happy Learning 

Team Inspiron Technologies 

Leave a comment

Your email address will not be published. Required fields are marked *