What is the difference between == and .equals() when comparing strings in Java?
== checks reference equality; .equals() checks content equality.
Can a class in Java inherit from multiple classes? Explain.
No, Java does not support multiple inheritance with classes to avoid ambiguity (diamond problem). Interfaces are used instead.
What’s the default value of a boolean, int, and Object inside a class?
false, 0, and null, respectively.
In what scenario might DFS be preferred over BFS?
When exploring a deep solution space or using less memory is critical.
What’s the difference between a Set and a List in Java?
Set does not allow duplicates; List allows ordered, duplicate elements.
What is the output of this code?
5 + 7 = 12
What is the difference between abstract classes and interfaces in Java
Abstract classes can have fields and implemented methods; interfaces could only have method declarations.
What’s the difference between final and static in Java?
final marks variables unchangeable, static makes members shared across instances.
What is the time complexity of BFS and DFS on a graph with V vertices and E edges?
O(V + E)
What’s the time complexity of inserting an element in a LinkedList vs an ArrayList?
LinkedList: O(1) at ends, O(n) in middle; ArrayList: O(1) amortized at end, O(n) elsewhere
What happens if you try to access an index out of bounds in an array?
It throws an ArrayIndexOutOfBoundsException.
Given a class hierarchy, how do you achieve polymorphism in Java?
By using superclass references to call overridden methods of subclass objects.
Given the code below, what is the output?
double d = 10 / 4;
System.out.println(d);
2.0
What is the difference between an adjacency matrix and an adjacency list?
Matrix uses O(V²) space; list uses O(V + E). Matrix is better for dense graphs.
What is the difference between a HashMap and a TreeMap in Java? When would you use one over the other?
Use HashMap when you just need fast key-value lookups and don't care about order.
Use TreeMap when you need the keys to be automatically sorted.
What will be the output of the following code, and why?
int a = 10;
int b = 20;
System.out.println("Sum is: " + a + b);
Sum is: 1020
What does the super keyword do in a constructor?
Calls the constructor of the superclass.
Why should you avoid using == to compare floating-point numbers in Java?
Due to rounding errors and precision issues, it's unreliable for comparing doubles or floats.
Modify DFS to detect a cycle in a directed graph. What’s the key concept needed?
Use a recursion stack in addition to visited to track the path and detect back edges.
Why is HashSet faster than TreeSet for basic operations?
HashSet has average O(1) operations using hashing; TreeSet uses a Red-Black Tree with O(log n) operations.
How to fix the following code
String s = null;
if (s.equals("hello")) { ... }
if ("hello".equals(s)) { ... }
You are building a simulation for different types of payment processors (e.g., Stripe, Square, BankTransfer). Each processor has its own complex way of handling authentication and payment processing.
You want to:
Allow switching between processors at runtime
Enforce a common set of operations
Allow adding new processors without changing existing code
How would you design this using polymorphism?
Use polymorphism via interfaces or abstract classes to define a common contract and behavior for all payment processors.
Why does the following method not compile
public static void printStrings(List<String> list) {
for (String s : list) {
System.out.println(s);
}
}
public static void main(String[] args) {
List<String> strings = new ArrayList<>();
List<Object> objects = new ArrayList<>();
strings.add("hello");
objects.add("world");
printStrings(strings);
printStrings(objects);
}
This doesn't compile because List<Object> is not a subtype of List<String>, and vice versa — Java generics are invariant.
Given a connected undirected graph, how can you tell if it's a tree using DFS or BFS?
It must have no cycles and exactly V - 1 edges.
You’re given a basic Stack class with push(), pop(), and isEmpty(). Using only this stack, describe how would you reverse the contents of an ArrayList<String> in-place?
This works because a stack is LIFO (Last-In, First-Out), so when you pop the elements back, they’re in reverse order.