1.
HAS-A relationships are based on inheritance, rather than usage.
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.
2.
Array or collection of superclass references can be used to
access a mixture of superclass and subclass objects.
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.
3.
Aggregation is a special form of association.
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.
4.
An interface cannot have an inner class.
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.
5.
Method overloading is done during _______.
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.
6.
Ad hoc polymorphism is ____________.
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.
7.
The inheriting class cannot override the definition
of existing methods by providing its own implementation.
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.
8.
The two most common reasons to use inheritance are( choose 2)
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.
9.
The benefits of the Object Orientation are: (choose two)
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.
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)
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
11.
The methods in class object are (choose four)
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.
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)
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.
13.
At run-time, a Java program is nothing
more than objects ‘talking’ to ___________.
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.
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?
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.
15.
The relation between the Car and Owner or
BankAccount and Customer is an example of
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.
16.
Subclassing polymorphism is sometimes
called “true polymorphism”.
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).
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);
}
}
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.
18.
Polymorphism is one interface with __________.
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.
19.
Interfaces are fast as they require extra indirection
to find the corresponding method in the actual class.
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.
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();
}
}
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.
21.
Examples of class are( choose 3)
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.
22.
In OO, the concept of IS-A is based on
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.