Quiz Space

Programming Concepts using Java · Quiz 2 · 3 Aug 2025 · May 2025 term

Question 8: The merge(K key, V value, remappingFunction) function is …

Question 8

+7 marksOne correct option

The merge(K key, V value, remappingFunction) function is defined as: If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.

Consider the Java code given below.

java
import java.util.*;
public class DepartmentScores {
public static void main(String[] args) {
Map<String, Integer> dept1 = new TreeMap<>();
dept1.put("HR", 75);
dept1.put("Finance", 80);
dept1.put("Tech", 95);
Map<String, Integer> dept2 = new TreeMap<>();
dept2.put("Finance", 85);
dept2.put("Tech", 90);
dept2.put("HR", 78);
dept2.put("Admin", 70);
Map<String, Integer> combined = new TreeMap<>();
for (Map.Entry<String, Integer> e : dept1.entrySet())
combined.put(e.getKey(), e.getValue()); //LINE 1
for (Map.Entry<String, Integer> e : dept2.entrySet())
combined.merge(e.getKey(), e.getValue(),
(oldVal, newVal) -> Math.max(oldVal, newVal));
System.out.println(combined);
}
}

Choose the correct option.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 8 of 15 in the IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 3 Aug 2025, in the May 2025 term (IIT M DEGREE AN EXAM QDB2 03 Aug 2025). It carries 7 marks.

More questions from this paper

  1. Q1Consider the Java code given below. Choose the correct option.
  2. Q2Consider the following Java code. Choose the correct option.
  3. Q3Consider the following Java code. Identify the first assert statement that throws the AssertionError when the class is …
  4. Q4You are developing a logistics system. The following files are part of a Java application. File: TransportUnit.java Fil…
  5. Q5Method Stream.iterate(e, f) returns an infinite sequential ordered Stream produced by iterative application of a functi…
  6. Q6Consider the Java code given below. What will the output be?
  7. Q7Consider the code given below. Choose the correct option.
  8. Q9Consider the Java code given below. Choose the correct option.
  9. Q10Consider the code given below. What will the output be?
  10. Q11Consider the Java code given below. How does class GenericExample look after type erasure?
  11. Q12The following Java code maps books to the number of times they have been borrowed from a library and classifies them as…
  12. Q13Consider the Java code given below. Identify the appropriate option(s) to fill in place of LINE 1 such that the output …
  13. Q14From among the options, choose the code segment(s) that produce(s) the same output as the Java code given in the CODE B…
  14. Q15Consider the Java code given below, which prints the names of employees. From among the options, identify the appropria…