Java Oops Quiz

Reviewed by Godwin Iheuwa
Godwin Iheuwa, MS, Computer Science |
Computer Expert
Review Board Member
Godwin is a proficient Database Administrator currently employed at MTN Nigeria. He holds as MS in Computer Science from the University of Bedfordshire, where he specialized in Agile Methodologies and Database Administration. He also earned a Bachelor's degree in Computer Science from the University of Port Harcourt. With expertise in SQL Server Integration Services (SSIS) and SQL Server Management Studio, Godwin's knowledge and experience enhance the authority of our quizzes, ensuring accuracy and relevance in the realm of computer science.
, MS, Computer Science
Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Srividya A
S
Srividya A, Software Engineer
Srividya A G is a software engineer who enjoying taking and creating quizzes on various topics.
Quizzes Created: 2 | Total Attempts: 40,908
Questions: 22 | Attempts: 40,864

SettingsSettingsSettings
Java Oops Quiz - Quiz

Welcome to our Java OOPs Quiz, where you'll embark on an enriching journey into the world of Object-Oriented Programming (OOP) with Java. This comprehensive quiz is designed to test your understanding of essential OOP concepts and principles, helping you sharpen your programming skills and deepen your knowledge of Java.

Explore the core concepts of OOP, including classes, objects, inheritance, polymorphism, and encapsulation. Challenge yourself with a series of thought-provoking questions that cover various aspects of Java OOP, from basic syntax to advanced techniques. This quiz offers an invaluable opportunity to expand your knowledge and improve your programming abilities.


Java OOPs Questions and Answers

  • 1. 

    HAS-A relationships are based on inheritance, rather than usage.  

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The given statement is false because HAS-A relationships are based on usage, not inheritance. In object-oriented programming, the HAS-A relationship represents a relationship between two classes where one class has a reference to another class as a member variable. This is also known as composition or aggregation. Inheritance, on the other hand, represents an "is-a" relationship between classes, where one class inherits the properties and behaviors of another class.

    Rate this question:

  • 2. 

      Array or collection of superclass references can be used to access a mixture of superclass and subclass objects.  

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    An array or collection of superclass references can be used to access a mixture of superclass and subclass objects because the superclass is a general representation of its subclasses. This allows for polymorphism, where the superclass references can be used to access the common methods and attributes shared by both the superclass and subclass objects. This provides flexibility and allows for code reuse, as the same array or collection can be used to work with different types of objects as long as they are subclasses of the superclass.

    Rate this question:

  • 3. 

    Aggregation is a special form of association.  

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Aggregation is a special form of association where one class is composed of multiple instances of another class. In aggregation, the objects have a "has-a" relationship, where one object is a part or component of another object. This is different from regular association, where objects have a "knows-a" relationship. Therefore, the statement that "Aggregation is a special form of association" is true.

    Rate this question:

  • 4. 

      An interface cannot have an inner class.  

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    False. An interface can have an inner class. Inner classes are used to provide a way to logically group classes that are only used in one place. They increase encapsulation and can make code more readable and maintainable. Therefore, it is possible for an interface to have an inner class.

    Rate this question:

  • 5. 

    Method overloading is done during _______.

    • A.

      Runtime

    • B.

      Dynamic binding

    • C.

      Program compilation

    • D.

      Late binding

    Correct Answer
    C. Program compilation
    Explanation
    Method overloading is done during program compilation. During compilation, the compiler checks the number, order, and types of arguments in the method calls and matches them with the available overloaded methods. This allows the compiler to determine the appropriate method to execute based on the arguments provided. Method overloading is resolved at compile-time, ensuring that the correct method is called when the program is executed.

    Rate this question:

  • 6. 

    Ad hoc polymorphism is ____________.

    • A.

      Method Overloading

    • B.

      Method Overriding

    • C.

      Subclassing polymorphism

    • D.

      Dynamic binding

    Correct Answer
    A. Method Overloading
    Explanation
    Ad hoc polymorphism refers to the ability of a programming language to perform different operations on different types of data, depending on the context. It allows multiple methods with the same name but different parameters to be defined. This is known as method overloading, where different versions of a method are created to handle different types or numbers of arguments. Therefore, the correct answer to the question is method overloading.

    Rate this question:

  • 7. 

    The inheriting class cannot override the definition of existing methods by providing its own implementation.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The given statement is false. In an inheritance hierarchy, the inheriting class has the ability to override the definition of existing methods by providing its own implementation. This is one of the key features of inheritance, as it allows for customization and specialization of behavior in derived classes.

    Rate this question:

  • 8. 

    The two most common reasons to use inheritance are( choose 2)

    • A.

      To promote code reuse

    • B.

      To use abstraction

    • C.

      To use interface

    • D.

      To use polymorphism

    Correct Answer(s)
    A. To promote code reuse
    D. To use polymorphism
    Explanation
    Inheritance is commonly used to promote code reuse as it allows classes to inherit properties and methods from a parent class, reducing the need for duplicating code. It also enables polymorphism, which allows objects of different classes to be treated as objects of the same parent class, providing flexibility and extensibility in the code.

    Rate this question:

  • 9. 

    The benefits of the Object Orientation are: (choose two)

    • A.

      Inheritance

    • B.

      Flexibility

    • C.

      Maintainability

    • D.

      Polymorphism

    Correct Answer(s)
    A. Inheritance
    D. Polymorphism
    Explanation
    Object orientation offers several benefits, two of which are inheritance and polymorphism.

    Inheritance: Inheritance allows a class to inherit attributes and methods from another class, promoting code reusability and facilitating hierarchical relationships between classes.

    Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling flexibility and dynamic behavior in method invocation.

    While flexibility and maintainability are indeed benefits of object orientation, the question specifically asks for two choices, and inheritance and polymorphism are two primary features directly associated with object-oriented programming.

    Rate this question:

  • 10. 

    Below is the sample code :   1   class Hotel { 2   public int bookings; 3   public void book() { 4   bookings++; 5   } 6   } 7   public class SuperHotel extends Hotel { 8   public void book() { 9   bookings--; 10  } 11  public void book(int size) { 12  book(); 13  super.book(); 14  bookings += size; 15  } 16  public static void main(String args[]) { 17  Hotel hotel = new Hotel(); 18  hotel.book(2); 19  System.out.print(hotel.bookings); 20  }}   How can we correct the above code? (choose all that apply)  

    • A.

      By adding argument "int size" to the method book at line number 3.

    • B.

      By removing argument '2' at line number 18.

    • C.

      By creating object of "SuperHotel" subclass at line 17 & calling book(2) from it at line 18

    • D.

      No correction needed.

    Correct Answer
    A. By adding argument "int size" to the method book at line number 3.
    Explanation
    By adding the argument "int size" to the method book at line number 3.
    Explanation: Since the SuperHotel class has a method book(int size), the base class (Hotel) should also have a compatible method with the same signature

    Rate this question:

  • 11. 

     The methods in class object are (choose four)

    • A.

      Clone

    • B.

      Notify

    • C.

      Concat

    • D.

      Wait

    • E.

      Equals

    • F.

      Compare

    Correct Answer(s)
    A. Clone
    B. Notify
    D. Wait
    E. Equals
    Explanation
    The correct answer is clone, notify, wait, and equals. These four methods are part of the class object. The clone method is used to create a copy of an object. The notify method is used to wake up a single thread that is waiting on the object's monitor. The wait method is used to make a thread wait until another thread notifies it. The equals method is used to compare two objects for equality. The concat method, although listed as an option, is not part of the class object.

    Rate this question:

  • 12. 

    Given the following sample code:   public class Example5{       public float Twin(float a, float b) {...         } public float Twin(float a1, float b1) { ...}           }   How can we correct the above code ?(choose two)  

    • A.

      By placing overriding method into subclass.

    • B.

      By changing the name of the class.

    • C.

      By replacing overloading from overriding.

    • D.

      By changing the name of the arguments.

    Correct Answer(s)
    A. By placing overriding method into subclass.
    D. By changing the name of the arguments.
    Explanation
    The code you provided doesn't show any inheritance or subclass relationship, but the method names (Twin) and the attempt to override suggest that this might be intended for a subclass. In Java, when you want to override a method in a subclass, you need to ensure that the method in the subclass has the same signature (method name, return type, and parameter types) as the method in the superclass. This allows for polymorphism, where you can use the subclass reference to access overridden methods.
    D. By changing the name of the arguments:
    In Java, method overloading is allowed, where you can define multiple methods in the same class with the same name but different parameter lists. In the given code, the two methods have the same name (Twin) but the same parameter types (float). To overload the methods, you need to change the parameter types or the number of parameters. Changing the name of the arguments is one way to achieve this.
    In summary, A is related to the concept of method overriding in a subclass, and D is related to the concept of method overloading by changing the name of the arguments.

    Rate this question:

  • 13. 

    At run-time, a Java program is nothing more than objects ‘talking’ to ___________.

    • A.

      Other objects

    • B.

      Other methods

    • C.

      Other classes

    • D.

      Other binders

    Correct Answer
    A. Other objects
    Explanation
    At runtime, a Java program consists of objects communicating with each other. Objects are instances of classes and they interact with each other by invoking methods and exchanging messages. This allows for the flow of data and execution of operations between different objects, enabling the program to perform its intended functionality.

    Rate this question:

  • 14. 

    If you don't have the access to the source code for a class, but you want to change the way a method of that class works, then could you use subclassing to do that that is to extend the "bad" class and override the method with your own better code? 

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Subclassing allows you to create a new class that inherits the properties and methods of the existing class. By subclassing the "bad" class, you can create a new class that extends its functionality and overrides the method with your own improved code. This way, you can change the way the method works without having access to the source code of the original class. Therefore, the given answer "True" is correct.

    Rate this question:

  • 15. 

       The relation between the Car and Owner or BankAccount and Customer is an example of  

    • A.

      Aggregation

    • B.

      Composition

    • C.

      Association

    • D.

      None

    Correct Answer
    C. Association
    Explanation
    The relationship between Car and Owner or bank account and Customer is an example of association. The association represents a relationship between two or more classes, where each class has its independent existence and can exist without the other. In this case, a car can have an owner, and a bank account can have a customer, but both the car and the bank account can exist independently of their respective owners or customers.

    Rate this question:

  • 16. 

     Subclassing polymorphism is sometimes called “true polymorphism”.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    Subclassing polymorphism is often referred to as "subtype polymorphism" or "inheritance polymorphism." "True polymorphism" typically refers to the more general concept of polymorphism, which can be achieved through various mechanisms in programming languages, not just through subclassing. It encompasses not only inheritance polymorphism but also parametric polymorphism (generics) and ad-hoc polymorphism (overloading).

    Rate this question:

  • 17. 

    Consider the below code and choose the correct output. public class Main { public int a; public long b; public void test(long  b) { System.out.println("long b"); } public void test(int a) { System.out.println("int a"); } public static void main(String[] args) {                 Main e=new Main();         e.test(9*1000000000);               }   }  

    • A.

      Int a

    • B.

      Long b

    • C.

      Long a

    • D.

      Error

    Correct Answer
    B. Long b
    Explanation
    In the main method, an instance of the Main class is created, and the test method is called with the argument 9*1000000000. Since this value exceeds the range of an int, it is treated as a long, and the test method with the long parameter is invoked, printing "long b" to the console.

    Rate this question:

  • 18. 

    Polymorphism is one interface with __________.

    • A.

      Multiple methods

    • B.

      Single  method

    • C.

      Multiple record

    • D.

      Single record

    Correct Answer
    A. Multiple methods
    Explanation
    Polymorphism is one interface with multiple methods. It is a fundamental concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. In the context of this question, an interface is a contract that defines a set of methods that implementing classes must provide. Polymorphism enables an object to invoke the appropriate method based on the actual object's class, even if the specific class is unknown at compile-time. This is achieved by having multiple methods with the same name but different implementations in different classes, which is why polymorphism is associated with multiple methods.

    Rate this question:

  • 19. 

    Interfaces are fast as they require extra indirection to find the corresponding method in the actual class.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    Interfaces are not necessarily faster than classes because they require extra indirection to find the corresponding method in the actual class.

    Rate this question:

  • 20. 

    Consider the code below and choose the correct option. class GameShape { public void displayShape() { System.out.println("displaying shape");    }    // more code }   class PlayerPiece extends GameShape { public void movePiece() { System.out.println("moving game piece");    }    // more code }   public class TestShapes {    public static void main (String[] args) {       PlayerPiece shape = new PlayerPiece();       shape.displayShape();       shape.movePiece();    } }  

    • A.

      PlayingPiece class inherits the generic movePiece() method

    • B.

      PlayingPiece class inherits the generic displayShape() method

    • C.

      GameShape class inherits the generic displayShape() method

    • D.

      GameShape class inherits the generic movePiece() method

    Correct Answer
    B. PlayingPiece class inherits the generic displayShape() method
    Explanation
    The correct answer is that the PlayerPiece class inherits the generic displayShape() method. This is because the PlayerPiece class is a subclass of the GameShape class, and when a subclass inherits from a superclass, it also inherits all of its methods. In this case, the displayShape() method is defined in the GameShape class and is accessible to the PlayerPiece class.

    Rate this question:

  • 21. 

    Examples of class are( choose 3)

    • A.

      White

    • B.

      Length

    • C.

      Classroom

    • D.

      Car

    • E.

      Person

    Correct Answer(s)
    B. Length
    C. Classroom
    E. Person
    Explanation
    In this context, a class can be understood as a category or group of objects or entities that share common characteristics or properties. Each of these selected items represents a specific group or type within their respective contexts, such as a classroom within a school or educational setting, a car as a type of vehicle, and a person as an individual human being.

    Rate this question:

  • 22. 

    In OO, the concept of IS-A is based on  

    • A.

      Class inheritance

    • B.

      Interface implementation.

    • C.

      Both

    • D.

      None

    Correct Answer
    A. Class inheritance
    Explanation
    In object-oriented programming (OO), the concept of "IS-A" is based on class inheritance. The "IS-A" relationship signifies that a subclass is a type of its superclass. It is a fundamental principle of object-oriented design, emphasizing the relationship between classes in terms of specialization and generalization. Class inheritance allows a subclass to inherit properties and behaviors from its superclass.

    Rate this question:

Godwin Iheuwa |MS, Computer Science |
Computer Expert
Godwin is a proficient Database Administrator currently employed at MTN Nigeria. He holds as MS in Computer Science from the University of Bedfordshire, where he specialized in Agile Methodologies and Database Administration. He also earned a Bachelor's degree in Computer Science from the University of Port Harcourt. With expertise in SQL Server Integration Services (SSIS) and SQL Server Management Studio, Godwin's knowledge and experience enhance the authority of our quizzes, ensuring accuracy and relevance in the realm of computer science.

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Apr 01, 2024
    Quiz Edited by
    ProProfs Editorial Team

    Expert Reviewed by
    Godwin Iheuwa
  • Dec 02, 2011
    Quiz Created by
    Srividya A
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.