A variable declaration tells the computer to create a space in memory to store a value of a specific type, like int or String.
Click to reveal answer
beginner
What does variable initialization mean?
Initialization means giving a variable its first value when you create it, so it’s ready to use.
Click to reveal answer
beginner
How do you declare and initialize an integer variable named age with the value 25?
You write: int age = 25; This creates the variable and sets its value to 25.
Click to reveal answer
intermediate
Can you declare a variable without initializing it? What happens if you try to use it?
Yes, you can declare a variable without giving it a value. But if you try to use it before setting a value, Java will give an error.
Click to reveal answer
beginner
What is the difference between int x; and int x = 0;?
int x; declares the variable but does not set a value. int x = 0; declares and sets the value to zero.
Click to reveal answer
What does the following code do? double price = 19.99;
ACreates a method named price
BDeclares a variable named price and sets it to 19.99
CDeclares a variable named price without a value
DDeclares a variable named price and sets it to 1999
✗ Incorrect
The code declares a double variable called price and initializes it with 19.99.
Which of these is a valid variable declaration and initialization in Java?
Aint number = 10;
Bnumber int = 10;
Cint = 10 number;
Dint number 10;
✗ Incorrect
Option A correctly declares an int variable named number and initializes it with 10.
What happens if you try to use a variable before initializing it?
AThe variable will store null
BJava will assign a default value automatically
CThe program will run fine
DJava will give a compile-time error
✗ Incorrect
Java requires variables to be initialized before use, otherwise it gives a compile-time error.
Which keyword is used to declare a variable in Java?
Adeclare
Bint
CNo keyword, just the type
Dvar
✗ Incorrect
In Java, you declare a variable by specifying its type followed by its name, no special keyword like 'declare' is used.
What is the correct way to declare a String variable named name and set it to "Alice"?
AString name = "Alice";
Bstring name = 'Alice';
CString name = Alice;
DString name := "Alice";
✗ Incorrect
Option A correctly declares a String variable and initializes it with "Alice" using double quotes.
Explain in your own words what variable declaration and initialization mean in Java.
Think about how you tell the computer to remember a value.
You got /4 concepts.
Write an example of declaring and initializing a variable for storing a person's height in centimeters.
Use a type that can hold decimal values if needed.
You got /4 concepts.
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
Step 1: Understand variable declaration and initialization
Declaring a variable means telling the computer the type and name. Initializing means giving it a value.
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.
Final Answer:
int age = 25; -> Option A
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
Step 1: Review Java variable naming rules
Variable names cannot start with a digit. They must start with a letter, underscore, or dollar sign.
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.
Final Answer:
int 1stNumber = 10; -> Option C
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
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.
Step 2: Determine the printed value
The System.out.println(y); prints the value of y, which is 15.
Final Answer:
15 -> Option A
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
Step 1: Check variable declaration and assignment types
The variable number is declared as int, which means it can only hold integer values.
Step 2: Identify type mismatch in assignment
Assigning "10" (a String) to an int variable causes a type mismatch error.
Final Answer:
Cannot assign a String to an int variable -> Option D
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
Step 1: Understand variable declaration rules for different types
In Java, variables of different types must be declared separately with their own type keyword.
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.
Final Answer:
int count = 10; double price = 5.5; boolean available = true; -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.