0% found this document useful (0 votes)
337 views16 pages

All MCQ

The document contains a set of multiple choice questions about object-oriented programming concepts in Java. It includes 10 questions about concepts like inheritance, encapsulation, polymorphism, abstraction, and the differences between overloading and overriding. The questions are in a quiz format with answers that explain the reasoning.

Uploaded by

sfdsf fsd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
337 views16 pages

All MCQ

The document contains a set of multiple choice questions about object-oriented programming concepts in Java. It includes 10 questions about concepts like inheritance, encapsulation, polymorphism, abstraction, and the differences between overloading and overriding. The questions are in a quiz format with answers that explain the reasoning.

Uploaded by

sfdsf fsd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Oops

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Concepts
of OOPs”.

1. Which of the following is not OOPS concept in Java?


a) Inheritance
b) Encapsulation
c) Polymorphism
d) Compilation
View Answer
Answer: d
Explanation: There are 4 OOPS concepts in Java. Inheritance, Encapsulation,
Polymorphism and Abstraction.

2. Which of the following is a type of polymorphism in Java?


a) Compile time polymorphism
b) Execution time polymorphism
c) Multiple polymorphism
d) Multilevel polymorphism
View Answer
Answer: a
Explanation: There are two types of polymorphism in Java. Compile time
polymorphism (overloading) and runtime polymorphism (overriding).

3. When does method overloading is determined?


a) At run time
b) At compile time
c) At coding time
d) At execution time
View Answer
Answer: b
Explanation: Overloading is determined at compile time. Hence, it is also known as
compile time polymorphism.

4. When Overloading does not occur?


a) More than one method with same name but different method signature and
different number or type of parameters
b) More than one method with same name, same signature but different number of
signature
c) More than one method with same name, same signature, same number of
parameters but different type
d) More than one method with same name, same number of parameters and type
but different signature
View Answer
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.
Advertisement: Join Sanfoundry@Linkedin
Pause
Unmute
Loaded: 31.92%
Fullscreen
5. Which concept of Java is a way of converting real world objects in terms of class?
a) Polymorphism
b) Encapsulation
c) Abstraction
d) Inheritance
View Answer
Answer: c
Explanation: Abstraction is the concept of defining real world objects in terms of
classes or interfaces.

6. Which concept of Java is achieved by combining methods and attribute into a


class?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
View Answer
Answer: a
Explanation: Encapsulation is implemented by combining methods and attribute
into a class. The class acts like a container of encapsulating properties.

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.

10. Method overriding is combination of inheritance and polymorphism?


a) True
b) false
View Answer
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.
1. Which of the following is not relevant to OOPS?
A Object and Class
B Encapsulation and Inheritance
C Enumerated type and Structure
D Constructor and Method
Answer
 C
Enumerated type and Structure is not related to OOPS.
 
2. Can we overload constructor in derived class?
A Yes
B No
Answer
 B
No, we cannot overload constructor in derived class.
 
3. Which is an abstract data type?
A Double
B String
C Enum
D Class
Answer
 D
Class is an abstract data type.
 
4. Which keyword is used to inherit a class in Java?
A inherit
B implement
C extend
D extends
Answer
 D
“extends” is used to inherit a class in Java.
 
5. A private member of a class is accessible to
________________.
A only members of the same class
B members to the same package
C in subclass
D everywhere
Answer
 A
A private member of a class is accessible to only members of the same class.
 
6. In OOPs in Java, private, public & protected are
________________.
A Interfaces
B Classes
C Method signature
D Access Modifiers
Answer
 D
Private, public & protected are Access Modifiers in Java OOPs.
 
7. Which doesn’t have a body?
A Class
B Abstract Method
C Method
D Interface
Answer
 B
An Abstract Method doesn’t have a body.
public abstract int my_method(int a, int b);
As you can see this method has no body.

 
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?

Classname obj = new() Classname()

Classname obj = new Classname;

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

Test.java:16: error: Person() has private access in Person

Person p = new Person();

Test.java:17: error: age has private access in Person

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.

1. Which of this keyword must be used to inherit a class?


a) super
b) this
c) extent
d) extends
View Answer
Answer: d
Explanation: None.
2. A class member declared protected becomes a member of subclass of which
type?
a) public member
b) private member
c) protected member
d) static member
View Answer
Answer: b
Explanation: A class member declared protected becomes a private member of
subclass.

3. Which of these is correct way of inheriting class A by class B?


a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}
View Answer
Answer: c
Explanation: None.

4. 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) 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

5. What will be the output of the following Java program?

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

6. What will be the output of the following Java program?

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

7. What will be the output of the following Java program?

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

1. What is not type of inheritance?


a) Single inheritance
b) Double inheritance
c) Hierarchical inheritance
d) Multiple inheritance
View Answer
Answer: b
Explanation: Inheritance is way of acquiring attributes and methods of parent class.
Java supports hierarchical inheritance directly.

2. Using which of the following, multiple inheritance in Java can be implemented?


a) Interfaces
b) Multithreading
c) Protected methods
d) Private methods
View Answer
Answer: a
Explanation: Multiple inheritance in java is implemented using interfaces. Multiple
interfaces can be implemented by a class.
3. All classes in Java are inherited from which class?
a) java.lang.class
b) java.class.inherited
c) java.class.object
d) java.lang.Object
View Answer
Answer: d
Explanation: All classes in java are inherited from Object class. Interfaces are not
inherited from Object Class.

4. In order to restrict a variable of a class from inheriting to subclass, how variable


should be declared?
a) Protected
b) Private
c) Public
d) Static
View Answer
Answer: b
Explanation: By declaring variable private, the variable will not be available in
inherited to subclass.

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

6. Static members are not inherited to subclass.


a) True
b) False
View Answer
Answer: b
Explanation: Static members are also inherited to subclasses.

7. Which of the following is used for implementing inheritance through an interface?


a) inherited
b) using
c) extends
d) implements
View Answer
Answer: d
Explanation: Interface is implemented using implements keyword. A concrete class
must implement all the methods of an interface, else it must be declared abstract.

8. Which of the following is used for implementing inheritance through class?


a) inherited
b) using
c) extends
d) implements
View Answer
Answer: c
Explanation: Class can be extended using extends keyword. One class can extend
only one class. A final class cannot be extended.

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.

10. Does Java support multiple level inheritance?


a) True
b) False
View Answer
Answer: a
Explanation: Java supports multiple level inheritance through implementing multiple
interfaces.

You might also like