Java Question Bank Unit 2
Java Question Bank Unit 2
B. 0
C. 4
D. Compilation Error
4. Which of the following can loop Answer: A. 5
through an array without referring to
the elements by index? Explanation: The integer variable i
declared before using it in for loop
A. do-while loop and can be accessible after for loop
execution completes. In for loop,
B. for (traditional) the i value will be incremented
C. for-each until the condition fails ( i < 5) so i
value is 5.
D. while
Answer: C. for-each
7. What is the output of the following
Explanation: Explanation: While a program?
traditional for loop often loops
through an array, it uses an index public class Test{
to do so, making Option B public static void main(String []args)
incorrect. The for-each loop goes {
through each element, storing it in
a variable. Option C is correct. int i = 0;
for(i = 0; i < 10; i++){
5. What keyword is used to end the break;
current loop iteration and proceed
execution with the next iteration of that }
loop? System.out.println(i);
A. break }
B. continue }
C. end
D. skip
Answer: B. continue A. 1
Explanation: The continue keyword B. 0
is used to end the current iteration
in a for loop (or a while loop), and C. 10
continues to the next iteration. D. 9
Answer: B. 0
6. What is the output of the following Explanation: When a break
code snippet? statement is encountered inside a
int i = 0; loop, the loop is immediately
terminated and the program
for(i = 0 ; i < 5; i++){ control resumes at the next
} statement following the loop.
System.out.println(i);
A. 5
Answer: A A. ( )
C. 3 7 5 3 7 5 D. false false
D. 3 4 5 3 7 5 Answer: C
Answer: B Explanation: The boolean b1 in the
fix() method is a different boolean
Explanation: The reference than the b1 in the start() method.
variables a1 and a3 refer to the The b1 in the start() method is not
same long array object. When the updated by the fix() method.
[1] element is updated in the fix()
method, it is updating the array
referred to by a1. The reference
variable a2 refers to the same 21. What will be the output of the
program?
array object.
So Output: 3+7+5+" "3+7+5 class Main {
public static void main(String [] args)
Output: 15 15 Because Numeric
values will be added {
Main p = new Main();
20. What will be the output of the p.start();
program?
}
class Main {
void start()
public static void main(String [] args)
{
{
String s1 = "s";
Main p = new Main();
String s2 = fix(s1);
p.start();
System.out.println(s1 + " " + s2);
}
}
void start()
String fix(String s1)
{
{
boolean b1 = false;
s1 = s1 + "st";
boolean b2 = fix(b1);
System.out.print(s1 + " ");
System.out.println(b1 + " " +
b2); return "st";
} }
{ A. s st
b1 = true; B. sst st
return b1; C. st s st
} D. sst s st
} Answer: D
22. With x = 0, which of the following 24. What will be the output of the
are legal lines of Java code for changing program?
the value of x to 1?
class Main {
1. x++;
public static void main(String [] args)
2. x = x + 1;
{
3. x += 1;
int x=20;
4. x =+ 1;
String sup = (x < 15) ? "s" : (x <
A. 1, 2 & 3 22)? "t" : "h";
B. 1 & 4 System.out.println(sup);
C. 1, 2, 3 & 4 }
D. 3 & 2 }
Answer: C A. small
Explanation: Operator ++ increases B. tiny
value of variable by 1. x = x + 1
C. huge
can also be written in shorthand
form as x += 1. Also x =+ 1 will set D. Compilation fails
the value of x to 1.
Answer: B
Explanation: This is an example of
23. What is the output of this program? a nested ternary operator. The
second evaluation (x < 22) is true,
class Main {
so the ""t"" value is assigned to
public static void main(String args[]) sup.
{ 25. What will be the output of the
program?
double var1 = 2 + 4;
class Bitwise
double var2 = var1 / 4;
{
int var3 = 2 + 4;
public static void main(String []
int var4 = var3 / 4; args)
System.out.print(var2 + " " + {
var4);
int x = 11 & 9;
}
int y = x ^ 3;
}
System.out.println( y | 12 );
} class increment
} {
A. 7 public static void main(String
args[])
B. 0
{
C. 14
int g = 5;
D. 8
System.out.print(++g * 8);
Answer: C
}
Explanation: The & operator
produces a 1 bit when both bits are }
1. The result of the & operation is
A. 44
9. The ^ operator produces a 1 bit
when exactly one bit is 1; the B. 56
result of this operation is 10. The |
operator produces a 1 bit when at C. 48
least one bit is 1; the result of this D. 40
operation is 14.
Answer: C
11=1011
Explanation: Operator ++ has more
9= 1001(binary value) preference than *, thus g becomes
First condition (int x = 11 & 9;) 6 and when multiplied by 8 gives
48.
&-And operator check both are true
else return false so
X take 9. 27. What is the output of relational
operators?
X=9.
A. Integer
B. Boolean
Second condition (int y = x ^ 3;)
C. Characters
^ power operator
D. Double
X=1001 ^
Answer: B
3=0011.
Explanation: All relational
operators return a boolean value
Outcome is 1010 ie. true and false.
30. Which of these statements is 32. What is the output of this program?
correct?
class ternary_operator
A. true and false are numeric
values 1 and 0 {
System.out.print(z); C. *
} D. >>
} Answer: A
Explanation: the highest
precedence operator is ().
A. 0
35. What should be expression1
B. 1 evaluate to in using ternary operator as
C. 3 in this line?
D. -4 expression1 ? expression2 :
expression3
Answer: C
A. Integer
Explanation: ~
B. Floating - point numbers
C. Boolean
33. What is the output of this program?
D. None of the mentioned
class Output
Answer: C
{
Explanation: The controlling
public static void main(String condition of ternary operator must
args[]) evaluate to boolean.
{
boolean a = true;
boolean b = false; 36. What is the value stored in x in
boolean c = a ^ b; following lines of code?
System.out.println(!c); int x, y, z;
} x = 0;
} y = 1;
A. 0 x = y = z = 8;
B. 1 A. 0
C. FALSE B. 1
D. TRUE C. 9
Answer: C D. 8
Answer: C A. ~
B. 32
C. 33 45. What will be the output of the
following Java program?
D. 31
class bitwise_operator
Answer: D
{
Explanation: The left shift operator
shifts all of the bits in a value to public static void main(String
the left specified number of times. args[])
For each shift left, the high order {
bit is shifted out and lost, zero is
brought in from the right. When a int var1 = 42;
left shift is applied to an integer
int var2 = ~var1;
operand, bits are lost once they are
shifted past the bit position 31. System.out.print(var1 + " " +
var2);
43. Which right shift operator preserves
the sign of the value? }
A. << }
B. >> A. 42 42
B. 43 43
C. <<=
C. 42 -43
D. >>= D. 42 43
Answer: B Answer: C
Explanation: ~ Explanation: Unary not operator, ~,
inverts all of the bits of its
operand. 42 in binary is 00101010
44. Which of these statements are in using ~ operator on var1 and
incorrect? assigning it to var2 we get inverted
value of 42 i:e 11010101 which is
A. The left shift operator, <<, -43 in decimal.
shifts all of the bits in a value to
the left specified number of times
B. The right shift operator, >>, 46. What will be the output of the
shifts all of the bits in a value to following Java program?
the right specified number of class bitwise_operator
times
{
C. The left shift operator can be
used as an alternative to public static void main(String
multiplying by 2 args[])
D. The right shift operator {
automatically fills the higher
order bits with 0 int a = 3;
int b = 6;
Answer: D
int c = a | b;
int d = a & b; 49. Which of the following loops will
execute the body of loop even when
System.out.println(c + " " + d); condition controlling the loop is initially
} false?
} A. do-while
A. 7 2 B. while
B. 7 7 C. for
D. 5 2 Answer: A
Answer: B
Explanation: two case constants in
the same switch can have identical
values is an incorrect statement.
Explanation: A switch statement
allows you to compare a variable to
a list of values for equality.
System.out.println("Hello");
}
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.