ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
class T {
int n;
{ n += 5; }
T() { this(1); n *= 2; }
T(int k) { n += k; }
}
// in main:
System.out.print(new T().n);- A. 12
- B. 22
- C. 7
- D. 11
Correct answer: A. 12
Instance initializers run once per object, inside the constructor that calls super() (here T(int)), before its body: n = 5, then + 1 = 6, then T()'s remaining body doubles it to 12.
More OOP with C++ and Java questions
- 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…
- How many of these statements about Java exceptions are correct? (i) An overriding method may throw any unchecked exception, even if the…
- What happens when this code is compiled? java try { new java.io.FileReader("x.txt"); } catch (java.io.IOException | Exception e) {…
- What is printed? java try { throw null; } catch (NullPointerException e) { System.out.print("NPE"); } catch (Exception e) {…