Here are 10 essential multiple-choice questions on Java Variables, covering key concepts with practical examples.
Question 1
Which of the following is a valid way to declare a variable in Java?
int number = 10;
var number = 10;
Integer number = 10;
All of the above
Question 2
What will be the output of the following code?
public class Geeks {
public static void main(String[] args) {
int a;
System.out.println(a);
}
}
0
Compilation Error
Runtime Error
Undefined Behavior
Question 3
Which of the following is true about variable names in Java?
Variable names can start with a number
Variable names can contain special characters like @ or #
Variable names are case-sensitive
Variable names can be a Java keyword
Question 4
What is the default value of a boolean instance variable in Java?
true
false
null
Compilation Error
Question 5
What will be the output of the following Java program?
public class Main {
static int x;
public static void main(String[] args) {
System.out.println(x);
}
}
0
Compilation Error
Runtime Error
Undefined Behavior
Question 6
What is the correct way to declare a constant variable in Java?
final int MAX = 100;
const int MAX = 100;
int const MAX = 100;
MAX = 100;
Question 7
What will be the output of this code?
public class Example {
int num = 5;
public static void main(String[] args) {
Example obj = new Example();
System.out.println(obj.num);
}
}
Compilation Error
5
Runtime Error
Undefined Behavior
Question 8
Which of the following statements about var keyword is FALSE?
var can be used for local variables
var can be used for instance variables
var must be initialized at the time of declaration
var is introduced in Java 10
Question 9
What will be the output of this Java program?
public class Demo {
public static void main(String[] args) {
int x = 10;
{
int x = 20;
System.out.println(x);
}
}
}
20
10
Compilation Error
Runtime Error
Question 10
Which of the following statements is correct?
Static variables are shared among all objects of a class
Local variables are automatically initialized
Instance variables cannot be final
A final variable can be reassigned
There are 10 questions to complete.