Java Variables Essentials Quiz Quiz

Explore the key concepts of Java variables with these easy but insightful questions. Great for beginners looking to strengthen their Java foundational knowledge.

  1. Java Variable Declaration

    Which of the following correctly declares a variable to store an integer value in Java?

    1. int num;
    2. integer num;
    3. int = num;
    4. num int;

    Explanation: The correct way to declare an integer variable in Java is 'int num;'. The option 'integer num;' uses an incorrect keyword, 'num int;' has the order reversed, and 'int = num;' uses assignment incorrectly.

  2. Variable Initialization

    How do you correctly assign the value 10 to an already declared integer variable named x?

    1. x : 10;
    2. int x = 10;
    3. 10 = x;
    4. x = 10;

    Explanation: 'x = 10;' assigns the value 10 to x, which is already declared. 'int x = 10;' redeclares x, '10 = x;' uses assignment in reverse, and 'x : 10;' is invalid syntax.

  3. Multiple Variable Declaration

    Which line correctly declares two Java variables named a and b of type int and assigns them both the value 5?

    1. integer a = 5, b = 5;
    2. int a = 5, b = 5;
    3. int a == 5, b == 5;
    4. a int, b int = 5;

    Explanation: 'int a = 5, b = 5;' properly declares both variables and assigns their values. 'int a == 5, b == 5;' uses the wrong assignment operator, 'a int, b int = 5;' uses an incorrect syntax, and 'integer a = 5, b = 5;' uses a non-Java keyword.

  4. Valid Variable Names

    Which of these is a valid variable name in Java?

    1. userName
    2. user-name
    3. class
    4. 2users

    Explanation: 'userName' is valid in Java. '2users' cannot begin with a digit, 'user-name' contains an illegal character, and 'class' is a reserved keyword.

  5. Data Types for Text

    What is the correct data type in Java for storing a string of text?

    1. String
    2. string
    3. Text
    4. Character

    Explanation: Java uses 'String' (capital S) for text. 'Text' and 'string' are not valid Java types, while 'Character' only holds a single character.

  6. Default Value of int

    If an int variable is declared as a class member but not initialized, what is its default value in Java?

    1. undefined
    2. null
    3. 1
    4. 0

    Explanation: Class member variables of type int default to 0. 'null' is for objects, 'undefined' is not a Java keyword, and '1' is not the default.

  7. Case Sensitivity

    Are variable names in Java case sensitive?

    1. Yes
    2. No
    3. Sometimes
    4. Only for classes

    Explanation: Java variable names are case sensitive. 'No' is incorrect, 'Only for classes' is misleading, and 'Sometimes' is inaccurate.

  8. Uninitialized Local Variables

    What happens if you try to use a local int variable in Java without initializing it first?

    1. It defaults to 0
    2. Compilation error
    3. It defaults to null
    4. It runs with no problem

    Explanation: Java requires local variables to be initialized before use, else a compilation error occurs. Class members default to 0, but not local variables. 'null' isn't valid for int, and the code won't just run.

  9. Constant Declaration

    How do you declare a constant (unchangeable) variable in Java?

    1. final int MAX = 10;
    2. int constant MAX = 10;
    3. const int MAX = 10;
    4. int MAX = constant 10;

    Explanation: 'final' makes a variable unchangeable. 'const' is not a Java keyword. 'int constant MAX = 10;' and 'int MAX = constant 10;' use incorrect syntax and keywords.

  10. Floating-Point Numbers

    Which data type is typically used in Java to store a decimal value like 3.14?

    1. int
    2. boolean
    3. char
    4. double

    Explanation: 'double' stores decimal values such as 3.14. 'int' is for whole numbers, 'char' is for single characters, and 'boolean' stores true/false values.