java_2025_05_23_212759 Flashcards

(24 cards)

1
Q

Can class be declared as static

A

A top level class cannot be declared as static
Nested class can be declared as static
A static nested class cannot have access to the instance member of the outer class.

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

Math.random is not recommended

A

Math.random uses pseud-random and can be predicted if the seed is known. Instead use SecureRandom for security critical applications.

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

what happens when you pass null to overloaded method

A

Java will try to resolve the most specific method. If there is an ambiguity it results in a compilation error.

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

How String.intern works

A

Intern() moves a string to a string pool. If the string already exists in the pool it returns a reference to that string instead of creating a new object.

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

Can a class extend multiple interfaces with the same signature

A

Yes, as long it provides implementation for the method.

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

How do you make a HashMap thread-safe? What are the alternatives?

A

Use Collections.synchronizedMap():
Map synchronizedMap = Collections.synchronized(myhashmap);
Use ConcurrentHashMap for better concurrency

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

HashMap vs LinkedHashMap

A

HashMap: Does not maintain any order of keys.
LinkedHashMap: Maintains the insertion order or access order (with accessOrder=true in constructor).

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

How does HashMap handle collisions? How did it change in Java 8?

A

Before Java 8: Collisions are resolved using separate chaining (linked list). Multiple keys mapping to the same bucket are stored in a linked list.
In Java8 If the number of entries in a bucket exceeds a threshold (default 8), the linked list is replaced with a balanced binary tree (red-black tree) for faster lookups (O(log n) instead of O(n)).

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

What is the purpose of WeakHashMap?

A

A WeakHashMap uses weak references for keys.
If a key is no longer referenced elsewhere in the application, the key-value pair is automatically removed from the map by the garbage collector.

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

How does ConcurrentHashMap work internally?

A

Before Java 8: Used a segmented lock mechanism. The map was divided into segments, and each segment could be locked independently for better concurrency.
Java 8 and later: Uses a CAS (Compare-And-Swap) based approach with a bucket-level lock, significantly improving performance.

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

What is EnumMap? Why is it faster than other maps?

A

EnumMap is a specialized map for enums as keys.
It uses an array internally for storage, where the ordinal of the enum is used as the index.
No hashing is required, leading to constant-time performance.

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

Can you override a method which throws an exception with a method which does not?

A

Yes, you can override a method and remove or reduce the exception type. However, you cannot throw a new checked exception.

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

What is Optional ? How does it prevent NullPointerException?

A

Optional is a Wrapper class that prevents null value by enforcing checks before accessing the value.

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

Why are String object immutable in Java

A

Prevents modification of sensitive dataString literal are stored in a pool to optimize the memoryImmutable objects are thread safe

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

Difference between StringBuffer and StringBuilder

A

StringBuffer is Thread-safe, while StringBuilder is notStringBuffer is slow in performance, while StringBuilder is fast

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

What is Autoboxing & Unboxing in Java

A

Autoboxing: Converting a primitive value to a Wrapper class
Unboxing: Converting a Wrapper class to primitive
Java automatically performs the conversion.

17
Q

Difference between HashMap and HashTable

A

HashMap is not ThreadSafe, while Hashtable isHashMap is fast, while HashTable is slow

18
Q

What is the difference between stack and heap memory?

A

Stack memory stores method calls and local variables, while heap memory stores objects.

19
Q

What is Java Reflection, and why is it used?

A

Reflection allows runtime access to classes, methods, and fields, often used for frameworks.

20
Q

What are the differences between Serializable and Externalizable?

A

Serializable uses a default serialization mechanism, while Externalizable requires custom serialization logic.

21
Q

What happens if an exception is thrown in the finally block?

A

The exception is propagated, and the original exception (if any) is suppressed.

22
Q

Can the finally block be skipped?

A

Yes, it can be skipped if the JVM exits (e.g., System.exit(0)), or if the thread is terminated.

23
Q

Will the finally block execute if there is a return in both try and catch?

A

Yes, but the return value of the finally block will override other return values.

24
Q

Difference Between @Component and @Configuration in Spring Boot

A

A class annotated with @Configuration ensures that methods annotated with @Bean are executed only once per container.

If you use @Component, the @Bean methods inside it might create new instances every time they are called.