ProgrammingHardProduct-company placement · Java
Which line fails to compile?
Which line fails to compile?
class P { final void f(int x) { } }
class C extends P {
void f(long x) { } // L1
void f(int y) { } // L2
}- A. L1 only
- B. Both
- C. Neither
- D. L2 only
Correct answer: D. L2 only
final forbids overriding f(int); renaming the parameter does not change the signature. f(long) is a different signature, a legal overload.
More OOP with C++ and Java questions
- What is printed? java class P { int x; P(int x) { this.x = x; } public boolean equals(Object o) { return o != null && getClass() ==…
- What is printed? java Boolean b1 = true, b2 = true; Character c1 = 'a', c2 = 'a'; Short s1 = 100, s2 = 100; System.out.print((b1 == b2) +…
- What is printed? java List<Integer> l = Arrays.asList(3, 1, 2); Collections.sort(l); System.out.print(l + " "); l.clear();
- What is printed? java List<String> l = new ArrayList<>(List.of("a", "b", "c")); ListIterator<String> it = l.listIterator(); while…
- What is printed? java System.out.print("a.b.c".split(".").length + " " + "a.b.c".split(".").length);
- What is printed? java System.out.print("a.b".replace(".", "x") + " " + "a.b".replaceAll(".", "x"));