Question 31Asked in DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 2 (1:00 PM)

Given the following code, what will be the result? public class Test {

Given the following code, what will be the result? public class Test { public static void main(String[] args) { Object obj = new String("Hello"); String str = (String) obj; System.out.println(str); } }
  1. AThrows ClassCastException.
  2. BRuntime error.
  3. CHello will be printed.
  4. DCompilation error.
Show answerHide answer

Correct answer: (C) Hello will be printed.

Explanation

obj actually refers to a String object, so the downcast (String) obj succeeds at run time and "Hello" is printed. ClassCastException occurs only when the object is not of the target type.

From DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 2 (1:00 PM)

This is question 31 of DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 2 (1:00 PM). See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.