Quiz Space

Programming Concepts using Java · End Term · 3 Sept 2023 · May 2023 term · Set QPD1-S1

Question 5: Consider the Java code given below.\ You may make use of …

Question 5

+4 marksOne correct option

Consider the Java code given below.
You may make use of the method description given below.
getOrDefault(Object key, V defaultValue): Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

java
import java.util.*;
class Player{
String name;
String year;
int runs;
// Constructor to initialize name, year and runs
}
public class MapTest{
public static void printPlayers(ArrayList<Player> pL) {
var map = new TreeMap<String, Integer>();
for(Player p:pL) {
map.put(p.name, map.getOrDefault(p.name, 0)+p.runs);
}
for (Map.Entry<String, Integer> e:map.entrySet()) {
System.out.println(e.getKey()+" = "+e.getValue());
}
}
public static void main(String[] args) {
ArrayList<Player> pList = new ArrayList<Player>();
pList.add(new Player("Dhoni", "2015", 756));
pList.add(new Player("Kohli", "2017", 1050));
pList.add(new Player("Dhoni", "2017", 345));
pList.add(new Player("Kohli", "2016", 675));
printPlayers(pList);
}
}

What will the output be?

  1. A

    Dhoni = 756
    Kohli = 1050

  2. B

    Dhoni = 345
    Kohli = 675

  3. C

    Dhoni = 1101
    Kohli = 1725

  4. D

    Kohli = 1725
    Dhoni = 1101

Show answer

Correct answer

  • C

    Dhoni = 1101
    Kohli = 1725

Question 5 of 23 in the IIT Madras BS Programming Concepts using Java (Java) End Term paper sat on 3 Sept 2023, in the May 2023 term (IIT M DIPLOMA ET1 EXAM QPD1 S2 03 Sep). It carries 4 marks.

More questions from this paper

  1. Q1Method Stream.iterate(e, f) returns an infinite sequential ordered Stream produced by iterative application of a functi…
  2. Q2Consider the following java code What will the output be?
  3. Q3Consider the java code given below What will the output be?
  4. Q4Figure question
  5. Q6Consider the Java code given below Choose the correct option(s) to be filled in place of CODE BLOCK so that the program…
  6. Q7Consider the following java code What will the output be?
  7. Q8Consider the following java code Identify the first assert statement that throws the AssertionError when the class is e…
  8. Q9Consider the code given below. Choose the correct option.
  9. Q10Consider the Java code given below Choose the correct option to be filled in place of CODE BLOCK so that the program ge…
  10. Q11Consider the code given below.\ Assume that, before execution of the given code, the files "file1.txt" and "file2.txt" …
  11. Q12Consider the code given below. What will the output be?
  12. Q13Consider the code given below. What will the output be?
  13. Q14Consider the Java code given below Choose the correct option(s) to be filled in place of LINE 1 so that the program gen…
  14. Q15Consider the three Java files given below.\ Pack1Test.java: Pack2Test.java: Test.java: Choose the correct option.
  15. Q16ConcurrentHashMap is a hash table implementation that supports full concurrency of retrievals and high expected concurr…
  16. Q17Consider the Java program given below and select the correct option from among the given choices.
  17. Q18Consider the Java program given below. Initial state: window with Submit and Cancel buttons, empty label.\ On clicking …
  18. Q19Consider the following code Which of the following is/are possible output(s)?
  19. Q20Consider the code given below. Choose the correct option(s) to be filled in place of CODE BLOCK so that the output is: …
  20. Q21Consider the Java code given below. Choose the correct option.
  21. Q22Consider the code given below. What will the output be?
  22. Q23Consider the following java code: Identify the line/s which has/have error.