Java Fundamentals: Key Questions and Explanations


Java Fundamentals: Key Questions and Explanations



❓ What is Java and how does it work?

💡 Explain the concept of OOP (Object-Oriented Programming) in Java.

🧠 What are the different types of memory used by Java?

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

🔐 What are access modifiers in Java?

✅ Explain the use of the final keyword in Java.

🔎 What is the difference between == and equals() in Java?

🔁 Explain the concept of method overloading and method overriding.

🛠️ What is a constructor in Java?

⚡ Explain the concept of static methods and variables in Java.

🔄 What is the difference between ArrayList and LinkedList in Java?

🚨 What is exception handling in Java?

🔒 What is a Singleton class in Java?

⚙️ What is multithreading in Java?

🔝 What is the super keyword in Java?

🗝️ What is the difference between HashMap and Hashtable?

💬 What is the transient keyword in Java?

🧳 Explain the use of volatile keyword in Java.

🔡 What are String, StringBuilder, and StringBuffer in Java?

💡 What is the default keyword in interfaces in Java 8


ANSWERS:-

❓  What is Java and how does it work?

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It follows the "Write Once, Run Anywhere" (WORA) principle due to its platform-independent nature, meaning Java programs can run on any system with a compatible Java Virtual Machine (JVM).


Java code is compiled into bytecode (.class files) by the Java Compiler (javac). This bytecode is then interpreted and executed by the JVM, making Java secure, portable, and robust.

===================================================================

💡 Explain the concept of OOP (Object-Oriented Programming) in Java.

Java follows Object-Oriented Programming (OOP), which is based on the concept of objects and classes. The key principles of OOP are:


Encapsulation – Wrapping data (variables) and methods together within a class.

Abstraction – Hiding implementation details and exposing only essential features.

Inheritance – Allowing a class to acquire properties and behavior of another class.

Polymorphism – The ability to take multiple forms (method overloading and overriding).

===================================================================

🧠  What are the different types of memory used by Java?

Java uses different types of memory in the Java Runtime Environment (JRE):


Heap Memory – Stores objects and class instances.

Stack Memory – Stores method calls, local variables, and function calls.

Method Area (Metaspace in Java 8+) – Stores class metadata, method code, and runtime constants.

PC Register – Keeps track of instruction execution in a thread.

Native Method Stack – Stores native method calls used in JNI.

===================================================================

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

Component

Description

JDK (Java Development Kit)

Includes JRE + development tools (compiler, debugger, etc.). Used for development.

JRE (Java Runtime Environment)

Contains JVM + libraries needed to run Java applications.

JVM (Java Virtual Machine)

Executes Java bytecode. It converts bytecode into machine code at runtime.

🔐 What are access modifiers in Java?

Access modifiers define the scope of a variable, method, or class.

Modifier

Scope

private

 

Accessible only within the same class.

 

default (no modifier)

Accessible within the same package.

 

Protected

Accessible within the same package and subclasses.

 

public

Accessible from anywhere.

 

Explain the use of the final keyword in Java.

final variable – Value cannot be changed after initialization.

final method – Cannot be overridden by subclasses.

final class – Cannot be extended (e.g., final class MyClass {}).

===================================================================

🔎 What is the difference between == and equals() in Java?

  • == compares memory reference (checks if two objects are the same instance).
  • equals() compares actual values (overridden in String and wrapper classes).

Example:

String s1 = new String("Hello");

String s2 = new String("Hello");

System.out.println(s1 == s2); 

System.out.println(s1.equals(s2));     

===================================================================

🔁 Explain the concept of method overloading and method overriding.

Method Overloading – Defining multiple methods with the same name but different parameters within the same class. (Compile-time polymorphism)

Method Overriding – Redefining a method in a subclass with the same name and parameters as in the parent class. (Runtime polymorphism)

===================================================================

🛠️ What is a constructor in Java?

A constructor is a special method that initializes an object.

Types:

Default Constructor – Provided automatically if no constructor is defined.

Parameterized Constructor – Accepts arguments to initialize an object.

Example:

class Car {

    String model;

    Car(String m) { model = m; }

}

===================================================================

Explain the concept of static methods and variables in Java.

static variable – Shared among all instances of a class.

static method – Can be called without creating an instance.

Example:

class Example {

    static int count = 0;

    static void display() { System.out.println("Static method"); }

}

===================================================================

🔄 What is the difference between ArrayList and LinkedList in Java?

Feature

ArrayList

LinkedList

Storage

Dynamic array

Doubly linked list

Access time

Fast (O(1))

Slow (O(n))

Insertion/Deletion

Slow (O(n))

Fast (O(1))

🚨 What is exception handling in Java?

Java handles runtime errors using:

1️⃣ try-catch → Used to handle exceptions.
2️⃣ throw → Used to explicitly throw an exception.
3️⃣ throws → Used in method signatures to indicate potential exceptions.
4️⃣ finally → Always executes, whether an exception occurs or not.

Example:

try {

    int a = 5 / 0;

} catch (ArithmeticException e) {

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

}

===================================================================

🔒 What is a Singleton class in Java?

A Singleton class allows only one instance of a class.

Example:

class Singleton {

    private static Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {

        if (instance == null) instance = new Singleton();

        return instance;

    }

}

===================================================================

⚙️ What is multithreading in Java?

Multithreading allows parallel execution of tasks.


Ways to create threads:

  1. Extending Thread class.
  2. Implementing Runnable interface.

Example:

class MyThread extends Thread {

    public void run() { System.out.println("Thread Running"); }

}

===================================================================

🔝 What is the super keyword in Java?

Calls parent class constructor: super();

Calls parent class method: super.methodName();

🗝️ What is the difference between HashMap and Hashtable?

Feature

HashMap

Hashtable

Thread-safe?

No

Yes

Allows null keys?

Yes

No

💬 What is the transient keyword in Java?

A transient variable is not serialized when an object is saved.

Example:

class Example implements Serializable {

    transient int temp; // will not be saved

}

===================================================================

🧳 Explain the use of volatile keyword in Java.

Ensures visibility of changes to a variable across threads.

Example:

volatile int flag = 1; 

===================================================================

🔡 What are String, StringBuilder, and StringBuffer in Java?

Feature

String

StringBuilder

StringBuffer

Mutable?

No(immutable)

Yes

Yes

Thread-safe?

Yes

No

Yes


💡 What is the default keyword in interfaces in Java 8?

Allows methods with implementations in interfaces.

Example:

interface MyInterface {

    default void show() { System.out.println("Default Method"); }

}




Comments

Popular Posts