Syntax & Basics
OOP (Object-Oriented Programming)
Data Types & Variables
Graphs (BFS/DFS)
Data Structures
100

What is the difference between == and .equals() when comparing strings in Java?

== checks reference equality; .equals() checks content equality.

100

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.

100

What’s the default value of a boolean, int, and Object inside a class?

false, 0, and null, respectively.

100

In what scenario might DFS be preferred over BFS?

When exploring a deep solution space or using less memory is critical.

100

What’s the difference between a Set and a List in Java?

Set does not allow duplicates; List allows ordered, duplicate elements.

200

What is the output of this code?

int x = 5; System.out.println(x++ + ++x);


5 + 7 = 12

200

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.

200

What’s the difference between final and static in Java?

final marks variables unchangeable, static makes members shared across instances.

200

What is the time complexity of BFS and DFS on a graph with V vertices and E edges?

O(V + E)

200

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

300

What happens if you try to access an index out of bounds in an array?  

It throws an ArrayIndexOutOfBoundsException.

300

Given a class hierarchy, how do you achieve polymorphism in Java?

By using superclass references to call overridden methods of subclass objects.

300

Given the code below, what is the output?

double d = 10 / 4;

System.out.println(d);

 

2.0

300

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.

300

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.

400

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

400

What does the super keyword do in a constructor?

Calls the constructor of the superclass.

400

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.

400

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.

400

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.

500

How to fix the following code

String s = null;

if (s.equals("hello")) { ... }

if ("hello".equals(s)) { ... }

500

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.

500

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.

500

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.

500

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.