0
0
Javaprogramming~5 mins

Variable declaration and initialization in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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;
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
Which of these is a valid variable declaration and initialization in Java?
Aint number = 10;
Bnumber int = 10;
Cint = 10 number;
Dint number 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
Which keyword is used to declare a variable in Java?
Adeclare
Bint
CNo keyword, just the type
Dvar
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";
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.