Basic Java Interview Questions and Answers

1. What is Java?

Java is a high-level, object-oriented programming language developed by Sun Microsystems. It is platform-independent due to its "write once, run anywhere" capability using the Java Virtual Machine (JVM).

What are the key features of Java

Java has several key features including: Object-Oriented, Platform Independence, Multi-threading, Security, Robustness, and Automatic Memory Management.

2. What is JVM, JDK, and JRE?

JVM (Java Virtual Machine): Runs Java bytecode and provides platform independence.
JDK (Java Development Kit): Contains tools for developing, compiling, and running Java programs.
JRE (Java Runtime Environment): Includes JVM and libraries required to run Java applications.

3. What is the difference between a class and an object?

A class is a blueprint or template for creating objects.

An object is an instance of a class with a defined state and behavior.

4. What is the difference between primitive and reference data types?

Primitive types (int, char, boolean, etc.) store actual values, while reference types (String, Arrays, Classes) store memory addresses pointing to objects.

5. What is encapsulation?

Encapsulation is the principle of wrapping data (variables) and code (methods) together into a single unit, restricting direct access to some components.

6. What is inheritance?

Inheritance allows a class to acquire the properties and behavior of another class using the extends keyword.

7. What is abstraction?

Abstraction is the process of hiding implementation details and exposing only relevant functionality using abstract classes or interfaces.

8. What are interfaces in Java?

An interface defines a contract that implementing classes must follow. It contains only abstract methods (Java 8 introduced default and static methods).

9. What is the difference between abstract class and interface?

An abstract class can have both abstract and concrete methods, whereas an interface only had abstract methods before Java 8.

10. What is method overloading in java?

Method overloading is defining multiple methods in the same class with the same name but different parameter lists.

11. What is the final keyword in Java?

Method overloading is defining multiple methods in the same class with the same name but different parameter lists.

12. What is the difference between == and .equals() in java?

== compares memory references, while .equals() checks content equality (overridden in String and wrapper classes).

13. What are static methods and variables?

Static methods and variables belong to the class rather than any specific object and are shared among all instances.

14. What is a constructor in Java?

A constructor is a special method invoked when an object is created, used to initialize object properties.

15. What is exception handling?

Exception handling manages runtime errors using try, catch, finally, throw, and throws keywords.

16. What are checked and unchecked exceptions?

Checked exceptions (IOException, SQLException) must be handled, while unchecked exceptions (NullPointerException, ArithmeticException) occur at runtime.

17. What is multithreading?

Multithreading allows multiple threads to run concurrently, improving application performance.

18. What is the difference between String, StringBuffer, and StringBuilder?

String is immutable, while StringBuffer and StringBuilder are mutable. StringBuffer is thread-safe, whereas StringBuilder is faster but non-thread-safe.

19. What is garbage collection?

Garbage collection automatically frees unused memory using the JVM’s garbage collector.

20. What is the difference between ArrayList and LinkedList?

ArrayList is better for indexing, whereas LinkedList is more efficient for insertions and deletions.

21. What are lambda expressions?

Lambda expressions (introduced in Java 8) enable functional programming by allowing concise syntax for implementing functional interfaces.

22. What is the Stream API?

The Stream API (Java 8) provides a powerful way to process collections using functional programming constructs like map, filter, and reduce.

23. What is the difference between deep copy and shallow copy?

A shallow copy copies only references, while a deep copy duplicates the entire object structure.

24. What are volatile and synchronized keywords?

Volatile Keyword

The volatile keyword ensures that reads and writes to a variable are directly done from and to the main memory, not from CPU caches. This guarantees visibility (i.e., if one thread updates the variable, all other threads will see the updated value immediately).

synchronized Keyword

The synchronized keyword is used to lock access to a critical section of code to ensure only one thread executes it at a time. It provides both visibility and atomicity, making it useful for thread safety.

25. What is serialization?

Serialization in Java is the process of converting an object into a byte stream so that it can be saved to a file, transmitted over a network, or stored in a database. This byte stream can later be deserialized to reconstruct the original object.

line

Copyrights © 2024 letsupdateskills All rights reserved