Question discussion

Given the following code, what will be the result?

358835Medium DSSSB TGT Computer Science 0 replies 0 views
0
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);
    }
}
  • AThrows ClassCastException.
  • BRuntime error.
  • CHello will be printed.
  • DCompilation error.
Correct answer: C

Explanation

The object really holds a String, so the cast to String succeeds and it prints Hello.

Discussion 0

Nobody has replied yet. Start the discussion!