Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

lOMoARcPSD|16896511

Java [Question Bank] Unit - 2

Advanced Java Programming -- (Dr. A.P.J. Abdul Kalam Technical University)

Studocu is not sponsored or endorsed by any college or university


Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)
lOMoARcPSD|16896511

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: D. while

1. Which type of loop is best known for Answer: B. for (traditional)


its boolean condition that controls entry Explanation: A traditional for loop
to the loop? is best known for having a loop
A. do-while loop variable counting up or down as
the loop progresses. Therefore,
B. for (traditional) Option B is correct.
C. for-each
D. while 3. Which type of loop is guaranteed to
Answer: D. while have the body execute at least once?

Explanation: A while loop has a A. do-while loop


condition that returns a boolean B. for (traditional)
that controls the loop. It appears at
the beginning and is checked C. for-each
before entering the loop. D. while
Therefore, Option D is correct.
Answer: A. do-while loop
Explanation: With a do while loop
2. Which type of loop is best known for the condition is not evaluated until
using anS index or counter? the end of the loop. Because of
A. do-while loop that a do while loop will always
execute at least once.
B. for (traditional)
C. for-each

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

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

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

The Java break statement is used }


to break the loop or switch
System.out.println(i);
statements.
}
}
8. What is the output of the following
program? }
public class Test{ A. Program will print all even
public static void main(String numbers between 0 to 10
[]args){ B. Program will print all odd
int i = 0; numbers between 0 to 10
C. Program gives a compilation
for(i = 0; i < 10; i++){
error
continue;
D. None of the above
}
Answer: B. Program will print all odd
System.out.println(i); numbers between 0 to 10
} Explanation: Option B is the correct
choice. For loop starts with 0 and
} goes up to 9 after that the
A. 10 condition becomes false. Inside the
loop, if condition checks if the
B. 0 current value of variable i is
C. Compilation error divisible by 2 by checking the
remainder. If it is 0, the current
D. 9 iteration is skipped using the
continue statement. If not, the
Answer: A. 10
number is odd (not divisible by 2)
Explanation: Java continue keyword and the value is printed.
makes for loop to skip the current
iteration and continue with the
next iteration. There will be total 10. Which of the following can be
10 iterations after which the value operands of arithmetic operators?
of variable i becomes 10 and that
would make the for loop condition A. Characters
false. So finally the value of B. Boolean
variable i is 10 after the loop hence
Option A is correct. C. Numeric
D. Both Numeric & Characters

9. What is the output of the following Answer: D


program? Explanation: The operand of
public class Test{ arithmetic operators can be any of
numeric or character type, But not
public static void main(String boolean.
[]args){
for(int i = 0; i < 10; i++){
11. Modulus operator, %, can be applied
if(i % 2 == 0){ to which of these?
continue; A. Both Integers and floating -
point numbers

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

B. Integers 14. Can 8 byte long data type be


automatically type cast to 4 byte float
C. Floating - point numbers data type?
D. None of the mentioned A. TRUE
Answer: A B. FALSE
Explanation: Modulus operator can C. Can be true or false
be applied to both integers and
floating point numbers. D. can not say
Answer: A
12. Decrement operator, −−, Explanation: Both data types have
decreases the value of variable by what different memory representation
number? that's why 8-byte integral data
type can be stored to 4-byte
A. 1 floating point data type.
B. 2
C. 3 15. What is/are highest order
D. 4 precedence operator(s) in Java?

Answer: A A. ( )

Explanation: Decrement operator, B. { }


âˆ'âˆ', decreases the value of C. Both A & B
variable by 1.
D. None of these
Answer: C
13. Which of these statements are
incorrect? Explanation: The operator
precedence is responsible for
A. Assignment operators can be evaluating the expressions. In Java,
used only with numeric and parentheses() and Array subscript[]
character data type have the highest precedence in
B. Assignment operators are more Java.
efficiently implemented by Java
run-time system than their
equivalent long forms 16. The && and || operators
C. Assignment operators run A. Compare two boolean values
faster than their equivalent long
forms B. Compare two numeric values
C. Combine two boolean values
D. None of the mentioned
Answer: D D. Combine two numeric values
Answer: C
Explanation: The statement
"Assignment operators can be used Explanation: Therefore, the
only with numeric and character contents of a Boolean variable are
data type" is incorrect. either 1 or 0 when displayed. Table
Explanation: Assignment operators 7.1 Boolean conversions. An
are used to assign a value to a integer is a number of the set
variable. {. . . , -2, -1,0, 1,2, ... }.
The compare() method of Java
Boolean class compares the two

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

Boolean values (x and y) and V − Value, and is mainly used to


returns an integer value based on represent parameter type of value
the result of this method. of a map. N − Number, and is
mainly used to represent numbers.
T − Type, and is mainly used to
represent first generic type
parameter. S − Type, and is mainly
used to represent second generic
type parameter.
This means to insert a new tab at
this specific point in the text. In the
below example, "\t" is used inside
the println statement. It is similar
to pressing the tab on our
keyboard.

19. What will be the output of the


program?
17. Which of the following is the correct class Main {
expression that evaluates to true if the
number x is between 1 and 100 or the public static void main(String [] args)
number is negative?
{
A. ((x < 100) && (x > 1)) && (x <
Main p = new Main();
0)
p.start();
B. ((x < 100) && (x > 1)) || (x <
0) }
C. (1 > x > 100) || (x < 0) void start()
D. 1 < x < 100 || x < 0 {
Answer: B long [] a1 = {3,4,5};
Explanation: long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] +
a1[2] + " ");
18. Select from among the following
character escape code which is not System.out.println(a2[0] + a2[1]
available in Java. + a2[2]);
A. \\ }
B. \v long [] fix(long [] a3)
C. \a {
D. \t a3[1] = 7;
Answer: C return a3;
Explanation: The backslash \ is an }
escape character in Java Strings.
That means backslash has a }
predefined meaning in Java. You A. 12 15
have to use double backslash \\ to
define a single backslash. B. 15 15

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

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";

} }

boolean fix(boolean b1) }

{ A. s st

b1 = true; B. sst st

return b1; C. st s st

} D. sst s st

} Answer: D

A. true true Explanation: When the fix() method


is first entered, start()'s s1 and
B. true false fix()'s s1 reference variables both
refer to the same String object
C. false true

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

(with a value of "slip"). Fix()'s s1 is A. 0 1


reassigned to a new object that is
B. 1 1
created when the concatenation
occurs (this second String object C. 1.5 1
has a value of "slipstream"). When
the program returns to start(), D. 1.5 1.0
another String object is created, Answer: C
referred to by s2 and with a value
of "stream". Explanation: ~

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 );

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

} 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.

1010 is a value of 10.


Third condition( y | 12 ) is 0r 28. Which of these is returned by
"greater than", "less than" and "equal
operator,
to" operators?
(10 |12) in the binary value.
A. Integers
10 = 1010 ( 0r operator).
B. Floating - point numbers
12=1100.
C. Boolean
Output is 1110 is a value of 14.
D. None of the mentioned
Answer: C
26. What is the output of this program?

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

Explanation: All relational Explanation: True and false are


operators return a boolean value keywords, they are non numeric
ie. true and false. values which do no relate to zero
or non zero numbers.

23. Which of the following operators can


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

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 {

B. true and false are numeric public static void main(String


values 0 and 1 args[])

C. true is any non zero value and {


false is 0 int x = 3;
D. true and false are non numeric int y = ~ x;
values
int z;
Answer: D
z = x > y ? x : y;

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

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

Explanation: Ex-OR: Different Answer: D


Values=Always False Explanation: The value stored from
the above lines of code is 8 for the
variable X, Y and Z
34. Which of these have highest
precedence?
A. () 37. What is the order of precedence
(highest to lowest) of following
B. ++ operators?

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

1. & class Main


2. ^ {
3. ?: public static void main(String args[])
A. 1 -> 2 -> 3 {
B. 2 -> 1 -> 3 int x = 8;
C. 3 -> 2 -> 1 System.out.println(++x * 3
+ " " + x);
D. 2 -> 3 -> 1
}
Answer: A
}
Explanation: ~
A. 24 8
B. 24 9
38. What is the output of this program?
C. 27 8
class operators
D. 27 9
{
Answer: D
public static void main(String
args[]) Explanation: ~
{
int var1 = 5; 40. Which of these is not a bitwise
operator?
int var2 = 6;
A. &
int var3;
B. &=
var3 = ++ var2 * var1 /
var2 + var2; C. |=
System.out.print(var3); D. <=
} Answer: D
} Explanation: <= is a relational
operator.
A. 10
B. 11
41. Which operator is used to invert all
C. 12 the digits in a binary representation of a
D. 56 number?

Answer: C A. ~

Explanation: Operator ++ has the B. <<<


highest precedence than / , * and C. >>>
+. var2 is incremented to 7 and
then used D. ^
in expression, var3 = 7 * 5 / 7 + 7, Answer: A
gives 12.
Explanation: Unary not operator, ~,
inverts all of the bits of its operand
in binary representation.
39. What is the output of this program?

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

Explanation: The right shift


operator automatically fills the
42. On applying Left shift operator, <<, higher order bit with its previous
on integer bits are lost one they are contents each time a shift occurs.
shifted past which position bit? This also preserves the sign of the
A. 1 value.

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

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

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

C. 7 5 D. none of the mentioned

D. 5 2 Answer: A

Answer: A Explanation: In some cases, we


have to execute a body of the loop
Explanation: And operator at least once even if the condition
produces 1 bit if both operand are is false. This type of operation can
1. Or operator produces 1 bit if any be achieved by using a do-while
bit of the two operands in 1. loop.
47. Which of these selection statements
test only for equality?
50. Which of these jump statements
A. if can skip processing the remainder of
B. switch the code in its body for a particular
iteration?
C. if & switch
A. break
D. none of the mentioned
B. return
Answer: B
C. exit
Explanation: Switch selection
statements test only for equality, D. continue
Switch statements compare the Answer: D
controlling variable and its
constant cases for equality. Explanation: Continue jump
statements is used to skip
remainder process of code in the
48. Which of these are selection body of its particular iteration.
statements in Java?
A. if() 51. Which of this statement is incorrect?
B. for() A. switch statement is more
C. continue efficient than a set of nested ifs

D. break B. two case constants in the same


switch can have identical values
Answer: A
C. switch statement can only test
Explanation: The correct answer to for equality, whereas if statement
the question “Which of these are can evaluate any type of boolean
Selection Statements in Java” is expression
option (a). if(). And all the others
are not coming under Selection D. it is possible to create a nested
statements in Java. switch statements

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

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.

53. What will be the output of the


following Java program?
class comma_operator
52. What will be the output of the
following Java program? {

class selection_statements public static void main(String


args[])
{
{
public static void main(String
args[]) int sum = 0;

{ for (int i = 0, j = 0; i < 5 & j <


5; ++i, j = i + 1)
int var1 = 5;
sum += i;
int var2 = 6;
System.out.println(sum);
if ((var2 = 1) == var1)
}
System.out.print(var2);
}
else
A. 5
System.out.print(++var2);
B. 6
}
C. 14
}
D. compilation error
A. 1
Answer: B.
B. 2
Explanation: Using comma
C. 3 operator, we can include more than
one statement in the initialization
D. 4
and iteration portion of the for
Answer: B loop. Therefore both ++i and j = i +
1 is executed i gets the value –
Explanation: var2 is initialised to 1. 0,1,2,3,4 & j gets the values
The conditional statement returns -0,1,2,3,4,5.
false and the else part gets
executed.

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)


lOMoARcPSD|16896511

54. What will be the output of the


following Java 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

Answer: D
Explanation: Every final variable is
compile time constant.

Downloaded by rinkal sarvaiya (rinkalmca@gmail.com)

You might also like