Java Questions and Answers Part-2

1. Which of the following is a type of polymorphism in Java?
a) Compile time polymorphism
b) Multiple polymorphism
c) Execution time polymorphism
d) Multilevel polymorphism

Answer: a
Explanation: There are two types of polymorphism in Java - 1) Compile time polymorphism (overloading) and 2) runtime polymorphism (overriding).

2. What is it called where child object gets killed if parent object is killed?
a) Aggregation
b) Encapsulation
c) Composition
d) Association

Answer: c
Explanation: Composition occurs when child object gets killed if parent object gets killed.

3. Which concept of Java is a way of converting real world objects in terms of class?
a) Polymorphism
b) Abstraction
c) Inheritance
d) Encapsulation

Answer: b
Explanation: Abstraction is the concept of defining real world objects in terms of classes or interfaces.

4. How can we identify whether a compilation unit is class or interface from a .class file?
a) Java source file header
b) Extension of compilation unit
c) The class or interface name should be postfixed with unit type
d) We cannot differentiate between class and interface

Answer: a
Explanation: The Java source file contains a header that declares the type of class or interface, its visibility with respect to other classes, its name and any superclass it may extend, or interface it implements.

5. Which of the following is not OOPS concept in Java?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Compilation

Answer: d
Explanation: There are 4 OOPS concepts in Java i.e Inheritance, Encapsulation, Polymorphism and Abstraction.

6. When Overloading does not occur?
a) More than one method with same name, same signature, same number of parameters but different type
b) More than one method with same name but different method signature and different number or type of parameters
c) More than one method with same name, same signature but different number of signature
d) More than one method with same name, same number of parameters and type but different signature

Answer: d
Explanation: Overloading occurs when more than one method with same name but different constructor and also when same signature but different number of parameters and/or parameter type.

7. Method overriding is combination of inheritance and polymorphism?
a) True
b) false

Answer: a
Explanation: In order for method overriding, method with same signature in both superclass and subclass is required with same signature. That satisfies both concepts inheritance and polymorphism.

8. What is it called if an object has its own lifecycle and there is no owner?
a) Composition
b) Encapsulation
c) Association
d) Aggregation

Answer: c
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.

9. When does method overloading is determined?
a) At run time
b) At compile time
c) At execution time
d) At coding time

Answer: b
Explanation: Overloading is determined at compile time. Hence, it is also known as compile time polymorphism.

10. What is it called where object has its own lifecycle and child object cannot belong to another parent object?
a) Composition
b) Association
c) Encapsulation
d) Aggregation

Answer: d
Explanation: Aggregation occurs when objects have their own life cycle and child object can associate with only one parent object.