Bird
Raised Fist0
Javaprogramming~20 mins

Variable declaration and initialization in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of variable initialization with expressions
What is the output of the following Java code?
Java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = a * 2;
        int c = b - a / 5;
        System.out.println(c);
    }
}
A9
B10
C8
D7
Attempts:
2 left
💡 Hint
Remember the order of operations: division before subtraction.
❓ Predict Output
intermediate
2:00remaining
Value of variable after multiple declarations
What is the value of variable num after running this code?
Java
public class Main {
    public static void main(String[] args) {
        int num = 10;
        num = num + 5;
        num = num * 2;
        System.out.println(num);
    }
}
A15
B20
C30
D25
Attempts:
2 left
💡 Hint
Follow the operations step by step.
❓ Predict Output
advanced
2:00remaining
Output with uninitialized local variable
What error or output does this code produce?
Java
public class Main {
    public static void main(String[] args) {
        int x;
        // System.out.println(x); // Uncommenting this line causes error
        x = 5;
        System.out.println(x);
    }
}
A0
B5
CCompilation error: variable x might not have been initialized
DRuntime error
Attempts:
2 left
💡 Hint
The variable is assigned before printing.
❓ Predict Output
advanced
2:00remaining
Output of variable shadowing in nested blocks
What is the output of this Java program?
Java
public class Main {
    public static void main(String[] args) {
        int val = 10;
        {
            int val = 20;
            System.out.println(val);
        }
        System.out.println(val);
    }
}
ACompilation error due to duplicate variable declaration
B10\n20
C20\n10
D20\n20
Attempts:
2 left
💡 Hint
Java allows redeclaring a variable in an inner block (shadowing).
🧠 Conceptual
expert
3:00remaining
Understanding default values of instance variables
Consider the following Java class. What is the output when new Test().printValues(); is called?
Java
public class Test {
    int a;
    boolean b;
    double c;
    String d;

    void printValues() {
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}
ACompilation error due to uninitialized variables
BGarbage values printed
C0\ntrue\n0.0\n""
D0\nfalse\n0.0\nnull
Attempts:
2 left
💡 Hint
Instance variables have default values if not initialized.

Practice

(1/5)
1. Which of the following is a correct way to declare and initialize an integer variable named age with the value 25 in Java?
easy
A. int age = 25;
B. int age;
C. age = 25;
D. int 25 = age;

Solution

  1. Step 1: Understand variable declaration and initialization

    Declaring a variable means telling the computer the type and name. Initializing means giving it a value.
  2. Step 2: Check each option for correct syntax

    int age = 25; declares int age and sets it to 25 correctly. int age; declares but does not initialize. age = 25; tries to assign without declaration. int 25 = age; uses invalid syntax.
  3. Final Answer:

    int age = 25; -> Option A
  4. Quick Check:

    Declaration + initialization = int age = 25; [OK]
Hint: Declare type, name, then assign value with = [OK]
Common Mistakes:
  • Forgetting the data type before the variable name
  • Assigning a value without declaring the variable first
  • Using numbers as variable names
2. Which of the following lines of code will cause a syntax error in Java?
easy
A. double price = 19.99;
B. String name = "John";
C. int 1stNumber = 10;
D. boolean isActive = true;

Solution

  1. Step 1: Review Java variable naming rules

    Variable names cannot start with a digit. They must start with a letter, underscore, or dollar sign.
  2. Step 2: Check each option for naming errors

    int 1stNumber = 10; uses 1stNumber which starts with a digit, causing a syntax error. Others are valid declarations.
  3. Final Answer:

    int 1stNumber = 10; -> Option C
  4. Quick Check:

    Variable names cannot start with digits [OK]
Hint: Variable names cannot start with numbers [OK]
Common Mistakes:
  • Starting variable names with digits
  • Using spaces in variable names
  • Missing semicolon at the end
3. What will be the output of the following Java code?
int x = 5;
int y = x + 10;
System.out.println(y);
medium
A. 15
B. 10
C. 5
D. Syntax Error

Solution

  1. Step 1: Understand variable initialization and addition

    Variable x is set to 5. Variable y is set to x + 10, which is 5 + 10 = 15.
  2. Step 2: Determine the printed value

    The System.out.println(y); prints the value of y, which is 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    5 + 10 = 15 [OK]
Hint: Add values after initialization to find output [OK]
Common Mistakes:
  • Confusing variable names
  • Forgetting to initialize variables before use
  • Assuming output is the first variable's value
4. Identify the error in the following Java code snippet:
int number;
number = "10";
System.out.println(number);
medium
A. Variable number is not declared
B. Print statement syntax is incorrect
C. Missing semicolon after declaration
D. Cannot assign a String to an int variable

Solution

  1. Step 1: Check variable declaration and assignment types

    The variable number is declared as int, which means it can only hold integer values.
  2. Step 2: Identify type mismatch in assignment

    Assigning "10" (a String) to an int variable causes a type mismatch error.
  3. Final Answer:

    Cannot assign a String to an int variable -> Option D
  4. Quick Check:

    int variables accept only integers [OK]
Hint: Match variable type with assigned value type [OK]
Common Mistakes:
  • Assigning text (String) to number variables
  • Forgetting to use quotes for Strings
  • Confusing variable declaration and assignment
5. You want to declare and initialize three variables in one line: an integer count with 10, a double price with 5.5, and a boolean available with true. Which of the following is the correct way to do this in Java?
hard
A. int count = 10, double price = 5.5, boolean available = true;
B. int count = 10; double price = 5.5; boolean available = true;
C. int count = 10; double price = 5.5, boolean available = true;
D. int count = 10, price = 5.5, available = true;

Solution

  1. Step 1: Understand variable declaration rules for different types

    In Java, variables of different types must be declared separately with their own type keyword.
  2. Step 2: Analyze each option's syntax

    int count = 10, double price = 5.5, boolean available = true; tries to declare different types separated by commas in one statement, which is invalid. int count = 10; double price = 5.5; boolean available = true; declares each variable with its type and ends each statement with a semicolon, which is correct. int count = 10; double price = 5.5, boolean available = true; mixes semicolon and comma incorrectly. int count = 10, price = 5.5, available = true; declares all variables with one type keyword, which is invalid for different types.
  3. Final Answer:

    int count = 10; double price = 5.5; boolean available = true; -> Option B
  4. Quick Check:

    Different types need separate declarations [OK]
Hint: Declare each type separately with semicolons [OK]
Common Mistakes:
  • Trying to declare different types in one statement with commas
  • Missing semicolons between statements
  • Assuming one type keyword covers all variables
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