12. Overriding equals without hashCode
A Java class Point overrides equals to compare x and y but does not override hashCode. What happens with a HashSet<Point>?
- A.Two equal points can both be added and contains often fails
- B.It works normally because HashSet only uses equals
- C.It fails to compile without a hashCode method
- D.All points land in one bucket and lookups slow down
Show answer
Answer: A. Two equal points can both be added and contains often fails
HashSet first uses hashCode to choose a bucket, then equals inside the bucket. The default hashCode is per object identity, so equal points usually go to different buckets and are never compared. Always override both, or use a Java record.
13. Changing a key after putting it in a set
In Java you add a mutable object to a HashSet, then change a field used by its hashCode. What happens next?
- A.The set rehashes it automatically
- B.contains and remove may fail to find it
- C.The set throws ConcurrentModificationException
- D.The object is copied, so nothing changes
Show answer
Answer: B. contains and remove may fail to find it
The entry stays in the bucket chosen by the old hash. Lookups now compute the new hash and search a different bucket, so the object is effectively lost while still taking space. Use immutable keys.
14. TreeMap or HashMap in Java
You need a Java map that can return all orders with timestamps between 10:00 and 11:00. Which fits and what does it cost?
- A.HashMap, with O(1) operations and range views
- B.HashMap, because it keeps keys sorted
- C.TreeMap, with O(log n) operations and range views
- D.TreeMap, with O(1) operations and no ordering
Show answer
Answer: C. TreeMap, with O(log n) operations and range views
TreeMap is a red-black tree that keeps keys sorted, so subMap(from, to) returns a range directly. Each operation is O(log n). HashMap is faster for exact lookups but has no key order, so ranges need a full scan.
15. Grouping into lists in Java
Which Java line adds order o to the list for its city, creating the list if needed?
- A.map.getOrDefault(city, new ArrayList<>()).add(o)
- B.map.put(city, List.of(o))
- C.map.computeIfAbsent(city, k -> new ArrayList<>()).add(o)
- D.map.putIfAbsent(city, new ArrayList<>()).add(o)
Show answer
Answer: C. map.computeIfAbsent(city, k -> new ArrayList<>()).add(o)
computeIfAbsent returns the existing list or creates, stores and returns a new one. getOrDefault returns a fresh list that is never stored, put with List.of replaces the list each time (and is immutable), and putIfAbsent returns null on first insert.
16. C-style integer division in JS
In C and Java, -7 / 2 with integers gives -3. Which JavaScript expression gives the same result?
- A.Math.floor(-7 / 2)
- B.-Math.ceil(7 / 2)
- C.Math.trunc(-7 / 2)
- D.Math.ceil(-7 / 2) - 1
Show answer
Answer: C. Math.trunc(-7 / 2)
C and Java integer division truncates towards zero, so -3.5 becomes -3. Math.trunc does the same for both signs. Math.floor rounds down to -4, which only matches truncation for positive numbers, and the other two options also give -4.
17. Too many open files error
A busy Java service starts failing with "Too many open files". What should you check first?
- A.Free disk space on the root filesystem
- B.The number of free inodes on /var
- C.The kernel version and installed patches
- D.Its open file limit and current fd count
Show answer
Answer: D. Its open file limit and current fd count
EMFILE means the process hit its per-process descriptor limit (RLIMIT_NOFILE). Compare "Max open files" in /proc/<pid>/limits with the count in /proc/<pid>/fd. Then raise the limit (for example LimitNOFILE in the systemd unit) and check for fd leaks.
18. SQL offline, Java online
The "avg_order_value_30d" feature is computed in SQL for training and re-written in Java for the online service. Values often differ slightly. What fixes the root cause?
- A.Round both values to the nearest rupee
- B.Retrain the model more often to absorb the gap
- C.Add a tolerance check on the model output
- D.Define the feature once and use it for both paths
Show answer
Answer: D. Define the feature once and use it for both paths
Two implementations drift apart through edge cases like nulls, windows and time zones. A single definition, served through a feature store or a shared library, removes this source of training-serving skew.
19. Mapping a response to a class
In REST Assured, how do you turn a JSON order response into an Order Java object?
- A....then().body(Order.class)
- B....then().extract().as(Order.class)
- C....when().cast(Order.class)
- D....given().as(Order.class)
Show answer
Answer: B. ...then().extract().as(Order.class)
extract().as(Order.class) deserialises the body using Jackson or Gson, if on the classpath. You can then use normal Java assertions on the fields. Unknown fields may need the mapper configured to ignore them.
20. What a thread dump reveals
Under load, a Java service stops responding but CPU is low. A thread dump shows 180 of 200 threads in BLOCKED state on the same monitor. What does that suggest?
- A.The CPU is simply too fast for this kind of workload
- B.Lock contention: many threads wait for one synchronized section
- C.The service is writing too few log files under load
- D.The network card has run out of bandwidth
Show answer
Answer: B. Lock contention: many threads wait for one synchronized section
BLOCKED threads are waiting to enter a synchronized block or method held by another thread. When most threads queue on the same lock, throughput collapses even with idle CPU. The stack traces show which code holds the lock, so it can be narrowed or removed.
21. An explicit wait in Selenium
What does this Selenium Java code do?
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement pay = wait.until(
ExpectedConditions.elementToBeClickable(By.cssSelector("[data-test=pay]")));
pay.click();- A.Waits exactly 10 seconds, then clicks
- B.Clicks immediately and retries for 10 seconds
- C.Polls until the element is visible and enabled, up to 10 seconds, then clicks it; otherwise throws TimeoutException
- D.Sets an implicit wait of 10 seconds for all elements
Show answer
Answer: C. Polls until the element is visible and enabled, up to 10 seconds, then clicks it; otherwise throws TimeoutException
Explicit waits poll a condition and return as soon as it is true. Avoid mixing them with implicit waits, which can combine in surprising ways and make timeouts much longer than expected.
22. An unbracketed string
On an engine where AND is applied before OR, how is this string read?
Java OR Python AND Bangalore
- A.(Java OR Python) AND Bangalore
- B.Java AND Python AND Bangalore
- C.Only results that say 'Java OR Python'
- D.Java OR (Python AND Bangalore)
Show answer
Answer: D. Java OR (Python AND Bangalore)
With AND applied first, the engine pairs Python with Bangalore and then adds anyone who mentions Java anywhere. You get Java profiles from every city. Brackets, (Java OR Python) AND Bangalore, make the meaning clear on any engine.