Bird
Raised Fist0
Javaprogramming~20 mins

Default values in Java - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. In Java, what is the default value of an uninitialized int variable inside a class?
easy
A. 0
B. null
C. false
D. 1

Solution

  1. Step 1: Understand default values for primitive types

    In Java, primitive numeric types like int default to 0 if not initialized.
  2. Step 2: Apply to int variable

    An uninitialized int inside a class will have the default value 0.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    int default = 0 [OK]
Hint: Remember: int defaults to zero, not null or false [OK]
Common Mistakes:
  • Confusing int default with null (which is for objects)
  • Thinking boolean default applies to int
  • Assuming uninitialized variables cause compile error
2. Which of the following is the correct way to declare a boolean instance variable with default value in Java?
easy
A. boolean flag = true;
B. boolean flag = null;
C. boolean flag = 0;
D. boolean flag;

Solution

  1. Step 1: Check valid boolean declarations

    In Java, a boolean can be declared without initialization; it defaults to false.
  2. Step 2: Identify invalid initializations

    Assigning null or 0 to boolean is invalid and causes errors.
  3. Final Answer:

    boolean flag; -> Option D
  4. Quick Check:

    boolean defaults to false if uninitialized [OK]
Hint: Declare boolean without value to get default false [OK]
Common Mistakes:
  • Assigning null to boolean (not allowed)
  • Using 0 or 1 as boolean values
  • Assuming default boolean is true
3. What will be the output of the following Java code?
class Test {
    int x;
    boolean b;
    String s;
    void printValues() {
        System.out.println(x + "," + b + "," + s);
    }
}
public class Main {
    public static void main(String[] args) {
        Test t = new Test();
        t.printValues();
    }
}
medium
A. 0,false,null
B. 0,true,null
C. 0,false,
D. Compilation error

Solution

  1. Step 1: Identify default values for fields

    Instance variables: int defaults to 0, boolean to false, and String (object) to null.
  2. Step 2: Understand print output

    Printing concatenates values with commas, so output is "0,false,null".
  3. Final Answer:

    0,false,null -> Option A
  4. Quick Check:

    int=0, boolean=false, String=null [OK]
Hint: Remember objects default to null, primitives to zero or false [OK]
Common Mistakes:
  • Assuming String defaults to empty string instead of null
  • Thinking boolean defaults to true
  • Expecting compilation error due to uninitialized variables
4. Consider this Java code snippet:
public class Sample {
    static int count;
    public static void main(String[] args) {
        int number;
        System.out.println(count);
        System.out.println(number);
    }
}

What is the error in this code?
medium
A. Static variable 'count' cannot be accessed in static method
B. Local variable 'number' might not have been initialized
C. Syntax error in class declaration
D. No error, code runs fine

Solution

  1. Step 1: Check variable initialization rules

    Static variable 'count' is initialized to 0 by default and can be printed.
  2. Step 2: Analyze local variable 'number'

    Local variables must be explicitly initialized before use; 'number' is not initialized.
  3. Final Answer:

    Local variable 'number' might not have been initialized -> Option B
  4. Quick Check:

    Local vars need explicit init [OK]
Hint: Local variables need explicit initialization before use [OK]
Common Mistakes:
  • Assuming all variables get default values
  • Thinking static variables can't be accessed in static methods
  • Ignoring compiler error for uninitialized local variables
5. You want to create a Java class with an array of double values representing temperatures. You declare:
double[] temps = new double[5];

What are the default values stored in temps immediately after this declaration?
hard
A. All elements are null
B. All elements are NaN
C. All elements are 0.0
D. Array elements are uninitialized and cause error if accessed

Solution

  1. Step 1: Understand default values for array elements

    In Java, arrays of primitive types get default values for each element; for double, it's 0.0.
  2. Step 2: Apply to the given array

    After new double[5], all 5 elements are initialized to 0.0 automatically.
  3. Final Answer:

    All elements are 0.0 -> Option C
  4. Quick Check:

    double array defaults to 0.0 [OK]
Hint: Primitive arrays auto-fill with zero values for their type [OK]
Common Mistakes:
  • Thinking array elements default to null (only for object arrays)
  • Assuming elements are NaN by default
  • Believing uninitialized array elements cause runtime error
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard