Which keyword is used to declare a variable in Java?
A
var B
let C
int D
define
Analysis & Theory
You declare variables in Java by specifying the data type, such as `int`, `double`, etc.
What is the output of:
int x = 10; System.out.println(x);
A
x B
10 C
x = 10 D
Error
Analysis & Theory
The value of the variable `x`, which is 10, will be printed.
Which of these is a valid variable name in Java?
A
2value B
value_2 C
value-2 D
value 2
Analysis & Theory
Variable names cannot start with numbers or contain dashes/spaces. `value_2` is valid.
What is the default value of an `int` variable in a Java class (if not initialized)?
A
0 B
null C
undefined D
1
Analysis & Theory
Integer variables have a default value of `0` in Java (only for instance variables).
What happens if you try to use a local variable without initializing it?
A
It will be assigned 0 by default B
It will be assigned null C
Compiler error D
It will be auto-initialized
Analysis & Theory
Local variables in Java must be initialized before use; otherwise, it causes a compile-time error.
Which data type is used to store decimal numbers in Java?
A
int B
String C
float D
boolean
Analysis & Theory
`float` and `double` are used for decimal values in Java.
How do you declare a constant in Java?
A
final int x = 10; B
const int x = 10; C
static int x = 10; D
let x = 10;
Analysis & Theory
In Java, `final` is used to declare a constant (unchangeable) value.
Which of the following correctly assigns a character to a variable?
A
char c = 'A'; B
char c = "A"; C
char c = A; D
char c = 65.0;
Analysis & Theory
Character literals are enclosed in single quotes like `'A'`.
What is the size of an `int` in Java?
A
2 bytes B
4 bytes C
8 bytes D
Depends on the platform
Analysis & Theory
`int` is always 4 bytes in Java (32-bit signed integer).
Which keyword is used to automatically determine the type of a variable in Java (Java 10+)?
A
define B
auto C
let D
var
Analysis & Theory
Java 10 introduced the `var` keyword for type inference in local variables.