Consider the following code snippet:
int num1 = 5;
double num2 = 3.14;
char ch = 'A';
boolean flag = true;
Which of the following data types is used to store a whole number in the variable num1
?
a) int
b) double
c) char
d) boolean
What is the value stored in the variable num2
?
a) 5
b) 3
c) 3.14
d) A
What is the data type of the variable ch
?
a) int
b) double
c) char
d) boolean
What is the value stored in the variable flag
?
a) true
b) false
c) 'A'
d) 'T'
Explanation:
The correct answer is a) int
. The int
data type is used to store whole numbers in Java.
The correct answer is c) 3.14
. The double
data type is used to store decimal numbers in Java. In this case, num2
is initialized with the value 3.14.
The correct answer is c) char
. The char
data type is used to store single characters in Java. In this case, ch
is initialized with the value 'A'
.
The correct answer is a) true
. The boolean
data type is used to store either true
or false
values in Java. In this case, flag
is initialized with the value true
.
Make sure to revise and understand the different primitive data types in Java before attempting similar questions.