Java Part 2: Questions on Operators and Control Statements
b
What is the output of this program? class rightshift_operator { public static void main(String args[]) { int x; x = 10; x = x >> 1; System.out.println(x); } } a) 10 b) 5 c) 2 d) 20
b
What is the output of this program? class selection_statements { public static void main(String args[]) { int var1 = 5; int var2 = 6; if ((var2 = 1) == var1) System.out.print(var2); else System.out.print(++var2); } } a) 1 b) 2 c) 3 d) 4
c
What is the output of this program? class ternary_operator { public static void main(String args[]) { int x = 3; int y = ~ x; int z; z = x > y ? x : y; System.out.print(z); } } a) 0 b) 1 c) 3 d) -4
d
What is the output of this program? class Output { public static void main(String args[]) { final int a=10,b=20; while(a<b) { System.out.println("Hello"); } System.out.println("World"); } } a) Hello b) run time error c) Hello world d) compile time error
d
What is the output of this program? class operators { public static void main(String args[]) { int x = 8; System.out.println(++x * 3 + " " + x); } } a) 24 8 b) 24 9 c) 27 8 d) 27 9
c
What is the output of this program? class operators { public static void main(String args[]) { int var1 = 5; int var2 = 6; int var3; var3 = ++ var2 * var1 / var2 + var2; System.out.print(var3); } } a) 10 b) 11 c) 12 d) 56
a
Can 8 byte long data type be automatically type cast to 4 byte float data type? a) True b) False
a
Decrement operator, −−, decreases value of variable by what number? a) 1 b) 2 c) 3 d) 4
c
Modulus operator, %, can be applied to which of these? a) Integers b) Floating - point numbers c) Both Integers and floating - point numbers. d) None of the mentioned
d
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
a
What is the order of precedence (highest to lowest) of following operators? 1. & 2. ^ 3. ?: a) 1 -> 2 -> 3 b) 2 -> 1 -> 3 c) 3 -> 2 -> 1 d) 2 -> 3 -> 1
b
What is the output of relational operators? a) Integer b) Boolean c) Characters d) Double
a
What is the output of this program? class Modulus { public static void main(String args[]) { double a = 25.64; int b = 25; a = a % 10; b = b % 10; System.out.println(a + " " + b); } } a) 5.640000000000001 5 b) 5.640000000000001 5.0 c) 5 5 d) 5 5.640000000000001
c
What is the output of this program? class Output { public static void main(String args[]) { boolean a = true; boolean b = false; boolean c = a ^ b; System.out.println(!c); } } a) 0 b) 1 c) false d) true
a
What is the output of this program? class Output { public static void main(String args[]) { int a = 1; int b = 2; int c = 3; a |= 4; b >>= 1; c <<= 1; a ^= c; System.out.println(a + " " + b + " " + c); } } a) 3 1 6 b) 2 2 3 c) 2 3 4 d) 3 3 6
d
What is the output of this program? class Output { public static void main(String args[]) { int a = 1; int b = 2; int c; int d; c = ++b; d = a++; c++; b++; ++a; System.out.println(a + " " + b + " " + c); } } a) 3 2 4 b) 3 2 3 c) 2 3 4 d) 3 4 4
d
What is the output of this program? class Output { public static void main(String args[]) { int a = 5; int b = 10; first: { second: { third: { if (a == b >> 1) break second; } System.out.println(a); } System.out.println(b); } } } a) 5 10 b) 10 5 c) 5 d) 10
b
What is the output of this program? class Output { public static void main(String args[]) { int x , y = 1; x = 10; if (x != 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); } } a) 1 b) 2 c) Runtime error owing to division by zero in if condition. d) Unpredictable behavior of program.
d
What is the output of this program? class Relational_operator { public static void main(String args[]) { int var1 = 5; int var2 = 6; System.out.print(var1 > var2); } } a) 1 b) 0 c) true d) false
c
What is the output of this program? class bitwise_operator { public static void main(String args[]) { int var1 = 42; int var2 = ~var1; System.out.print(var1 + " " + var2); } } a) 42 42 b) 43 43 c) 42 -43 d) 42 43
a
What is the output of this program? class bitwise_operator { public static void main(String args[]) { int a = 3; int b = 6; int c = a | b; int d = a & b; System.out.println(c + " " + d); } } a) 7 2 b) 7 7 c) 7 5 d) 5 2
d
What is the output of this program? class bool_operator { public static void main(String args[]) { boolean a = true; boolean b = !true; boolean c = a | b; boolean d = a & b; boolean e = d ? b : c; System.out.println(d + " " + e); } } a) false false b) true ture c) true false d) false true
b
What is the output of this program? class comma_operator { public static void main(String args[]) { int sum = 0; for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1) sum += i; System.out.println(sum); } } a) 5 b) 6 c) 14 d) compilation error
c
What is the output of this program? class increment { public static void main(String args[]) { double var1 = 1 + 5; double var2 = var1 / 4; int var3 = 1 + 5; int var4 = var3 / 4; System.out.print(var2 + " " + var4); } } a) 1 1 b) 0 1 c) 1.5 1 d) 1.5 1.0
c
What is the output of this program? class increment { public static void main(String args[]) { int g = 3; System.out.print(++g * 8); } } a) 25 b) 24 c) 32 d) 33
c
What is the output of this program? class jump_statments { public static void main(String args[]) { int x = 2; int y = 0; for ( ; y < 10; ++y) { if (y % x == 0) continue; else if (y == 8) break; else System.out.print(y + " "); } } } a) 1 3 5 7 b) 2 4 6 8 c) 1 3 5 7 9 d) 1 2 3 4 5 6 7 8 9
d
What is the output of this program? class leftshift_operator { public static void main(String args[]) { byte x = 64; int i; byte y; i = x << 2; y = (byte) (x << 2) System.out.print(i + " " + y); } } a) 0 64 b) 64 0 c) 0 256 d) 256 0
c
What is the output of this program? class Output { public static void main(String args[]) { int a,b,c,d; a=b=c=d=20 a+=b-=c*=d/=20 System.out.println(a+" "+b+" "+c+" "+d); } } a) compile time error b) runtime error c) a=20 b=0 c=20 d=1 d) none of the mentioned
d
What is the output of this program? class Output { public static void main(String args[]) { int x=y=z=20; } } a) compile and runs fine b) 20 c) run time error d) compile time error
d
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
c
What should be expression1 evaluate to in using ternary operator as in this line? expression1 ? expression2 : expression3 a) Integer b) Floating - point numbers c) Boolean d) None of the mentioned
d
Which of the following can be operands of arithmetic operators? a) Numeric b) Boolean c) Characters d) Both Numeric & Characters
a
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
d
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
a
Which of these are selection statements in Java? a) if() b) for() c) continue d) break
a
Which of these have highest precedence? a) () b) ++ c) * d) >>
d
Which of these is not a bitwise operator? a) & b) &= c) |= d) <=
c
Which of these is returned by "greater than", "less than" and "equal to" operators? a) Integers b) Floating - point numbers c) Boolean d) None of the mentioned
a
Which of these jump statements can skip processing remainder of code in its body for a particular iteration? a) break b) return c) exit d) continue
c
Which of these lines of code will give better performance? 1. a | 4 + c >> b & 7; 2. (a | ((( 4 * c ) >> b ) & 7 )) a) 1 will give better performance as it has no parentheses. b) 2 will give better performance as it has parentheses. c) Both 1 & 2 will give equal performance. d) Dependent on the computer system.
d
Which of these operators can skip evaluating right hand operand? a) ! b) | c) & d) &&
b
Which of these selection statements test only for equality? a) if b) switch c) if & switch d) None of the mentioned
d
Which of these statement is correct? a) true and false are numeric values 1 and 0. b) true and false are numeric values 0 and 1. c) true is any non zero value and false is 0. d) true and false are non numeric values.
b
Which of these statement is incorrect? a) switch statement is more efficient than a set of nested ifs. b) two case constants in the same switch can have identical values. c) switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression. d) it is possible to create a nested switch statements.
d
Which of these statements are incorrect? a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms. b) Assignment operators run faster than their equivalent long forms. c) Assignment operators can be used only with numeric and character data type. d) None
c
Which of these statements are incorrect? a) Equal to operator has least precedence. b) Brackets () have highest precedence. c) Division operator, /, has higher precedence than multiplication operator. d) Addition operator, +, and subtraction operator have equal precedence.
d
Which of these statements are incorrect? a) The left shift operator, <<, shifts all of the bits in a value to the left specified number of times. b) The right shift operator, >>, shifts all of the bits in a value to the right specified number of times. c) The left shift operator can be used as an alternative to multiplying by 2. d) The right shift operator automatically fills the higher order bits with 0.
a
Which operator is used to invert all the digits in binary representation of a number? a) ~ b) <<< c) >>> d) ^
b
Which right shift operator preserves the sign of the value? a) << b) >> c) <<= d) >>=
c
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