Java questions Flashcards

(31 cards)

1
Q

What are the four pillars of OOP in Java?

A

Encapsulation, Inheritance, Polymorphism, and Abstraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain encapsulation with an example.

A

Hiding internal state and exposing behavior via methods; e.g., private fields with public getters/setters in a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between abstraction and encapsulation?

A

Abstraction focuses on exposing essential behavior/contract; encapsulation focuses on hiding implementation details and controlling access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is polymorphism? Give two forms.

A

The ability of a single interface to represent different underlying forms. Compile-time (method overloading) and runtime (method overriding).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between overloading and overriding?

A

Overloading: same method name, different parameters within a class. Overriding: subclass provides its own implementation of a superclass method with same signature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an interface vs an abstract class?

A

Interface: contract of methods (and default/static methods); abstract class: can hold state and partial implementation. Java 8+ interfaces can have default/static methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the main access modifiers in Java?

A

private, default (package-private), protected, public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the ‘final’ keyword used for (variable, method, class)?

A

Variable: constant reference; Method: cannot be overridden; Class: cannot be subclassed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between == and equals()?

A

== compares references for objects; equals() compares logical equality. For primitives, == compares values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an enum in Java and a common pattern using it?

A

A special class for fixed constants. Pattern: enum-based singleton (thread-safe).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between checked and unchecked exceptions?

A

Checked must be declared/handled; unchecked (RuntimeException) need not be declared and often indicate programming errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain try-with-resources.

A

A try statement that declares AutoCloseable resources and closes them automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create an immutable class in Java?

A

Private final fields, no setters, defensive copies for mutables, final class, init via constructor only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

List the main Java Collections interfaces.

A

Collection, List, Set, Queue, Deque, Map (separate hierarchy).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

ArrayList vs LinkedList trade-offs?

A

ArrayList: O(1) random access, amortized O(1) append; LinkedList: O(1) add/remove at ends but O(n) random access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

HashSet vs TreeSet vs LinkedHashSet?

A

HashSet: unordered O(1) avg; TreeSet: sorted O(log n); LinkedHashSet: insertion order O(1) avg.

17
Q

How do HashMap and ConcurrentHashMap differ?

A

HashMap is not thread-safe; ConcurrentHashMap supports concurrent access with bucket-level locking and non-blocking reads.

18
Q

What underlies ArrayDeque and common uses?

A

Resizable circular array; efficient stack/queue ops at both ends.

19
Q

Binary search vs linear search complexity?

A

Summary:
Linear search is slower for large datasets (O(n)), works on unsorted data.
Binary search is much faster (O(log n)), but only works on sorted data.

Linear search checks each element one by one until it finds the target or reaches the end.

Time complexity: O(n) (where n is the number of elements)
Binary search repeatedly divides a sorted array in half to find the target.

Time complexity: O(log n) (requires the array to be sorted)

20
Q

Difference between process and thread?

A

Process has its own memory; threads share memory within a process.

21
Q

What are synchronized blocks/methods for?

A

Synchronized blocks and methods in Java are used to control access to shared resources in multithreaded environments.

They prevent multiple threads from executing critical sections of code simultaneously, avoiding data inconsistency and race conditions.

By locking an object or method, only one thread can execute the synchronized code at a time.

22
Q

What is a deadlock and how to prevent it?

A

A deadlock is a situation in concurrent programming where two or more threads are blocked forever, each waiting for the other to release a resource.

Prevention techniques:

Acquire locks in a fixed global order.
Use lock timeouts.
Minimize the scope and number of locks.
Avoid nested locking.
Use higher-level concurrency utilities (e.g., java.util.concurrent).
Deadlocks can be avoided by careful design and by ensuring threads do not hold resources while waiting for others.

23
Q

Explain ThreadPoolExecutor benefits.

A

Reuses threads, controls concurrency, manages queuing, provides lifecycle hooks.

24
Q

What are CompletableFuture and common methods?

A

Async API: supplyAsync, thenApply, thenCompose, thenAccept, allOf, anyOf, exceptionally, handle, join.

25
Main memory areas of the JVM?
Heap, Stack, Metaspace, Code cache, plus thread stacks/native memory.
26
Common garbage collectors in modern JVMs?
Serial, Parallel, G1 (default), ZGC, Shenandoah; each balances throughput vs latency.
27
Stream vs Collection?
Collection stores data; Stream is a processing pipeline (map/filter/reduce) and doesn't store elements.
28
Checked exceptions with lambdas—how to handle?
Wrap in custom functional interfaces, rethrow as unchecked, or handle inside the lambda.
29
What is Optional and when to use it?
Represents presence/absence; preferred as return type to avoid nulls, avoid as fields/params or in collections.
30
What is SOLID?
Five OO design principles: SRP, OCP, LSP, ISP, DIP.
31
Explain dependency injection benefits.
Promotes loose coupling, testability, and separation of concerns (e.g., Spring DI).