Java Language | 10 Minute‐Test 3


Instruction

  • Total number of questions : 10.
  • Time alloted : 10 minutes.
  • Each question carry 1 mark.
  • No Negative marks
  • DO NOT refresh the page.
  • All the best :-).

1.

Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;

A.
1 and 2
B.
2 and 3
C.
3 and 4
D.
All statements are correct.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal.(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.(3) actually works, even though a cast is not necessary, because a long can store a byte.

2.

Which of these values can a boolean variable contain?

A.
true & false
B.
0 & 1
C.
Any integer value
D.
true

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Boolean variable can contain only one of two possible values, true and false.

3.

Literals in java must be appended by which of these?

A.
L
B.
l
C.
D
D.
L and l

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Data type long literals are appended by an upper or lowercase L.

4.

What is the error in this code?
byte b = 50;
b = b * 50;

A.
b can not contain value 100, limited by its range.
B.
* operator has converted b * 50 into int, which can not be converted to byte without casting.
C.
b can not contain value 50.
D.
No error in this code

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

While evaluating an expression containing int, bytes or shorts , the whole expression is converted to int then evaluated and result is also of type int.

5.

What will this code print?

int arr[] = new int [5];
System.out.print(arr);

A.
0
B.
value stored in arr[0].
C.
00000
D.
Class [email protected] hashcode in hexadecimal form

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

If we trying to print any reference variable internally, toString() will be called which is implemented to return the String in following form:
[email protected] in hexadecimal form

6.

With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;

A.
1, 2 & 3
B.
1 & 4
C.
1, 2, 3 & 4
D.
3 & 2

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Operator ++ increases value of variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also x =+ 1 will set the value of x to 1.

7.

On applying Left shift operator, <<, on an integer bits are lost one they are shifted past which position bit?

A.
1
B.
32
C.
33
D.
31

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The left shift operator shifts all of the bits in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31.

8.

Which of the following operators can operate on a boolean variable?
1. &&
2. ==
3. ?:
4. +=

A.
3 & 2
B.
1 & 4
C.
1, 2 & 4
D.
1, 2 & 3

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values.

9.

What is the value stored in x in following lines of code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;

A.
0
B.
1
C.
9
D.
8

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

None.

10.

Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

A.
do-while
B.
while
C.
for
D.
None of the mentioned
Submit your test now to view the Results and Statistics with answer explanation.