Core Java PDF
Core Java PDF
Core Java PDF
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
Answer:b
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
Answer:a
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
a) 1 and 2
b) 2 and 3
c) 3 and 4
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: a
Explanation:An expression involving bytes, ints, shorts, literal numbers, the entire expression is
promoted to int before any calculation is done.
a) 1.7e-308
b) 3.4e-038
c) 1.7e+308
d) 3.4e-050
Answer: b
a) int
b) float
c) double
d) long
Answer:c
7. 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
8. 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
a) static
b) constant
c) protected
d) final
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.
10. 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
b) private members of class can only be accessed by other members of the class.
c) private members of class can be inherited by a sub class, and become protected members in sub
class.
d) protected members of a class can be inherited by a sub class, and become private members of the
sub class.
Answer: c
a) Abstraction
b) Encapsulation
c) Polymorphism
Answer: c
a) class
b) object
c) variable
d) character array
Answer: a
14. 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
15. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
Answer: b
Explanation: Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.
16. Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()
Answer: d
a) String is a class.
b) Strings in java are mutable.
d) Java defines a peer class of String, called StringBuffer, which allows string to be altered.
Answer: b
Explanation: Strings in Java are immutable that is they can not be modified.
18. 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
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.
a) Methods
b) Constructors
Answer: c
c) Copy of argument is made into the formal parameter of the subroutine and changes made on
parameters of subroutine have effect on original argument.
d) Reference to original argument is passed to formal parameter of the subroutine and changes made
on parameters of subroutine have effect on original argument.
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.
21. 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
a) Default constructor is called at the time of declaration of the object if a constructor has not been
defined.
c) finalize() method is called when a object goes out of scope and is no longer needed.
Explanation: finalize() method is called just prior to garbage collection. It is not called when object goes
out of scope.
a) -128 to 127
b) 0 to 256
c) 0 to 32767
d) 0 to 65535
Answer:d
Explanation: Char occupies 16-bit in memory, so it supports 2^16 i:e from 0 to 65535.
24. Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
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.
b) 0 & 1
c) Any integer value
d) true
Answer:a
Explanation: Boolean variable can contain only one of two possible values, true and false.
26. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java?
a) ASCII
b) ISO-LATIN-1
Answer:d
Explanation: First 0 to 127 character set in Unicode are same as those of ISO-LAIN-1 and ASCII.
a) boolean b1 = 1;
b) boolean b2 = ‘false’;
c) boolean b3 = false;
d) boolean b4 = ‘true’
Answer:c
b) ABCDEFG
c) 0x99fffa
d) 99671246
Answer:a
Explanation:Data type long literals are appended by an upper or lowercase L. 0x99fffL is hexadecimal
long literal.
a) Integer
b) Boolean
c) Character
d) Integer or Boolean
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).
a) L
b) l
c) D
d) L and I
Answer:d
a) integer
b) float
c) boolean
Answer:d
32. Which of these can not be used for a variable name in Java?
a) identifier
b) keyword
Answer:b
Explanation:Keywords are specially reserved words which can not be used for naming a user defined
variable, example : class, int, for etc.
a) “Hello World”
b) “Hello\nWorld”
c) “\”Hello World””
d) “Hello
world”
Answer:d
Explanation:all string literals must begin and end in same line.
34. Which of these is necessary condition for automatic type conversion in Java?
Answer:b
a) prototype( )
b) prototype(void)
c) public prototype(void)
d) public prototype( )
Answer: d
byte b = 50;
b = b * 50;
b) * operator has converted b * 50 into int, which can not be converted to byte without casting.
Answer: b
Explanation:While evaluating an expression containing int, bytes or shorts , the whole expression is
converted to int then evaluated and result is also of type int
36. 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
Answer: a
38. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc
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
System.out.print(arr);
a) 0
c) 00000
d) Garbage value
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].
b) Array can be initialized using comma separated expressions surrounded by curly braces.
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};
a) Row
b) Column
Answer:a
a) super
b) this
c) extent
d) extends
Answer: d
44. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
Answer: b
Explanation: whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.
45. 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
a) class B + class A {}
c) class B extends A {}
d) class B extends class A {}
Answer: c
b) private members of class can only be accessed by other members of the class.
c) private members of class can be inherited by a sub class, and become protected members in sub
class.
d) protected members of a class can be inherited by a sub class, and become private members of the
sub class.
Answer: c
a) Numeric
b) Boolean
c) 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. .
50. 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
b) 2
c) 3
d) 4
Answer: a
a) Assignment operators are more efficiently implemented by Java run-time system than their
equivalent long forms.
c) Assignment operators can be used only with numeric and character data type.
d) None
Answer: d
a) &
b) &=
c) |=
d) <=
Answer:d
54. Which operator is used to invert all the digits in binary representation of a number?
a) ~
b) <<<
c) >>>
d) ^
Answer:a
Explanation: Unary not operator, ~, inverts all of the bits of its operand in binary representation.
55. 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.
56. Which right shift operator preserves the sign of the value?
a) <<
b) >>
c) <<=
d) >>=
Answer: b
57. Which of these statements are incorrect?
a) The left shift operator, <<, shifts all of the bite in a value to the left specified number of times.
b) The right shift operator, >>, shifts all of the bite in a value to the right specified number of times.
d) The right shift operator automatically fills the higher order bits with 0.
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.
a) Integer
b) Boolean
c) Characters
d) Double
Answer: b
59. Which of these is returned by greater than, <, and equal to, ==, operator?
a) Integers
c) Boolean
Answer:c
Explanation: All relational operators return a boolean value i:e true and false.
60. Which of the following operators can operate on a boolean variable?
1. &&
2. ==
3. ?:
4. +=
a) 3 & 2
b) 1 & 4
c) 1, 2 & 4
d) 1, 2 & 3
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.
61. 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.
a) ()
b) ++
c) *
d) >>
Answer: a
64. What should be expression1 evaluate to in using ternary operator as in this line?
a) Integer
c) Boolean
Answer:c
Explanation: The controlling condition of ternary operator must evaluate to boolean.
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
a) 0
b) 1
c) 9
d) 8
Answer: d
1. &
2. ^
3. ?:
a) 1 -> 2 -> 3
b) 2 -> 1 -> 3
c) 3 -> 2 -> 1
d) 2 -> 3 -> 1
Answer: a
Answer: c
a) if
b) switch
c) if & switch
Answer: b
Explanation: switch statements checks for equality between the controlling variable and its constant
cases.
a) if()
b) for()
c) continue
d) break
Answer:a
Explanation: continue and break are jump statements, and for is an looping statement.
70. 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
Answer: a
71. Which of these jump statements can skip processing remainder of code in its body for a particular
iteration?
a) break
b) return
c) exit
d) continue
Answer: d
b) two case constants in the same switch can have identical values.
c) switch statement can only test for equality, whereas if statement can evaluate any type of boolean
expression.
Explanation: No two case constants in the same switch can have identical values.
73. What is the stored in the object obj in following lines of code?
box obj;
b) NULL
d) Garbage
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.
a) class
b) struct
c) int
Answer:a
Answer: a
a) malloc
b) alloc
c) new
d) give
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.
78. What is the return type of Constructors?
a) int
b) float
c) void
Answer: d
Explanation: Constructors does not have any return type, not even void.
79. 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.
80. 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.
81. 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
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.
82. Which function is used to perform some action when the object is to be destroyed?
a) finalize()
b) delete()
c) main()
Answer: a
a) Default constructor is called at the time of declaration of the object if a constructor has not been
defined.
Answer: c
Explanation: finalize() method is called just prior to garbage collection. it is not called when object goes
out of scope.
1. a | 4 + c >> b & 7;
Answer: c
Explanation: Parentheses do not degrade the performance of the program. Adding parentheses to
reduce ambiguity does not negatively affect your system.
85. What is the output of this program?
1. class average {
3. {
5. double result;
6. result = 0;
7. for (int i = 0; i < 6; ++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
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;
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 byte’s 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 {
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 {
3. {
4. double r, pi, a;
5. r = 9.8;
6. pi = 3.14;
7. a = pi * r * r;
8. System.out.println(a);
9. }
10. }
a) 301.5656
b) 301
c) 301.56
d) 301.56560000
Answer:a
output:
$ javac area.java
$ java area
301.5656
1. class array_output {
3. {
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
Answer:a
output:
$ javac array_output.java
$ java array_output
iiiii
1. class mainclass {
3. {
4. char a = 'A';
5. a++;
6. System.out.print((int)a);
7. }
8. }
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 {
3. {
4. boolean var1 = true;
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
output:
$ javac mainclass.java
$ java mainclass
true
92. What is the output of this program?
1. class booloperators {
3. {
7. }
8. }
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 {
3. {
7. }
8. }
a) 162
b) 65 97
c) 67 95
d) 66 98
Answer:b
$ javac asciicodes.java
$ java asciicodes
65 97
1. class evaluate {
3. {
5. int d[] = a;
6. int sum = 0;
9. System.out.println(sum);
10. }
11. }
a) 38
b) 39
c) 40
d) 41
Answer:c
output:
$ javac evaluate.java
$ java evaluate
40
1. class array_output {
3. {
6. array_variable[i] = i/2;
7. array_variable[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;
9. }
11. }
12. }
a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
Answer:d
Explanation:Second print statement doesn’t have access to y , scope y was limited to the block defined
after initialization of x.
output:
$ javac variable_scope.java
1. class dynamic_initialization {
3. {
4. double a, b;
5. a = 3.0;
6. b = 4.0;
8. System.out.println(c);
9. }
10. }
a) 5.0
b) 25.0
c) 7.0
d) Compilation Error
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
1. class char_increment {
3. {
4. char c1 = 'D';
5. char c2 = 84;
6. c2++;
7. c1++;
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 {
3. {
4. double a = 295.04;
5. int b = 300;
6. byte c = (byte) a;
7. byte d = (byte) b;
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 byte’s range i:e -128 to 127 hence d
contains 300 modulo 256 i:e 44.
output:
$ javac conversion.java
$ java conversion
39 44
100. What is the output of this program?
1. class A {
3. }
4. class B extends A {
6. }
9. {
12. }
13. }
a) b is : 2
b) b is : 1
c) Compilation Error.
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.
9. What is the output of this program?
1. class main_arguments {
3. {
5. int x;
6. argument[0] = args;
7. x = argument[0].length;
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 {
3. {
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
1. class array_output {
3. {
6. array_variable[i] = 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 {
3. {
8. int sum = 0;
11. arr[i][j] = j + 1;
15. System.out.print(sum);
16. }
17. }
a) 11
b) 10
c) 13
d) 14
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 {
3. {
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. class array_output {
3. {
7. System.out.print(array_variable[i] + "");
8. }
9. }
10. }
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
Answer:d
Explanation:None.
output:
$ javac array_output.java
$ java array_output
iiiiiiiiii
1. class array_output {
5. int sum = 0;
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
1. class increment {
3. {
4. double var1 = 1 + 5;
6. int var3 = 1 + 5;
9.
10. }
11. }
a) 1 1
b) 0 1
c) 1.5 1
d) 1.5 1.0
Answer:c
output:
$ javac increment.java
$ java increment
1.5 1
1. class Modulus {
3. {
4. double a = 25.64;
5. int b = 25;
6. a = a % 10;
7. b = b % 10;
9. }
10. }
a) 5.640000000000001 5
b) 5.640000000000001 5.0
c) 5 5
d) 5 5.640000000000001
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 {
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 Output {
3. {
4. int x , y;
5. x = 10;
6. x++;
7. --x;
8. y = x++;
10. }
11. }
a) 11 11
b) 10 10
c) 11 10
d) 10 11
Answer: c
output:
$ javac Output.java
$ java Output
11 10
10. What is the output of this program?
1. class Output {
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;
14. }
15. }
a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4
Answer: d
output:
$ javac Output.java
$ java Output
344
1. class bitwise_operator {
3. {
8. }
a) 42 42
b) 43 43
c) 42 -43
d) 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
1. class bitwise_operator {
3. {
4. int a = 3;
5. int b = 6;
6. int c = a | b;
7. int d = a & b;
9. }
10. }
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)
10. }
11. }
a) 0 64
b) 64 0
c) 0 256
d) 256 0
Answer:d
output:
$ javac leftshift_operator.java
$ java leftshift_operator
256 0
1. class rightshift_operator {
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
output:
$ javac rightshift_operator.java
$ java rightshift_operator
1. class Output {
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. }
a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6
Answer: a
output:
$ javac Output.java
$ java Output
316
1. class Relational_operator {
3. {
4. int var1 = 5;
5. int var2 = 6;
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 {
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;
10. }
11. }
a) false false
b) true ture
c) true false
d) false true
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 {
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
output:
$ javac ternary_operator.java
$ java ternary_operator
1. class Output {
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
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
1. class Output {
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
output:
$ javac Output.java
$ java Output
false
1. class operators {
3. {
4. int var1 = 5;
5. int var2 = 6;
6. int var3;
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;
6. }
7. }
a) 24 8
b) 24 9
c) 27 8
d) 27 9
Answer: d
output:
$ javac operators.java
$ java operators
27 9
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
output:
$ javac ternary_operator.java
$ java ternary_operator
1. class Output {
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.
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
1. class selection_statements {
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
1. class comma_operator {
3. {
4. int sum = 0;
6. sum += i;
7. System.out.println(sum);
8. }
9. }
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
3. {
4. int x = 2;
5. int y = 0;
7. if (y % x == 0)
8. continue;
9. else if (y == 8)
10. break;
11. else
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
output:
$ javac jump_statments.java
$ java jump_statments
13579
1. class Output {
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
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
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)
11. }
12. System.out.println(a);
13. }
14. System.out.println(b);
15. }
16. }
17. }
a) 5 10
b) 10 5
c) 5
d) 10
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
1. class main_class {
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 can’t be created in a class.
output:
$ javac main_class.java
1. class box {
2. int width;
3. int height;
4. int length;
5. }
6. class mainclass {
8. {
11. obj.height = 2;
14. System.out.print(y);
15. }
16. }
a) 12
b) 200
c) 400
d) 100
Answer: b
output:
$ javac mainclass.java
$ java mainclass
200
1. class box {
2. int width;
3. int height;
4. int length;
5. }
6. class mainclass {
8. {
11. obj1.height = 1;
12. obj1.length = 2;
13. obj1.width = 1;
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
$ java mainclass
1. class box {
2. int width;
3. int height;
4. int length;
5. }
6. class mainclass {
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
1. class box {
2. int width;
3. int height;
4. int length;
5. int volume;
6. box() {
7. width = 5;
8. height = 5;
9. length = 6;
10. }
13. }
14. }
17. {
18. box obj = new box();
19. obj.volume();
20. System.out.println(obj.volume);
21. }
22. }
a) 100
b) 150
c) 200
d) 250
Answer: b
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 {
10. {
12. obj.x = 5;
13. obj.y = 5;
14. System.out.println(obj.isequal); }
15. }
a) false
b) true
c) 0
d) 1
Answer: b
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() {
12. System.out.println(volume);
13. }
14. }
17. {
19. obj.volume();
20. }
21. }
a) 150
b) 200
c) Runtime error
d) Compilation error
Answer: a
output:
$ javac Output.java
$ java Output
150
1. class area {
2. int width;
3. int length;
4. int area;
6. this.width = width;
7. this.length = length;
8. }
9.
10. }
11. class Output {
13. {
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
6. What is the output of this program?
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. }
13. {
15. int a = 0;
16. obj.add(6);
17. System.out.println(obj.x);
18. }
19. }
a) 5
b) 6
c) 7
d) 8
Answer: c
output:
$ javac Overload_methods.java
$ java Overload_methods
1. class overload {
2. int x;
3. int y;
5. x = a + 1;
6. }
8. x = a + 2;
9. }
10. }
13. {
15. int a = 0;
17. System.out.println(obj.x);
18. }
19. }
a) 6
b) 7
c) 8
d) 9
Answer: c
output:
$ javac Overload_methods.java
$ java Overload_methods
1. class overload {
2. int x;
3. double y;
5. x = a + b;
6. }
8. y = c + d;
9. }
10. overload() {
11. this.x = 0;
12. this.y = 0;
13. }
14. }
17. {
19. int a = 2;
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 {
2. int a;
3. int b;
5. i *= 2;
6. j /= 2;
7. }
8. }
9. class Output {
11. {
17. }
18. }
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. }
15. {
17. obj.meth(obj);
19. }
20. }
a) 10 20
b) 20 10
c) 20 40
d) 40 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
1. class string_demo {
3. {
5. System.out.println(obj);
6. }
7. }
a) I
b) like
c) Java
d) IlikeJava
Answer: d
output:
$ javac string_demo.java
$ java string_demo
IlikeJava
1. class string_class {
3. {
5. System.out.println(obj.charAt(3));
6. }
7. }
a) I
b) L
c) K
d) E
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
1. class string_class {
3. {
5. System.out.println(obj.length());
6. }
7. }
a) 9
b) 10
c) 11
d) 12
Answer: c
output:
$ javac string_class.java
$ java string_class
11
1. class string_class {
3. {
9. }
10. }
a) hello hello
b) world world
c) hello world
d) world hello
Answer: c
output:
$ javac string_class.java
$ java string_class
hello world
1. class string_class {
3. {
4. String obj = "hello";
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
6. What is the output of this program?
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. }
15. {
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.
output:
$ javac inheritance_demo.java
$ java inheritance_demo
2
7. What is the output of this program?
1. class A {
2. int i;
3. }
4. class B extends A {
5. int j;
6. void display() {
7. super.i = j + 1;
9. }
10. }
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
output:
$ javac inheritance.java
$ java inheritance
23
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;
9. }
10. }
13. {
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
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. }
17. {
20. }
21. }
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;
10. }
11. }
14. {
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
$ java Output
12
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. }
15. {
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.
output:
$ javac method_overriding.java
$ java method_overriding
1. final class A {
2. int i;
3. }
4. class B extends A {
5. int j;
7. }
8. class inheritance {
10. {
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
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;
10. }
13. {
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
1. class A {
3. System.out.println("A");
4. }
5. }
6. class B extends A {
8. System.out.println("B");
9. }
10. }
11. class Dynamic_dispatch {
13. {
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;
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. }
15. {
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