1.
A category can be used to add new instance variables to a class.
Correct Answer
B. FALSE
Explanation
This statement is false because a category in Objective-C is used to add new methods to an existing class, not new instance variables. Instance variables can only be added to a class through subclassing or modifying the original class directly.
2.
The lineid <Painting> myObject;
Correct Answer
C. Says that myObject conforms to the Painting protocol
Explanation
The line "myObject;" does not specify any category or protocol. It simply declares a variable named "myObject" without assigning any specific type to it. Therefore, it cannot be determined that it conforms to the Painting protocol based on this line alone.
3.
The conformsToProtocol: method can be used to see if an object belongs to a class that implements a particular protocol
Correct Answer
A. TRUE
Explanation
The conformsToProtocol: method in Objective-C can be used to check if an object belongs to a class that adheres to a specific protocol. This method returns a boolean value, true if the object conforms to the protocol and false otherwise. It is a useful way to determine if an object supports certain methods or behaviors defined in a protocol before interacting with it.
4.
A category can be used to override other methods in the class, but it's not considered good programming practice.
Correct Answer
A. TRUE
Explanation
Using a category in Objective-C allows you to add additional methods to a class, even if you don't have access to the original source code. This can be useful for extending functionality or fixing bugs in third-party libraries. However, overriding methods using categories can lead to confusion and make the code harder to understand and maintain. It is generally recommended to use subclassing or composition instead, as they provide clearer and more maintainable solutions. Therefore, the statement that using a category to override methods is not considered good programming practice is true.
5.
The following@interface GObj: NSObject <Painting>
Correct Answer
D. Starts the definition of a class called GObj that conforms to the
Explanation
The correct answer is "Starts the definition of a class called GObj that conforms to the Painting protocol". This is because the "@" symbol followed by "interface" indicates the declaration of a class called GObj, and the "" indicates that the class conforms to the Painting protocol.
6.
You could add a category to theNSString class, for example, to extend its functionality.
Correct Answer
A. TRUE
Explanation
Adding a category to the NSString class allows for extending its functionality. Categories in Objective-C provide a way to add methods to an existing class without subclassing it. By adding a category to the NSString class, additional methods can be included that enhance the functionality of NSString objects. This can be useful when needing to add custom functionality or behavior to NSString instances without modifying the original class implementation.
7.
The following:@interface Fraction (MathOps) ...@end
Correct Answer
A. Defines a category called MathOps
Explanation
The given code snippet defines a category called MathOps. Categories in Objective-C allow you to add methods to an existing class without modifying its original implementation. By defining a category called MathOps, the code is extending the functionality of the Fraction class by adding additional methods specific to mathematical operations.
8.
If you start a class definition that conforms to a specified protocol, you need to relist all the methods you will implement for that protocol in the @interface section.
Correct Answer
B. FALSE
Explanation
When starting a class definition that conforms to a specified protocol, it is not necessary to relist all the methods you will implement for that protocol in the @interface section. The @interface section only needs to declare that the class conforms to the protocol by including the protocol name in angle brackets after the class name. The actual implementation of the protocol methods can be listed in the @implementation section of the class. Therefore, the statement is false.
9.
A category can be used to add new instance variables to a class.
Correct Answer
B. FALSE
Explanation
This statement is false because a category in Objective-C cannot add new instance variables to a class. Categories can only add new methods to a class, not new instance variables. Instance variables can only be added to a class through subclassing or by modifying the class's original implementation.
10.
All of the methods in a category must be implemented
Correct Answer
B. FALSE
Explanation
In object-oriented programming, a category is a way to group related methods together. In Objective-C, for example, categories allow you to add methods to an existing class without modifying its original implementation. However, it is not necessary to implement all of the methods in a category. You can choose to implement only the methods that are relevant to your specific needs. Therefore, the statement "All of the methods in a category must be implemented" is false.
11.
A category can be a good way to divide the implementation of a class into logical sections
Correct Answer
A. TRUE
Explanation
A category in the implementation of a class allows for dividing the code into logical sections. This helps in organizing and managing the codebase effectively. By grouping related methods and properties together, it becomes easier to understand and maintain the code. Categories also provide a way to extend the functionality of a class without modifying its original implementation. Overall, using categories can enhance code readability, reusability, and maintainability, making it a good way to divide the implementation of a class.