Question 1:
What is the difference between JDK, JRE, and JVM in Java?
Corrected Interview-Ready Answer:
• JDK (Java Development Kit): A software package used to develop Java
applications. It contains the JRE, a Java compiler (javac), and other
development tools.
• JRE (Java Runtime Environment): Provides the environment required to run Java
applications. It includes the JVM and core Java libraries, but no development
tools.
• JVM (Java Virtual Machine): The engine that executes Java bytecode. It has
components like the JIT (Just-In-Time) compiler and garbage collector.
Quick mnemonic:
• JDK → For Developing (includes JRE + tools)
• JRE → For Running (includes JVM + libraries)
• JVM → For Executing bytecode
Question 2:
What are the main features of Java that make it popular?
Java is popular because of features like:
1. Platform Independence – Write once, run anywhere using the JVM.
2. Object-Oriented – Supports encapsulation, inheritance, polymorphism, and
abstraction.
3. Robust – Strong memory management, exception handling, and type checking.
4. Secure – No pointers, bytecode verification, and built-in security APIs.
5. Multithreaded – Can handle multiple tasks simultaneously.
6. High Performance – Through JIT compiler optimizations.
7. Portable – Same bytecode can run on any device with a JVM.
Question 3:
What are the data types in Java, and how are they classified?
In Java, there are two main types of data types:
1. Primitive Data Types – These store simple values directly in memory. There are 8
types:
o byte (8-bit)
o short (16-bit)
o int (32-bit)
o long (64-bit)
o float (32-bit)
o double (64-bit)
o char (16-bit, Unicode)
o boolean (true/false)
2. Non-Primitive (Reference) Data Types – These store references (memory
addresses) to objects. Examples:
o String
o Arrays
o Classes
o Interfaces
Extra Tip for Interviews:
If they ask “Why are Strings non-primitive?”, you should answer:
Because Strings in Java are objects (instances of the String class), not a built-in
primitive type.
Question 4:
What are variables in Java, and what are the different types of variables?
A variable in Java is a named memory location used to store data. Its value can
change during program execution.
Variables in Java are mainly classified into three types:
1. Local Variables – Declared inside a method, constructor, or block. Accessible
only within that block and created when the block is executed.
2. Instance Variables – Declared inside a class but outside any method. They are
tied to a specific object (non-static) and each object gets its own copy.
3. Static Variables (Class Variables) – Declared with the static keyword inside a
class. Shared among all objects of the class and created only once in memory.
Extra Tip:
If they ask “How is a variable’s type decided?”, then you can say:
It depends on the data type assigned to it — primitive or non-primitive.
Question 5:
What is the difference between == and .equals() in Java?
== operator:
• For primitive types → compares actual values.
• For objects → compares memory references (i.e., whether both references point
to the same object).
.equals() method:
• Defined in the Object class, so all classes inherit it.
• By default, behaves like == (compares references).
• Often overridden (e.g., in String, Integer, etc.) to compare the contents/data
instead of references.
Question 6:
What is a constructor in Java, and what are its types?
A constructor in Java is a special block of code that is executed when an object is
created. Its main purpose is to initialize the object’s state (assign values to variables).
Key points:
• The constructor’s name must be the same as the class name.
• It has no return type, not even void.
• It is automatically called during object creation using the new keyword.
Types of constructors:
1. No-argument constructor – Does not take any parameters and usually sets
default values.
2. Parameterized constructor – Takes parameters to initialize an object with
specific values.
Question 7:
What is the difference between a constructor and a method in Java?
Difference between Constructor and Method in Java
Feature Constructor Method
Performs operations or defines
Purpose Initializes an object when it is created
object behavior
Name Must be the same as the class name Can be any valid identifier
Must have a return type (can be
Return Type No return type (not even void)
void)
Called automatically when an object is Called explicitly by the
Invocation
created using new programmer
Overloading Can be overloaded Can be overloaded
Inheritance Not inherited by subclasses Can be inherited by subclasses
Default Java provides a default constructor if No default method is created
Form none is defined automatically
Question 8:
What are access modifiers in Java, and what types are available?
In Java, access modifiers define the scope (visibility) of classes, methods, and
variables. There are four types:
1. public – Accessible from anywhere in the program, across all classes and
packages.
2. private – Accessible only within the same class.
3. protected – Accessible within the same class, within subclasses (even in
different packages), and within the same package.
4. default (no keyword) – Accessible only within the same package.
5. Access Modifier Table:
Modifier Same Class Same Package Subclass (diff package) Other Packages
public
protected
default
private
Question 9:
What is the static keyword in Java, and when do we use it?
static is a non-access modifier in Java that makes a variable, method, or block belong
to the class rather than to any specific object.
Key points:
• Static variables → Shared among all objects of the class.
• Static methods → Can be called without creating an object. They can only
access other static members directly.
• Static blocks → Used to initialize static data; executed once when the class is
loaded into memory.
Question 10:
What is the final keyword in Java, and what are its uses?
final Keyword in Java
final is a non-access modifier used to restrict changes to variables, methods, and
classes.
Uses:
1. Final variable – Once assigned, its value cannot be changed (acts like a
constant).
final int MAX_VALUE = 100;
2. Final method – Cannot be overridden by subclasses.
class Parent {
final void display() {
[Link]("Parent method");
3. Final class – Cannot be inherited.
final class Utility {
// methods
Key points:
• A final variable must be initialized at the time of declaration or in the
constructor.
• final is different from finally (exception handling) and finalize() (garbage
collection).
Question 11:
What is the this keyword in Java, and when do we use it?
this Keyword in Java
this is a reference variable in Java that refers to the current object of the class.
Uses:
1. Differentiate instance variables from local variables when they have the same
name.
class Example {
int x;
Example(int x) {
this.x = x; // 'this.x' is instance variable, 'x' is local variable
}
2. Call another constructor in the same class (constructor chaining).
this(10, 20);
3. Pass the current object as an argument to another method.
someMethod(this);
4. Return the current object from a method.
return this;
Key point:
this can only be used in non-static methods, because static methods belong to the
class, not to any object.
Question 12:
What is the super keyword in Java, and when do we use it?
super Keyword in Java
super is a reference variable in Java that refers to the parent (superclass) object of the
current class.
Uses:
1. Access parent class variables when they are hidden by subclass variables.
class Parent {
int x = 10;
class Child extends Parent {
int x = 20;
void display() {
[Link](super.x); // prints 10
2. Call parent class methods when overridden in the subclass.
[Link]();
3. Call parent class constructor from a subclass constructor (must be the first
statement in the constructor).
super(100);
Key point:
super is often used to avoid ambiguity or to explicitly access functionality from the
parent class.
Question 13:
Difference between this and super in Java
Feature this super
Refers to Current class object Parent (superclass) object
Used for
Refers to current class variables Refers to parent class variables
variables
Used for methods Calls current class methods Calls parent class methods
Calls another constructor in the Calls a constructor in the
Constructor call
same class (this(...)) parent class (super(...))
Access scope Only current class members Parent class members
Usage in static
Cannot be used Cannot be used
context
class Parent {
int x = 10;
void display() { [Link]("Parent method"); }
class Child extends Parent {
int x = 20;
void show() {
[Link](this.x); // 20
[Link](super.x); // 10
[Link](); // Calls Parent method } }