Recall & Review
beginner
What is a variable declaration in Java?
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? <br>
double price = 19.99;✗ 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?
✗ 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?
✗ 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?
✗ 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"?✗ 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.