0% found this document useful (0 votes)
401 views

100 MCQs On Java

The document contains 20 multiple choice questions about Java data types, variables, operators, and other Java concepts. The questions cover topics like data type ranges, literals, conversions between types, and sample code outputs.

Uploaded by

Dr. Karthickeyan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
401 views

100 MCQs On Java

The document contains 20 multiple choice questions about Java data types, variables, operators, and other Java concepts. The questions cover topics like data type ranges, literals, conversions between types, and sample code outputs.

Uploaded by

Dr. Karthickeyan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 26

Java

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

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

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.

4. An expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float

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

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


a) int
b) float
c) double
d) long
View Answer

7. What is the output of this program?

1. class average {
2. publicstaticvoid main(Stringargs[])
3. {
4. doublenum[]={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

8. What is the output of this program?

1. class conversion {
2. publicstaticvoid main(Stringargs[])
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

9. What is the output of this program?

1. class increment {
2. publicstaticvoid main(Stringargs[])
3. {
4. int g =3;
5. System.out.print(++g *8);
6. }
7. }
a) 25
b) 24
c) 32
d) 33

10. What is the output of this program?

1. class area {
2. publicstaticvoid main(Stringargs[])
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

11. 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
12. Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned

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


a) True & False
b) 0 & 1
c) Any integer value
d) true

14. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java?
a) ASCII
b) ISO-LATIN-1
c) None of the mentioned
d) ASCII and ISO-LATIN1

15. 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’

16. What is the output of this program?

1. classarray_output{
2. publicstaticvoid main(Stringargs[])
3. {
4. chararray_variable[]=newchar[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) iiiii
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned

17. What is the output of this program?

1. classmainclass{
2. publicstaticvoid main(Stringargs[])
3. {
4. char a ='A';
5. a++;
6. System.out.print((int)a);
7. }
8. }
a) 66
b) 67
c) 65
d) 64

18. What is the output of this program?

1. classmainclass{
2. publicstaticvoid main(Stringargs[])
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

19. What is the output of this program?


1. classbooloperators{
2. publicstaticvoid main(Stringargs[])
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

20. What is the output of this program?


1. classasciicodes{
2. publicstaticvoid main(Stringargs[])
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
21. Which of these is data type long literal?
a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246

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


a) Integer
b) Boolean
c) Character
d) Integer or Boolean
23. Literals in java must be preceded by which of these?
a) L
b) l
c) D
d) L and I

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


a) integer
b) float
c) boolean
d) all of the mentioned

25. Which of these can not be used for a variable name in Java?
a) identifier
b) keyword
c) identifier & keyword
d) None of the mentioned

26. What is the output of this program?

1. class evaluate {
2. publicstaticvoid main(Stringargs[])
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

27. What is the output of this program?

1. classarray_output{
2. publicstaticvoid main(Stringargs[])
3. {
4. intarray_variable[]=newint[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

28. What is the output of this program?

1. classvariable_scope{
2. publicstaticvoid main(Stringargs[])
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

29. Which of these is incorrect string literal?


a) “Hello World”
b) “Hello\nWorld”
c) “\”Hello World””
d) “Hello
world”

30. What is the output of this program?

1. classdynamic_initialization{
2. publicstaticvoid main(Stringargs[])
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

31. Which of these is necessary condition for automatic type conversion in Java?
a) The destination type is smaller than source type.
b) The destination type is larger than source type.
c) The destination type can be larger or smaller than source type.
d) None of the mentioned

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

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

34. If an expression contains double, int, float, long, then whole expression will promoted into
which of these data types?
a) long
b) int
c) double
d) float

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

36. What is the output of this program?

1. classchar_increment{
2. publicstaticvoid main(Stringargs[])
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

37. What is the output of this program?

1. class conversion {
2. publicstaticvoid main(Stringargs[])
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

38. What is the output of this program?

1. class A {
2. finalpublicint calculate(int a, int b){return1;}
3. }
4. class B extends A {
5. publicint calculate(int a, int b){return2;}
6. }
7. publicclass output {
8. publicstaticvoid main(Stringargs[])
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.

39. What is the output of this program?

1. classmain_arguments{
2. publicstaticvoid main(String[]args)
3. {
4. String[][] argument =newString[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

40. What is the output of this program?

1. class c {
2. publicvoid main(String[]args)
3. {
4. System.out.println("Hello"+args[0]);
5. }
6. }
a) Hello c
b) Hello
c) Hello world
d) Runtime Error.

41. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc

42. Which of these is an incorrect array declaration?


a) intarr[] = new int[5] b) int [] arr = new int[5] c) intarr[] arr = new int[5] d) intarr[] = int [5] new

43. What will this code print?


intarr[] = new int [5];
System.out.print(arr);
a) 0
b) value stored in arr[0].
c) 00000
d) Garbage value

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

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

46. Which of the following can be operands of arithmetic operators?


a) Numeric
b) Boolean
c) Characters
d) Both Numeric & Characters

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

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

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


a) 1
b) 2
c) 3
d) 4

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

51. Which of these is not a bitwise operator?


a) &
b) &=
c) |=
d) <= [expand title="View Answer"] Answer:d Explanation: <= is a relational operator. [/expand]
2. Which operator is used to invert all the digits in binary representation of a number? a) ~ b)
<<< c) >>>
d) ^

52. Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) None of the mentioned

53. Which of these is used to access member of class before object of that class is created?
a) public
b) private
c) static
d) protected

54. Which of these is used as default for a member of a class if no access specifier is used for
it?
a) private
b) public
c) public, within its own package
d) protected

55. What is the process by which we can control what parts of a program can access the
members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion

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


a) if
b) switch
c) if & switch
d) None of the mentioned

57. Which of these are selection statements in Java?


a) if()
b) for()
c) continue
d) break

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

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

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

61. What is the stored in the object obj in following lines of code?
boxobj;
a) Memory address of allocated memory of object.
b) NULL
c) Any arbitrary pointer
d) Garbage

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


a) class
b) struct
c) int
d) None of the mentioned

63. 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;

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


a) malloc
b) alloc
c) new
d) give

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

66. What is the return type of a method that does not returns any value?
a) int
b) float
c) void
d) double

67. What is the process of defining more than one method in a class differentiated by method
signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned

68. Which of the following is a method having same name as that of it’s class?
a) finalize
b) delete
c) class
d) constructor
69. Which method can be defined only once in a program?
a) main method
b) finalize method
c) static method
d) private method

70. Which of these statement is incorrect?


a) All object of a class are allotted memory for the all the variables defined in the class.
b) If a function is defined public it can be accessed by object of other class by inheritation.
c) main() method must be made public.
d) All object of a class are allotted memory for the methods defined in the class.

71. Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) None of the mentioned

72. Which of these is used to access member of class before object of that class is created?
a) public
b) private
c) static
d) protected

73. Which of these is used as default for a member of a class if no access specifier is used for
it?
a) private
b) public
c) public, within its own package
d) protected

74. What is the process by which we can control what parts of a program can access the
members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion

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

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


a) super
b) this
c) extent
d) extends

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

78. A class member declared protected becomes member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member

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

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

81. 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. classinheritance_demo{
14. publicstaticvoid main(Stringargs[])
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
82. 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. publicstaticvoid main(Stringargs[])
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

83. What is the output of this program?

1. class A {
2. publicint i;
3. privateint 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. publicstaticvoid main(Stringargs[])
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

84. What is the output of this program?


1. class A {
2. publicint i;
3. publicint j;
4. A(){
5. i =1;
6. j =2;
7. }
8. }
9. class B extends A {
10. int a;
11. B(){
12. super();
13. }
14. }
15. classsuper_use{
16. publicstaticvoid main(Stringargs[])
17. {
18. B obj=new B();
19. System.out.println(obj.i+" "+obj.j)
20. }
21. }
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error

85. What is the output of this program?

1. class A {
2. publicint i;
3. protectedint j;
4. }
5. class B extends A {
6. int j;
7. void display(){
8. super.j=3;
9. System.out.println(i +" "+ j);
10. }
11. }
12. class Output {
13. publicstaticvoid main(Stringargs[])
14. {
15. B obj=new B();
16. obj.i=1;
17. obj.j=2;
18. obj.display();
19. }
20. }
a) 1 2
b) 2 1
c) 1 3
d) 3 1

86. Which of these keyword can be used in subclass to call the constructor of superclass?
a) super
b) this
c) extent
d) extends

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

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


a) static
b) constant
c) protected
d) final

89. 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();

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

91. Which of these class is superclass of every class in Java?


a) String class
b) Object class
c) Abstract class
d) ArrayList class

92. Which of these method of Object class can clone an object?


a) Objectcopy()
b) copy()
c) Object clone()
d) clone()

93. Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned

94. Which of these keywords can be used to prevent inheritance of a class?


a) super
b) constant
c) Class
d) final

95. Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) None of the mentioned

96. Which of these method of class String is used to extract more than one character at a time a
String object?
a) getchars()
b) GetChars()
c) Getchars()
d) getChars()

97. Which of these methods is an alternative to getChars() that stores the characters in an array
of bytes?
a) getBytes()
b) GetByte()
c) giveByte()
d) Give Bytes()

98. Which of these method of class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()

99. Which of these method of class String is used to extract a single character from a String
object?
a) CHARAT()
b) chatat()
c) charAt()
d) ChatAt()

100. Which of these methods can be used to convert all characters in a String into a character
array?
a) charAt()
b) getChars()
c) toCharArray()
d) All of the mentioned

You might also like