---Advertisement---

Java Interview Questions and Answers (Part-II)

By Manisha

Published On:

---Advertisement---

 41. How is an abstract class different from an interface in Java?

Answer:

  • An abstract class can include both implemented (concrete) and unimplemented (abstract) methods. It supports constructors and instance variables.
  • An interface, especially before Java 8, only allowed abstract methods and constants. Since Java 8, interfaces can also have default and static methods.
  • Java supports multiple interfaces but only single class inheritance, making interfaces crucial for flexibility and loose coupling in large systems.

Q42. What is the Java Memory Model (JMM), and why is it important?

Answer:
The Java Memory Model (JMM) ensures proper interaction between threads and shared memory. It addresses:

  • Visibility: Ensures changes made by one thread are visible to others.
  • Atomicity: Guarantees indivisible operations.
  • Ordering: Enforces execution order via the “happens-before” rule.
  • It underpins synchronization mechanisms like volatile, synchronized, and java.util.concurrent locks, preventing data inconsistency and race conditions.

Q43. What does the this keyword do in Java?

Answer:
In Java, this refers to the current class instance. It’s commonly used to:

  • Differentiate between class fields and method parameters with the same name.
  • Invoke other constructors within the same class (this()).
  • Pass the current object as an argument in method calls.

java

class Student {

    String name;

    Student(String name) {

        this.name = name; // Resolves naming conflict

    }

}


Q44. What are Generics in Java and why should you use them?

Answer:
Generics enable you to create type-safe code without specifying exact data types upfront. They’re used in classes, methods, and interfaces.

Benefits:

  • Avoids ClassCastException at runtime.
  • Enables code reusability for multiple data types.
  • Enhances readability and maintainability.

Example:

java

List<String> names = new ArrayList<>();

names.add(“Alice”); // Only Strings allowed


Q45. What are access modifiers in Java?

Answer:
Access modifiers in Java control the scope and visibility of classes, methods, and variables:

ModifierScope
privateWithin the same class only
(default)Within the same package
protectedPackage + subclasses (even in other packages)
publicAccessible from everywhere

Use them to encapsulate your code and improve security and design.


Q46. Why is the synchronized keyword used in Java?

Answer:
The synchronized keyword ensures thread safety by allowing only one thread at a time to access a method or block of code.

It prevents race conditions and inconsistent data states in concurrent programming.

java

public synchronized void updateCounter() {

    count++;

}

Use it when multiple threads access shared mutable data.


Q47. What is a static method and how is it different from instance methods?

Answer:
A static method belongs to the class, not to any specific object. Key points:

  • Declared using the static keyword.
  • Can be called using the class name directly.
  • Can’t use this or access non-static members.

Example:

java

class Utils {

    static int square(int x) {

        return x * x;

    }

}

Utils.square(5); // No object creation needed

Static methods are commonly used for utility or helper functions.


Q48. What are Java 8 Streams?

Answer:
Streams in Java 8 provide a functional programming approach to processing data collections.

Core features:

  • Declarative syntax (like SQL).
  • Laziness: Operations are computed only when needed.
  • Parallelism: Easily switch between sequential and parallel streams.

Example:

java

List<String> list = Arrays.asList(“apple”, “banana”, “cherry”);

list.stream().filter(s -> s.startsWith(“a”)).forEach(System.out::println);

Use Streams for cleaner, faster, and more readable code.


Q49. What is garbage collection in Java?

Answer:
Garbage Collection (GC) in Java is the process of automatically managing memory by removing objects that are no longer in use.

Key concepts:

  • No need for manual deallocation like in C++.
  • Frees up heap memory.
  • Uses algorithms like Mark and Sweep, G1GC, and CMS.
  • Prevents memory leaks and OutOfMemoryError.

JVM flags like -Xmx and -XX:+UseG1GC help tune performance.


Q50. What’s the difference between implements and extends?

Answer:

  • implements is used when a class agrees to follow the contract of an interface.
  • extends is used when a class inherits from another class or an interface extends another interface.

Examples:

java

interface Drawable { void draw(); }

class Circle implements Drawable { public void draw() { } }

class Animal {}

class Dog extends Animal {}

Note: Java supports multiple interface implementation, but only single class inheritance.

---Advertisement---

Leave a Comment