ProgrammingModerateAccenture · Capgemini technical pattern
Which line fails to compile?
Which line fails to compile?
final int[] arr = {1, 2, 3};
arr[0] = 10; // L1
arr = new int[3]; // L2
System.out.print(arr[0]); // L3- A. L2
- B. L1
- C. L3
- D. Both L1 and L2
Correct answer: A. L2
final fixes the reference, not the object: the array’s contents can change (L1 is fine), but arr cannot be pointed at a new array (L2 fails).
More OOP with C++ and Java questions
- What is printed? java class P { static int c = 0; P() { c++; } } class Q extends P { Q() { c += 10; } } // in main: new P(); new Q(); new…
- What is printed? java class A { int x = 10; A() { x = 20; } { x = 30; } } // in main: System.out.print(new A().x);
- What is printed? java interface I { default String who() { return "I"; } } class P { public String who() { return "P"; } } class C extends…
- What is printed? java StringBuilder a = new StringBuilder("hi"); StringBuilder b = new StringBuilder("hi"); System.out.print(a.equals(b) +…
- What happens when this Java code is compiled and run? java Object o = new ArrayList<String>(); List<String> l = (List<String>) o; String s…
- What is printed? cpp struct A { A() { cout << "D"; } A(const A&) { cout << "C"; } A& operator=(const A&) { cout << "="; return this; } }…