ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
Map<String, Integer> m = new HashMap<>();
System.out.print(m.put("a", 1));
System.out.print(" " + m.put("a", 2));
System.out.print(" " + m.size());- A. 1 2 2
- B. null 1 1
- C. null 1 2
- D. 0 1 1
Correct answer: B. null 1 1
put returns the previous value for the key, or null if there was none. The second put replaces 1 with 2 and returns 1. The key is the same, so the size stays 1.
More OOP with C++ and Java questions
- What does sizeof print for an empty class in C++? cpp class Empty { }; int main() { cout << sizeof(Empty); }
- What is printed? java System.out.print("apple".compareTo("apricot") + " " + "app".compareTo("apple") + " " + "b".compareTo("a"));
- What is printed? java class P { static void sm() { System.out.print("P"); } void im() { System.out.print("P"); } } class C extends P {…
- What is printed? java class P { int x = 10; int getX() { return x; } } class C extends P { int x = 20; int getX() { return x; } } // in…
- What is printed (default JVM settings)? java Integer x = 127, y = 127; Integer p = 128, q = 128; System.out.print((x == y) + " " + (p ==…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("Object"); } static void f(String s)…