ProgrammingHardProduct-company placement · Java
Which line fails to compile?
Which line fails to compile?
abstract class Shape { abstract double area(); }
// in main:
Shape a = new Shape(); // L1
Shape b = new Shape() { double area() { return 1; } }; // L2
Shape[] c = new Shape[3]; // L3- A. L2 only
- B. L1 and L3
- C. L1 and L2
- D. L1 only
Correct answer: D. L1 only
An abstract class cannot be instantiated directly, but an anonymous subclass that implements area() can, and an array of an abstract type only creates references (all null).
More OOP with C++ and Java questions
- Which lines fail to compile? java class P { void f() { } static void g() { } } class C extends P { static void f() { } // L1 void g() { }…
- Which line fails to compile? java class P { Number get() { return 1; } int size() { return 0; } } class C extends P { Integer get() {…
- What is printed? java class T { int n; { n += 5; } T() { this(1); n = 2; } T(int k) { n += k; } } // in main: System.out.print(new T().n);
- What is printed? java class A { public String toString() { return "A"; } } class B extends A { public String toString() { return "B" +…
- What happens when Z is compiled? java interface X { int V = 1; } interface Y { int V = 2; } class Z implements X, Y { int get() { return…
- What happens when this is compiled? java interface A { default void hello() { System.out.print("A"); } } interface B extends A { void…