Core Java

Multiple Choice Question & Answer:-

 

1. What is the range of data type short in Java?

 

 a) -128 to 127

 b) -32768 to 32767

 c) -2147483648 to 2147483647

 d) None of the mentioned

 

Answer:b

 Explanation:Short occupies 16 bits in memory. Its range is from -32768 to 32767.

 

2. What is the range of data type byte in Java?

 

 a) -128 to 127

 b) -32768 to 32767

 c) -2147483648 to 2147483647

 d) None of the mentioned

 

Answer:a

 Explanation:Byte occupies 8 bits in memory. Its range is from -128 to 127.

 

3. Which of the following are legal lines of Java code?

 

 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

 d) All statements are correct.

 

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.

 

5. Which of these literals can be contained in a data type float variable?

 

 a) 1.7e-308

 b) 3.4e-038

 c) 1.7e+308

 d) 3.4e-050

 

Answer: b

 Explanation:Range of data type float is 3.4e-038 to 3.4e+308.

 

6. Which data type value is returned by all transcendental math functions?

 

 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

 

9. Which of these keywords can be used to prevent Method overriding?

 

 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

 

11. Which of the following statements are incorrect?

 

 a) public members of class can be accessed by any code in the program.

 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

 Explanation: private members of a class cannot be inherited by a sub class.

 

12. Which of these is supported by method overriding in Java?

 

 a) Abstraction

 b) Encapsulation

 c) Polymorphism

 d) None of the mentioned

 

Answer: c

 

13. String in Java is a?

 

 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

 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.

 

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

 

17. Which of the following statements are incorrect?

 

 a) String is a class.

 b) Strings in java are mutable.

 c) Every string is an object of class String.

 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

 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.

 

19. Which of these can be overloaded?

 

 a) Methods

 b) Constructors

 c) All of the mentioned

 d) None of the mentioned

 

Answer: c

 

20. Which of these is correct about passing an argument by call-by-value process?

 

 a) Copy of argument is made into the formal parameter of the subroutine.

 b) Reference to original argument is passed to formal parameter of the subroutine.

 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

 

22. Which of the following statements are incorrect?

 

 a) Default constructor is called at the time of declaration of the object if a constructor has not been defined.

 b) Constructor can be parameterized.

 c) finalize() method is called when a object goes out of scope and is no longer needed.

 d) finalize() method must be declared protected.

 

Answer: c

 Explanation: finalize() method is called just prior to garbage collection. It is not called when object goes out of scope.

 

23. What is the numerical range of a char in Java?

 

 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

 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.

 

25. Which of these values can a boolean variable contain?

 

 a) True & False

 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

 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.

 

27. Which one is a valid declaration of a boolean?

 

 a) boolean b1 = 1;

 b) boolean b2 = ‘false’;

 c) boolean b3 = false;

 d) boolean b4 = ‘true’

 

Answer:c

 Explanation: Boolean can only be assigned true or false literals.

 

 

28. Which of these is data type long literal?

 

 a) 0x99fffL

 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.

 

29. Which of these can be returned by the operator & ?

 

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

 

30. Literals in java must be preceded by which of these?

 a) L

 b) l

 c) D

 d) L and I

 

Answer:d

 Explanation:Data type long literals are appended by an upper or lowercase L.

 

31. Literal can be of which of these data types?

 

 a) integer

 b) float

 c) boolean

 d) all of the mentioned

 

Answer:d

 

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

 

33. Which of these is incorrect string literal?

 

 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?

 

 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

 

35. What is the prototype of the default constructor of this class?

 public class prototype { }

 

 a) prototype( )

 b) prototype(void)

 c) public prototype(void)

 d) public prototype( )

 

Answer: d

 

35. What is the error in this code?

 byte b = 50;

 b = b * 50;

 

 a) b can not contain value 100, limited by its range.

 b) * operator has converted b * 50 into int, which can not be converted to byte without casting.

 c) b can not contain value 50.

 d) No error in this code

 

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

 Explanation:If any operand is double the result of expression is double.

 

37. What is Truncation is Java?

 

 a) Floating-point value assigned to an integer type.

 b) Integer value assigned to floating type.

 c) Floating-point value assigned to an Floating type.

 d) Integer value assigned to floating type.

 

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.

 

39. Which of these is an incorrect array declaration?

 

 a) int arr[] = new int[5]

 b) int [] arr = new int[5]

 c) int arr[] arr = new int[5]

 d) int arr[] = int [5] new

 

Answer:d

 Explanation:Operator new must be succeeded by array type and array size.

 

40. What will this code print?

 int arr[] = new int [5];

 System.out.print(arr);

 

 a) 0

 b) value stored in arr[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].

 

41. Which of these is an incorrect Statement?

 

 a) It is necessary to use new operator to initialize an array.

 b) Array can be initialized using comma separated expressions surrounded by curly braces.

 c) Array can be initialized when they are declared.

 d) None of the mentioned

 

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

 

42. Which of these is necessary to specify at time of array initialization?

 

 a) Row

 b) Column

 c) Both Row and Column

 d) None of the mentioned

 

Answer:a

 

43. Which of these keyword must be used to inherit a class?

 

 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

 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.

 

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

 Explanation: A class member declared protected becomes private member of subclass.

 

46. Which of these is correct way of inheriting class A by class B?

 

 a) class B + class A {}

 b) class B inherits class A {}

 c) class B extends A {}

 d) class B extends class A {}

 

Answer: c

 

47. Which of the following statements are incorrect?

 

 a) public members of class can be accessed by any code in the program.

 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

 Explanation: private members of a class cannot be inherited by a sub class.

 

 

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

 

49. Modulus operator, %, can be applied to which of these?

 

 a) Integers

 b) Floating – point numbers

 c) Both Integers and floating – point numbers.

 d) None of the mentioned

 

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

 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.

 

 

 

 

51. Decrement operator, –, decreases value of variable by what number?

 

 a) 1

 b) 2

 c) 3

 d) 4

 

Answer: a

 

52. Which of these statements are incorrect?

 

 a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms.

 b) Assignment operators run faster than their equivalent long forms.

 c) Assignment operators can be used only with numeric and character data type.

 d) None

 

Answer: d

 

53. Which of these is not a bitwise operator?

 

 a) &

 b) &=

 c) |=

 d) <=

 

Answer:d

 Explanation: <= is a relational operator.

 

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.

 c) The left shift operator can be used as an alternative to multiplying by 2.

 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.

 

58. What is the output of relational operators?

 

 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

b) Floating - point numbers

c) Boolean

d) None of the mentioned

 

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.

 

62. Which of these statement is correct?

 

a) true and false are numeric values 1 and 0.

b) true and false are numeric values 0 and 1.

c) true is any non zero value and false is 0.

d) true and false are non numeric values.

 

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.

 

 

63. Which of these have highest precedence?

 

 a) ()

 b) ++

 c) *

 d) >>

 

Answer: a

 Explanation: Order of precedence is (highest to lowest) a -> b -> c -> d.

 

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

 

65. What is the value stored in x in following lines of code?

 int x, y, z;

 x = 0;

 y = 1;

 x = y = z = 8;

 

 a) 0

 b) 1

 c) 9

 d) 8

 

Answer: d

 

66. What is the order of precedence (highest to lowest) of following operators?

 

 1. &

 2. ^

 3. ?:

 

 a) 1 -> 2 -> 3

 b) 2 -> 1 -> 3

 c) 3 -> 2 -> 1

 d) 2 -> 3 -> 1

 

Answer: a

 

67. Which of these statements are incorrect?

 

 a) Equal to operator has least precedence.

 b) Brackets () have highest precedence.

 c) Division operator, /, has higher precedence than multiplication operator.

 d) Addition operator, +, and subtraction operator have equal precedence.

 

Answer: c

 Explanation: Division operator, /, has equal precedence as of multiplication operator. In expression involving multiplication and division evaluation of expression will begin from right side when no brackets are used.

 

 

68. Which of these selection statements test only for equality?

 

 a) if

 b) switch

 c) if & switch

 d) None of the mentioned

 

Answer: b

 Explanation: switch statements checks for equality between the controlling variable and its constant cases.

 

69. Which of these are selection statements in Java?

 

 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

 d) None of the mentioned

 

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

 

72. Which of these statement is correct?

 

 a) switch statement is more efficient than a set of nested ifs.

 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.

 d) it is possible to create a nested switch statements.

 

Answer: b

 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;

 

 a) Memory address of allocated memory of object.

 b) NULL

 c) Any arbitrary pointer

 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.

 

74. Which of these keywords is used to make a class?

 

 a) class

 b) struct

 c) int

 d) None of the mentioned

 

Answer:a

 

75. Which of the following is a valid declaration of an object of class Box?

 

 a) Box obj = new Box();

 b) Box obj = new Box;

 c) obj = new Box();

 d) new Box obj;

 

Answer: a

 

76. Which of these operators is used to allocate memory for an object?

 

 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.

 

77. Which of these statement is incorrect?

 

 a) Every class must contain a main() method.

 b) Applets do not require a main() method at all.

 c) There can be only one main() method in a program.

 d) main() method must be made public.

 

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

 d) None of the mentioned

 

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

 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.

 

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

 

83. Which of the folowing stements are incorrect?

 

 a) Default constructor is called at the time of declaration of the object if a constructor has not been defined.

 b) Constructor can be parameterized.

 c) finalize() method is called when a object goes out of scope and is no longer needed.

 d) finalize() method must be declared protected.

 

Answer: c

 Explanation: finalize() method is called just prior to garbage collection. it is not called when object goes out of scope.

 

84. Which of these lines of code will give better performance?

 

 1. a | 4 + c >> b & 7;

 2. (a | ((( 4 * c ) >> b ) & 7 ))

 

 a) 1 will give better performance as it has no parentheses.

 b) 2 will give better performance as it has parentheses.

 c) Both 1 & 2 will give equal performance.

 d) Dependent on the computer system.

 

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 {

 

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

 

86. What is the output of this program?

 

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

 

87. What is the output of this program?

 

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

 

88. What is the output of this program?

 

1.    class area {

 

2.        public static void main(String args[])

 

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

 

89. What is the output of this program?

 

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

 

 output:

 $ javac array_output.java

 $ java array_output

 i i i i i

 

90. What is the output of this program?

 

1.    class mainclass {

 

2.        public static void main(String args[])

 

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

 

91. What is the output of this program?

 

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

 

 output:

 $ javac mainclass.java

 $ java mainclass

 true

 

92. What is the output of this program?

 

1.    class booloperators {

 

2.        public static void main(String args[])

 

3.        {

 

4.            boolean var1 = true;

 

5.                boolean var2 = false;

 

6.                System.out.println((var2 & var2));

 

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

 

93. What is the output of this program?

 

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

 

94. What is the output of this program?

 

1.    class evaluate {

 

2.        public static void main(String args[])

 

3.        {

 

4.            int a[] = {1,2,3,4,5};

 

5.                int d[] = a;

 

6.                int sum = 0;

 

7.                for (int j = 0; j < 3; ++j)

 

8.                sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);

 

9.                System.out.println(sum);

 

10.        }

 

11.    }

 

 a) 38

 b) 39

 c) 40

 d) 41

 

Answer:c

 

 output:

 $ javac evaluate.java

 $ java evaluate

 40

 

95. What is the output of this program?

 

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

 1 2 3 4 5

 

96. What is the output of this program?

 

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 doesn’t 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

 

 

97. What is the output of this program?

 

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

 

 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

 

98. What is the output of this program?

 

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

 E U

 

99. What is the output of this program?

 

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 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 {

 

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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9. What is the output of this program?

 

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

 1 2 3

 

10. What is the output of this program?

 

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

 

6. What is the output of this program?

 

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

 0 2 4 6 8

 

7. What is the output of this program?

 

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

 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

 

8. What is the output of this program?

 

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

 

9. What is the output of this program?

 

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

 

 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

 i i i i i i i i i i

 

10. What is the output of this program?

 

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

 

6. What is the output of this program?

 

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

 

 output:

 $ javac increment.java

 $ java increment

 1.5 1

 

7. What is the output of this program?

 

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

 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

 

8. What is the output of this program?

 

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

 

9. What is the output of this program?

 

1.    class Output {

 

2.        public static void main(String args[])

 

3.        {   

 

4.             int x , y;

 

5.             x = 10;

 

6.             x++;

 

7.             --x;

 

8.             y = x++;

 

9.             System.out.println(x + " " + y);

 

10.        }

 

11.    }

 

 

 a) 11 11

 b) 10 10

 c) 11 10

 d) 10 11

 

Answer: c

 Explanation: x is initialized to 10 then increased by 1 by ++ operator making it 11. x is again decreased by — operator making it 10, next x is incremented by post increment and intialized to y, here the value of x obtained before increment operator is executed, so value of y is 10 and value of x is 11.

 

 output:

 $ javac Output.java

 $ java Output

 11 10

 

10. What is the output of this program?

 

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

 

 output:

 $ javac Output.java

 $ java Output

 3 4 4

 

6. What is the output of this program?

 

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

 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

 

7. What is the output of this program?

 

1.    class bitwise_operator {

 

2.        public static void main(String args[])

 

3.        {   

 

4.             int a = 3;

 

5.             int b = 6;

 

6.                 int c = a | b;

 

7.             int d = a & b;             

 

8.             System.out.println(c + " "  + d);

 

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

 7 2

 

8. What is the output of this program?

 

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

 

 a) 0 64

 b) 64 0

 c) 0 256

 d) 256 0

 

Answer:d

 

 output:

 $ javac leftshift_operator.java

 $ java leftshift_operator

 256 0

 

9. What is the output of this program?

 

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

 

10. What is the output of this program?

 

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

 

 a) 3 1 6

 b) 2 2 3

 c) 2 3 4

 d) 3 3 6

 

Answer: a

 

 output:

 $ javac Output.java

 $ java Output

 3 1 6

 

6. What is the output of this program?

 

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

 

7. What is the output of this program?

 

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

 

 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

 

8. What is the output of this program?

 

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

 

 output:

 $ javac ternary_operator.java

 $ java ternary_operator

 3

 

9. What is the output of this program?

 

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

 

10. What is the output of this program?

 

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

 

 output:

 $ javac Output.java

 $ java Output

 false

 

6. What is the output of this program?

 

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

 

7. What is the output of this program?

 

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

 

8. What is the output of this program?

 

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

 

 output:

 $ javac ternary_operator.java

 $ java ternary_operator

 3

 

10. What is the output of this program?

 

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

 

6. What is the output of this program?

 

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

 

7. What is the output of this program?

 

1.    class comma_operator {

 

2.        public static void main(String args[])

 

3.        {   

 

4.             int sum = 0;

 

5.             for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)

 

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

 6

 

8. What is the output of this program?

 

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

 $ java jump_statments

 1 3 5 7 9

 

9. What is the output of this program?

 

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

 

10. What is the output of this program?

 

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

 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

 

6. What is the output of this program?

 

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 can’t be created in a class.

 

 output:

 $ javac main_class.java

 Exception in thread “main” java.lang.Error: Unresolved compilation problem:

 Duplicate local variable x

 

7. Which of the following statements is correct?

 

 a) Public method is accessible to all other classes in the hierarchy

 b) Public method is accessible only to subclasses of its parent class

 c) Public method can only be called by object of its class.

 d) Public method can be accessed by calling object of the public class.

 

Answer: a

 

8. What is the output of  this program?

 

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.             obj.width = 10;

 

11.             obj.height = 2;

 

12.             obj.length = 10;

 

13.             int y = obj.width * obj.height * obj.length;

 

14.             System.out.print(y);

 

15.        }

 

16.    }

 

 a) 12

 b) 200

 c) 400

 d) 100

 

Answer: b

 

 output:

 $ javac mainclass.java

 $ java mainclass

 200

 

9. What is the output of this program?

 

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

 $ java mainclass

 1

 

10. What is the output of this program?

 

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

 

6. What is the output of this program?

 

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

 

11.        void volume() {

 

12.             volume = width*height*length;

 

13.        }

 

14.    }   

 

15.    class constructor_output {

 

16.        public static void main(String args[])

 

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

 

7. What is the output of this program?

 

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

 d) 1

 

Answer: b

 

 output:

 $ javac Output.java

 $ java Output

 true

 

8. What is the output of this program?

 

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

 

 output:

 $ javac Output.java

 $ java Output

 150

 

10. What is the output of this program?

 

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

 6 5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

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

 

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

 7

 

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

 

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

 d) 9

 

Answer: c

 

 output:

 $ javac Overload_methods.java

 $ java Overload_methods

 8

 

8. What is the output of this program?

 

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

 

9. What is the output of this program?

 

1.    class test {

 

2.        int a;

 

3.        int b;

 

4.        void meth(int i , int j) {

 

5.            i *= 2;

 

6.            j /= 2;

 

7.        }         

 

8.    }   

 

9.    class Output {

 

10.        public static void main(String args[])

 

11.        {

 

12.            test obj = new test();

 

13.             int a = 10;

 

14.            int b = 20;            

 

15.            obj.meth(a , b);

 

16.            System.out.println(a + " " + b);       

 

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

 

10. What is the output of this program?

 

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

 

 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

 

 

 

 

 

 

 

 

 

 

 

 

6. What is the output of this program?

 

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

 

7. What is the output of this program?

 

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

 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

 I

 

8. What is the output of this program?

 

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

 

 output:

 $ javac string_class.java

 $ java string_class

 11

 

9. What is the output of this program?

 

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

 d) world hello

 

Answer: c

 

 output:

 $ javac string_class.java

 $ java string_class

 hello world

 

10. What is the output of this program?

 

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

 

 

 

 

 

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

 

13.    class inheritance_demo {

 

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.

 

 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;

 

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

 

 output:

 $ javac inheritance.java

 $ java inheritance

 2 3

 

8. What is the output of this program?

 

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

 

9. What is the output of this program?

 

1.    class A {

 

2.        public int i;