Hide all answers
Hide all answers
View all answers
View all answers
Print
Try the Quiz
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
• 264 to 264
• 263 to 263  1 • 232 to 232  1
Answer: 263 to 263  1The type long is a 64bit 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)
• (C)
• (A), (B) & (C)
• (A) & (B)
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) & (B)
• (A) & (C)
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: • '\uffff'
• '\u0000' • " " (space)
• '\u0001'
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: 13When 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: 27When 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) • (A), (B), (C) & (D)
• (A) & (B)
• (C) & (D)
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 = 3, y = 2, z = 6 • x = 4, y = 1, z = 5
• x = 7, y = 1, z = 5
• x = 4, y = 2, z = 6
Answer: x = 3, y = 2, z = 6Both 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: 4The modulus operator (%) may be used with floatingpoint 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: 5a % 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: 57 12. Choose the operations that can be performed on String objects: (A) + (B) + = (C)  (D) % (E) ^ • (A) & (B) • (A)
• (D)/option>
• (D) & (E)
• None of these
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) • 1
• 5 • 2
• 8
• 3
Answer: 5In 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. • (A), (C) & (E) • (C)
• (D) & (E)
• (A)
• (C) & (E)
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)
• (B), (D), (E) & (F) • (D) & (F)
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) & (D)
• (A) & (C) • (B) & (C)
• (A) & (D)
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), (B) & (C)
• (A), (C) & (D)
• (B) & (C)
• (A) & (D)
• (A) & (C)
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) & (D) • (A), (B) & (C)
• (A), (C) & (D)
• (B) & (C)
• (A) & (D)
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: 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), (B) & (C)
• (A), (C) & (D)
• (A) & (E)
• (C) & (E)
• (A), (C) & (E)
Answer: (A), (C) & (E)
Try the Quiz : Java Programming : Basic Operations : Multiple Choice
