Professional Documents
Culture Documents
int i = 0;
for(i = 0 ; i < 5; i++){ The Java break statement is used to break the
loop or switch statements.
}
System.out.println(i);
8. What is the output of the following program?
A. 5
public class Test{
B. 0
public static void main(String []args){
C. 4
int i = 0;
D. Compilation Error
for(i = 0; i < 10; i++){
Answer: A. 5
continue;
Explanation: The integer variable i declared
before using it in for loop and can be accessible }
after for loop execution completes. In for loop, System.out.println(i);
} the number is odd (not divisible by 2) and the
value is printed.
}
A. 10
10. Which of the following can be operands of
B. 0 arithmetic operators?
C. Compilation error A. Characters
D. 9 B. Boolean
Answer: A. 10 C. Numeric
Explanation: Java continue keyword makes for D. Both Numeric & Characters
loop to skip the current iteration and continue
with the next iteration. There will be total 10 Answer: D
iterations after which the value of variable i
Explanation: The operand of arithmetic
becomes 10 and that would make the for loop
operators can be any of numeric or character
condition false. So finally the value of variable i
type, But not boolean.
is 10 after the loop hence Option A is correct.
This means to insert a new tab at this specific Output: 15 15 Because Numeric values will be
point in the text. In the below example, "\t" is added
used inside the println statement. It is similar
to pressing the tab on our keyboard.
20. What will be the output of the program?
class Main {
19. What will be the output of the program?
public static void main(String [] args)
class Main {
{
public static void main(String [] args)
Main p = new Main();
{
p.start();
Main p = new Main();
}
p.start();
void start()
}
{
void start()
boolean b1 = false;
{
boolean b2 = fix(b1);
long [] a1 = {3,4,5};
System.out.println(b1 + " " + b2);
long [] a2 = fix(a1);
}
System.out.print(a1[0] + a1[1] + a1[2] + "
"); boolean fix(boolean b1)
System.out.println(a2[0] + a2[1] + a2[2]); {
} b1 = true;
long [] fix(long [] a3) return b1;
{ }
a3[1] = 7; }
return a3; A. true true
} B. true false
} C. false true
A. 12 15 D. false false
B. 15 15 Answer: C
Explanation: The boolean b1 in the fix()
method is a different boolean than the b1 in the
start() method. The b1 in the start() method is 22. With x = 0, which of the following are legal
not updated by the fix() method. lines of Java code for changing the value of x to
1?
1. x++;
21. What will be the output of the program?
2. x = x + 1;
class Main {
3. x += 1;
public static void main(String [] args)
4. x =+ 1;
{
A. 1, 2 & 3
Main p = new Main();
B. 1 & 4
p.start();
C. 1, 2, 3 & 4
}
D. 3 & 2
void start()
Answer: C
{
Explanation: Operator ++ increases value of
String s1 = "s"; variable by 1. x = x + 1 can also be written in
shorthand form as x += 1. Also x =+ 1 will set
String s2 = fix(s1); the value of x to 1.
System.out.println(s1 + " " + s2);
} 23. What is the output of this program?
String fix(String s1) class Main {
{ public static void main(String args[])
s1 = s1 + "st"; {
System.out.print(s1 + " "); double var1 = 2 + 4;
return "st"; double var2 = var1 / 4;
} int var3 = 2 + 4;
} int var4 = var3 / 4;
A. s st System.out.print(var2 + " " + var4);
B. sst st }
C. st s st }
D. sst s st A. 0 1
Answer: D B. 1 1
Explanation: When the fix() method is first C. 1.5 1
entered, start()'s s1 and fix()'s s1 reference
variables both refer to the same String object D. 1.5 1.0
(with a value of "slip"). Fix()'s s1 is reassigned
to a new object that is created when the Answer: C
concatenation occurs (this second String object Explanation: ~
has a value of "slipstream"). When the
program returns to start(), another String
object is created, referred to by s2 and with a 24. What will be the output of the program?
value of "stream".
class Main { when at least one bit is 1; the result of this
operation is 14.
public static void main(String [] args)
11=1011
{
9= 1001(binary value)
int x=20;
First condition (int x = 11 & 9;)
String sup = (x < 15) ? "s" : (x < 22)? "t" :
"h"; &-And operator check both are true else
return false so
System.out.println(sup);
X take 9.
}
X=9.
}
A. small
Second condition (int y = x ^ 3;)
B. tiny
^ power operator
C. huge
X=1001 ^
D. Compilation fails
3=0011.
Answer: B
Explanation: This is an example of a nested
ternary operator. The second evaluation (x < Outcome is 1010
22) is true, so the ""t"" value is assigned to
sup. 1010 is a value of 10.
25. What will be the output of the program? Third condition( y | 12 ) is 0r operator,
{ 10 = 1010 ( 0r operator).
int x = 11 & 9;
int y = x ^ 3; 26. What is the output of this program?
} {
A. 7 {
B. 0 int g = 5;
C. 14 System.out.print(++g * 8);
D. 8 }
Answer: C }
27. What is the output of relational operators? Explanation: Operator short circuit and, &&,
and short circuit or, ||, skip evaluating right
A. Integer hand operand when output can be determined
by left operand alone.
B. Boolean
C. Characters
30. Which of these statements is correct?
D. Double
A. true and false are numeric values 1 and
Answer: B 0
Explanation: All relational operators return a B. true and false are numeric values 0 and
boolean value ie. true and false. 1
C. true is any non zero value and false is 0
28. Which of these is returned by "greater than", D. true and false are non numeric values
"less than" and "equal to" operators?
Answer: D
A. Integers
Explanation: True and false are keywords,
B. Floating - point numbers they are non numeric values which do no relate
C. Boolean to zero or non zero numbers.
boolean b = false; x = 0;
boolean c = a ^ b; y = 1;
System.out.println(!c); x = y = z = 8;
} A. 0
} B. 1
C. 9 Explanation: Operator ++ has the highest
precedence than / , * and +. var2 is
D. 8 incremented to 7 and then used
Answer: D in expression, var3 = 7 * 5 / 7 + 7, gives 12.
Explanation: The value stored from the above
lines of code is 8 for the variable X, Y and Z
39. What is the output of this program?
class Main
37. What is the order of precedence (highest to
lowest) of following operators? {
1. & public static void main(String args[])
2. ^ {
3. ?: int x = 8;
A. 1 -> 2 -> 3 System.out.println(++x * 3 + " " +
x);
B. 2 -> 1 -> 3
}
C. 3 -> 2 -> 1
}
D. 2 -> 3 -> 1
A. 24 8
Answer: A
B. 24 9
Explanation: ~
C. 27 8
D. 27 9
38. What is the output of this program?
Answer: D
class operators
Explanation: ~
{
public static void main(String args[])
40. Which of these is not a bitwise operator?
{
A. &
int var1 = 5;
B. &=
int var2 = 6;
C. |=
int var3;
D. <=
var3 = ++ var2 * var1 / var2 +
var2; Answer: D
System.out.print(var3); Explanation: <= is a relational operator.
}
} 41. Which operator is used to invert all the digits
in a binary representation of a number?
A. 10
A. ~
B. 11
B. <<<
C. 12
C. >>>
D. 56
D. ^
Answer: C
Answer: A
Explanation: Unary not operator, ~, inverts all previous contents each time a shift occurs. This
of the bits of its operand in binary also preserves the sign of the value.
representation.
A. 1 {
C. 33 {
Explanation: The left shift operator shifts all of System.out.print(var1 + " " + var2);
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 }
the right. When a left shift is applied to an A. 42 42
integer operand, bits are lost once they are B. 43 43
shifted past the bit position 31. C. 42 -43
43. Which right shift operator preserves the sign D. 42 43
of the value? Answer: C
A. << Explanation: Unary not operator, ~, inverts all
B. >> of the bits of its operand. 42 in binary is
00101010 in using ~ operator on var1 and
C. <<= assigning it to var2 we get inverted value of 42
D. >>= i:e 11010101 which is -43 in decimal.
Answer: B
Explanation: ~ 46. What will be the output of the following Java
program?
class bitwise_operator
44. Which of these statements are incorrect?
{
A. The left shift operator, <<, shifts all of
the bits in a value to the left specified public static void main(String args[])
number of times {
B. The right shift operator, >>, shifts all of int a = 3;
the bits in a value to the right specified
number of times int b = 6;
Answer: A
Explanation: And operator produces 1 bit if 50. Which of these jump statements can skip
both operand are 1. Or operator produces 1 bit processing the remainder of the code in its body
if any bit of the two operands in 1. for a particular iteration?
class comma_operator {
{ {
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i System.out.println("Hello");
+ 1)
}
System.out.println("World");
}
}
A. Hello
B. run time error
C. Hello world
D. compile time error
Answer: D
Explanation: Every final variable is compile
time constant.