Java Multiple choice questions and answers


Answer the following questions about basic operations in the Java programming language.


1. The type long can be used to store values in the following range:
β€’ -231 to 231 – 1
β€’ -263 to 263 – 1
β€’ -264 to 264
β€’ -232 to 232 – 1
Answer:Answer: -263 to 263 – 1
The type long is a 64-bit two’s complement that can be used for long integers.

2. Which of the following is not a hexadecimal number?
(A) 999
(B) (hex)23
(C) 0x556
(D) 0x1F2
β€’ (A) & (B)
β€’ (A)
β€’ (C)
β€’ (A), (B) & (C)
Answer:Answer: (A) & (B)
Hexadecimal numbers start with “0x” in the Java programming language.

3. Select the invalid assignment statements from the following:
(A) float x = 238.88;
(B) double y = 0x443;
β€’ (B)
β€’ (B) & (D)
β€’ (A) & (C)
β€’ (A) & (B)
Answer:Answer: (A) & (C)
238.88 is a double in Java. To assign this value to a float, 238.88f must be used. A variable of type boolean cannot be converted to an integer in Java.

4. If not assigned a value, a variable of type char has the following default value:
β€’ ‘u0000’
β€’ ‘uffff’
β€’ ” ” (space)
β€’ ‘u0001’
Answer:Answer: ‘u0000’
The value of a variable of type char is 16 bits of data formatted as a Unicode character.

5. 15 & 29 = ?
β€’ 44
β€’ 13
β€’ 14
β€’ 12
Answer:Answer: 13
When written in binary format, 15 is 1111, and 29 is 11101. 1111 & 11101 is 1101, which is 13 in decimal.

6. 27 | 8 =?
β€’ 8
β€’ 27
β€’ 19
β€’ 35
Answer:Answer: 27
When written in binary format, 27 is 11011, and 8 is 1000. 11011 | 1000 is 11011, which is 27.

7. Identify the statements that are correct:
(A) int a = 13, a>>2 = 3
(B) int b = -8, b>>1 = -4
(C) int a = 13, a>>>2 = 3
(D) int b = -8, b>>>1 = -4
β€’ (A), (B), (C) & (D)
β€’ (A) & (B)
β€’ (A), (B) & (C)
β€’ (C) & (D)
Answer:Answer: (A), (B) & (C)
The shift operation “y1 >>> y2” is identical to “y1 >> y2” for all positive values of y1. It shifts the bits in y1 to the right by y2 positions.

8. Consider the following code: int x, y, z;
y = 1;
z = 5;
x = 0 – (++y) + z++;

After execution of this, what will be the values of x, y and z?
β€’
x = 4, y = 1, z = 5
β€’
x = 3, y = 2, z = 6
β€’
x = -7, y = 1, z = 5
β€’
x = 4, y = 2, z = 6
Answer:Answer:
x = 3, y = 2, z = 6
Both y and z get incremented as a result of the last statement, but y gets incremented before and z after the assisgment of the calculated value to x.

9. What will be the result of the expression
a % b
when a and b are of type int and their values are a = 10 and b = 6?
β€’ 1.66
β€’ 4
β€’ 1
β€’ None of these
Answer:Answer: 4
The modulus operator (%) may be used with floating-point as well as integer types. It returns the remainder of a division operation. Therefore, 10 % 6 will return 4.

10. What will be the result of the expression
a % b
when a and b are of type int and their values are a = -17 and b = -6?
β€’ -5
β€’ -23
β€’ 5
β€’ None of these
Answer:Answer: 5
a % b calculates the remainder when a is divided by b

11. What will be the value of a after execution of the following statements:
int a = 23, b = 34;
a = ((a < b) ? (b + a) : (b – a);

β€’ 11
β€’ 57
β€’ 23
β€’ 34
β€’
Error. Cannot be executed.
Answer:Answer: 57

12. Choose the operations that can be performed on String objects:
(A) +
(B) + =
(C) –
(D) %
(E) ^

β€’
(A)
β€’
(D)/option>
β€’
(A) & (B)
β€’
(D) & (E)
β€’
None of these
Answer:Answer:
(A) & (B)
The “+” and “+ =” operators are overlaoded in Java. They join the two operands into one String object.

13. (1 | 4) + (4 & 2) = ?
(in base ten)

β€’ 5
β€’ 1
β€’ 2
β€’ 8
β€’ 3
Answer:Answer: 5
In binary, 1 | 4 is 1 | 100 = 101, which is 5 in decimal. Similarly, 4 & 2 is 100 & 10 = 0, which is 0 in decimal.

14. Of the following functions from the class java.lang.Math, select those that produce a value of 25, when a = 25.7.
β€’
(C)
β€’
(A), (C) & (E)
β€’
(D) & (E)
β€’
(A)
β€’
(C) & (E)
Answer:Answer:
(A), (C) & (E)
The funtions ceil, rint and round all return the value 25 in this case. The difference lies in the type of argument returned: ceil and rint return a double, where as round will return an int or a long, depending upon the type of a (float or double, respectively).

15. In the code below, what data types the variable x can have?
byte b1 = 5;
byte b2 = 10;
x = b1 * b2;(A) byte
(B) int
(C) short
(D) long
(E) float
(F) double

β€’
(A), (B), (D) & (E)
β€’
(B), (C) & (D)
β€’
(B), (D) & (E)
β€’
(D) & (F)
β€’
(B), (D), (E) & (F)
Answer:Answer:
(B), (D), (E) & (F)

16. Given the declarations
int x , m = 2000;
short y;
byte b1 = -40, b2;
long n;
Which of the following assignment statements will evaluate correctly?
(A) x = m * b1;
(B) y = m * b1;
(C) n = m * 3L;
(D) x = m * 3L;

β€’
(A), (B) & (C)
β€’
(A) & (C)
β€’
(A), (C) & (D)
β€’
(B) & (C)
β€’
(A) & (D)
Answer:Answer:
(A) & (C)

17. Given the declarations
boolean b;
short x1 = 100, x2 = 200, x3 = 300; Which of the following statements are evaluated to true?
(A) b = x1 * 2 == x2;
(B) b = x1 + x2 != 3 * x1;
(C) b = (x3 – 2*x2<0) || ((x3 = 400)<2**x2);
(D) b = (x3 – 2*x2>0) || ((x3 = 400) 2*x2);

β€’
(A) & (C)
β€’
(A), (B) & (C)
β€’
(A), (C) & (D)
β€’
(B) & (C)
β€’
(A) & (D)
Answer:Answer:
(A) & (C)

18. In which of the following code fragments, is the variable x evaluated to 8?
(A) int x = 32;
x = x>>2;
(B) int x = 33;
x = x>>2;
(C) int x = 35;
x = x>>2;
(D) int x = 16;
x = x>>1;

β€’
(A), (B) & (C)
β€’
(A), (C) & (D)
β€’
(B) & (C)
β€’
(A), (B), (C) & (D)
β€’
(A) & (D)
Answer:Answer:
(A), (B), (C) & (D)

19. Consider the following code snippet:
…..
…..
try

β€’
Error. Won’t compile
β€’
Division by zero
β€’
Division by zero
Catch block

β€’
Catch block
Answer:Answer:
Catch block

20. Which of the following represent legal flow control statements?
(A) break;
(B) break();
(C) continue outer;
(D) continue(inner);
(E) return;
(F) exit();

β€’
(A), (C) & (E)
β€’
(A), (B) & (C)
β€’
(A), (C) & (D)
β€’
(A) & (E)
β€’
(C) & (E)
Answer:Answer:
(A), (C) & (E)

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks