What is the difference between a primitive type and a reference type in Java?
Primitive types store actual values (such as int, long, boolean). Reference types store a reference to an object (such as String, List, or a custom class). Primitives cannot be null; references can be null.
Name the 8 primitive types in Java.
byte, short, int, long, float, double, char, boolean.
What is the default value of an int field? What about a local variable?
An int field defaults to 0. A local variable has no default value and must be initialized before use.
What is the difference between == and .equals() for objects in Java?
== compares reference identity (whether two references point to the exact same object). .equals() compares logical/value equality as defined by the class (when overridden).
Why can == be misleading with Integer objects?
Because Integer uses caching for some small values (commonly -128 to 127). Two Integer objects in that range may refer to the same cached instance, so == may appear to work even though it is not reliable for value comparison.
Is Java pass-by-value or pass-by-reference?
Java is always pass-by-value. For objects, the value passed is the reference, which is why you can mutate the object but cannot reassign the caller’s variable.
What happens when you pass an object to a method in Java?
A copy of the reference is passed by value. The method can modify the object through that reference, but reassigning the parameter does not affect the caller’s reference.
What does the final keyword mean for a variable?
A final variable cannot be reassigned after initialization. If it is a reference, the reference cannot point to another object, but the object itself may still be mutable.
What does final mean for a method?
A final method cannot be overridden by subclasses.
What does final mean for a class?
A final class cannot be extended. Example: String is final.
Does final make an object immutable?
No. final only prevents reassignment of the reference. The object can still change unless the object’s state itself is designed to be immutable.
What is autoboxing in Java?
Autoboxing is the automatic conversion between primitives and their wrapper types, for example int to Integer.
What is unboxing in Java?
Unboxing is the automatic conversion from a wrapper type to its primitive type, for example Integer to int.
Why can autounboxing cause NullPointerException?
If a wrapper object like Integer is null and Java tries to unbox it to a primitive int, it throws NullPointerException.
What is String immutability and why is it useful?
A String cannot be changed after creation. Immutability improves safety, thread-friendliness, caching, and predictable behavior for hashing and reuse.
What is the difference between String, StringBuilder, and StringBuffer?
String is immutable. StringBuilder is mutable and not synchronized, so it is faster in single-threaded contexts. StringBuffer is mutable and synchronized, so it is thread-safe but usually slower.
Why is repeated string concatenation in a loop often inefficient?
Because each concatenation can create a new String object. StringBuilder is usually more efficient for repeated concatenation.
What is an enum in Java and when would you use it?
An enum is a special type for a fixed set of constants, such as statuses or roles. It improves type safety and readability over raw strings or ints.
What is null in Java?
null means a reference points to no object. Accessing methods or fields on a null reference causes NullPointerException.
What is the difference between stack and heap in Java?
The stack stores method frames and local variables. The heap stores objects and arrays. Stack memory is managed per thread; heap memory is managed by the garbage collector.
What is encapsulation?
Encapsulation is bundling data and behavior together and hiding internal implementation details so that object invariants are protected.
Why is encapsulation important?
It prevents invalid states, reduces coupling, and makes code easier to change safely.
What is inheritance?
Inheritance lets a class reuse and extend the behavior of another class using extends.
Why is composition often preferred over inheritance?
Composition is usually more flexible and less tightly coupled. Inheritance can create fragile class hierarchies and make changes in the parent class risky for subclasses.