All MCQ
All MCQ
This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Concepts
of OOPs”.
7. What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
View Answer
Answer: d
Explanation: It is a relationship where all objects have their own lifecycle and there
is no owner. This occurs where many to many relationships are available, instead of
one to one or one to many.
8. What is it called where child object gets killed if parent object is killed?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
View Answer
Answer: b
Explanation: Composition occurs when child object gets killed if parent object gets
killed. Aggregation is also known as strong Aggregation.
9. What is it called where object has its own lifecycle and child object cannot belong
to another parent object?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
View Answer
Answer: a
Explanation: Aggregation occurs when objects have their own life cycle and child
object can associate with only one parent object.
8. We can’t create an instance of ___________.
A Nested class
B Parent class
C Abstract class
D Anonymous class
Answer
C
We can’t create an instance of Abstract class.
9. Constructor can return a value ___________.
A True
B False
Answer
B
False, constructor can’t return anything.
10. OOPs is invented by _____________.
A James Gosling
B Rasmus Lerdorf
C Alan Kay
D Tim Berners-Lee
Answer
C
Alan Kay is the founder of OOPs.
11. Which Feature of OOP boost the code reusability?
A Encapsulation
B Polymorphism
C Inheritance
D Abstraction
Answer
C
Inheritance boost the code reusability.
12. Which of the following syntax used to create an object
of Class in Java?
A
Classname obj = new() Classname()
B
Classname obj = new Classname;
C
Classname obj = new Classname();
D None of the above
Answer
C
Here is the syntax to create an object of Class in Java:
Classname obj = new Classname();
13. Which is used to create an Abstract class?
A Creating at least one member function as a pure virtual function
B Creating at least one member function as a virtual function
C Declaring as Abstract class using virtual keyword
D Declaring as Abstract class using static keyword
Answer
A
In order to create an Abstract class, you should create at least one member function as a
pure virtual function.
14. What is the output of the following Java code?
class Person
{
private int age;
private Person()
{
age = 24;
}
}
public class Test
{
public static void main(String[] args)
{
Person p = new Person();
System.out.println(p.age);
}
}
A 24
B Compilation error
C Runtime error
D None of the above
Answer
B
A private constructor can not be used to initialize an object out of the class that it is
defined within due to it’s no longer accessible to the external class. Here is the output of
the above code:
$javac Test.java
System.out.println(p.age);
^
2 errors
15. Which of the following is common class for exception
handling?
A Try
B Object
C Exceptions
D Errors
Answer
C
Exceptions is a common class for exception handling in Java.
16. What is the output of the following Java code?
class A
{
int data = 5;
A() {
data = 10;
}
}
public class Test
{
public static void main(String args[])
{
A obj = new A();
System.out.println(obj.data);
}
}
A 5
B 10
C Compilation error
D Runtime error
Answer
B
The values attributed within the constructor overwrite the values initialized with
declaration.
17. Is there any compiler error?
class Point
{
int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public static void main(String args[])
{
Point obj = new Point();
}
}
A True
B False
Answer
A
The main function calls the constructor without paramater, but there is only one
constructor defined in Pont class which takes two parameters.
a) B,E
b) A,C
c) C,E
d) T,H
View Answer
Answer: a
Explanation: If one is extending any class, then they should use extends keyword
not implements.
Advertisement: Join Sanfoundry@Linkedin
1. class A
2. {
3. int i;
4. void display()
5. {
6. System.out.println(i);
7. }
8. }
9. class B extends A
10. {
11. int j;
12. void display()
13. {
14. System.out.println(j);
15. }
16. }
17. class inheritance_demo
18. {
19. public static void main(String args[])
20. {
21. B obj = new B();
22. obj.i=1;
23. obj.j=2;
24. obj.display();
25. }
26. }
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 inheritance_demo.java
$ java inheritance_demo
2
1. class A
2. {
3. int i;
4. }
5. class B extends A
6. {
7. int j;
8. void display()
9. {
10. super.i = j + 1;
11. System.out.println(j + " " + i);
12. }
13. }
14. class inheritance
15. {
16. public static void main(String args[])
17. {
18. B obj = new B();
19. obj.i=1;
20. obj.j=2;
21. obj.display();
22. }
23. }
a) 2 2
b) 3 3
c) 2 3
d) 3 2
View Answer
Answer: c
Explanation: None
output:
$ javac inheritance.java
$ java inheritance
2 3
1. class A
2. {
3. public int i;
4. public int j;
5. A()
6. {
7. i = 1;
8. j = 2;
9. }
10. }
11. class B extends A
12. {
13. int a;
14. B()
15. {
16. super();
17. }
18. }
19. class super_use
20. {
21. public static void main(String args[])
22. {
23. B obj = new B();
24. System.out.println(obj.i + " " + obj.j)
25. }
26. }
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
View Answer
Answer: a
Explanation: Keyword super is used to call constructor of class A by constructor of
class B. Constructor of a initializes i & j to 1 & 2 respectively.
output:
$ javac super_use.java
$ java super_use
1 2
5. If super class and subclass have same variable name, which keyword should be
used to use super class?
a) super
b) this
c) upper
d) classname
View Answer
Answer: a
Explanation: Super keyword is used to access hidden super class variable in
subclass.
Advertisement: Join Sanfoundry@Linkedin
9. What would be the result if a class extends two interfaces and both have a
method with same name and signature? Lets assume that the class is not
implementing that method.
a) Runtime error
b) Compile time error
c) Code runs successfully
d) First called method is executed successfully
View Answer
Answer: b
Explanation: In case of such conflict, compiler will not be able to link a method call
due to ambiguity. It will throw compile time error.