Objective-c Quiz: Categories And Protocols

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Malek BELKAHLA
M
Malek BELKAHLA
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,617
Questions: 11 | Attempts: 1,617

SettingsSettingsSettings
Objective-c Quiz: Categories And Protocols - Quiz

Objective-C Quiz: Categories and Protocols


Questions and Answers
  • 1. 

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

    • A.

      TRUE

    • B.

      FALSE

    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.

    Rate this question:

  • 2. 

    The lineid <Painting> myObject;

    • A.

      Says that myObject is part of the Painting category

    • B.

      Says that myObject conforms to the Painting category

    • C.

      Says that myObject conforms to the Painting protocol

    • D.

      Is not valid Objective-C syntax

    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.

    Rate this question:

  • 3. 

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

    • A.

      TRUE

    • B.

      FALSE

    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.

    Rate this question:

  • 4. 

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

    • A.

      TRUE

    • B.

      FALSE

    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.

    Rate this question:

  • 5. 

    The following@interface GObj: NSObject <Painting>

    • A.

      Starts the definition of a Painting category

    • B.

      Starts the definition of a Painting protocol

    • C.

      Painting protocol

    • D.

      Starts the definition of a class called GObj that conforms to the

    • E.

      None of the above

    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.

    Rate this question:

  • 6. 

    You could add a category to theNSString class, for example, to extend its functionality.

    • A.

      TRUE

    • B.

      FALSE

    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.

    Rate this question:

  • 7. 

    The following:@interface Fraction (MathOps)  ...@end

    • A.

      Defines a category called MathOps

    • B.

      Defines a protocol called MathOps

    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.

    Rate this question:

  • 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.

    • A.

      TRUE

    • B.

      FALSE

    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.

    Rate this question:

  • 9. 

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

    • A.

      TRUE

    • B.

      FALSE

    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.

    Rate this question:

  • 10. 

    All of the methods in a category must be implemented

    • A.

      TRUE

    • B.

      FALSE

    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.

    Rate this question:

  • 11. 

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

    • A.

      TRUE

    • B.

      FALSE

    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.

    Rate this question:

Quiz Review Timeline +

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.