Skip to content
C

Java Interview Questions

Advanced Java Interview Questions

Object class internals, varargs, initializer blocks, type conversions, wrapper classes, and modern Java language features from records to text blocks.

Question 1: What is enum?

Ans

An enum defines a fixed set of named constants and can also contain fields, methods, and constructors.

Enums are useful when a value must come from a controlled set such as order status or user role.

Example

java
enum Status { NEW, PAID, CANCELLED } Status s = Status.PAID;

Important Point

Enums are type-safe compared with using arbitrary integer constants or strings.

Question 2: What is autoboxing?

Ans

Autoboxing is automatic conversion from a primitive to its corresponding wrapper object, such as int to Integer. Unboxing converts the wrapper back to the primitive.

It allows primitive values to work with APIs such as generic collections that require reference types.

Example

java
Integer x = 10; // boxing int y = x; // unboxing

Important Point

Unboxing a null wrapper throws NullPointerException.

Question 3: What is immutable class?

Ans

An immutable class creates objects whose observable state cannot change after construction.

Immutability makes objects easier to reason about and safer to share, especially across threads.

Example

java
final class User { private final String name; User(String name) { this.name = name; } public String getName() { return name; } }

Important Point

For mutable fields such as lists, defensive copies may be required; simply making fields final is not enough.

Question 4: What is recursion?

Ans

Recursion is when a method calls itself on a smaller or simpler version of the problem until a base condition is reached.

It is useful for trees, divide-and-conquer algorithms, and problems naturally defined recursively.

Example

java
static int fact(int n) { if (n <= 1) return 1; return n * fact(n - 1); }

Important Point

Every recursive solution needs a correct base case; deep recursion can overflow the thread stack.

Question 5: What is shallow copy vs deep copy?

Ans

A shallow copy duplicates the outer object but may keep references to the same nested mutable objects. A deep copy creates independent copies of the nested state as required by the design.

The distinction matters when copied objects contain mutable collections or child objects.

Example

text
shallow: new object -> same child reference deep: new object -> copied child object

Important Point

Java does not provide one universal deep-copy mechanism; the correct approach depends on the object model.

Question 6: What is object class?

Ans

java.lang.Object is the root class of the Java class hierarchy. Classes ultimately inherit methods such as toString(), equals(), hashCode(), and getClass().

Understanding Object explains why these methods are available on ordinary Java objects.

Example

java
Object x = "Java"; System.out.println(x.toString());

Important Point

Interfaces do not extend Object, although their implementing objects still inherit Object methods through their class.

Question 7: What is method signature?

Ans

For Java methods, the signature used for overloading consists of the method name and parameter types (including type parameter effects under Java's rules), not the return type alone.

It matters when the compiler decides which overloaded method matches a call.

Example

java
void add(int a, int b) {} void add(double a, double b) {}

Important Point

Return type alone cannot distinguish overloads.

Question 8: What is varargs?

Ans

Varargs allows a method to accept zero or more arguments of the same type using type.... Inside the method, the parameter behaves like an array.

It is useful when callers may naturally provide a variable number of values.

Example

java
static int sum(int... nums) { int total = 0; for (int n : nums) total += n; return total; }

Important Point

A varargs parameter must be the last parameter.

Question 9: What is widening and narrowing?

Ans

Widening converts a value to a type that can represent a broader range, such as int to long. Narrowing converts to a potentially smaller type, such as long to int, and may lose data.

Widening primitive conversions are generally implicit; narrowing usually requires an explicit cast.

Example

java
int x = 10; long y = x; int z = (int) y;

Important Point

Floating-point to integer conversion truncates the fractional part.

Question 10: What is a wrapper class?

Ans

Wrapper classes represent primitive values as objects, such as Integer for int and Boolean for boolean.

They are required where Java APIs use reference types, such as generic collections.

Example

java
Integer age = 25;

Important Point

Wrapper objects can be null, unlike primitives.

Question 11: What is record?

Ans

A record is a concise Java type for transparent data carriers. The compiler supplies components, accessors, a canonical constructor, equals, hashCode, and toString according to record rules.

Records are useful for immutable-style DTOs and value-like data.

Example

java
record Student(int id, String name) {} Student s = new Student(1, "Amit");

Important Point

A record's components are final fields, but referenced mutable objects can still themselves be mutable.

Question 12: What is sealed class?

Ans

A sealed class or interface restricts which types may directly extend or implement it.

It is useful when the set of permitted subtypes is intentionally controlled.

Example

java
sealed interface Result permits Success, Failure {} final class Success implements Result {} final class Failure implements Result {}

Important Point

Permitted subclasses must follow Java's rules such as being final, sealed, or non-sealed.

Question 13: What is switch expression?

Ans

A switch expression produces a value, unlike the older statement-oriented switch style.

It can make branching assignments shorter and reduce fall-through mistakes.

Example

java
int days = switch (month) { case 2 -> 28; case 4, 6, 9, 11 -> 30; default -> 31; };

Important Point

The exact available switch features depend on the Java version used by the project.

Question 14: What is local variable type inference var?

Ans

var lets the compiler infer the type of a local variable from its initializer. It does not make Java dynamically typed.

It can reduce repetition when the type is obvious from the right side.

Example

java
var names = new ArrayList<String>();

Important Point

`var` cannot be used for fields, method parameters, or method return types, and the initializer must provide enough type information.

Question 15: What is text block?

Ans

A text block is a Java multiline string literal that makes formatted text easier to write.

It is useful for JSON, SQL, HTML, and other multiline content embedded in source code.

Example

java
String json = """ {"name":"Amit"} """;

Important Point

Indentation and escaping follow Java's text-block rules; do not treat it as a raw string literal.

Continue Your Preparation