ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class T {
static String hi() { return "hi"; }
}
// in main:
T t = null;
System.out.print(t.hi());- A. NullPointerException
- B. hi
- C. null
- D. Compilation error
Correct answer: B. hi
A static method call through a reference is compiled as T.hi(); the reference is never dereferenced, so its being null does not matter. (Calling an instance method on null would throw NullPointerException.)
More OOP with C++ and Java questions
- What is printed? java char x = 'A'; int i = 0; System.out.print(true ? 1 : 2.0); System.out.print(" "); System.out.print(true ? x : i);
- What is printed? cpp struct B { void f(int) { cout << "Bi"; } void f(double) { cout << "Bd"; } }; struct D : B { void f(double) { cout <<…
- How many of these are correct about C++ virtual functions on a typical implementation? (i) Each class with virtual functions has a table…
- A method call such as shape.area() runs different code depending on whether shape refers to a Circle or a Square object at run time. Which…
- A Java class declares only the constructor Account(String id) and no other constructor. What happens with the statement new Account()?
- What is printed? java int x = 5; x = x++ + ++x; System.out.print(x);