0
0
Javaprogramming~20 mins

Default values in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Java Default Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of uninitialized instance variables
What is the output of this Java program?
Java
public class Test {
    int number;
    boolean flag;
    String text;

    public static void main(String[] args) {
        Test obj = new Test();
        System.out.println(obj.number + "," + obj.flag + "," + obj.text);
    }
}
A0,false,empty string
B0,true,null
CCompilation error
D0,false,null
Attempts:
2 left
💡 Hint
Instance variables have default values if not initialized.
Predict Output
intermediate
2:00remaining
Default values of local variables
What happens when you try to compile and run this Java code?
Java
public class Test {
    public static void main(String[] args) {
        int x;
        System.out.println(x);
    }
}
ACompilation error: variable x might not have been initialized
BPrints 0
CRuntime error
DPrints a random number
Attempts:
2 left
💡 Hint
Local variables must be initialized before use.
Predict Output
advanced
2:00remaining
Default values in arrays
What is the output of this Java program?
Java
public class Test {
    public static void main(String[] args) {
        boolean[] flags = new boolean[3];
        System.out.println(flags[1]);
    }
}
Anull
Btrue
Cfalse
DCompilation error
Attempts:
2 left
💡 Hint
Array elements have default values based on their type.
Predict Output
advanced
2:00remaining
Default values of object fields in inheritance
What is the output of this Java program?
Java
class Parent {
    int value;
}

class Child extends Parent {
    boolean flag;
}

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        System.out.println(c.value + "," + c.flag);
    }
}
A0,true
B0,false
CCompilation error
Dnull,false
Attempts:
2 left
💡 Hint
Inherited fields also get default values.
🧠 Conceptual
expert
2:00remaining
Default values of static variables
Consider this Java class with static variables. What is the value of staticVar after the program runs?
Java
public class Test {
    static int staticVar;

    public static void main(String[] args) {
        System.out.println(staticVar);
    }
}
A0
BUndefined behavior
CCompilation error
Dnull
Attempts:
2 left
💡 Hint
Static variables also get default values if not initialized.