1.
10.1 Q1: Polymorphism enables you to:
Correct Answer
A. A. program in the general.
Explanation
Polymorphism allows you to program in the general, meaning that you can write code that can work with objects of different types without needing to know the specific type at compile time. This allows for code reusability and flexibility, as the same code can be used with different types of objects as long as they adhere to a common interface or superclass. Polymorphism enables you to write more generic and flexible code, making it easier to maintain and extend your programs.
2.
10.1 Q2: Which of the following statements about interfaces is false?
Correct Answer
A. A. An interface describes a set of methods that can be called on an object, providing a default implementation for the methods.
Explanation
Option a is false because an interface does not provide a default implementation for the methods. It only describes the set of methods that should be implemented by the classes that implement the interface. The classes implementing the interface are responsible for providing their own implementation for the methods.
3.
10.2 Q1: For which of the following would polymorphism not provide a clean solution?
Correct Answer
C. C. A program to compute a 5% savings account interest for a variety of clients.
Explanation
Polymorphism allows objects of different classes to be treated as objects of a common superclass. In option c, a program to compute a 5% savings account interest for a variety of clients, there is no need for different classes or different behavior based on the type of client. The calculation of the interest is the same for all clients, so there is no need for polymorphism in this scenario.
4.
10.2 Q2: Polymorphism allows for specifics to be dealt with during:
Correct Answer
A. A. execution.
Explanation
Polymorphism allows for specifics to be dealt with during execution. This means that during the runtime of a program, the appropriate method or behavior is chosen based on the actual type of the object being operated on, rather than the declared type. This allows for flexibility and extensibility in code, as different objects can be treated in a generic way, while still executing specific behaviors based on their individual types.
5.
10.3 Q1: Which statement best describes the relationship between superclass and subclass types?
Correct Answer
D. D. A subclass reference can be assigned to a superclass variable, but a superclass reference cannot be assigned to a subclass variable.
Explanation
In object-oriented programming, a subclass is a specialized version of a superclass. Therefore, a subclass reference can be assigned to a superclass variable because the subclass inherits all the properties and methods of the superclass. However, a superclass reference cannot be assigned to a subclass variable because the subclass may have additional properties and methods that are not present in the superclass. Thus, option d correctly describes the relationship between superclass and subclass types.
6.
10.4 Q1: A(n) class cannot be instantiated.
Correct Answer
C. C. abstract.
Explanation
An abstract class cannot be instantiated because it is meant to be a base class for other classes. It is designed to be inherited by other classes and provides a blueprint for those classes to follow. Abstract classes contain one or more abstract methods, which are meant to be implemented by the subclasses. Therefore, an abstract class cannot be instantiated on its own as it is incomplete and lacks the necessary implementation details for its methods.
7.
10.4 Q2: Non-abstract classes are called:
Correct Answer
D. D. concrete classes.
Explanation
Non-abstract classes are called concrete classes because they provide specific implementations for all their methods and can be instantiated to create objects. Unlike abstract classes, concrete classes can be directly used to create objects and are fully implemented with all their methods defined. They are considered concrete because they provide the actual implementation details and can be used to create instances or objects in a program.
8.
10.5 Q1: It is a UML convention to denote the name of an abstract class in:
Correct Answer
B. B. italics.
Explanation
In UML, the convention to denote the name of an abstract class is to use italics. This helps to visually distinguish abstract classes from concrete classes.
9.
10.5 Q2: If the superclass contains only abstract method declarations, the superclass is used for:
Correct Answer
B. B. interface inheritance.
Explanation
If the superclass contains only abstract method declarations, it means that the superclass is an interface rather than a concrete class. In Java, interfaces are used to define a contract of methods that a class implementing the interface must provide. Therefore, if the superclass contains only abstract method declarations, it is used for interface inheritance, where other classes can implement the interface and provide their own implementation for those abstract methods.
10.
10.5.1 Q1: Which of the following could be used to declare abstract method method1 in abstract class Class1 (method1 returns an int and takes no arguments)?
Correct Answer
C. C. public abstract int method1();
Explanation
The correct answer is c. public abstract int method1(). This is because in an abstract class, abstract methods are declared using the "abstract" keyword. The method1() in this case is abstract because it is declared with the "abstract" keyword and it has no implementation. Additionally, the return type "int" and the absence of any arguments match the requirements stated in the question.
11.
10.5.1 Q2: Which of the following statements about abstract superclasses is true?
Correct Answer
A. A. abstract superclasses may contain data.
Explanation
Abstract superclasses are classes that cannot be instantiated and are meant to be extended by subclasses. They can contain data members, just like any other class. The purpose of an abstract superclass is to provide a blueprint for its subclasses, so it can have data members that are common to all subclasses. The subclasses can then inherit these data members and use them as needed. Therefore, option a is correct.
12.
Consider the abstract superclass 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 FooAny concrete subclass that extends class Foo:
Correct Answer
D. D. Both (a) and (b)
Explanation
The correct answer is d. Both (a) and (b). This is because any concrete subclass that extends class Foo must implement a method called calculate, as stated in the abstract superclass. Additionally, the instance variable "a" is private, so it cannot be accessed by any subclass.
13.
10.5.5 Q1: Consider classes A, B and C, where A is an abstract superclass, B is a concrete class that inherits from A and C is a concrete class that inherits from B. Class A declares abstract method originalMethod, implemented in class B. Which of the following statements is true of class C?
Correct Answer
D. D. None of the above.
14.
10.5.6 Q1: When a superclass variable refers to a subclass object and a method is called on that object, the proper implementation is determined at execution time. What is the process of determining the correct method to call?
Correct Answer
D. D. late binding.
Explanation
The process of determining the correct method to call when a superclass variable refers to a subclass object at execution time is known as late binding. Late binding allows the program to dynamically determine the appropriate implementation of the method based on the actual type of the object being referred to. This allows for flexibility and polymorphism in object-oriented programming.
15.
10.5.6 Q2: Every object in Java knows its own class and can access this information through method .
Correct Answer
A. A. getClass.
Explanation
In Java, every object knows its own class and can access this information through the method "getClass". This method returns a Class object which represents the runtime class of the object. It can be used to retrieve information about the object's class, such as its name, superclass, interfaces, annotations, and more. Therefore, option a, "getClass", is the correct answer.
16.
10.5.7 Q1: Assigning a subclass reference to a superclass variable is safe:
Correct Answer
B. B. because the subclass object is an object of its superclass.
Explanation
Assigning a subclass reference to a superclass variable is safe because the subclass object is an object of its superclass. This means that the subclass inherits all the properties and behaviors of its superclass, so it can be treated as an object of its superclass without any issues.
17.
10.6 Q1: Classes and methods are declared final for all but the following reasons:
Correct Answer
C. C. final methods are static.
Explanation
Final methods and classes prevent further inheritance because they cannot be overridden or extended by subclasses. This is one of the main reasons why classes and methods are declared final. Final methods allow inlining the code, which means that the method code is directly inserted into the calling code, improving performance. However, the statement that final methods are static is incorrect. Final methods can be either static or non-static, but the final keyword is used to prevent the method from being overridden, not to specify whether it is static or not.
18.
10.6 Q2: All of the following methods are implicitly final except:
Correct Answer
A. A. a method in an abstract class.
Explanation
The correct answer is a method in an abstract class. This is because methods in an abstract class are meant to be overridden by the subclasses, so they are not implicitly final. In contrast, private methods, methods declared in a final class, and static methods are implicitly final and cannot be overridden.
19.
10.6 Q3: Declaring a method final means:
Correct Answer
D. D. it cannot be overridden.
Explanation
Declaring a method as final means that it cannot be overridden by any subclass. This is useful when a method in a superclass should not be modified or extended in any way by its subclasses. By declaring a method as final, it ensures that the implementation of the method in the superclass is the final and definitive version that cannot be changed or overridden by any subclass.
20.
10.7 Q1: An interface may contain:
Correct Answer
C. C. public static final data and public abstract methods.
Explanation
An interface in Java can contain public static final data and public abstract methods. Public static final data represents constants that can be accessed by other classes. Public abstract methods are methods without a body that must be implemented by classes that implement the interface. The combination of these two elements allows an interface to define constants and specify the behavior that implementing classes must follow.
21.
Which of the following does not complete the sentence correctly?An interface .
Correct Answer
D. D. can be instantiated.
Explanation
An interface cannot be instantiated because it is an abstract concept that defines a set of methods that implementing classes must provide. It cannot be used to create objects directly.
22.
10.7.1 Q1: The UML distinguishes an interface from other classes by placing the word “interface” in above the interface name.
Correct Answer
C. C. guillemets.
Explanation
The UML distinguishes an interface from other classes by placing the word "interface" in guillemets above the interface name.
23.
10.7.2 Q1: Interfaces can have methods.
Correct Answer
D. D. any number of
Explanation
Interfaces in programming languages can have any number of methods. This means that an interface can define any number of abstract methods that must be implemented by any class that implements the interface. This allows for flexibility and customization in the implementation of the interface, as different classes can choose to implement different subsets of the methods defined in the interface. Therefore, the correct answer is "d. any number of".
24.
10.7.3 Q1: Which keyword is used to specify that a class will define the methods of an interface?
Correct Answer
B. B. implements.
Explanation
The keyword "implements" is used to specify that a class will define the methods of an interface. When a class implements an interface, it is required to provide implementations for all the methods declared in that interface. This allows the class to fulfill the contract defined by the interface and ensures that the class can be used interchangeably with other classes that implement the same interface. The "implements" keyword is a fundamental concept in object-oriented programming and is used to achieve interface implementation and polymorphism.
25.
10.7.3 Q2: Which of the following is not possible?
Correct Answer
B. B. A class that inherits from two classes.
Explanation
A class can implement multiple interfaces (option a) and inherit from one class while implementing an interface (option c). However, a class cannot inherit from two classes simultaneously (option b). This is because multiple inheritance is not allowed in most programming languages, including Java.
26.
10.7.4 Q1: A class that implements an interface but does not declare all of the interface’s methods must be declared:
Correct Answer
C. C. abstract.
Explanation
When a class implements an interface but does not declare all of the interface's methods, it means that the class is not providing concrete implementations for all the methods required by the interface. In this case, the class must be declared as abstract because it cannot be instantiated directly. Abstract classes are meant to be extended by other classes that will provide the missing implementations for the interface's methods.
27.
10.7.7 Q1: Constants declared in an interface are implicitly .
Correct Answer
B. B. static.
Explanation
Constants declared in an interface are implicitly static. In Java, constants in an interface are implicitly static and final, meaning they cannot be changed and are associated with the class rather than an instance of the class. The static keyword allows the constants to be accessed without creating an instance of the interface.