Java MCQs on literals & variables of Java Programming Language.
1. Which of these is long data type literal? Answer: a 2. Which of these can be returned by the operator &? Answer: d 3. Literals in java must be appended by which of these? Answer: d 4. Literal can be of which of these data types? Answer: d 5. Which of these can not be used for a variable name in Java? Answer: b 6. What will be the output of the following Java program? a) 38 7. What will be the output of the following Java program? a) 0 2 4 6 8 8. What will be the output of the following Java program? a) 5 6 5 6 9. Which of these is an incorrect string literal?
a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246
Clarification: Data type long literals are appended by an upper or lowercase L. 0x99fffL is hexadecimal long literal.
a) Integer
b) Boolean
c) Character
d) Integer or Boolean
Clarification: We can use binary ampersand operator on integers/chars (and it returns an integer) or on booleans (and it returns a boolean).
a) L
b) l
c) D
d) L and I
Clarification: Data type long literals are appended by an upper or lowercase L.
a) integer
b) float
c) boolean
d) all of the mentioned
Clarification: None
a) identifier
b) keyword
c) identifier & keyword
d) none of the mentioned
Clarification: Keywords are specially reserved words which can not be used for naming a user defined variable, example: class, int, for etc.
class evaluate
{
public static void main(String args[])
{
int a[] = {1,2,3,4,5};
int d[] = a;
int sum = 0;
for (int j = 0; j < 3; ++j)
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
System.out.println(sum);
}
}
b) 39
c) 40
d) 41
Clarification: None.
output:
$ javac evaluate.java
$ java evaluate
40
class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i/2;
array_variable[i]++;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
b) 1 2 3 4 5
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
Clarification: When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.
output:
$ javac array_output.java
$ java array_output
1 2 3 4 5
class variable_scope
{
public static void main(String args[])
{
int x;
x = 5;
{
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}
b) 5 6 5
c) Runtime error
d) Compilation error
Clarification: Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x.
output:
$ javac variable_scope.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem: y cannot be resolved to a variable
a) “Hello World”
b) “HellonWorld”
c) “”Hello World””
d) "Hello
world"
Answer: d
Clarification: All string literals must begin and end in the same line.
10. What will be the output of the following Java program?
-
class dynamic_initialization -
{ -
public static void main(String args[])
-
{ -
double a, b;
-
a = 3.0;
-
b = 4.0;
-
double c = Math.sqrt(a * a + b * b);
-
System.out.println(c);
-
} -
}
a) 5.0
b) 25.0
c) 7.0
d) Compilation Error
Clarification: Variable c has been dynamically initialized to square root of a * a + b * b, during run time.
output:
$ javac dynamic_initialization.java
$ java dynamic_initialization
5.0
