Objective-c Quiz: Categories And Protocols

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Malek BELKAHLA
M
Malek BELKAHLA
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,622
| Attempts: 1,622 | Questions: 11
Please wait...
Question 1 / 11
0 %
0/100
Score 0/100
1. You could add a category to theNSString class, for example, to extend its functionality.

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.

Submit
Please wait...
About This Quiz
Objective-c Quiz: Categories And Protocols - Quiz

This Objective-C quiz assesses knowledge on categories and protocols, focusing on their definitions, usage, and limitations. It tests understanding of protocol conformity, category functionality, and best practices in... see moreObjective-C programming, essential for developers. see less

2. The conformsToProtocol: method can be used to see if an object belongs to a class that implements a particular protocol

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.

Submit
3. The following:

@interface Fraction (MathOps)
  ...
@end

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.

Submit
4. A category can be a good way to divide the implementation of a class into logical sections

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.

Submit
5. A category can be used to add new instance variables to a class.

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.

Submit
6. A category can be used to override other methods in the class, but it's not considered good programming practice.

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.

Submit
7. A category can be used to add new instance variables to a class.

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.

Submit
8. The line

id <Painting> myObject;

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.

Submit
9. The following

@interface GObj: NSObject <Painting>

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.

Submit
10. 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.

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.

Submit
11. All of the methods in a category must be implemented

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.

Submit
View My Results

Quiz Review Timeline (Updated): Feb 20, 2023 +

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

  • Current Version
  • Feb 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 13, 2010
    Quiz Created by
    Malek BELKAHLA
Cancel
  • All
    All (11)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
You could add a category to theNSString class, for example, to...
The conformsToProtocol: method can be used to see if an...
The following:@interface Fraction (MathOps)  ...@end
A category can be a good way to divide the implementation of a class...
A category can be used to add new instance variables to a class.
A category can be used to override other methods in the class, but...
A category can be used to add new instance variables to a class.
The lineid <Painting> myObject;
The following@interface GObj: NSObject <Painting>
If you start a class definition that conforms to a specified protocol,...
All of the methods in a category must be implemented
Alert!

Advertisement