100 MCQs On Java
100 MCQs On Java
4. An expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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();
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
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