Explore topic-wise MCQs in Java Programming.

This section includes 21 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the outcome of below code box class?
class Shape 
{
public int area()
{
return 0;
}
}

class Square extends Shape
{
public int area()
{
return 5;
}
}

class Rectangle extends Shape
{
public int area()
{
return 9;
}
}

public class box
{
public static void main(String[] args)
{
Shape obj = new Square();
Shape obj0 = new Rectangle();
obj = obj0;
System.out.println(obj.area());
}
}

A. Compilation Error
B. Runtime Error
C. Default value
D. 9
E. None of these
Answer» E. None of these
2.

What is the outcome of below code Box class?
class Shape 
{
public int area()
{
return 1;
}
}

class Square extends Shape
{
public int area()
{
return 2;
}
}

class Rectangle extends Shape
{
public int area()
{
return 3;
}
}

public class Box
{
public static void main(String[] args)
{
Shape obj = new Shape();
Square obj0 = new Square();
Rectangle obj1 = new Rectangle();
obj1 = (Rectangle)obj0;
System.out.println(obj0.area());
}
}

A. Compilation Error
B. Runtime Error
C. Default Value
D. All of above
E. None of these
Answer» B. Runtime Error
3.

What is the outcome of below code Output class?
class Shape 
{
public int area()
{
return 2;
}
}

class Square extends Shape
{
public int area()
{
return 5;
}
}

class Rectangle extends Shape
{
public int area()
{
return 7;
}
}

public class Output
{
public static void main(String[] args)
{
Shape obj = new Shape();
Square obj0 = new Square();
Rectangle obj1 = new Rectangle();
obj1 = (Rectangle)obj;
System.out.println(obj0.area());
}
}

A. Compilation Error
B. Runtime Error
C. Default value
D. 7
E. 5
Answer» C. Default value
4.

What is the outcome of below code SquareExample class?
class Shape 
{
public int area()
{
return 2;
}
}

class Square extends Shape
{
public int area()
{
return 7;
}
}

public class SquareExample
{
public static void main(String[] args)
{
Shape obj = new Shape();
Square obj0 = new Square();
obj0 = obj;
System.out.println(obj0.area());
}
}

A. Compilation Error
B. Runtime Error
C. Default Value
D. 7
E. None of these
Answer» B. Runtime Error
5.

What is the outcome of below code Rectangle_Example class?
class Shape 
{
public int area()
{
return 2;
}
}

class Rectangle extends Shape
{
public int area()
{
return 6;
}
}

public class Rectangle_Example
{
public static void main(String[] args)
{
Shape obj = new Shape();
Rectangle obj0 = new Rectangle();
obj = obj0;
System.out.println(obj.area());
}
}

A. 6
B. Runtime Error
C. Default Value
D. Compilation Error
E. None of these
Answer» B. Runtime Error
6.

Which two classes use the Shape class correctly?

A. public class Circle implements Shape
{
private int radius;
}

B. public abstract class Circle extends Shape
{
private int radius;
}

C. public class Circle extends Shape
{
private int radius;
public void draw();
}

D. public abstract class Circle implements Shape
{
private int radius;
public void draw();
}

E. public class Circle extends Shape
{
private int radius;
public void draw()
{
/* code here */
}
}

F. public abstract class Circle implements Shape
{
private int radius;
public void draw()
{
/* code here */
}
}

A. C,E
B. T,H
C. A,C
D. B,E
E. None of these
Answer» E. None of these
7.

What is the output of this program?

class N
{
int K;
}
class M extends N
{
int L;
void display()
{
super.K = L + 5;
System.out.println(K + " " + L);
}
}
public class inheritance_Example
{
public static void main(String args[])
{
M object = new M();
object.K=4;
object.L=3;
object.display();
}
}

A. 8 3
B. 4 3
C. 3 8
D. 3 4
E. None of these
Answer» B. 4 3
8.

What is the output of this program?

class N
{
public int K;
public int L;
N()
{
K = 5;
L = 2;
}
}
class M extends N
{
int p;
M()
{
super();
}
}
public class inheritance_super_use
{
public static void main(String args[])
{
M obj = new M();
System.out.println(obj.K + " " + obj.L);
}
}

A. 5 1
B. 1 5
C. 5 2
D. 2 5
E. None of these
Answer» D. 2 5
9.

What is the output of this program?

class N
{
int K;
void display()
{
System.out.println(K);
}
}
class M extends N
{
int L;
void display()
{
System.out.println(L);
}
}
public class inheritance_Example
{
public static void main(String args[])
{
M object = new M();
object.K=5;
object.L=6;
object.display();
}
}

A. 2
B. 3
C. 4
D. 5
E. 6
Answer» F.
10.

What is the outcome of below Square_Shape_Example class?
class Shape 
{
public int area()
{
return 1;
}
}

class Square extends Shape
{
public int area()
{
return 5;
}
}

public class Square_Shape_Example
{
public static void main(String[] args)
{
Shape obj = new Shape();
Square square = new Square();
obj = square;
System.out.println(obj.area());
}
}

A. 0
B. Compilation Error
C. Runtime Error
D. Default value
E. 5
Answer» F.
11.

What is the output of this program?

final class n
{
int K;
}
class n1 extends n
{
int L;
System.out.println(L + " " + K);
}
public class inheritance_Example
{
public static void main(String args[])
{
n1 obj = new n1();
obj.display();
}
}

A. Runtime Error
B. 4 4
C. Compilation Error
D. 5 5
E. None of these
Answer» D. 5 5
12.

What is the output of this program?

class N
{
int K;
int L;
N()
{
K = 3;
L = 5;
}
}
public class Result
{
public static void main(String args[])
{
N obj1 = new N();
N obj2 = new N();
System.out.print(obj1.equals(obj2));
}
}

A. true false
B. false
C. true
D. false true
E. None of these
Answer» C. true
13.

What is the output of this program?

class n
{
int K;
public void display()
{
System.out.println(K);
}
}
class m extends n
{
int L;
public void display()
{
System.out.println(L);
}
}
public class Dynamic_dispatch_Example
{
public static void main(String args[])
{
m obj2 = new m();
obj2.K = 3;
obj2.L = 5;
n r;
r = obj2;
r.display();
}
}

A. 1
B. 2
C. 3
D. 4
E. 5
Answer» F.
14.

Which two classes use the Shape class correctly?

A. C,E
B. T,H
C. A,C
D. B,E
E. None of these
Answer» E. None of these
15.

Which of these is correct way of inheriting class N by class M?

A. class M extends class N {}
B. class M + class N {}
C. class M inherits class N {}
D. class M extends N{}
E. None of these
Answer» E. None of these
16.

Does Java support multiple level inheritance?

A. False
B. True
C. NA
D. NA
E. NA
Answer» C. NA
17.

What is the outcome of below Square_Shape_Example class?

A. 0
B. Compilation Error
C. Runtime Error
D. Default value
E. 5
Answer» F.
18.

What is the outcome of below code Output class?

A. Compilation Error
B. Runtime Error
C. Default value
D. 7
E. 5
Answer» C. Default value
19.

What is the outcome of below code SquareExample class?

A. Compilation Error
B. Runtime Error
C. Default Value
D. 7
E. None of these
Answer» B. Runtime Error
20.

What is the outcome of below code Rectangle_Example class?

A. 6
B. Runtime Error
C. Default Value
D. Compilation Error
E. None of these
Answer» B. Runtime Error
21.

What is the outcome of below code Box class?

A. Compilation Error
B. Runtime Error
C. Default Value
D. All of above
E. None of these
Answer» B. Runtime Error