ProgrammingModerateTCS NQT · Infosys interview pattern
Which line fails to compile?
Which line fails to compile?
abstract class Shape { abstract double area(); }
class Sq extends Shape { double area() { return 4; } }
// in main:
Shape s1 = new Sq(); // L1
Shape s2 = new Shape(); // L2
Shape[] arr = new Shape[3]; // L3- A. L1
- B. L3
- C. L2
- D. None of them
Correct answer: C. L2
An abstract class cannot be instantiated, so new Shape() fails. L1 uses a concrete subclass through a base reference, and L3 only creates an array of null references of type Shape, which is allowed.
More OOP with C++ and Java questions
- What is printed? java class K { K() { this(5); System.out.print("A"); } K(int x) { System.out.print("B" + x); } } // in main: new K();
- Which line fails to compile? java final int[] arr = {1, 2, 3}; arr[0] = 10; // L1 arr = new int[3]; // L2 System.out.print(arr[0]); // L3
- 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) +…