Java Questions
Java Questions
SET-1
Java Questions & Answers Integer and Floating Data Types
Answer:b
Explanation:Short occupies 16 bits in memory. Its range is from -32768 to 32767.
Answer:a
Explanation:Byte occupies 8 bits in memory. Its range is from -128 to 127.
Answer: d
Explanation:Statements (1), (2), (3), and (4) are correct. (1) is correct because when a
floating-point number (a double in this case) is cast to an int, it simply loses the digits after
the decimal.(2) and (4) are correct because a long can be cast into a byte. If the long is over
127, it loses its most significant (leftmost) bits.(3) actually works, even though a cast is not
necessary, because a long can store a byte.
4. An expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float
Answer: b
Explanation:Range of data type float is 3.4e-038 to 3.4e+308.
Answer:c
Explanation:None.
1. class average {
2. public static void main(String args[])
3. {
4. double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
5. double result;
6. result = 0;
7. for (int i = 0; i < 6; ++i)
8. result = result + num[i];
9. System.out.print(result/6);
10.
11. }
12. }
a) 16.34
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666
Answer:c
Explanation:None.
output:
$ javac average.java
$ java average
16.46666666666667
a) 38 43
b) 39 44
c) 295 300
d) 295.04 300
Answer:b
Explanation:Type casting a larger variable into a smaller variable results in modulo of larger
variable by range of smaller variable. b contains 300 which is larger than bytes range i:e -128
to 127 hence d contains 300 modulo 256 i:e 44.
output:
$ javac conversion.java
$ java conversion
39 44
1. class increment {
2. public static void main(String args[])
3. {
4. int g = 3;
5. System.out.print(++g * 8);
6. }
7. }
a) 25
b) 24
c) 32
d) 33
Answer:c
Explanation:Operator ++ has more preference than *, thus g becomes 4 and when multiplied
by 8 gives 32.
output:
$ javac increment.java
$ java increment
32
1. class area {
2. public static void main(String args[])
a) 301.5656
b) 301
c) 301.56
d) 301.56560000
Answer:a
Explanation:None.
output:
$ javac area.java
$ java area
301.5656
Answer:d
Explanation: Char occupies 16-bit in memory, so it supports 2^16 i:e from 0 to 65535.
12. Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned
Answer:c
Explanation: Unicode defines fully international character set that can represent all the
characters found in all human languages. Its range is from 0 to 65536.
14. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java?
a) ASCII
b) ISO-LATIN-1
c) None of the mentioned
d) ASCII and ISO-LATIN1
Answer:d
Explanation: First 0 to 127 character set in Unicode are same as those of ISO-LAIN-1 and
ASCII.
Answer:c
Explanation: Boolean can only be assigned true or false literals.
1. class array_output {
2. public static void main(String args[])
3. {
4. char array_variable [] = new char[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = 'i';
7. System.out.print(array_variable[i] + "" );
8. i++;
9. }
10. }
11. }
a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned
Answer:a
Explanation:None.
output:
$ javac array_output.java
$ java array_output
iiiii
a) 66
b) 67
c) 65
d) 64
Answer:a
Explanation: ASCII value of A is 65, on using ++ operator character value increments by
one.
output:
$ javac mainclass.java
$ java mainclass
66
1. class mainclass {
2. public static void main(String args[])
3. {
4. boolean var1 = true;
5. boolean var2 = false;
6. if (var1)
7. System.out.println(var1);
8. else
9. System.out.println(var2);
10. }
11. }
a) 0
b) 1
c) true
d) false
Answer:c
Explanation:None.
output:
$ javac mainclass.java
$ java mainclass
true
1. class booloperators {
2. public static void main(String args[])
a) 0
b) 1
c) true
d) false
Answer:d
Explanation: boolean & operator always returns true or false. var1 is defined true and var2
is defined false hence their & operator result is false.
output:
$ javac booloperators.java
$ java booloperators
false
1. class asciicodes {
2. public static void main(String args[])
3. {
4. char var1 = 'A';
5. char var2 = 'a';
6. System.out.println((int)var1 + " " + (int)var2);
7. }
8. }
a) 162
b) 65 97
c) 67 95
d) 66 98
Answer:b
Explanation:ASCII code for A is 65 and for a is 97.
output:
$ javac asciicodes.java
$ java asciicodes
65 97
Answer:d
Explanation: We can use binary ampersand operator on integers/chars (and it returns an
integer) or on booleans (and it returns a boolean).
Answer:d
Explanation:Data type long literals are appended by an upper or lowercase L.
Answer:d
Explanation:None
25. Which of these can not be used for a variable name in Java?
a) identifier
b) keyword
c) identifier & keyword
d) None of the mentioned
Answer:b
Explanation:Keywords are specially reserved words which can not be used for naming a user
defined variable, example : class, int, for etc.
1. class evaluate {
2. public static void main(String args[])
3. {
a) 38
b) 39
c) 40
d) 41
Answer:c
Explanation:None
output:
$ javac evaluate.java
$ java evaluate
40
1. class array_output {
2. public static void main(String args[])
3. {
4. int array_variable [] = new int[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = i/2;
7. array_variable[i]++;
8. System.out.print(array_variable[i] + " ");
9. i++;
10. }
11.
12. }
13. }
a) 0 2 4 6 8
b) 1 2 3 4 5
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
Answer:b
Explanation:When an array is declared using new operator then all of its elements are
initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes
in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment
condition of for loop.
output:
$ javac array_output.java
$ java array_output
12345
1. class variable_scope {
2. public static void main(String args[])
3. {
4. int x;
5. x = 5;
6. {
7. int y = 6;
8. System.out.print(x + " " + y);
9. }
10. System.out.println(x + " " + y);
11. }
12. }
a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
Answer:d
Explanation:Second print statement doesnt have access to y , scope y was limited to the
block defined after initialization of x.
output:
$ javac variable_scope.java
Exception in thread main java.lang.Error: Unresolved compilation problem: y cannot be
resolved to a variable
Answer:d
Explanation:all string literals must begin and end in same line.
1. class dynamic_initialization {
2. public static void main(String args[])
3. {
4. double a, b;
5. a = 3.0;
6. b = 4.0;
7. double c = Math.sqrt(a * a + b * b);
8. System.out.println(c);
9. }
10. }
Answer:a
Explanation:Variable c has been dynamically initialized to square root of a * a + b * b, during
run time.
output:
$ javac dynamic_initialization.java
$ java dynamic_initialization
5.0
31. Which of these is necessary condition for automatic type conversion in Java?
a) The destination type is smaller than source type.
b) The destination type is larger than source type.
c) The destination type can be larger or smaller than source type.
d) None of the mentioned
Answer:b
Explanation:None.
Answer: d
Explanation:None.
34. If an expression contains double, int, float, long, then whole expression will promoted
into which of these data types?
a) long
b) int
c) double
d) float
Answer: c
Explanation:If any operand is double the result of expression is double.
Answer: a
Explanation:None.
1. class char_increment {
2. public static void main(String args[])
3. {
4. char c1 = 'D';
5. char c2 = 84;
6. c2++;
7. c1++;
8. System.out.println(c1 + " " + c2);
9. }
10. }
a) E U
b) U E
c) V E
d) U F
Answer:a
Explanation:Operator ++ increments the value of character by 1. c1 and c2 are given values
D and 84, when we use ++ operator their values increments by 1, c1 and c2 becomes E and U
respectively.
output:
$ javac char_increment.java
$ java char_increment
EU
1. class conversion {
2. public static void main(String args[])
3. {
4. double a = 295.04;
5. int b = 300;
6. byte c = (byte) a;
7. byte d = (byte) b;
8. System.out.println(c + " " + d);
9. }
10. }
a) 38 43
b) 39 44
c) 295 300
d) 295.04 300
Answer:b
Explanation:Type casting a larger variable into a smaller variable results in modulo of larger
variable by range of smaller variable. b contains 300 which is larger than bytes range i:e -128
to 127 hence d contains 300 modulo 256 i:e 44.
output:
$ javac conversion.java
$ java conversion
39 44
1. class A {
2. final public int calculate(int a, int b) { return 1; }
3. }
4. class B extends A {
5. public int calculate(int a, int b) { return 2; }
6. }
7. public class output {
8. public static void main(String args[])
9. {
10. B object = new B();
11. System.out.print("b is " + b.calculate(0, 1));
12. }
13. }
a) b is : 2
b) b is : 1
c) Compilation Error.
d) An exception is thrown at runtime.
Answer:c
Explanation:The code does not compile because the method calculate() in class A is final and
so cannot be overridden by method of class b.
1. class main_arguments {
2. public static void main(String [] args)
3. {
4. String [][] argument = new String[2][2];
5. int x;
6. argument[0] = args;
7. x = argument[0].length;
8. for (int y = 0; y < x; y++)
9. System.out.print(" " + argument[0][y]);
10. }
11. }
a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3
Answer:d
Explanation:In argument[0] = args;, the reference variable arg[0], which was referring to an
array with two elements, is reassigned to an array (args) with three elements.
Output:
$ javac main_arguments.java
$ java main_arguments
123
1. class c {
2. public void main( String[] args )
3. {
4. System.out.println( "Hello" + args[0] );
5. }
6. }
a) Hello c
b) Hello
c) Hello world
d) Runtime Error.
Answer:d
Explanation:A runtime error will occur owning to the main method of the code fragment not
being declared static.
Output:
$ javac c.java
Exception in thread main java.lang.NoSuchMethodError: main
Answer:c
Explanation:Operator new allocates block of memory specified by the size of array, and gives
the reference of memory allocated to the array variable.
Answer:d
Explanation:Operator new must be succeeded by array type and array size.
Answer:d
Explanation:arr is an array variable, it is pointing to array if integers. Printing arr will print
garbage value. It is not same as printing arr[0].
Answer: a
Explanation:Array can be initialized using both new and comma separated expressions
surrounded by curly braces example : int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};
1. class array_output {
2. public static void main(String args[])
3. {
4. int array_variable [] = new int[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = i;
7. System.out.print(array_variable[i] + " ");
8. i++;
9. }
10. }
11. }
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
Answer:a
Explanation:When an array is declared using new operator then all of its elements are
initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes
in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment
condition of for loop.
output:
$ javac array_output.java
$ java array_output
02468
1. class multidimention_array {
2. public static void main(String args[])
3. {
4. int arr[][] = new int[3][];
5. arr[0] = new int[1];
6. arr[1] = new int[2];
7. arr[2] = new int[3];
8. int sum = 0;
9. for (int i = 0; i < 3; ++i)
10. for (int j = 0; j < i + 1; ++j)
11. arr[i][j] = j + 1;
12. for (int i = 0; i < 3; ++i)
13. for (int j = 0; j < i + 1; ++j)
14. sum + = arr[i][j];
15. System.out.print(sum);
16. }
17. }
a) 11
b) 10
Answer:b
Explanation:arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains 1
element, 2nd row contains 2 elements and 3rd row contains 3 elements. each element of array
is given i + j value in loop. sum contains addition of all the elements of the array.
output:
$ javac multidimention_array.java
$ java multidimention_array
10
1. class evaluate {
2. public static void main(String args[])
3. {
4. int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
5. int n = 6;
6. n = arr[arr[n] / 2];
7. System.out.println(arr[n] / 2);
8. }
9. }
a) 3
b) 0
c) 6
d) 1
Answer:d
Explanation:Array arr contains 10 elements. n contains 6 thus in next line n is given value 2
printing arr[2]/2 i:e 2/2 = 1.
output:
$ javac evaluate.java
$ java evaluate
1
1. class array_output {
2. public static void main(String args[])
3. {
4. char array_variable [] = new char[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = 'i';
7. System.out.print(array_variable[i] + "");
8. }
9. }
10. }
Answer:d
Explanation:None.
output:
$ javac array_output.java
$ java array_output
iiiiiiiiii
1. class array_output {
2. public static void main(String args[])
3. {
4. int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
5. int sum = 0;
6. for (int i = 0; i < 3; ++i)
7. for (int j = 0; j < 3 ; ++j)
8. sum = sum + array_variable[i][j];
9. System.out.print(sum / 5);
10. }
11. }
a) 8
b) 9
c) 10
d) 11
Answer:b
Explanation:None.
output:
$ javac array_output.java
$ java array_output
9
SET-2
Java Questions & Answers Arithmetic Operators
1. Which of the following can be operands of arithmetic operators?
a) Numeric
b) Boolean
c) Characters
d) Both Numeric & Characters
Answer:d
Explanation: The operand of arithmetic operators can be any of numeric or character type,
But not boolean.
Answer:c
Explanation: Modulus operator can be applied to both integers and floating point numbers. .
3. 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
Answer: d
Explanation: Operator ++ increases value of variable by 1. x = x + 1 can also be written in
shorthand form as x += 1. Also x =+ 1 will set the value of x to 1.
Answer: a
Explanation: None.
Answer: d
Explanation: None.
1. class increment {
2. public static void main(String args[])
3. {
4. double var1 = 1 + 5;
5. double var2 = var1 / 4;
6. int var3 = 1 + 5;
7. int var4 = var3 / 4;
8. System.out.print(var2 + " " + var4);
9.
10. }
11. }
a) 1 1
b) 0 1
c) 1.5 1
d) 1.5 1.0
Answer:c
Explanation: None
output:
$ javac increment.java
$ java increment
1.5 1
1. class Modulus {
2. public static void main(String args[])
3. {
4. double a = 25.64;
5. int b = 25;
6. a = a % 10;
7. b = b % 10;
8. System.out.println(a + " " + b);
9. }
10. }
a) 5.640000000000001 5
b) 5.640000000000001 5.0
c) 5 5
Answer: a
Explanation: Modulus operator returns the remainder of a division operation on the operand.
a = a % 10 returns 25.64 % 10 i:e 5.640000000000001. Similarly b = b % 10 returns 5.
output:
$ javac Modulus.java
$ java Modulus
5.640000000000001 5
1. class increment {
2. public static void main(String args[])
3. {
4. int g = 3;
5. System.out.print(++g * 8);
6. }
7. }
a) 25
b) 24
c) 32
d) 33
Answer:c
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied
by 8 gives 32.
output:
$ javac increment.java
$ java increment
32
a) 11 11
b) 10 10
c) 11 10
d) 10 11
1. class Output {
2. public static void main(String args[])
3. {
4. int a = 1;
5. int b = 2;
6. int c;
7. int d;
8. c = ++b;
9. d = a++;
10. c++;
11. b++;
12. ++a;
13. System.out.println(a + " " + b + " " + c);
14. }
15. }
a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4
Answer: d
Explanation: None.
output:
$ javac Output.java
$ java Output
344
a) ~ b) <<< c) >>>d) ^
Answer:a
Explanation: Unary not operator, ~, inverts all of the bits of its operand in binary
representation.
13. 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
Answer: d Explanation: The left shift operator shifts all of the bite 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 right. When a left shift is applied to an integer operand, bits are lost once
they are shifted past the bit position 31.
14. Which right shift operator preserves the sign of the value?
Answer: b
Explanation: None.
Answer: d
Explanation: The right shift operator automatically fills the higher order bit with its previous
contents each time a shift occurs. This also preserves the sign of the value.
1. class bitwise_operator {
2. public static void main(String args[])
3. {
4. int var1 = 42;
5. int var2 = ~var1;
6. System.out.print(var1 + " " + var2);
7. }
8. }
a) 42 42
b) 43 43
c) 42 -43
Answer:c
Explanation: Unary not operator, ~, inverts all of the bits of its operand. 42 in binary is
00101010 in using ~ operator on var1 and assigning it to var2 we get inverted value of 42 i:e
11010101 which is -43 in decimal.
output:
$ javac bitwise_operator.java
$ java bitwise_operator
42 -43
a) 7 2
b) 7 7
c) 7 5
d) 5 2
Answer: a
Explanation: And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if
any bit of the two operands in 1.
output:
$ javac bitwise_operator.java
$ java bitwise_operator
72
1. class leftshift_operator {
2. public static void main(String args[])
3. {
4. byte x = 64;
5. int i;
6. byte y;
7. i = x << 2;
8. y = (byte) (x << 2)
9. System.out.print(i + " " + y);
10. }
11. }
Answer:d
Explanation: None.
output:
$ javac leftshift_operator.java
$ java leftshift_operator
256 0
1. class rightshift_operator {
2. public static void main(String args[])
3. {
4. int x;
5. x = 10;
6. x = x >> 1;
7. System.out.println(x);
8. }
9. }
a) 10
b) 5
c) 2
d) 20
Answer: b
Explanation: Right shift operator, >>, devides the value by 2.
output:
$ javac rightshift_operator.java
$ java rightshift_operator
5
1. class Output {
2. public static void main(String args[])
3. {
4. int a = 1;
5. int b = 2;
6. int c = 3;
7. a |= 4;
8. b >>= 1;
9. c <<= 1;
10. a ^= c;
11. System.out.println(a + " " + b + " " + c);
12. }
13. }
Answer: a
Explanation: None.
output:
$ javac Output.java
$ java Output
316
Answer: b
Explanation: None.
22. Which of these is returned by greater than, <, and equal to, ==, operator?
Answer:c Explanation: All relational operators return a boolean value i:e true and false.
1. && 2. == 3. ?: 4. +=
Answer: d Explanation: Operator Short circuit AND, &&, equal to, == , ternary if-then-
else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on
numeric values.
24. Which of these operators can skip evaluating right hand operand?
a) ! b) | c) & d) &&
Answer: d Explanation: Operator short circuit and, &&, and short circuit or, ||, skip
evaluating right hand operand when output can be determined by left operand alone.
Answer: d Explanation: true and false are keywords, they are non numeric values which do
no relate to zero or non zero numbers. true and false are boolean values.
1. class Relational_operator {
2. public static void main(String args[])
3. {
4. int var1 = 5;
5. int var2 = 6;
6. System.out.print(var1 > var2);
7. }
8. }
a) 1
b) 0
c) true
d) false
Answer:d
Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is
returned.
output:
$ javac Relational_operator.java
$ java Relational_operator
false
1. class bool_operator {
2. public static void main(String args[])
3. {
4. boolean a = true;
5. boolean b = !true;
6. boolean c = a | b;
7. boolean d = a & b;
8. boolean e = d ? b : c;
9. System.out.println(d + " " + e);
10. }
11. }
Answer: d
Explanation: Operator | returns true if any one operand is true, thus c = true | false is true.
Operator & returns a true if both of the operand is true thus d is false. Ternary operator ?:
assigns left of : if condition is true and right hand of : if condition is false. d is false thus e
= d ? b : c , assigns c to e , e contains true.
output:
$ javac bool_operator.java
$ java bool_operator
false true
1. class ternary_operator {
2. public static void main(String args[])
3. {
4. int x = 3;
5. int y = ~ x;
6. int z;
7. z = x > y ? x : y;
8. System.out.print(z);
9. }
10. }
a) 0
b) 1
c) 3
d) -4
Answer:c
Explanation: None.
output:
$ javac ternary_operator.java
$ java ternary_operator
3
1. class Output {
2. public static void main(String args[])
3. {
4. int x , y = 1;
5. x = 10;
6. if (x != 10 && x / 0 == 0)
7. System.out.println(y);
8. else
9. System.out.println(++y);
10. }
11. }
a) 1
b) 2
c) Runtime error owing to division by zero in if condition.
d) Unpredictable behavior of program.
Answer: b
Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left hand
operand is false thus division by zero in if condition does not give an error.
output:
$ javac Output.java
$ java Output
2
1. class Output {
2. public static void main(String args[])
3. {
4. boolean a = true;
5. boolean b = false;
6. boolean c = a ^ b;
7. System.out.println(!c);
8. }
9. }
a) 0
b) 1
c) false
d) true
Answer: c
Explanation: None.
output:
$ javac Output.java
$ java Output
false
32. 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
Answer:c
Explanation: The controlling condition of ternary operator must evaluate to boolean.
Answer: d
Explanation: None.
Answer: a
Explanation: None.
Answer: c
Explanation: Division operator, /, has equal precedence as of multiplication operator. In
1. class operators {
2. public static void main(String args[])
3. {
4. int var1 = 5;
5. int var2 = 6;
6. int var3;
7. var3 = ++ var2 * var1 / var2 + var2;
8. System.out.print(var3);
9. }
10. }
a) 10
b) 11
c) 12
d) 56
Answer:c
Explanation: Operator ++ has the highest precedence than / , * and +. var2 is incremented to
7 and then used in expression, var3 = 7 * 5 / 7 + 7, gives 12.
output:
$ javac operators.java
$ java operators
12
1. class operators {
2. public static void main(String args[])
3. {
4. int x = 8;
5. System.out.println(++x * 3 + " " + x);
6. }
7. }
a) 24 8
b) 24 9
c) 27 8
d) 27 9
Answer: d
Explanation: Operator ++ has higher precedence than multiplication operator, *, x is
incremented to 9 than multiplied with 3 giving 27.
output:
$ javac operators.java
$ java operators
27 9
1. class ternary_operator {
2. public static void main(String args[])
3. {
4. int x = 3;
5. int y = ~ x;
6. int z;
7. z = x > y ? x : y;
8. System.out.print(z);
9. }
10. }
a) 0
b) 1
c) 3
d) -4
Answer:c
Explanation: None.
output:
$ javac ternary_operator.java
$ java ternary_operator
3
Answer: c
Explanation: Parentheses do not degrade the performance of the program. Adding parentheses
to reduce ambiguity does not negatively affect your system.
1. class Output {
2. public static void main(String args[])
3. {
4. int x , y = 1;
5. x = 10;
6. if (x != 10 && x / 0 == 0)
7. System.out.println(y);
8. else
9. System.out.println(++y);
10. }
11. }
Answer: b
Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left hand
operand is false thus division by zero in if condition does not give an error.
output:
$ javac Output.java
$ java Output
2
Answer: b
Explanation: switch statements checks for equality between the controlling variable and its
constant cases.
Answer:a
Explanation: continue and break are jump statements, and for is an looping statement.
43. 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
Answer: a
Explanation: None.
44. Which of these jump statements can skip processing remainder of code in its body for a
particular iteration?
Answer: d
Explanation: None.
Answer: b
Explanation: No two case constants in the same switch can have identical values.
1. class selection_statements {
2. public static void main(String args[])
3. {
4. int var1 = 5;
5. int var2 = 6;
6. if ((var2 = 1) == var1)
7. System.out.print(var2);
8. else
9. System.out.print(++var2);
10. }
11. }
a) 1
b) 2
c) 3
d) 4
Answer:b
Explanation: var2 is initialised to 1. The conditional statement returns false and the else part
gets executed.
output:
$ javac selection_statements.java
$ java selection_statements
2
1. class comma_operator {
2. public static void main(String args[])
3. {
a) 5
b) 6
c) 14
d) compilation error
Answer: b
Explanation: Using comma operator , we can include more than one statement in the
initialization and iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed
i gets the value 0,1,2,3,4 & j gets the values -0,1,2,3,4,5.
output:
$ javac comma_operator.java
$ java comma_operator
6
1. class jump_statments {
2. public static void main(String args[])
3. {
4. int x = 2;
5. int y = 0;
6. for ( ; y < 10; ++y) {
7. if (y % x == 0)
8. continue;
9. else if (y == 8)
10. break;
11. else
12. System.out.print(y + " ");
13. }
14. }
15. }
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
Answer:c
Explanation: Whenever y is divisible by x remainder body of loop is skipped by continue
statement, therefore if condition y == 8 is never true as when y is 8, remainder body of loop
is skipped by continue statements of first if. Control comes to print statement only in cases
when y is odd.
output:
$ javac jump_statments.java
1. class Output {
2. public static void main(String args[])
3. {
4. int x, y = 1;
5. x = 10;
6. if (x != 10 && x / 0 == 0)
7. System.out.println(y);
8. else
9. System.out.println(++y);
10. }
11. }
a) 1
b) 2
c) Runtime error owing to division by zero in if condition.
d) Unpredictable behavior of program.
Answer: b
Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left hand
operand is false thus division by zero in if condition does not give an error.
output:
$ javac Output.java
$ java Output
2
1. class Output {
2. public static void main(String args[])
3. {
4. int a = 5;
5. int b = 10;
6. first: {
7. second: {
8. third: {
9. if (a == b >> 1)
10. break second;
11. }
12. System.out.println(a);
13. }
14. System.out.println(b);
15. }
16. }
17. }
a) 5 10
b) 10 5
c) 5
Answer: d
Explanation: b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of if is executed
and block second is exited. Control goes to end of the block second executing the last print
statement, printing 10.
output:
$ javac Output.java
$ java Output
10
SET-3
Java Questions & Answers Class Fundamentals & Declaring objects
Answer: b
Explanation: Memory is allocated to an object using new operator. box obj; just declares a
reference to object, no memory is allocated to it hence it points to NULL.
Answer:a
Explanation: None.
Answer: a
Explanation: None.
Answer: c
Explanation: Operator new dynamically allocates memory for an object and returns a
reference to it. This reference is address in memory of the object allocated by new.
Answer: a
Explanation: Every class does not need to have a main() method, there can be only one
main() method which is made public.
1. class main_class {
2. public static void main(String args[])
3. {
4. int x = 9;
5. if (x == 9) {
6. int x = 8;
7. System.out.println(x);
8. }
9. }
10. }
a) 9
b) 8
c) Compilation error
d) Runtime error
Answer: c
Explanation: Two variables with the same name cant be created in a class.
output:
$ javac main_class.java
Exception in thread main java.lang.Error: Unresolved compilation problem:
Duplicate local variable x
Answer: a
Explanation: None.
1. class box {
2. int width;
3. int height;
4. int length;
5. }
6. class mainclass {
a) 12
b) 200
c) 400
d) 100
Answer: b
Explanation: None.
output:
$ javac mainclass.java
$ java mainclass
200
1. class box {
2. int width;
3. int height;
4. int length;
5. }
6. class mainclass {
7. public static void main(String args[])
8. {
9. box obj1 = new box();
10. box obj2 = new box();
11. obj1.height = 1;
12. obj1.length = 2;
13. obj1.width = 1;
14. obj2 = obj1;
15. System.out.println(obj2.height);
16. }
17. }
a) 1
b) 2
c) Runtime error
d) Garbage value
Answer: a
Explanation: When we assign an object to another object of same type, all the elements of
right side object gets copied to object on left side of equal to, =, operator.
output:
$ javac mainclass.java
1. class box {
2. int width;
3. int height;
4. int length;
5. }
6. class mainclass {
7. public static void main(String args[])
8. {
9. box obj = new box();
10. System.out.println(obj);
11. }
12. }
a) 0
b) 1
c) Runtime error
d) Garbage value
Answer: d
Explanation: Object obj of box class contains reference to the memory which was given to its
class instances. Printing obj will print the address of the memory.
output:
$ javac mainclass.java
$ java mainclass
box@130671e
11. What is the return type of a method that does not returns any value?
a) int
b) float
c) void
d) double
Answer: c
Explanation: Return type of an method must be made void if it is not returning any value.
12. What is the process of defining more than one method in a class differentiated by method
signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
13. Which of the following is a method having same name as that of its class?
a) finalize
b) delete
c) class
d) constructor
Answer: d
Explanation: A constructor is a method that initializes an object immediately upon creation. It
has the same name as that of class in which it resides.
Answer: a
Explanation: main() method can be defined only once in a program. Program execution
begins from the main() method by javas run time system.
Answer: d
Explanation: All object of class share a single copy of methods defined in a class, Methods
are allotted memory only once. All the objects of the class have access to methods of that
class are allotted memory only for the variables not for the methods.
1. class box {
2. int width;
3. int height;
4. int length;
5. int volume;
6. void volume(int height, int length, int width) {
7. volume = width*height*length;
8. }
9. }
10. class Prameterized_method{
a) 0
b) 1
c) 6
d) 25
Answer: c
Explanation: None.
output:
$ Prameterized_method.java
$ Prameterized_method
6
1. class equality {
2. int x;
3. int y;
4. boolean isequal(){
5. return(x == y);
6. }
7. }
8. class Output {
9. public static void main(String args[])
10. {
11. equality obj = new equality();
12. obj.x = 5;
13. obj.y = 5;
14. System.out.println(obj.isequal());
15. }
16. }
a) false
b) true
c) 0
d) 1
Answer: b
Explanation: None.
output:
$ javac Output.java
$ java Output
true
1. class box {
2. int width;
3. int height;
4. int length;
5. int volume;
6. void volume() {
7. volume = width*height*length;
8. }
9. }
10. class Output {
11. public static void main(String args[])
12. {
13. box obj = new box();
14. obj.height = 1;
15. obj.length = 5;
16. obj.width = 5;
17. obj.volume();
18. System.out.println(obj.volume);
19. }
20. }
a) 0
b) 1
c) 25
d) 26
Answer:c
Explanation: None.
output:
$ javac Output.java
$ java Output
25
a) 1
b) 2
c) Runtime Error
d) Compilation Error
1. class area {
2. int width;
3. int length;
4. int volume;
5. area() {
6. width=5;
7. length=6;
8. }
9. void volume() {
10. volume = width*length*height;
11. }
12. }
13. class cons_method {
14. public static void main(String args[])
15. {
16. area obj = new area();
17. obj.volume();
18. System.out.println(obj.volume);
19. }
20. }
a) 0
b) 1
c) 30
d) error
Answer: d
Explanation: Variable height is not defined.
output:
$ javac cons_method.java
$ java cons_method
error: cannot find symbol height
22. Which keyword is used by method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this
Answer: d
Explanation: this keyword can be used inside any method to refer to the current object. this is
always a reference to the object on which the method was invoked.
23. Which of the following is a method having same name as that of its class?
a) finalize
b) delete
c) class
d) constructor
Answer: d
Explanation: A constructor is a method that initializes an object immediately upon creation. It
has the same name as that of class in which it resides.
24. Which operator is used by Java run time implementations to free the memory of an object
when it is no longer needed?
a) delete
b) free
c) new
d) None of the mentioned
Answer: d
Explanation: Java handles deallocation of memory automatically, we do not need to explicitly
delete an element. Garbage collection only occurs during execution of the program. When no
references to the object exist, that object is assumed to be no longer needed, and the memory
occupied by the object can be reclaimed.
25. Which function is used to perform some action when the object is to be destroyed?
a) finalize()
b) delete()
c) main()
d) None of the mentioned
Answer: a
Explanation: None.
a) 100
b) 150
c) 200
d) 250
Answer: b
Explanation: None.
output:
$ constructor_output.java
$ constructor_output
150
1. class equality {
2. int x;
3. int y;
4. boolean isequal() {
5. return(x == y);
6. }
7. }
8. class Output {
9. public static void main(String args[])
10. {
11. equality obj = new equality();
12. obj.x = 5;
13. obj.y = 5;
14. System.out.println(obj.isequal); }
15. }
a) false
b) true
c) 0
Answer: b
Explanation: None.
output:
$ javac Output.java
$ java Output
true
1. class box {
2. int width;
3. int height;
4. int length;
5. int volume;
6. void finalize() {
7. volume = width*height*length;
8. System.out.println(volume);
9. }
10. protected void volume() {
11. volume = width*height*length;
12. System.out.println(volume);
13. }
14. }
15. class Output {
16. public static void main(String args[])
17. {
18. box obj = new box();
19. obj.volume();
20. }
21. }
a) 150
b) 200
c) Runtime error
d) Compilation error
Answer: a
Explanation: None.
output:
$ javac Output.java
$ java Output
150
1. class area {
2. int width;
3. int length;
4. int area;
5. void area(int width, int length) {
6. this.width = width;
7. this.length = length;
8. }
9.
10. }
11. class Output {
12. public static void main(String args[])
13. {
14. area obj = new area();
15. obj.area(5 , 6);
16. System.out.println(obj.length + " " + obj.width);
17. }
18. }
a) 0 0
b) 5 6
c) 6 5
d) 5 5
Answer: c
Explanation: this keyword can be used inside any method to refer to the current object. this is
always a reference to the object on which the method was invoked.
output:
$ javac Output.java
$ java Output
65
31. What is process of defining two or more methods within same class that have same name
but different parameters declaration?
a) method overloading
b) method overriding
c) method hiding
d) None of the mentioned
Answer: a
Explanation: Two or more methods can have same name as long as their parameters
declaration is different, the methods are said to be overloaded and process is called method
overloading. Method overloading is a way by which Java implements polymorphism.
Answer: c
Explanation: None.
Answer: a
Explanation: When we pass an argument by call-by-value a copy of argument is made into
the formal parameter of the subroutine and changes made on parameters of subroutine have
no effect on original argument, they remain the same.
34. What is the process of defining a method in terms of itself, that is a method that calls
itself?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer: d
Explanation: None.
Answer: c
Explanation: finalize() method is called just prior to garbage collection. It is not called when
object goes out of scope.
1. class overload {
a) 5
b) 6
c) 7
d) 8
Answer: c
Explanation: None.
output:
$ javac Overload_methods.java
$ java Overload_methods
7
1. class overload {
2. int x;
3. int y;
4. void add(int a){
5. x = a + 1;
6. }
7. void add(int a , int b){
8. x = a + 2;
9. }
10. }
11. class Overload_methods {
12. public static void main(String args[])
13. {
14. overload obj = new overload();
15. int a = 0;
16. obj.add(6, 7);
17. System.out.println(obj.x);
18. }
19. }
a) 6
b) 7
c) 8
Answer: c
Explanation: None.
output:
$ javac Overload_methods.java
$ java Overload_methods
8
1. class overload {
2. int x;
3. double y;
4. void add(int a , int b) {
5. x = a + b;
6. }
7. void add(double c , double d){
8. y = c + d;
9. }
10. overload() {
11. this.x = 0;
12. this.y = 0;
13. }
14. }
15. class Overload_methods {
16. public static void main(String args[])
17. {
18. overload obj = new overload();
19. int a = 2;
20. double b = 3.2;
21. obj.add(a, a);
22. obj.add(b, b);
23. System.out.println(obj.x + " " + obj.y);
24. }
25. }
a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4
Answer: d
Explanation: For obj.add(a,a); ,the function in line number 4 gets executed and value of x is
4. For the next function call, the function in line number 7 gets executed and value of y is 6.4
output:
$ javac Overload_methods.java
$ java Overload_methods
4 6.4
1. class test {
a) 10 20
b) 20 10
c) 20 40
d) 40 20
Answer: a
Explanation: Variables a & b are passed by value, copy of their values are made on formal
parameters of function meth() that is i & j. Therefore changes done on i & j are not reflected
back on original arguments. a & b remain 10 & 20 respectively.
output:
$ javac Output.java
$ java Output
10 20
1. class test {
2. int a;
3. int b;
4. test(int i, int j) {
5. a = i;
6. b = j;
7. }
8. void meth(test o) {
9. o.a *= 2;
10. O.b /= 2;
11. }
12. }
13. class Output {
14. public static void main(String args[])
15. {
16. test obj = new test(10 , 20);
17. obj.meth(obj);
18. System.out.println(obj.a + " " + obj.b);
19. }
20. }
Answer: b
Explanation: class objects are always passed by reference, therefore changes done are
reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose
variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a
& b becomes 20 & 10 respectively.
output:
$ javac Output.java
$ java Output
20 10
41. Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) None of the mentioned
Answer: b
Explanation: main() method must be specified public as it called by Java run time system,
outside of the program. If no access specifier is used then by default member is public within
its own package & cannot be accessed by Java run time system.
42. Which of these is used to access member of class before object of that class is created?
a) public
b) private
c) static
d) protected
Answer: c
Explanation: None.
43. Which of these is used as default for a member of a class if no access specifier is used for
it?
a) private
b) public
c) public, within its own package
d) protected
Answer: a
Explanation: When we pass an argument by call-by-value a copy of argument is made into
44. What is the process by which we can control what parts of a program can access the
members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer: c
Explanation: None.
Answer: c
Explanation: private members of a class can not be inherited by a sub class.
1. class access{
2. public int x;
3. private int y;
4. void cal(int a, int b){
5. x = a + 1;
6. y = b;
7. }
8. }
9. class access_specifier {
10. public static void main(String args[])
11. {
12. access obj = new access();
13. obj.cal(2, 3);
14. System.out.println(obj.x + " " + obj.y);
15. }
16. }
a) 3 3
b) 2 3
c) Runtime Error
d) Compilation Error
Answer: c
Explanation: None.
1. class access{
2. public int x;
3. private int y;
4. void cal(int a, int b){
5. x = a + 1;
6. y = b;
7. }
8. void print() {
9. system.out.println(" " + y);
10. }
11. }
12. class access_specifier {
13. public static void main(String args[])
14. {
15. access obj = new access();
16. obj.cal(2, 3);
17. System.out.println(obj.x);
18. obj.print();
19. }
20. }
a) 2 3
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: b
Explanation: None.
output:
$ javac access_specifier.java
$ java access_specifier
33
1. class static_out {
2. static int x;
3. static int y;
4. void add(int a, int b){
5. x = a + b;
6. y = x + b;
7. }
8. }
9. class static_use {
10. public static void main(String args[])
11. {
12. static_out obj1 = new static_out();
13. static_out obj2 = new static_out();
a) 7 7
b) 6 6
c) 7 9
d) 9 7
Answer: c
Explanation: None.
output:
$ javac static_use.java
$ java static_use
6 6.4
49. Which of these access specifier must be used for class so that it can be inherited by
another sub class?
a) public
b) private
c) protected
d) None of the mentioned
Answer: a
Explanation: None.
1. class test {
2. int a;
3. int b;
4. test(int i, int j) {
5. a = i;
6. b = j;
7. }
8. void meth(test o) {
9. o.a *= 2;
10. O.b /= 2;
11. }
12. }
13. class Output {
14. public static void main(String args[])
15. {
16. test obj = new test(10 , 20);
17. obj.meth(obj);
18. System.out.println(obj.a + " " + obj.b); }
19. }
a) 10 20
b) 20 10
Answer: b
Explanation: class objects are always passed by reference, therefore changes done are
reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose
variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a
& b becomes 20 & 10 respectively.
output:
$ javac Output.java
$ java Output
20 10
SET-4
Java Questions & Answers Arrays Revisited & Keyword static
Answer: b
Explanation: None.
2. Which of these keywords is used to prevent content of a variable from being modified?
a) final
b) last
c) constant
d) static
Answer: a
Explanation: A variable can be declared final, doing so prevents its content from being
modified. Final variables must be initialized when it is declared.
Answer: b
Explanation: static statements are run as soon as class containing then is loaded, prior to any
object declaration.
Answer: d
Explanation: All objects of class share same static variable, when object of a class are
declared, all the objects share same copy of static members, no copy of static variables are
made.
Answer: a
Explanation: None.
Answer: a
Explanation: main() method must be declared static, main() method is called by Javas run
time system before any object of any class exists.
1. class access{
2. public int x;
3. static int y;
4. void cal(int a, int b){
5. x += a ;
6. y += b;
7. }
8. }
9. class static_specifier {
10. public static void main(String args[])
11. {
12. access obj1 = new access();
13. access obj2 = new access();
14. obj1.x = 0;
15. obj1.y = 0;
16. obj1.cal(1, 2);
17. obj2.x = 0;
18. obj2.cal(2, 3);
19. System.out.println(obj1.x + " " + obj2.y);
20. }
21. }
a) 1 2
b) 2 3
c) 3 2
d) 1 5
Answer: d
Explanation: None.
1. class access{
2. static int x;
3. void increment(){
4. x++;
5. }
6. }
7. class static_use {
8. public static void main(String args[])
9. {
10. access obj1 = new access();
11. access obj2 = new access();
12. obj1.x = 0;
13. obj1.increment();
14. obj2.increment();
15. System.out.println(obj1.x + " " + obj2.x);
16. }
17. }
a) 1 2
b) 1 1
c) 2 2
d) Compilation Error
Answer: c
Explanation: All objects of class share same static variable, all the objects share same copy of
static members, obj1.x and obj2.x refer to same element of class which has been incremented
twice and its value is 2.
output:
$ javac static_use.java
$ java static_use
22
1. class static_out {
2. static int x;
3. static int y;
4. void add(int a , int b){
5. x = a + b;
6. y = x + b;
7. }
8. }
9. class static_use {
10. public static void main(String args[])
11. {
12. static_out obj1 = new static_out();
13. static_out obj2 = new static_out();
a) 7 7
b) 6 6
c) 7 9
d) 9 7
Answer: c
Explanation: None.
output:
$ javac static_use.java
$ java static_use
79
1. class Output {
2. public static void main(String args[])
3. {
4. int arr[] = {1, 2, 3, 4, 5};
5. for ( int i = 0; i < arr.length - 2; ++i)
6. System.out.println(arr[i] + " ");
7. }
8. }
a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
Answer: b
Explanation: arr.length() is 5, so the loop is executed for three times.
output:
$ javac Output.java
$ java Output
123
1. class Output {
2. public static void main(String args[])
3. {
4. int a1[] = new int[10];
5. int a2[] = {1, 2, 3, 4, 5};
6. System.out.println(a1.length + " " + a2.length);
7. }
8. }
Answer: a
Explanation: Arrays in java are implemented as objects, they contain an attribute that is
length which contains the number of elements that can be stored in the array. Hence a1.length
gives 10 and a2.length gives 5.
output:
$ javac Output.java
$ java Output
10 5
Answer: a
Explanation: None.
12. Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()
Answer: d
Explanation: None.
13. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
d) None of the mentioned
Answer: b
Explanation: Whenever a subclass needs to refer to its immediate superclass, it can do so by
use of the keyword super.
14. Which of these method of String class can be used to test to strings for equality?
a) isequal()
Answer: d
Explanation: None.
Answer: b
Explanation: Strings in Java are immutable that is they can not be modified.
1. class string_demo {
2. public static void main(String args[])
3. {
4. String obj = "I" + "like" + "Java";
5. System.out.println(obj);
6. }
7. }
a) I
b) like
c) Java
d) IlikeJava
Answer: d
Explanation: Java defines an operator +, it is used to concatenate strings.
output:
$ javac string_demo.java
$ java string_demo
IlikeJava
1. class string_class {
2. public static void main(String args[])
3. {
4. String obj = "I LIKE JAVA";
5. System.out.println(obj.charAt(3));
6. }
7. }
a) I
b) L
Answer: a
Explanation: charAt() is a method of class String which gives the character specified by the
index. obj.charAt(3) gives 4th character i:e I.
output:
$ javac string_class.java
$ java string_class
I
1. class string_class {
2. public static void main(String args[])
3. {
4. String obj = "I LIKE JAVA";
5. System.out.println(obj.length());
6. }
7. }
a) 9
b) 10
c) 11
d) 12
Answer: c
Explanation: None.
output:
$ javac string_class.java
$ java string_class
11
1. class string_class {
2. public static void main(String args[])
3. {
4. String obj = "hello";
5. String obj1 = "world";
6. String obj2 = obj;
7. obj2 = " world";
8. System.out.println(obj + " " + obj2);
9. }
10. }
a) hello hello
b) world world
c) hello world
Answer: c
Explanation: None.
output:
$ javac string_class.java
$ java string_class
hello world
1. class string_class {
2. public static void main(String args[])
3. {
4. String obj = "hello";
5. String obj1 = "world";
6. String obj2 = "hello";
7. System.out.println(obj.equals(obj1) + " " +
obj.equals(obj2));
8. }
9. }
a) false false
b) true true
c) true false
d) false true
Answer: d
Explanation: equals() is method of class String, it is used to check equality of two String
objects, if they are equal, true is retuned else false.
output:
$ javac string_class.java
$ java string_class
false true
22. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
d) None of the mentioned
23. A class member declared protected becomes member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member
Answer: b
Explanation: A class member declared protected becomes private member of subclass.
Answer: c
Explanation: None.
Answer: c
Explanation: private members of a class cannot be inherited by a sub class.
1. class A {
2. int i;
3. void display() {
4. System.out.println(i);
5. }
6. }
7. class B extends A {
8. int j;
9. void display() {
10. System.out.println(j);
11. }
12. }
13. class inheritance_demo {
14. public static void main(String args[])
15. {
a) 0
b) 1
c) 2
d) Compilation Error
Answer: c
Explanation: class A & class B both contain display() method, class B inherits class A, when
display() method is called by object of class B, display() method of class B is executed rather
than that of Class A.
output:
$ javac inheritance_demo.java
$ java inheritance_demo
2
1. class A {
2. int i;
3. }
4. class B extends A {
5. int j;
6. void display() {
7. super.i = j + 1;
8. System.out.println(j + " " + i);
9. }
10. }
11. class inheritance {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.i=1;
16. obj.j=2;
17. obj.display();
18. }
19. }
a) 2 2
b) 3 3
c) 2 3
d) 3 2
Answer: c
Explanation: None
output:
$ javac inheritance.java
1. class A {
2. public int i;
3. private int j;
4. }
5. class B extends A {
6. void display() {
7. super.j = super.i + 1;
8. System.out.println(super.i + " " + super.j);
9. }
10. }
11. class inheritance {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.i=1;
16. obj.j=2;
17. obj.display();
18. }
19. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: d
Explanation: class contains a private member variable j, this cannot be inherited by subclass
B and does not have access to it.
output:
$ javac inheritance.java
Exception in thread main java.lang.Error: Unresolved compilation problem:
The field A.j is not visible
1. class A {
2. public int i;
3. public int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class B extends A {
10. int a;
11. B() {
12. super();
13. }
14. }
15. class super_use {
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
Answer: a
Explanation: Keyword super is used to call constructor of class A by constructor of class B.
Constructor of a initializes i & j to 1 & 2 respectively.
output:
$ javac super_use.java
$ java super_use
12
1. class A {
2. public int i;
3. protected int j;
4. }
5. class B extends A {
6. int j;
7. void display() {
8. super.j = 3;
9. System.out.println(i + " " + j);
10. }
11. }
12. class Output {
13. public static void main(String args[])
14. {
15. B obj = new B();
16. obj.i=1;
17. obj.j=2;
18. obj.display();
19. }
20. }
a) 1 2
b) 2 1
c) 1 3
d) 3 1
Answer: a
Explanation: Both class A & B have member with same name that is j, member of class B
will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.
output:
$ javac Output.java
31. Which of these keyword can be used in subclass to call the constructor of superclass?
a) super
b) this
c) extent
d) extends
Answer: a
Explanation: None.
32. What is the process of defining a method in subclass having same name & type signature
as a method in its superclass?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned
Answer: b
Explanation: None.
Answer: d
Explanation: To disallow a method from being overridden, specify final as a modifier at the
start of its declaration. Methods declared as final cannot be overridden.
34. Which of these is correct way of calling a constructor having no parameters, of superclass
A by subclass B?
a) super(void);
b) superclass.();
c) super.A();
d) super();
Answer: d
Explanation: None.
Answer: c
Explanation: private members of a class cannot be inherited by a sub class.
Answer: c
Explanation: None.
1. class A {
2. int i;
3. void display() {
4. System.out.println(i);
5. }
6. }
7. class B extends A {
8. int j;
9. void display() {
10. System.out.println(j);
11. }
12. }
13. class method_overriding {
14. public static void main(String args[])
15. {
16. B obj = new B();
17. obj.i=1;
18. obj.j=2;
19. obj.display();
20. }
21. }
a) 0
b) 1
c) 2
d) Compilation Error
Answer: c
Explanation: class A & class B both contain display() method, class B inherits class A, when
display() method is called by object of class B, display() method of class B is executed rather
than that of Class A.
1. final class A {
2. int i;
3. }
4. class B extends A {
5. int j;
6. System.out.println(j + " " + i);
7. }
8. class inheritance {
9. public static void main(String args[])
10. {
11. B obj = new B();
12. obj.display();
13. }
14. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: d
Explanation: class A has been declared final hence it cannot be inherited by any other class.
Hence class B does not have member i, giving compilation error.
output:
$ javac inheritance.java
Exception in thread main java.lang.Error: Unresolved compilation problem:
i cannot be resolved or is not a field
1. class A {
2. public int i;
3. private int j;
4. }
5. class B extends A {
6. void display() {
7. super.j = super.i + 1;
8. System.out.println(super.i + " " + super.j);
9. }
10. }
11. class inheritance {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.i=1;
16. obj.j=2;
17. obj.display();
18. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: d
Explanation: class contains a private member variable j, this cannot be inherited by subclass
B and does not have access to it.
output:
$ javac inheritance.java
Exception in thread main java.lang.Error: Unresolved compilation problem:
The field A.j is not visible
1. class A {
2. public void display() {
3. System.out.println("A");
4. }
5. }
6. class B extends A {
7. public void display() {
8. System.out.println("B");
9. }
10. }
11. class Dynamic_dispatch {
12. public static void main(String args[])
13. {
14. A obj1 = new A();
15. B obj2 = new B();
16. A r;
17. r = obj1;
18. r.display();
19. r = obj2;
20. r.display();
21. }
22. }
a) A B
b) B A
c) Runtime Error
d) Compilation Error
Answer: a
Explanation: Call to display function of class A and class B is made by using dynamic
method dispatch, by using this method a call to an overridden function is resolved at run time,
rather than at compilation time.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
AB
1. class A {
2. int i;
3. public void display() {
4. System.out.println(i);
5. }
6. }
7. class B extends A {
8. int j;
9. public void display() {
10. System.out.println(j);
11. }
12. }
13. class Dynamic_dispatch {
14. public static void main(String args[])
15. {
16. B obj2 = new B();
17. obj2.i = 1;
18. obj2.j = 2;
19. A r;
20. r = obj2;
21. r.display();
22. }
23. }
a) 1
b) 2
c) 3
d) 4
Answer: b
Explanation: r is reference of type A, the program assigns a reference of object obj2 to r and
uses that reference to call function display() of class B.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
2
Answer: b
Explanation: Object class is superclass of every class in Java.
Answer: c
Explanation: None.
43. Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned
Answer: c
Explanation: None.
Answer: d
Explanation: Declaring a class final implicitly declares all of its methods final, and makes the
class inheritable.
45. Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) None of the mentioned
Answer: a
Explanation: A abstract class is incomplete by itself and relies upon its subclasses to provide
complete implementation. If we declare a class final then no class can inherit that class, an
abstract class needs its subclasses hence both final and abstract cannot be used for a same
class.
46. Which of these class relies upon its subclasses for complete implementation of its
methods?
a) Object class
b) abstract class
c) ArrayList class
d) None of the mentioned
1. abstract class A {
2. int i;
3. abstract void display();
4. }
5. class B extends A {
6. int j;
7. void display() {
8. System.out.println(j);
9. }
10. }
11. class Abstract_demo {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.j=2;
16. obj.display();
17. }
18. }
a) 0
b) 2
c) Runtime Error
d) Compilation Error
Answer: b
Explanation: class A is an abstract class, it contains a abstract function display(), the full
implementation of display() method is given in its subclass B, Both the display functions are
the same. Prototype of display() is defined in class A and its implementation is given in class
B.
output:
$ javac Abstract_demo.java
$ java Abstract_demo
2
1. class A {
2. int i;
3. int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class Output {
10. public static void main(String args[])
11. {
12. A obj1 = new A();
13. A obj2 = new A();
14. System.out.print(obj1.equals(obj2));
a) false
b) true
c) 1
d) Compilation Error
Answer: a
Explanation: obj1 and obj2 are two different objects. equals() is a method of Object class,
Since Object class is superclass of every class it is available to every object.
output:
$ javac Output.java
$ java Output
false
1. class Output {
2. public static void main(String args[])
3. {
4. Object obj = new Object();
5. System.out.print(obj.getclass());
6. }
7. }
a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error
Answer: c
Explanation: None.
output:
$ javac Output.java
$ java Output
class java.lang.Object
1. class A {
2. int i;
3. int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class Output {
10. public static void main(String args[])
11. {
12. A obj1 = new A();
a) true
b) false
c) String associated with obj1
d) Compilation Error
Answer: c
Explanation: toString() is method of class Object, since it is superclass of every class, every
object has this method. toString() returns the string associated with the calling object.
output:
$ javac Output.java
$ java Output
A@1cd2e5f
B. number_a
C. $1234
D. -volatile
2. Which of these are not legal identifiers. Select all the correct answers.
A. 1alpha
B. _abcd
C. xy+abc
D. transient
E. account-num
F. very_long_name
3. Which of the following are keywords in Java. Select all the correct answers.
A. friend
B. NULL
C. implement
D. synchronized
E. throws
4. Which of the following are Java keywords. Select all the correct answers.
A. super
B. strictfp
C. void
D. synchronize
E. instanceof
5. Which of these are Java keywords. Select all the correct answers
A. TRUE
B. volatile
D. native
E. interface
F. then
G. new
7. Using up to four characters, write the Java representation of integer literal 3 in hexadecimal.
8. Using up to four characters, write the Java representation of integer literal 10 in hexadecimal.
9. What is the minimum value of char type. Select the one correct answer.
A. 0
B. -215
C. -28
D. -215 - 1
E. -216
F. -216 - 1
10. How many bytes are used to represent the primitive data type int in Java. Select the one
correct answer.
A. 2
B. 4
C. 8
D. 1
11. What is the legal range of values for a variable declared as a byte. Select the one correct
answer.
A. 0 to 256
B. 0 to 255
C. -128 to 127
D. -128 to 128
E. -127 to 128
F. -215 to 215 - 1
12. The width in bits of double primitive type in Java is --. Select the one correct answer.
B. 64
C. 128
D. 8
E. 4
13. What would happen when the following is compiled and executed. Select the one correct
answer.
D. The program does not compile complaining about y not being initialized.
14. What would happen when the following is compiled and executed. Select the one correct
answer.
class example {
int x;
int y;
String name;
public static void main(String args[]) {
example pnt = new example();
System.out.println("pnt is " + pnt.name +
" " + pnt.x + " " + pnt.y);
}
}
A. The program does not compile because x, y and name are not initialized.
B. The program throws a runtime exception as x, y, and name are used before
initialization.
A. null
B. ""
C. NULL
D. 0
16. The initial value of a local variable of type String which is not explicitly initialized and which is
defined in a member function of a class. Select all the correct answer.
A. null
B. ""
C. NULL
D. 0
17. Which of the following are legal Java programs. Select all the correct answer.
B. package pkg;
import java.awt.*;
class C{};
C. package pkg1;
package pkg2;
import java.awt.*;
class C{};
D. package pkg;
import java.awt.*;
E. import java.awt.*;
class C{};
F. import java.awt.*;
package pkg;
class C {};
18. Which of the following statements are correct. Select all correct answers.
E. A Java file without any class or interface definitions can also be compiled.
19. What would be the results of compiling and running the following class. Select the one
correct answer.
class test {
public static void main() {
System.out.println("test");
}
}
C. The program compiles and runs but does not generate any output.
20. Which of these are valid declarations for the main method? Select all correct answers.
21. Which of the following are valid declarations for the main method. Select all correct answers.
22. What happens when the following program is compiled and executed with the arguments -
java test. Select the one correct answer.
class test {
A. The program compiles and runs but does not print anything.
B. The program compiles and runs and prints 0
23. What is the result of compiling and running this program ? Select the one correct answer.
A. The program does not compile as k is being read without being initialized.
B. The program does not compile because of the statement k = j = i = 1;
24. What gets printed on the standard output when the class below is compiled and executed by
entering "java test lets see what happens". Select the one correct answer.
25. What gets printed on the standard output when the class below is compiled and executed by
entering "java test lets see what happens". Select the one correct answer.
26. What all gets printed on the standard output when the class below is compiled and executed
by entering "java test lets see what happens". Select all correct answers.
A. java
B. test
C. lets
D. 3
E. 4
F. 5
G. 6
27. What happens when the following program is compiled and run. Select the one correct
answer.
28. What happens when the following program is compiled and run. Select the one correct
answer.
29. What happens when the following program is compiled and run. Select the one correct
answer.
1. a, b, c
2. a, c, d, e
3. d, e
4. a, b, c, e
5. b, c, d, e, g
7. Any of the following are correct answers - 0x03, 0X03, 0X3 or 0x3
8. Any of the following are correct answers - 0x0a, 0X0a, 0Xa, 0xa, 0x0A, 0X0A, 0XA, 0xA
9. a
10. b
11. c
12. b
15. a
16. e
17. a, b, d, e
18. b, d, e, f
19. d
20. b, c
21. a, b, c
22. a
23. d
24. e
25. a
26. c, e
27. e
28. c
29. e
30. c
32. Which of these assignments are valid. Select all correct answers.
F. short s = 28;
G. float f = 2.3;
H. double d = 2.3;
I. int I = '1';
J. byte b = 12;
33. What gets printed when the following program is compiled and run. Select the one correct
answer.
class test {
public static void main(String args[]) {
int i,j,k,l=0;
k = l++;
j = ++k;
i = j++;
System.out.println(i);
}
}
K. 0
L. 1
M. 2
N. 3
34. Which of these lines will compile? Select all correct answers.
O. short s = 20;
P. byte b = 128;
Q. char c = 32;
R. double d = 1.4;;
S. float f = 1.4;
T. byte e = 0;
35. The signed right shift operator in Java is --. Select the one correct answer.
U. <<;
V. >>
X. None of these.
36. What gets printed on the standard output when the class below is compiled and executed.
Select the one correct answer.
Y. 0
Z. 1
AA. 2
BB. 3
37. What gets printed on the standard output when the class below is compiled and executed.
Select the one correct answer.
CC. 0
DD. 1
EE. 2
FF. 3
38. What gets printed on the standard output when the class below is compiled and executed.
Select the one correct answer.
GG. 0
HH. 1
II. 2
JJ. 3
39. What gets printed on the standard output when the class below is compiled and executed.
Select the one correct answer.
KK. 0
LL. 1
MM. 2
NN.3
40. Which operator is used to perform bitiwse inversion in Java. Select the one correct answer.
OO.~
PP. !
QQ.&
RR. |
SS. ^
41. What gets printed when the following program is compiled and run. Select the one correct
answer.
TT. 3
UU. 0
VV. 1
WW. 11
XX. 252
YY. 214
ZZ. 124
AAA. -4
42. What gets displayed on the screen when the following program is compiled and run. Select
the one correct answer.
BBB. 71
CCC. 37
DDD. 17
EEE. 31
FFF. 1 3
GGG. 73
HHH. 75
43. What gets displayed on the screen when the following program is compiled and run. Select
the one correct answer.
III. 1 3
KKK. 51
LLL. 3 6
MMM. 17
NNN. 15
OOO. &
PPP. ^
QQQ. |
RRR. !
SSS.~
45. What gets displayed on the screen when the following program is compiled and run. Select
the one correct answer.
TTT. 1
UUU. 2
VVV. 3
WWW. 4
46. What gets displayed on the screen when the following program is compiled and run. Select
the one correct answer.
ZZZ. 3
AAAA. 4
47. What gets displayed on the screen when the following program is compiled and run. Select
the one correct answer.
BBBB. 5
CCCC. 2
DDDD. 80
EEEE. 0
FFFF. 64
48. What gets displayed on the screen when the following program is compiled and run. Select
the one correct answer.
GGGG. 1
HHHH. 0
IIII. 7
JJJJ. 5
KKKK. 23
LLLL. 2147483646
49. Which of the following are correct. Select all correct answers.
MMMM. Java provides two operators to do left shift - << and <<.
PPPP. For positive numbers, results of p[erators >> and >>> are same.
50. What is the result of compiling and running the following program. Select one correct
answer.
QQQQ. 63
RRRR. -1
SSSS. 0
TTTT. 1
UUUU. 127
VVVV. 128
WWWW. 255
51. What all gets printed when the following gets compiled and run. Select all correct answers.
XXXX. 0
YYYY. 1
ZZZZ. 2
AAAAA. 3
BBBBB. 4
GGGGG. The program does not compile because of problems in the if statement.
53. Which all lines are part of the output when the following code is compiled and run. Select all
correct answers.
HHHHH. 00
IIIII. 0 1
JJJJJ. 02
KKKKK. 03
LLLLL. 10
MMMMM. 1 1
NNNNN. 12
OOOOO. 13
PPPPP. 20
RRRRR. 22
SSSSS. 23
TTTTT. 30
UUUUU. 31
VVVVV. 32
WWWWW. 3 3
54. Which all lines are part of the output when the following code is compiled and run. Select all
correct answers.
YYYYY. 00
ZZZZZ. 01
AAAAAA. 02
BBBBBB. 03
CCCCCC. 10
DDDDDD. 1 1
EEEEEE. 12
FFFFFF. 13
GGGGGG. 2 0
HHHHHH. 2 1
IIIIII. 22
JJJJJJ. 23
KKKKKK. 30
LLLLLL. 31
NNNNNN. 3 3
55. Which all lines are part of the output when the following code is compiled and run. Select all
correct answers.
PPPPPP. 00
QQQQQQ. 0 1
RRRRRR. 02
SSSSSS. 03
TTTTTT. 10
UUUUUU. 1 1
VVVVVV. 12
WWWWWW. 13
XXXXXX. 20
YYYYYY. 21
ZZZZZZ. 22
AAAAAAA. 2 3
BBBBBBB. 3 0
CCCCCCC. 3 1
DDDDDDD. 3 2
EEEEEEE. 33
56. Which all lines are part of the output when the following code is compiled and run. Select all
correct answers.
FFFFFFF. 00
GGGGGGG. 0 1
HHHHHHH. 0 2
IIIIIII. 03
JJJJJJJ. 10
KKKKKKK. 11
LLLLLLL. 12
MMMMMMM. 13
NNNNNNN. 2 0
OOOOOOO.2 1
PPPPPPP. 22
QQQQQQQ. 23
RRRRRRR. 3 0
SSSSSSS. 31
TTTTTTT. 32
UUUUUUU. 3 3
57. Which all lines are part of the output when the following code is compiled and run. Select all
correct answers.
VVVVVVV. 0 0
WWWWWWW. 01
YYYYYYY. 03
ZZZZZZZ. 10
AAAAAAAA.1 1
BBBBBBBB. 1 2
CCCCCCCC. 1 3
DDDDDDDD. 20
EEEEEEEE. 2 1
FFFFFFFF. 22
GGGGGGGG. 23
HHHHHHHH. 30
IIIIIIII. 31
JJJJJJJJ. 32
KKKKKKKK. 3 3
31. c. It is not possible to assign an integer to a character in this case without a cast.
32. a, c, d, e. 2.3 is of type double. So it cannot be assigned to a float without a cast.
33. b
34. a, c, d, f. If RHS (Right hand side) is an integer within the correct range of LHS (Left hand
side), and if LHS is char, byte, or short, no cast is required. A decimal number is a double by default.
Assigning it to float requires a cast.
35. b
36. b. In the second assignment to variable b, the expression (i+=2) does not get evaluated.
37. d
38. c
39. d
40. a
42. c
43. f
44. b
45. a
46. d
47. d
48. f
49. d
50. b
51. a, c
53. b, c, d, e, g, h, i, j, l
54. q
55. b, c, d, g, h, l
56. b, c, d, g, h, l
57. b, c, d
A. The class does not compile because the top level class cannot be protected.
B. The program prints "abc"
D. The program does not compile because statement "test = test + test" is illegal.
E. package
F. friendly
G. private
H. protected
I. public
60. Write down the modifier of a method which makes the method available to all classes in the
same package and to all the subclases of this class.
61. Select the one most appropriate answer. A top level class without any modifier is accessible
to -
J. any class
63. Is this True or False. In Java a final class must be subclassed before it can be used.
64. Which of the following are true. Select all correct answers.
N. A static method may be invoked before even a single instance of the class is
constructed.
P. Abstract modifier can appear before a class or a method but not before a variable.
Q. final modifier can appear before a class or a variable but not before a method.
R. Synchronized modifier may appear before a method or a variable but not before a
class.
58. a
59. e
60. protected
61. b
62. False
64. a, b, c. final modifier may appear before a method or a variable but not before a class.
Questions on Collections
65. TreeMap class is used to implement which collection interface. Select the one correct answer.
A. Set
B. SortedSet
C. List
D. Map
E. SortedMap
69. Which of these are interfaces in the collection framework. Select all correct answers.
F. Set
G. List
H. Array
I. Vector
J. LinkedList
70. Which of these are interfaces in the collection framework. Select all correct answers.
K. HashMap
L. ArrayList
M. Collection
N. SortedMap
O. TreeMap
71. What is the name of collection interface used to maintain non-unique elements in order.
72. What is the name of collection interface used to maintain unique elements.
73. What is the name of collection interface used to maintain mappings pf keys to values.
P. True
Q. False
65. e
66. List
67. Map
68. Set
69. a,b
70. c,d
71. List
72. Set
73. Map
74. b
Questions on Events
75. Name the method defined in EventObject class that returns the Object generated from the
event. Select the one correct answer.
A. getEvent()
B. getObject()
C. getID()
D. getSource()
76. What is the return type of the method getID() defined in AWTEvent class. Select the one correct
answer.
E. int
F. long
G. Object
H. Component
I. short
J. KeyEvent
K. MouseEvent
L. ItemEvent
M. ActionEvent
78. Which event is generated when the position of a scrollbar is changed. Select the one correct
answer.
N. KeyEvent
O. MouseEvent
P. ItemEvent
Q. ActionEvent
R. AdjustmentEvent
79. Which of the following Objects can generate ActionEvent. Select all correct answer.
S. List
T. TextArea
U. CheckBoxMenuItem
V. Choice
80. Which of the following Objects can generate ItemEvent. Select all correct answer.
W. CheckBox
X. Button
Y. List
Z. MenuItem
81. Which method identifies the type of an event generated. Select the one correct answer.
AA. getSource()
BB. getType()
CC. getEventType()
DD. getID()
82. Which of the following are legal adapter classes in Java. Select all correct answers.
FF. ItemAdapter
GG. TextAdapter
HH. MouseAdapter
II. MouseMotionAdapter
83. Name the class of the argument of method actionPerformed() defined in the ActionListner
interface.
84. Which of these listner classes have corresponding adapter classes. Select all correct answers.
JJ. ContainerListner
KK. TextListner
LL. ItemListner
MM. MouseMotionListner
85. Which of these are valid adapter classes. Select all correct answers.
NN.ActionAdapter
OO.AdjustmentAdapter
PP. KeyAdapter
QQ.TextAdapter
86. Which of these methods are defined in MouseMotionListner interface. Select all correct answers.
RR. mouseClicked()
SS. mousePressed()
TT. mouseEntered()
UU. mouseDragged()
VV. mouseMoved()
87. What is the return type of the method getSource() defined in EventObject class. Select the one
correct answer.
WW. int
XX. long
YY. Object
ZZ. Component
75. d
76. a
77. d
78. e
79. a
80. a, c
81. d
82. d, e
83. ActionEvent
84. a, d
85. c
86. d, e
87. c
Questions on AWT
88. Which of the following classes are derived from the Container class. Select all correct answer.
A. Component
B. Panel
C. java.applet.Applet
D. Dialog
E. Frame
F. MenuComponent
89. Which of the following classes are derived from the Component class. Select all correct answer.
G. Container
H. Window
I. List
K. Choice
90. Name the class used to represent a GUI application window, which is optionaly resizable and can
have a title bar, an icon, and menus. Select the one correct answer.
L. Window
M. Panel
N. Dialog
O. Frame
91. Which abstract class is the super class of all menu related classes.
92. Which of these classes can be added to a Frame component. Select all correct answer.
P. Menu
Q. Button
R. PopupMenu
S. Window
T. List
93. Which class can be used to represent a checkbox with a textual label that can appear in a menu.
Select the one correct answer.
U. MenuBar
V. MenuItem
W. CheckBoxMenuItem
X. Menu
Y. CheckBox
94. Which of these classes can be added to a Component using the add method defined in Container
class. Select all correct answers.
Z. Button
AA. CheckBoxMenuItem
BB. Menu
CC. Canvas
96. Which of these events is generated when the component is added or removed?
a) ComponentEvent
b) ContainerEvent
c) FocusEvent
d) InputEvent
97. Which of these methods can be used to obtain the coordinates of a mouse?
a) getPoint()
b) getCoordinates()
c) getMouseXY()
d) getMouseCordinates()
100. Which of these methods is used to obtain the object that generated a WindowEvent?
a) getMethod()
b) getWindow()
c) getWindowEvent()
d) getWindowObject()
88. b, c, d, e
89. a, b, c, e
90. d
91. MenuComponent
92. b, c, e
94. a, d
95. d
96. b
97. a
98. b
99. b
100. b