Home Tutorial Java Scjp Part1 SCJP Module-1 Question-19

 
 

SCJP Module-1 Question-19
Posted on: July 8, 2010 at 12:00 AM
The sample example given below checks your knowledge of Object class and it also shows that how to store array of Integer object into an array.

Given a sample code:

1 public class Sample {
2 public static void main(String args[]) {
3    Object obj = new Integer[] { 1, 2, 3, 4, 5 };
    {
4        for (String str : (String[]) obj)
5        System.out.print(str);
    }
}}

What will be the result of above code ?

(1)    Compilation error at line number 3
(2)    Compilation error at line number 4  
(3)    Prints 12345
(4)    Raise runtime exception 

Answer:

(2)

Explanation:

It raise the exception java.lang.ClassCastException as Ljava.lang.Integer; cannot be cast to [Ljava.lang.String;

Related Tags for SCJP Module-1 Question-19:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.