52). Which of these is long data type literal?
[A]0x99fffL
[B]ABCDEFG
[C]0x99fffa
[D]99671246
Show Answer
Correct Answer: 0x99fffL
Notes:
Answer: a
Explanation: Data type long literals are appended by an upper or lowercase L. 0x99fffL is hexadecimal long literal.
53). Which of these can be returned by the operator &?
[A]Integer
[B]Boolean
[C]Character
[D]Integer or Boolean
Show Answer
Correct Answer: Integer or Boolean
Notes:
Answer: d
Explanation: We can use binary ampersand operator on integers/chars (and it returns an integer) or on booleans (and it returns a boolean).
54). Literals in java must be appended by which of these?
[A]L
[B]l
[C]D
[D] L and I
Show Answer
Correct Answer: L and I
Notes:
Answer: d
Explanation: Data type long literals are appended by an upper or lowercase L.
55). Literal can be of which of these data types?
[A]integer
[B]float
[C]boolean
[D]all of the mentioned
Show Answer
Correct Answer: all of the mentioned
Notes:
Answer: d
Explanation: None
56). Which of these can not be used for a variable name in Java?
[A]identifier
[B]keyword
[C]identifier & keyword
[D]none of the mentioned
Show Answer
Correct Answer: keyword
Notes:
Answer: b
Explanation: Keywords are specially reserved words which can not be used for naming a user defined variable, example: class, int, for etc.
57). What will be the output of the following Java program?
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);
}
}
Show Answer
Correct Answer: 40
Notes:
Answer: c
Explanation: None.
output:$ javac evaluate.java
$ java evaluate
40
58). What will be the output of the following Java program?
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++;
}
}
}
[A]0 2 4 6 8
[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
Show Answer
Correct Answer: 1 2 3 4 5
Notes:
Answer: b
Explanation: 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
59). What will be the output of the following Java program?
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);
}
}
[A]5 6 5 6
[B] 5 6 5
[C]Runtime error
[D]Compilation error
Show Answer
Correct Answer: Compilation error
Notes:
Answer: d
Explanation: 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
60). Which of these is an incorrect string literal?
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);
}
}
[A]“Hello World”
[B]“Hello\nWorld”
[C]“\”Hello World\””
[D]"Hello world"
Show Answer
Correct Answer: "Hello world"
Notes:
Answer: d
Explanation: All string literals must begin and end in the same line.
61). 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
Show Answer
Correct Answer: 5.0
Notes:
Answer: a
Explanation: 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