ProgrammingHardProduct-company placement · GATE CS
What happens?
What happens?
class T {
static String s() { return "S"; }
String i() { return "I"; }
}
// in main:
T t = null;
System.out.print(t.s());
System.out.print(t.i());- A. S is printed, then a NullPointerException is thrown
- B. A NullPointerException is thrown before anything is printed
- C. It does not compile: a static method cannot be called through a variable
- D. SI is printed
Correct answer: A. S is printed, then a NullPointerException is thrown
A static method call through a variable uses only its static type; the reference is not dereferenced, so t.s() works even though t is null. The instance call t.i() needs the object and throws.
More OOP with C++ and Java questions
- What is printed? java class O { int x = 1; class I { int x = 2; void p(int x) { System.out.print(x + "" + this.x + O.this.x); } } } // in…
- What is printed? java class P { int v; P(int v) { this.v = v; } public boolean equals(P o) { return o.v == v; } } // in main: List<P> list…
- Which lines fail to compile? cpp struct M { int v; M(int x) : v(x) { } M operator+(const M& o) const { return M(v + o.v); } }; int main()…
- Why does the C++ standard reject a copy constructor declared as T(T other) for a class T?
- A drawing program uses the Visitor pattern: Shape has accept(Visitor v), and Circle implements it as v.visit(this). Visitor has overloads…
- How many of these statements about Java (9 or later) are correct? 1. An interface can declare instance fields. 2. An abstract class can…