Java Interview Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Java Interview Questions and Answers

Core Java Questions

What are the main features of Java?

Platform Independence: Java code runs on any device with a JVM.

Object-Oriented: Java uses objects to structure code.

Simple: Java is easy to learn and write.

Secure: Java has built-in security features.

Multi-threaded: Java supports multiple tasks at once.

What is the Java Virtual Machine (JVM)?

JVM: Converts Java bytecode into machine code so it can run on different operating systems.

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

JDK: Includes tools for developing Java applications (e.g., compiler, JRE).

JRE: Runs Java applications (contains JVM).

JVM: Part of JRE that runs bytecode on any device.

What is a Class and an Object in Java?

Class: A blueprint for creating objects (e.g., a Car class).

Object: An instance of a class (e.g., your car is an object of the Car class).

What are Constructors in Java?

Constructor: Initializes an object when it's created.

Default Constructor: Provided by Java if no constructor is defined; uses default values.

Parameterized Constructor: Allows you to specify values when creating an object.

What is Inheritance in Java?

Inheritance: Allows a class to use properties and methods from another class (e.g., Car inherits from

Vehicle).
What is Polymorphism?

Polymorphism: An object can take many forms.

Method Overloading: Multiple methods with the same name but different parameters.

Method Overriding: A subclass provides a specific implementation of a method from its superclass.

What is Encapsulation?

Encapsulation: Keeps data safe by making fields private and providing public methods to access

them.

What is Abstraction?

Abstraction: Hides complex implementation details and shows only the essential features. Achieved

through abstract classes and interfaces.

What are Access Modifiers?

Public: Accessible from any other class.

Private: Accessible only within the same class.

Protected: Accessible within the same package and by subclasses.

Default: Accessible only within the same package (no modifier).


Additional Entry-Level Questions

What is a method in Java?

Method: A block of code that performs a specific task and is part of a class. For example,

printName() in a Person class prints the person's name.

What is the static keyword?

Static: Used for methods or variables that belong to the class, not to any specific object. For

example, static int count; belongs to the class and is shared among all instances.

What is the difference between an abstract class and an interface?

Abstract Class: A class that can't be instantiated and can have abstract methods (without body) and

non-abstract methods (with body).

Interface: A collection of abstract methods (no implementation). A class implements an interface by

providing the body for the methods.

What is the difference between a stack and a heap in memory?

Stack: Memory area that stores local variables and method calls. It's small and operates in a LIFO

(Last In, First Out) manner.

Heap: Memory area that stores objects. It's larger and used for dynamic memory allocation.

What are loops in Java? Explain for, while, and do-while loops.

For Loop: Executes a block of code a specific number of times. Example: for(int i = 0; i < 10; i++)

While Loop: Repeats a block of code as long as a condition is true. Example: while(condition)

Do-While Loop: Similar to the while loop but guarantees the block of code runs at least once.

Example: do { } while(condition)

What is a package in Java?

Package: A way to organize related classes and interfaces. For example, java.util is a package that

contains utility classes like ArrayList.


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

String: Immutable (cannot be changed after creation).

StringBuilder: Mutable and faster for string manipulation, but not thread-safe.

StringBuffer: Mutable and thread-safe, but slower than StringBuilder.

What is a try-catch block? How does it work?

Try-Catch Block: Used for handling exceptions. Code that might throw an exception goes inside try,

and the code to handle it goes inside catch.

Example:

try {

int result = 10 / 0;

} catch (ArithmeticException e) {

System.out.println('Cannot divide by zero.');

What is the difference between the equals() method and the '==' operator?

== Operator: Compares references (whether two objects point to the same memory location).

equals() Method: Compares the actual content or values of objects.

What is the use of the this keyword in Java?

this Keyword: Refers to the current object. It's used to avoid naming conflicts between class fields

and parameters or to call another constructor from within a constructor.

Example:

class Person {

private String name;

Person(String name) {

this.name = name; // 'this' refers to the instance variable 'name'

You might also like