Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Java Interview Questions

The document contains 25 Java interview questions and answers, covering a range of topics from basic to advanced concepts. Key topics include Java's features, differences between various Java components (JDK, JRE, JVM), data structures (ArrayList vs. LinkedList), exception handling, access modifiers, and multithreading. It also addresses advanced concepts like functional interfaces, streams, and deadlocks.

Uploaded by

ottacc2022
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Interview Questions

The document contains 25 Java interview questions and answers, covering a range of topics from basic to advanced concepts. Key topics include Java's features, differences between various Java components (JDK, JRE, JVM), data structures (ArrayList vs. LinkedList), exception handling, access modifiers, and multithreading. It also addresses advanced concepts like functional interfaces, streams, and deadlocks.

Uploaded by

ottacc2022
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Here are 25 Java interview questions along with their answers, covering basic to advanced

concepts:

1. What is Java?

Java is a high-level, object-oriented programming language developed by Sun Microsystems


(now owned by Oracle). It is platform-independent due to its "Write Once, Run Anywhere"
(WORA) feature enabled by the Java Virtual Machine (JVM).

2. What are the main features of Java?

 Platform Independence (JVM)


 Object-Oriented
 Automatic Memory Management (Garbage Collection)
 Multi-threaded
 Secure and Robust
 Rich API

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

Component Description
Includes JRE + development tools (compiler, debugger,
JDK (Java Development Kit)
etc.)
JRE (Java Runtime
Includes JVM + libraries to run Java applications
Environment)
JVM (Java Virtual Machine) Converts bytecode into machine-specific code

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

Feature Abstract Class Interface


Can have both abstract & Only abstract methods (Java 7); Java 8+
Methods
concrete methods allows default & static methods
Variables Can have instance variables Only public, static, and final constants
Multiple
Not supported Supported
Inheritance
Constructor Yes No

5. What is the difference between == and .equals()?


 == compares memory reference (checks if both objects refer to the same memory
location).
 .equals() compares content (checks if two objects have the same value).

Example:

String s1 = new String("Java");


String s2 = new String("Java");
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)

6. What are wrapper classes in Java?

Wrapper classes convert primitive types into objects.


Example:

 int → Integer
 double → Double
 char → Character

Example:

Integer num = Integer.valueOf(10); // Autoboxing


int value = num.intValue(); // Unboxing

7. What is the difference between ArrayList and LinkedList?

Feature ArrayList LinkedList


Storage Dynamic array Doubly linked list
Access time Fast (O(1) for get()) Slow (O(n) for get())
Insertion/Deletion Slow (O(n) at middle) Fast (O(1) at head/tail)

8. What is the difference between HashMap and Hashtable?

Feature HashMap Hashtable


Thread-safe No Yes (synchronized)
Null keys/values 1 null key, multiple null values No null keys or values
Performance Faster Slower

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

Feature String StringBuffer StringBuilder


Mutability Immutable Mutable Mutable
Thread-safe Yes Yes (synchronized) No (not synchronized)
Performance Slow Slower Fastest
Example:

StringBuilder sb = new StringBuilder("Hello");


sb.append(" Java");
System.out.println(sb); // Hello Java

10. What is an exception in Java?

An exception is an unexpected event that disrupts program execution. Java has:

 Checked Exceptions (e.g., IOException)


 Unchecked Exceptions (e.g., NullPointerException)
 Errors (e.g., OutOfMemoryError)

Example:

try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
}

11. What are Java access modifiers?

Modifier Scope
public Accessible everywhere
protected Accessible within the same package + subclasses
default Accessible within the same package only
private Accessible within the same class only

12. What is polymorphism?

Polymorphism allows a method to have different forms.

 Compile-time polymorphism (Method Overloading)


 Runtime polymorphism (Method Overriding)

Example:

class Animal {
void sound() { System.out.println("Animal makes a sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
}

13. What is multithreading in Java?


Multithreading allows concurrent execution of multiple threads.
Example:

class MyThread extends Thread {


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

14. What is the volatile keyword in Java?

volatile ensures that a variable's value is always read from main memory, preventing
caching issues in multi-threaded environments.

15. What is the final keyword in Java?

 Final variable → Cannot be reassigned.


 Final method → Cannot be overridden.
 Final class → Cannot be inherited.

Example:

final class MyClass { }


// class SubClass extends MyClass { } // Error: cannot inherit from final
class

16. What is serialization in Java?

Serialization converts an object into a byte stream for storage/transmission.

Example:

class Employee implements Serializable { int id; String name; }

17. What is the transient keyword?

transient prevents serialization of a variable.

Example:

class Employee implements Serializable {


transient int salary; // Won't be serialized
}

18. What is a constructor in Java?


A constructor initializes an object when it is created.

Example:

class Person {
Person() { System.out.println("Constructor called"); }
}

19. What is the difference between throw and throws?

 throw → Used to explicitly throw an exception.


 throws → Declares exceptions in a method signature.

Example:

void myMethod() throws IOException { }

20. What is a functional interface in Java?

A functional interface has exactly one abstract method.

Example:

@FunctionalInterface
interface MyInterface {
void sayHello();
}

21. What are Java Streams?

Java Streams process data in a functional style.

Example:

List<String> names = List.of("John", "Jane", "Jack");


names.stream().filter(n -> n.startsWith("J")).forEach(System.out::println);

22. What is the difference between Callable and Runnable?

 Runnable → Does not return a value.


 Callable → Returns a value.

Example:

Callable<Integer> task = () -> 10;


23. What is reflection in Java?

Reflection allows inspection/modification of classes, methods, and fields at runtime.

24. What is the purpose of the default method in interfaces?

default methods in interfaces provide implementation while allowing backward


compatibility.

25. What is a deadlock?

Deadlock occurs when two or more threads are waiting on each other, preventing execution.

Want more in-depth explanations? Let me know! 🚀

You might also like