ProgrammingHardAccenture · Cognizant pattern
What is printed?
What is printed?
List<Integer> l = Arrays.asList(1, 2, 3);
try {
l.set(0, 9);
System.out.print(l);
l.add(4);
} catch (UnsupportedOperationException e) {
System.out.print(" U");
}- A. [9, 2, 3] U
- B. U
- C. [9, 2, 3, 4]
- D. [9, 2, 3]
Correct answer: A. [9, 2, 3] U
Arrays.asList returns a fixed-size list backed by the array: set works because it does not change the size, but add throws UnsupportedOperationException. So the list prints, then U.
More OOP with C++ and Java questions
- What is printed? java Map<String, Integer> m = new HashMap<>(); System.out.print(m.put("a", 1)); System.out.print(" " + m.put("a", 2))…
- 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 ==…