MCQ

Java MCQ – Multiple Choice Questions and Answers – OOPs

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.
 

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

C
Enumerated type and Structure is not related to OOPS.
 

2. Can we overload constructor in derived class?

A Yes

B No

B
No, we cannot overload constructor in derived class.
 

3. Which is an abstract data type?

A Double

B String

C Enum

D Class

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

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

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

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

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

C
We can’t create an instance of Abstract class.
 

9. Constructor can return a value ___________.

A True

B False

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

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

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

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

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

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

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

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

A
The main function calls the constructor without paramater, but there is only one constructor defined in Pont class which takes two parameters.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *