You are on page 1of 14

INSTITUTE OF TECHNOLOGY & MANAGEMENT, ALIGARH

JAVA PROGRAMMING (C-401)


B.C.A. Second Year (4th Semester)
Session: 2022-23

QUESTION BANK (UNIT-2) - SOLUTION

Maintained By – Mr. Arshil Noor


(Assistant Professor)
Department Of Computer Science
&
Engineering
UNIT - 2: Explanation: A traditional for loop is best
known for having a loop variable counting up
1. Which type of loop is best known for its or down as the loop progresses. Therefore,
boolean condition that controls entry to the loop? Option B is correct.
A. do-while loop
B. for (traditional) 3. Which type of loop is guaranteed to have the
C. for-each body execute at least once?

D. while A. do-while loop

Answer: D. while B. for (traditional)

Explanation: A while loop has a condition that C. for-each


returns a boolean that controls the loop. It D. while
appears at the beginning and is checked before
entering the loop. Therefore, Option D is Answer: A. do-while loop
correct. Explanation: With a do while loop the
condition is not evaluated until the end of the
loop. Because of that a do while loop will
2. Which type of loop is best known for using anS always execute at least once.
index or counter?
A. do-while loop
B. for (traditional)
C. for-each
D. while
Answer: B. for (traditional)
4. Which of the following can loop through an the i value will be incremented until the
array without referring to the elements by index? condition fails ( i < 5) so i value is 5.
A. do-while loop
B. for (traditional) 7. What is the output of the following program?
C. for-each public class Test{
D. while public static void main(String []args){
Answer: C. for-each int i = 0;
Explanation: Explanation: While a traditional for(i = 0; i < 10; i++){
for loop often loops through an array, it uses
an index to do so, making Option B incorrect. break;
The for-each loop goes through each element, }
storing it in a variable. Option C is correct.
System.out.println(i);
}
5. What keyword is used to end the current loop
iteration and proceed execution with the next }
iteration of that loop?
A. break
B. continue A. 1
C. end B. 0
D. skip C. 10
Answer: B. continue D. 9
Explanation: The continue keyword is used to Answer: B. 0
end the current iteration in a for loop (or a
while loop), and continues to the next iteration. Explanation: When a break statement is
encountered inside a loop, the loop is
immediately terminated and the program
6. What is the output of the following code control resumes at the next statement following
snippet? the loop.

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.

11. Modulus operator, %, can be applied to which


9. What is the output of the following program?
of these?
public class Test{
A. Both Integers and floating - point
public static void main(String []args){ numbers
for(int i = 0; i < 10; i++){ B. Integers
if(i % 2 == 0){ C. Floating - point numbers
continue; D. None of the mentioned
} Answer: A
System.out.println(i); Explanation: Modulus operator can be applied
to both integers and floating point numbers.
}
}
12. Decrement operator, −−, decreases the
} value of variable by what number?
A. Program will print all even numbers A. 1
between 0 to 10
B. 2
B. Program will print all odd numbers
between 0 to 10 C. 3
C. Program gives a compilation error D. 4
D. None of the above Answer: A
Answer: B. Program will print all odd numbers Explanation: Decrement operator, âˆ'âˆ',
between 0 to 10 decreases the value of variable by 1.
Explanation: Option B is the correct choice.
For loop starts with 0 and goes up to 9 after
13. Which of these statements are incorrect?
that the condition becomes false. Inside the
loop, if condition checks if the current value of A. Assignment operators can be used only
variable i is divisible by 2 by checking the with numeric and character data type
remainder. If it is 0, the current iteration is
skipped using the continue statement. If not,
B. Assignment operators are more D. Combine two numeric values
efficiently implemented by Java run-time
system than their equivalent long forms Answer: C

C. Assignment operators run faster than Explanation: Therefore, the contents of a


their equivalent long forms Boolean variable are either 1 or 0 when
displayed. Table 7.1 Boolean conversions. An
D. None of the mentioned integer is a number of the set {. . . , -2, -1,0,
1,2, ... }.
Answer: D
The compare() method of Java Boolean class
Explanation: The statement "Assignment compares the two Boolean values (x and y) and
operators can be used only with numeric and returns an integer value based on the result of
character data type" is incorrect. Explanation: this method.
Assignment operators are used to assign a
value to a variable.

14. Can 8 byte long data type be automatically


type cast to 4 byte float data type?
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
Answer: A
Explanation: Both data types have different
memory representation that's why 8-byte
integral data type can be stored to 4-byte
floating point data type. 17. Which of the following is the correct
expression that evaluates to true if the number x is
between 1 and 100 or the number is negative?
15. What is/are highest order precedence A. ((x < 100) && (x > 1)) && (x < 0)
operator(s) in Java?
B. ((x < 100) && (x > 1)) || (x < 0)
A. ( )
C. (1 > x > 100) || (x < 0)
B. { }
D. 1 < x < 100 || x < 0
C. Both A & B
Answer: B
D. None of these
Explanation:
Answer: C
Explanation: The operator precedence is
responsible for evaluating the expressions. In 18. Select from among the following character
Java, parentheses() and Array subscript[] have escape code which is not available in Java.
the highest precedence in Java. A. \\
B. \v
16. The && and || operators C. \a
A. Compare two boolean values D. \t
B. Compare two numeric values Answer: C
C. Combine two boolean values
Explanation: The backslash \ is an escape C. 3 7 5 3 7 5
character in Java Strings. That means
backslash has a predefined meaning in Java. D. 3 4 5 3 7 5
You have to use double backslash \\ to define a Answer: B
single backslash.
Explanation: The reference variables a1 and a3
V − Value, and is mainly used to represent refer to the same long array object. When the
parameter type of value of a map. N − Number, [1] element is updated in the fix() method, it is
and is mainly used to represent numbers. T − updating the array referred to by a1. The
Type, and is mainly used to represent first reference variable a2 refers to the same array
generic type parameter. S − Type, and is object.
mainly used to represent second generic type
parameter. So Output: 3+7+5+" "3+7+5

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,

class Bitwise (10 |12) in the binary value.

{ 10 = 1010 ( 0r operator).

public static void main(String [] args) 12=1100.

{ Output is 1110 is a value of 14.

int x = 11 & 9;
int y = x ^ 3; 26. What is the output of this program?

System.out.println( y | 12 ); class increment

} {

} public static void main(String args[])

A. 7 {

B. 0 int g = 5;

C. 14 System.out.print(++g * 8);

D. 8 }

Answer: C }

Explanation: The & operator produces a 1 bit A. 44


when both bits are 1. The result of the & B. 56
operation is 9. The ^ operator produces a 1 bit
when exactly one bit is 1; the result of this C. 48
operation is 10. The | operator produces a 1 bit D. 40
Answer: C B. |
Explanation: Operator ++ has more preference C. &
than *, thus g becomes 6 and when multiplied
by 8 gives 48. D. &&
Answer: D

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.

D. None of the mentioned


Answer: C 31. What is the output of this program?

Explanation: All relational operators return a


boolean value ie. true and false. class Relational_operator
{
23. Which of the following operators can operate public static void main(String args[])
on a boolean variable? {
A. && int var1 = 5;
B. == int var2 = 6;
C. ?: System.out.print(var1 > var2);
D. += }
Answer: D }
Explanation: Operator Short circuit AND,
&&, equal to, == , ternary if-then-else, ?:, are
boolean logical operators. A. 1
B. 0
29. Which of these operators can skip evaluating C. TRUE
right hand operand?
D. FALSE
A. !
Answer: D
Explanation: Operator > returns a boolean A. 0
value. 5 is not greater than 6 therefore false is
returned. B. 1
C. FALSE

32. What is the output of this program? D. TRUE

class ternary_operator Answer: C

{ Explanation: Ex-OR: Different Values=Always


False
public static void main(String args[])
{
34. Which of these have highest precedence?
int x = 3;
A. ()
int y = ~ x;
B. ++
int z;
C. *
z = x > y ? x : y;
D. >>
System.out.print(z);
Answer: A
}
Explanation: the highest precedence operator
} is ().

35. What should be expression1 evaluate to in


using ternary operator as in this line?
A. 0
expression1 ? expression2 : expression3
B. 1
A. Integer
C. 3
B. Floating - point numbers
D. -4
C. Boolean
Answer: C
D. None of the mentioned
Explanation: ~
Answer: C
Explanation: The controlling condition of
33. What is the output of this program? ternary operator must evaluate to boolean.
class Output
{
public static void main(String args[]) 36. What is the value stored in x in following lines
{ of code?

boolean a = true; int x, y, z;

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.

45. What will be the output of the following Java


42. On applying Left shift operator, <<, on integer program?
bits are lost one they are shifted past which
position bit? class bitwise_operator

A. 1 {

B. 32 public static void main(String args[])

C. 33 {

D. 31 int var1 = 42;

Answer: D int var2 = ~var1;

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;

C. The left shift operator can be used as an int c = a | b;


alternative to multiplying by 2 int d = a & b;
D. The right shift operator automatically System.out.println(c + " " + d);
fills the higher order bits with 0
}
Answer: D
}
Explanation: The right shift operator
automatically fills the higher order bit with its A. 7 2
B. 7 7 Explanation: In some cases, we have to execute
a body of the loop at least once even if the
C. 7 5 condition is false. This type of operation can be
D. 5 2 achieved by using a do-while loop.

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?

47. Which of these selection statements test only A. break


for equality? B. return
A. if C. exit
B. switch D. continue
C. if & switch Answer: D
D. none of the mentioned Explanation: Continue jump statements is used
Answer: B to skip remainder process of code in the body
of its particular iteration.
Explanation: Switch selection statements test
only for equality, Switch statements compare
the controlling variable and its constant cases 51. Which of this statement is incorrect?
for equality.
A. switch statement is more efficient than
a set of nested ifs
48. Which of these are selection statements in B. two case constants in the same switch
Java? can have identical values
A. if() C. switch statement can only test for
B. for() equality, whereas if statement can evaluate
any type of boolean expression
C. continue
D. it is possible to create a nested switch
D. break statements
Answer: A Answer: B
Explanation: The correct answer to the Explanation: two case constants in the same
question “Which of these are Selection switch can have identical values is an incorrect
Statements in Java” is option (a). if(). And all statement. Explanation: A switch statement
the others are not coming under Selection allows you to compare a variable to a list of
statements in Java. values for equality.

49. Which of the following loops will execute the


body of loop even when condition controlling the
loop is initially false?
A. do-while 52. What will be the output of the following Java
program?
B. while
class selection_statements
C. for
{
D. none of the mentioned
public static void main(String args[])
Answer: A
{ sum += i;
int var1 = 5; System.out.println(sum);
int var2 = 6; }
if ((var2 = 1) == var1) }
System.out.print(var2); A. 5
else B. 6
System.out.print(++var2); C. 14
} D. compilation error
} Answer: B.
A. 1 Explanation: Using comma operator, we can
include more than one statement in the
B. 2 initialization and iteration portion of the for
C. 3 loop. Therefore both ++i and j = i + 1 is
executed i gets the value – 0,1,2,3,4 & j gets the
D. 4 values -0,1,2,3,4,5.
Answer: B
Explanation: var2 is initialised to 1. The
conditional statement returns false and the else
part gets executed.

54. What will be the output of the following Java


program?
class Output
{
53. What will be the output of the following Java
program? public static void main(String args[])

class comma_operator {

{ final int a=10,b=20;

public static void main(String args[]) while(a<b)

{ {

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.

You might also like