ProgrammingEasyWipro · Cognizant technical pattern
A Java class declares only the constructor Account(String id) and no other constructor. What happens with the statement new Account()?
- A. The compiler adds a default no-argument constructor automatically
- B. It compiles, and id is set to null
- C. It throws a NullPointerException at run time
- D. Compilation error: no no-argument constructor exists
Correct answer: D. Compilation error: no no-argument constructor exists
The compiler supplies a default constructor only when a class declares NO constructors. Once Account(String) exists, new Account() does not compile unless a no-arg constructor is written.
More OOP with C++ and Java questions
- What is printed? java int x = 5; x = x++ + ++x; System.out.print(x);
- What is printed? cpp class A { public: int x; A(int v) : x(v) {} }; int main() { A a(5); A b = a; b.x = 9; cout << a.x << b.x; }
- Which line fails to compile? java abstract class Shape { abstract double area(); } class Sq extends Shape { double area() { return 4; } }…
- 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…