Java MCQs
Java MCQs
This section of our 1000+ Java MCQs focuses on Abstract class in Java Programming Language.
Answer: a
Explanation: Thread is not an abstract class.
3. If a class inheriting an abstract class does not define all of its function then it will be known
as?
a) abstract
b) A simple class
c) Static class
d) None of the mentioned
View Answer
Answer: a
Explanation: Any subclass of an abstract class must either implement all of the abstract method
in the superclass or be itself declared abstract.
Answer: c
Explanation: Abstract class cannot be directly initiated with new operator, Since abstract class
does not contain any definition of implementation it is not possible to create an abstract object.
5. Which of these packages contains abstract keyword?
a) java.lang
b) java.util
c) java.io
d) java.system
View Answer
Answer: a
Explanation: None.
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
View Answer
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
1. class A {
2. public int i;
3. public int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class B extends A {
10. int a;
11. B() {
12. super();
13. }
14. }
15. class super_use {
16. public static void main(String args[])
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
View Answer
1. abstract class A {
2. int i;
3. abstract void display();
4. }
5. class B extends A {
6. int j;
7. void display() {
8. System.out.println(j);
9. }
10. }
11. class Abstract_demo {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.j=2;
16. obj.display();
17. }
18. }
a) 0
b) 2
c) Runtime Error
d) Compilation Error
View Answer
Answer: b
Explanation: class A is an abstract class, it contains a abstract function display(), the full
implementation of display() method is given in its subclass B, Both the display functions are the
same. Prototype of display() is defined in class A and its implementation is given in class B.
output:
$ javac Abstract_demo.java
$ java Abstract_demo
2
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 method_overriding {
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
View Answer
Answer: c
Explanation: class A & class B both contain display() method, class B inherits class A, when
display() method is called by object of class B, display() method of class B is executed rather
than that of Class A.
output:
$ javac method_overriding.java
$ java method_overriding
2
a) 1 2
b) 2 1
c) 1 3
d) 3 1
View Answer
Answer: a
Explanation: Both class A & B have member with same name that is j, member of class B will be
called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.
output:
$ javac Output.java
$ java Output
12