Top 50 java basic interview question

Top 50 Java Basic Interview Questions to Boost Your Confidence

WhatsApp Group Join Now
Telegram Group Join Now

Table of Contents

1. What is Java?

Java is a versatile, object-oriented programming language. It is platform-independent, meaning you can run it on different systems without modification.


2. Why is Java platform-independent?

Java programs are compiled into bytecode, which is executed by the JVM (Java Virtual Machine). This bytecode can run on any system with a compatible JVM.


3. What is the difference between JDK, JRE, and JVM?

  • JDK: Development kit for compiling and running Java programs.
  • JRE: Environment for executing Java programs.
  • JVM: Part of JRE that translates bytecode into machine code.

4. What are Java’s key features?

  • Object-Oriented: Everything revolves around objects and classes.
  • Robust: Strong memory management and exception handling.
  • Multithreaded: Enables concurrent programming.
  • Platform-Independent: Runs anywhere with a JVM.

5. What is a Java class?

A class defines a template for objects. It contains fields (variables) and methods that determine the behavior of objects created from it.


6. What is an object?

An object is an instance of a class. It has a state (attributes) and behavior (methods).


7. Explain OOP principles in Java.

  • Encapsulation: Binding data and methods together while restricting access.
  • Inheritance: Sharing properties and behaviors from parent to child classes.
  • Polymorphism: Using one interface to represent different types of behaviors.
  • Abstraction: Hiding implementation and showing only necessary details.

8. What is a constructor?

A constructor initializes an object when it is created. It has the same name as the class and no return type.


9. Can a class have multiple constructors?

Yes, this is known as constructor overloading, where constructors differ by parameter types or count.


10. What is inheritance?

Inheritance enables one class (child) to acquire the properties and methods of another (parent), promoting code reuse.


11. What is polymorphism?

Polymorphism allows a single method or object to behave in multiple forms, like method overloading (compile-time) and method overriding (runtime).


12. How does method overloading work?

Method overloading occurs when two or more methods in a class have the same name but different parameter lists.


13. What is method overriding?

When a subclass provides its own implementation for a method in the superclass, it is known as method overriding.


14. What is the difference between this and super?

  • this: Refers to the current class instance.
  • super: Refers to the parent class instance.

15. What is encapsulation?

Encapsulation restricts direct access to data members and allows it only through getter and setter methods.


16. What is abstraction?

Abstraction focuses on exposing essential features while hiding complex details. Achieved using abstract classes and interfaces.


17. What is an interface?

An interface is a blueprint for a class, containing abstract methods and static constants. Classes implement interfaces to define behavior.


18. Difference between abstract class and interface?

  • Abstract Class: Can have both abstract and concrete methods.
  • Interface: Contains only abstract methods (until Java 8, where default methods were introduced).

19. What are access modifiers?

  • Public: Accessible everywhere.
  • Private: Accessible only within the class.
  • Protected: Accessible in the same package and by subclasses.
  • Default: Accessible only in the same package.

20. How is Java’s memory managed?

Java uses automatic garbage collection to free up memory by removing objects that are no longer referenced.


21. What is a static variable?

A static variable belongs to the class rather than any individual object. It is shared across all objects.


22. What is a static method?

Static methods belong to the class and can be called without creating an object.


23. What is the final keyword?

  • Final Variable: Value cannot be changed.
  • Final Method: Cannot be overridden.
  • Final Class: Cannot be subclassed.

24. Explain the difference between == and .equals().

  • == compares references or memory addresses.
  • .equals() compares the actual content of objects.

25. What is the difference between String and StringBuilder?

  • String: Immutable sequence of characters.
  • StringBuilder: Mutable and allows changes to the character sequence.

26. What is a wrapper class?

A wrapper class converts a primitive data type into an object (e.g., int to Integer).


27. What is autoboxing?

Autoboxing is the automatic conversion of primitive types into their corresponding wrapper classes by the compiler.


28. What are Java Collections?

Java Collections is a framework for storing and manipulating groups of objects, such as lists, sets, and maps.


29. What is the purpose of the ArrayList?

An ArrayList is a resizable array that allows dynamic storage of elements.


30. What is the difference between Array and ArrayList?

  • Array: Fixed size; can store both primitives and objects.
  • ArrayList: Dynamic size; stores only objects.

31. What is a HashMap?

A HashMap stores key-value pairs and allows quick access using keys.


32. What is multithreading?

Multithreading allows multiple threads to run concurrently within a program, improving performance for tasks like IO operations.


33. What is the purpose of the synchronized keyword?

It ensures that only one thread can access a block of code at a time.


34. What is an exception in Java?

An exception is an unwanted or unexpected event that occurs during program execution. Java handles exceptions using try-catch blocks.


35. What is the difference between checked and unchecked exceptions?

  • Checked Exceptions: Must be handled during compilation (e.g., IOException).
  • Unchecked Exceptions: Occur at runtime (e.g., NullPointerException).

36. What is garbage collection?

Garbage collection is an automatic process in Java that removes objects that are no longer in use.


37. What is the finally block?

The finally block contains code that executes after try-catch, regardless of an exception.


38. What is serialization?

Serialization converts an object into a byte stream so it can be saved or transmitted.


39. What is the transient keyword?

It marks variables to be skipped during serialization.


40. What is the purpose of volatile in Java?

Volatile ensures that changes to a variable are visible to all threads immediately.


41. What is the difference between Comparable and Comparator?

  • Comparable: Used for natural ordering within a class.
  • Comparator: Allows custom ordering outside the class.

42. What are streams in Java?

Streams in Java provide functional programming capabilities for processing data collections.


43. What is a lambda expression?

A lambda expression simplifies the creation of anonymous functions.


44. What is a default method in interfaces?

A default method is a method with a body, introduced in Java 8, allowing interfaces to provide some functionality.


45. What is the purpose of the System.gc() method?

It requests the JVM to perform garbage collection, though it’s not guaranteed.


46. What is the difference between process and thread?

  • Process: A standalone program.
  • Thread: A smaller unit of execution within a process.

47. What is an immutable object?

An immutable object cannot be modified once created, such as instances of the String class.


48. What is type casting in Java?

Type casting converts one data type into another. It can be implicit (widening) or explicit (narrowing).


49. What is functional programming in Java?

Introduced in Java 8, functional programming uses lambda expressions and streams for declarative coding.


50. What is the difference between throw and throws?

  • throw: Used to explicitly throw an exception.
  • throws: Declares exceptions a method might throw.

Similar Posts

Leave a Reply

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