ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
class MyEx extends RuntimeException {
public String getMessage() { return "custom:" + super.getMessage(); }
}
public static void main(String[] a) {
try {
throw new MyEx();
} catch (MyEx e) {
System.out.print(e.getMessage());
}
}- A. custom:null
- B. null
- C. custom:
- D. Compilation error: getMessage() cannot be overridden
Correct answer: A. custom:null
new MyEx() calls the no-arg RuntimeException constructor, which stores a null message. super.getMessage() returns that null, and string concatenation renders it as the text "null", giving "custom:null".
More OOP with C++ and Java questions
- Does this compile? java interface I { void m() throws java.io.IOException; } class C implements I { public void m() throws…
- What is printed? java class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) {…
- What is printed? java // in main: String a = "hello"; String b = "hello"; String c = new String("hello"); String d = c.intern()…
- What is printed? java // in main: Integer a = 100; Integer b = 100; Integer c = 200; Integer d = 200; System.out.println((a == b) + " " +…
- What is printed? java // in main: String s1 = "Java"; String s2 = new StringBuilder("Java").toString(); System.out.println((s1 == s2) + "…
- What is printed? java // in main: String s = "hello"; s.concat(" world"); System.out.println(s); String t = s.concat(" world")…