1.
Polymorphism enables you to:
Correct Answer
A. Program in the general.
Explanation
Polymorphism enables you to program in the general by allowing you to write code that can work with objects of different classes, as long as they share a common interface or superclass. This means that you can write code that is more flexible and reusable, as it can handle different types of objects without needing to know their specific implementation details.
2.
Polymorphism specifically enables the creation of programs that handle:
Correct Answer
C. A wide variety of classes in a general manner
Explanation
Polymorphism allows the creation of programs that can work with different classes in a general manner. It enables the use of a single interface to represent multiple classes, allowing objects of different classes to be treated uniformly. This flexibility is particularly useful when dealing with different types of objects that share common behavior or attributes. It simplifies code implementation and maintenance, as it allows for code reuse and promotes modularity. Therefore, the correct answer is that polymorphism enables the handling of a wide variety of classes in a general manner.
3.
When used correctly, polymorphism will never require changes to be made to any part of the program.
Correct Answer
B. False
Explanation
False. Parts of a program that need modification are those that require direct knowledge of the particular class that's added to the hierarchy.
4.
Polymorphism enables objects of different classes that are related by a class hierarchy to be processed generically.
Correct Answer
A. True
Explanation
Polymorphism allows objects of different classes that are connected through a class hierarchy to be treated uniformly. This means that even though these objects have different types, they can be processed in a generic manner using a common interface or superclass. This flexibility in handling different objects simplifies code and promotes reusability. Therefore, the statement is true.
5.
Polymorphism allows for specifics to be dealt with during:
Correct Answer
A. Execution
Explanation
Polymorphism allows for specifics to be dealt with during execution. This means that the behavior of an object can be determined at runtime based on its specific type. During execution, the appropriate method or implementation will be chosen based on the actual type of the object, allowing for flexibility and dynamic behavior. Polymorphism is a key feature in object-oriented programming languages that enables code reusability and flexibility.
6.
Polymorphism allows the addition of classes providing they were at least considered during program development.
Correct Answer
B. False
Explanation
False. Polymorphism can be used to include classes that were not even envisioned during program development.
7.
Polymorphism allows you to command a wide variety of objects even if you do not know the objects' types.
Correct Answer
A. True
Explanation
Polymorphism is a concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. This means that even if you do not know the specific type of an object, you can still perform operations on it using methods defined in the superclass. This flexibility enables you to write code that can work with a wide variety of objects, making it easier to create reusable and adaptable code. Therefore, the statement that polymorphism allows you to command a wide variety of objects even if you do not know their types is true.
8.
Which statement best describes the relationship between the base class and derived class types?
Correct Answer
D. A derived class reference can be assigned to a base class variable, but a base class reference cannot be assigned to a derived class variable.
Explanation
A derived class reference can be assigned to a base class variable because a derived class is a specialization of the base class and contains all the properties and methods of the base class. However, a base class reference cannot be assigned to a derived class variable because a base class does not have all the properties and methods of the derived class. The derived class may have additional features that are not present in the base class.
9.
If a derived class reference is assigned to a base class variable, the variable must be cast back to the derived class before any derived class methods can be called with it.
Correct Answer
A. True
Explanation
When a derived class reference is assigned to a base class variable, the base class variable only has access to the methods and properties of the base class. In order to call any methods specific to the derived class, the base class variable must be cast back to the derived class. This allows the variable to regain its original derived class type and access the additional methods and properties that are specific to the derived class. Therefore, the statement is true.
10.
The abstract methods and properties of a class do not provide an implementation.
Correct Answer
A. True
Explanation
Abstract methods and properties in a class are declared without any implementation. They serve as placeholders for methods and properties that must be implemented in any class that inherits from the abstract class. This allows the abstract class to define a common interface that all its subclasses must adhere to, while leaving the specific implementation details to the subclasses. Therefore, the statement that abstract methods and properties do not provide an implementation is true.
11.
You may define implementations for abstract methods to act as default implementations.
Correct Answer
B. False
Explanation
False. Attempting to provide an implementation for an abstract method is a syntax error.
12.
An abstract base class can be used to declare references.
Correct Answer
A. True
Explanation
An abstract base class can be used to declare references because it provides a common interface for its derived classes. This allows objects of different derived classes to be referenced using a pointer or reference of the abstract base class type. By declaring a reference to the abstract base class, we can access the common functionality defined in the base class, while still being able to use specific functionality implemented in the derived classes. This helps in achieving polymorphism and code reusability.
13.
An abstract class may be derived from another abstract class.
Correct Answer
A. True
Explanation
An abstract class can indeed be derived from another abstract class. This is because abstract classes are designed to be extended and serve as a base for other classes. By deriving one abstract class from another, we can further define and specialize the behavior and attributes of the derived class while still maintaining the abstract nature of the base class. This allows for a hierarchical structure in the design of classes, where each level of abstraction adds more specific functionality.
14.
If a method needs to be called polymorphically, what type of reference should be used to point to the object that the method is being called with?
Correct Answer
A. A reference of the base class that defines the behavior of the object
Explanation
When a method needs to be called polymorphically, it means that the method can be overridden by a subclass. In order to achieve this, a reference of the base class that defines the behavior of the object should be used to point to the object. This allows the method to be called based on the actual type of the object at runtime, rather than the type of the reference. By using a reference of the base class, we ensure that the correct version of the method is called, even if the object is of a subclass type.
15.
Select the best true statement:
Correct Answer
D. Both B and C are correct statements.
Explanation
Both B and C are correct statements. Polymorphism is indicated by the use of the keyword virtual in a method signature, which allows a derived class to override the implementation of the base class method. Polymorphism is also indicated by the use of the keyword override in a method signature, which explicitly states that the method is intended to override a base class method. These keywords enable the flexibility and extensibility of object-oriented programming, allowing for dynamic method dispatch and the ability to treat objects of different classes as instances of a common base class.
16.
Which declaration declares abstract method method1 in abstract class Class1 (method1 returns an int and takes no arguments)?
Correct Answer
C. Public abstract int method1();
Explanation
The correct answer is "public abstract int method1();". This declaration correctly declares an abstract method named method1 in an abstract class named Class1. The method is declared with a return type of int and takes no arguments. The keywords "public" and "abstract" are used to specify the access level and the abstract nature of the method, respectively.
17.
Consider the abstract class below:
public abstract class Foo
{
private int a;
public int b;
public Foo( int aVal, int bVal ) {
a = aVal;
b = bVal;
} // end Foo constructor public abstract int calculate();
} // end class Foo
Any concrete subclass that extends class Foo:
Correct Answer
D. All of the above.
Explanation
The correct answer is "All of the above." Any concrete subclass that extends class Foo must implement a method called calculate. Additionally, the instance variable "a" is private, meaning it cannot be accessed by subclasses. Lastly, since Foo is an abstract class, it cannot be instantiated directly. Therefore, all of the given statements are true.
18.
Assigning a derived class reference to a base class variable is safe:
Correct Answer
B. Because the derived class is an object of its base class.
Explanation
Assigning a derived class reference to a base class variable is safe because the derived class is an object of its base class. This means that the derived class inherits all the properties and behaviors of the base class. Therefore, assigning a derived class reference to a base class variable allows us to access the base class's members through the derived class reference, ensuring that no data or functionality is lost in the process.
19.
Attempting to instantiate an object of an abstract class is a logic error.
Correct Answer
B. False
Explanation
False. This is a compilation error.
20.
The keyword sealed is applied to methods and classes to:
Correct Answer
A. Prevent overriding and inheritance
Explanation
The keyword sealed is used to prevent overriding and inheritance in methods and classes. By applying the sealed keyword, we ensure that a method or class cannot be overridden by any derived class, and it cannot be inherited by any other class. This helps in maintaining the integrity and stability of the code, as it prevents unintended modifications or extensions of the sealed elements.
21.
Classes and methods are declared sealed for all of the following reasons, except:
Correct Answer
C. Sealed methods are static.
Explanation
Sealed methods are not necessarily static. The purpose of declaring a method as sealed is to prevent further inheritance and overriding of that method in derived classes. Sealed methods can still be instance methods and can be called on objects of the class. On the other hand, static methods belong to the class itself and can be called without creating an instance of the class. Therefore, the statement that sealed methods are static is incorrect.
22.
Declaring a method sealed means:
Correct Answer
D. It cannot be overridden
Explanation
When a method is declared as sealed, it means that it cannot be overridden by any derived class. This is useful in situations where the base class wants to prevent any modifications or extensions to a particular method implementation. By sealing the method, the base class ensures that it retains control over the behavior of the method and prevents any changes that could potentially break its functionality. This helps in maintaining the integrity and consistency of the code.
23.
Sealing methods allows the compiler to optimize the program by "inlining code."
Correct Answer
A. True
Explanation
Sealing methods in programming refers to the practice of marking a class or method as "final" to prevent it from being overridden or extended by other classes. By sealing a method, the compiler can determine at compile-time that the method will not be overridden, allowing it to optimize the program by inlining the code. This means that the compiler can replace the method call with the actual code of the method, eliminating the overhead of a function call. Therefore, the statement "Sealing methods allows the compiler to optimize the program by 'inlining code'" is true.
24.
A(n) ________ is best used for providing services that bring together objects of otherwise unrelated classes.
Correct Answer
C. Interface
Explanation
An interface is best used for providing services that bring together objects of otherwise unrelated classes. This is because an interface allows classes to define a set of methods that they must implement, regardless of their class hierarchy. By implementing the same interface, objects of different classes can be treated uniformly and interact with each other through the common interface methods. This promotes loose coupling and flexibility in the design, allowing for greater reusability and extensibility of code.
25.
The purpose of an interface is to:
Correct Answer
B. Provide different types of objects with the comparable functionality, even though each will implement the functionality differently
Explanation
An interface in programming is a way to define a contract or a set of rules that a class must follow. It allows different types of objects to have the same functionality, even though each object may implement that functionality differently. This means that objects of different classes can be treated interchangeably as long as they adhere to the interface. Therefore, the purpose of an interface is to provide different types of objects with comparable functionality, regardless of how they implement it.