Question 71
HardWhat happens when a HashMap key object is mutated after insertion so that its hashCode changes?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Programming Languages
What happens when a HashMap key object is mutated after insertion so that its hashCode changes?
The map updates automatically
Nothing changes
An exception is thrown
get() with that key usually fails, because the entry sits in the bucket for the old hash
Sign up free to see the answer and a step-by-step explanation.
What does the 'volatile' keyword guarantee for a Java field?
Reads and writes go to main memory, so all threads see the latest value, and they aren't reordered across it
Thread-local storage
Atomic increments
The field can't change
Sign up free to see the answer and a step-by-step explanation.
What is the difference between synchronized methods and ReentrantLock?
ReentrantLock can't be reentered
synchronized is faster in every case
They are identical
ReentrantLock offers tryLock, timeouts, fairness and multiple conditions; synchronized is simpler and released automatically
Sign up free to see the answer and a step-by-step explanation.
Why does Java's String.hashCode() cache its value?
Strings are immutable, so the hash never changes and computing it once saves time
To save memory
Because it is random
It doesn't
Sign up free to see the answer and a step-by-step explanation.
What is type erasure in Java generics?
Types are checked only at run time
It makes generics faster
Generics are deleted from the code
Generic type information is removed at compile time, so List<String> and List<Integer> are the same class at run time
Sign up free to see the answer and a step-by-step explanation.
What happens when you call stream().map(...) without a terminal operation?
An exception is thrown
The list is changed
The map runs immediately
Nothing runs, because streams are lazy
Sign up free to see the answer and a step-by-step explanation.
Which garbage collector goal does Java's G1 collector focus on?
Reference counting
Predictable, short pause times on large heaps
Only collecting at exit
Never collecting
Sign up free to see the answer and a step-by-step explanation.
What does Optional.orElse(compute()) do differently from orElseGet(() -> compute())?
They are identical
orElse never calls it
orElseGet always calls it
orElse always calls compute(); orElseGet calls it only when the Optional is empty
Sign up free to see the answer and a step-by-step explanation.
What is a ConcurrentModificationException usually caused by?
Calling size()
Changing a collection structurally while iterating over it with a for-each loop or iterator
Two threads reading a list
Using a HashMap
Sign up free to see the answer and a step-by-step explanation.
In Java, what is autoboxing's surprising effect in: Long sum = 0L; for (long i = 0; i < n; i++) sum += i;
It overflows immediately
Each iteration creates a new Long object, making the loop much slower
sum stays 0
It doesn't compile
Sign up free to see the answer and a step-by-step explanation.