What will the following code print out to console?

public class Exam  {
    public static void main(String[] arg) {
        Object obj = null;
        String str = new String("str");
        str = (String) obj;
        obj = new String("obj");
        System.out.print(obj + ", " + str);  
    }
}
Explanation
The next commands will be executed in order:
1. obj is initialized with null
2. str is initialized with new String object with value "str"
3. null is assigned to str
4. "obj" string is assigned to obj

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes