0% found this document useful (0 votes)
10 views17 pages

Java Fundamentals - 40 Interview-Level Questions

The document provides a comprehensive overview of Java programming concepts, including definitions of Java, JVM, JRE, and JDK. It covers fundamental topics such as data types, object-oriented principles (inheritance, polymorphism, encapsulation, abstraction), exception handling, and collections. Additionally, it explains key features like method overloading, method overriding, and the differences between various data structures and modifiers.

Uploaded by

SUTARIYA TULASI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

Java Fundamentals - 40 Interview-Level Questions

The document provides a comprehensive overview of Java programming concepts, including definitions of Java, JVM, JRE, and JDK. It covers fundamental topics such as data types, object-oriented principles (inheritance, polymorphism, encapsulation, abstraction), exception handling, and collections. Additionally, it explains key features like method overloading, method overriding, and the differences between various data structures and modifiers.

Uploaded by

SUTARIYA TULASI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1️⃣ What is Java?

Answer:​
Java is a high-level, object-oriented, platform-independent programming language. It uses the
concept of "write once, run anywhere" because Java programs run on the JVM.

2️⃣ What is JVM, JRE, and JDK?


✅ JVM (Java Virtual Machine):​
Executes Java bytecode on any machine.

✅ JRE (Java Runtime Environment):​


Includes JVM + libraries needed to run Java programs.

✅ JDK (Java Development Kit):​


Includes JRE + tools to develop Java programs (compiler, debugger).

3️⃣ What are primitive data types in Java?


Answer:

●​ byte (8-bit)​

●​ short (16-bit)​

●​ int (32-bit)​

●​ long (64-bit)​

●​ float (32-bit, decimal)​

●​ double (64-bit, decimal)​

●​ char (16-bit, Unicode character)​


●​ boolean (true/false)​

✅ Example:
Java

int age = 25;


boolean isStudent = true;

4️⃣ What is the difference between == and


.equals()?
✅ == compares reference (memory address).​
✅ .equals() compares content/values.
Example:

Java

String a = new String("Hello");


String b = new String("Hello");

System.out.println(a == b); // false


System.out.println(a.equals(b)); // true

5️⃣ What is a class and an object?


✅ Class: Blueprint for objects.​
✅ Object: Instance of a class.
Example:

Java

class Car {
String model;
void drive() {
System.out.println("Driving...");
}
}

Car myCar = new Car();

6️⃣ What is a constructor?


Answer:​
A special method that is called when an object is created. It initializes the object.

✅ Example:
Java

class Student {
String name;
Student(String name) {
this.name = name;
}
}

7️⃣ What is method overloading?


Answer:​
Same method name with different parameter lists in same class.

✅ Example:
Java

int add(int a, int b) { return a + b; }


double add(double a, double b) { return a + b; }
8️⃣ What is method overriding?
Answer:​
Subclass provides a specific implementation of a method already defined in the parent class.

✅ Example:
Java

class Animal {
void sound() {
System.out.println("Animal sound");
}
}

class Dog extends Animal {


void sound() {
System.out.println("Bark");
}
}

9️⃣ What is inheritance?


Answer:​
Mechanism where one class acquires properties of another.

✅ Example:
Java

class Parent {
void show() {
System.out.println("Parent");
}
}
class Child extends Parent {
void display() {
System.out.println("Child");
}
}

10️⃣ What is polymorphism?


Answer:​
Ability to take many forms.

●​ Compile-time: method overloading​

●​ Run-time: method overriding​

11️⃣ What is encapsulation?


Answer:​
Binding data (fields) and code (methods) together.

●​ Achieved using private fields + public getters/setters.​

✅ Example:
Java

class Account {
private double balance;

public void setBalance(double balance) {


this.balance = balance;
}
public double getBalance() {
return balance;
}
}

12️⃣ What is abstraction?


Answer:​
Hiding implementation details and showing only functionality.

✅ Using:
●​ Abstract classes​

●​ Interfaces​

Abstract class example:

Java

abstract class Shape {


abstract void draw();
}

13️⃣ What is an interface in Java?


✅ Pure abstraction, no implementation (until Java 8+ default methods).​
✅ Class implements interface to define methods.
Example:

Java

interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Bark");
}
}

14️⃣ Difference between abstract class and


interface?
Feature Abstract Class Interface

Methods Can have implemented Only abstract (or default/static in Java 8+)
methods

Variables Can have fields Only public static final

Inheritance Single inheritance Multiple interfaces

15️⃣ What is the final keyword?


✅ Used to restrict modification.
✅ Examples:
●​ Final variable: value can't change.​

●​ Final method: can't override.​

●​ Final class: can't inherit.​

Code:

Java

final int MAX = 100;


final class Car {}

16️⃣ What is static in Java?


✅ Belongs to class, not instance.​
✅ Shared across all objects.
Example:

Java

class Counter {
static int count = 0;
Counter() { count++; }
}

17️⃣ Explain this keyword.


✅ Refers to current object.
Example:

Java

class Student {
String name;
Student(String name) {
this.name = name;
}
}
18️⃣ Difference between Array and
ArrayList?
✅ Array:
●​ Fixed size​

●​ Can store primitives and objects​

✅ ArrayList:
●​ Dynamic size​

●​ Only objects​

●​ Part of java.util​

Example:

Java

int[] arr = new int[5];


ArrayList<Integer> list = new ArrayList<>();
list.add(10);

19️⃣ What is exception handling?


✅ Mechanism to handle runtime errors.​
✅ Uses try-catch-finally.
Example:

Java

try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Cleanup");
}

20️⃣ What is checked and unchecked


exception?
✅ Checked: Checked at compile time. (IOException, SQLException)​
✅ Unchecked: RuntimeException and its subclasses.

21️⃣ What is the difference between == and


equals() for objects?
✅ == compares references (memory).​
✅ .equals() compares contents.

22️⃣ What is String immutability?


✅ Strings in Java cannot be changed once created.
Example:

Java

String s = "Hello";
s.concat("World"); // doesn't change s
23️⃣ How to reverse a String in Java?
✅ Example:
Java

String str = "hello";


String reversed = new StringBuilder(str).reverse().toString();
System.out.println(reversed); // olleh

24️⃣ What is a package?


✅ Group of related classes/interfaces.​
✅ Avoids name conflicts.
Example:

Java

package myapp.utils;

25️⃣ What is access modifier?


✅ Controls visibility.
●​ public​

●​ private​

●​ protected​

●​ default (package-private)​

Example:

Java
public class Test { }

26️⃣ What is Collection Framework?


✅ Set of classes/interfaces to store and manipulate groups of data.
✅ Interfaces:
●​ List​

●​ Set​

●​ Map​

●​ Queue​

✅ Classes:
●​ ArrayList​

●​ HashSet​

●​ HashMap​

27️⃣ What is HashMap in Java?


✅ Stores key-value pairs.​
✅ Allows null keys and values.​
✅ Unordered.
Example:

Java

Map<String, Integer> map = new HashMap<>();


map.put("A", 1);
map.put("B", 2);

28️⃣ What is inheritance in Java?


✅ One class acquires properties of another using extends.
Example:

Java

class Animal {}
class Dog extends Animal {}

29️⃣ What is constructor overloading?


✅ Multiple constructors with different parameters.
Example:

Java

class Person {
Person() {}
Person(String name) {}
}

30️⃣ Can Java support multiple inheritance?


✅ No for classes (avoids diamond problem).​
✅ Yes via interfaces.
31️⃣ What is the main method signature?
Java

public static void main(String[] args)

✅ public: accessible anywhere​


✅ static: no object needed​
✅ void: no return​
✅ main: entry point

32️⃣ What is super keyword?


✅ Refers to parent class.
Example:

Java

class Parent {
Parent() {
System.out.println("Parent");
}
}
class Child extends Parent {
Child() {
super();
System.out.println("Child");
}
}

33️⃣ What is a Wrapper class?


✅ Converts primitive to object.
✅ Examples:
●​ int → Integer​

●​ char → Character​

Java

Integer a = 5;

34️⃣ What is autoboxing and unboxing?


✅ Autoboxing: primitive → wrapper​
✅ Unboxing: wrapper → primitive
Java

Integer a = 5; // autoboxing
int b = a; // unboxing

35️⃣ Difference between ArrayList and


LinkedList?
Feature ArrayList LinkedList

Access Fast random access Slow random access


(index)

Insert/Delete Slower (shifting) Faster (no shifting)

Memory Less memory overhead More memory (node links)

36️⃣ What is final class?


✅ Cannot be extended.
Java

final class Car {}

37️⃣ How do you create a thread in Java?


✅ By extending Thread:
Java

class MyThread extends Thread {


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

✅ Or implementing Runnable:
Java

class MyRunnable implements Runnable {


public void run() {
System.out.println("Runnable running");
}
}

38️⃣ What is synchronization?


✅ Prevents concurrent access issues in multithreading.
Java

synchronized void print() { }


39️⃣ Explain try-catch-finally.
✅ try: code that may throw exception.​
✅ catch: handle exception.​
✅ finally: always runs for cleanup.

40️⃣ What is garbage collection?


✅ Automatic memory management.​
✅ Unreachable objects are collected by JVM.

You might also like