Objective C & OOP Examination 2

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 Indianou
I
Indianou
Community Contributor
Quizzes Created: 4 | Total Attempts: 2,165
Questions: 15 | Attempts: 466

SettingsSettingsSettings
Object Oriented Programming Quizzes & Trivia

Questions and Answers
  • 1. 

    Which is the correct syntax to declare a function pointer named "foo" returning an integer and having an integer as argument?

    • A.

      *( int foo( int ) )

    • B.

      Int ()( int ) * foo

    • C.

      Int ( * foo )( int )

    • D.

      Int * foo( int )

    • E.

      ( int * foo int )

    • F.

      ( int * foo int )

    Correct Answer
    F. ( int * foo int )
  • 2. 

    What can you assume when calling the "stringWithString:" method on a "NSString" object?

    • A.

      The returned object is retained

    • B.

      The returned object is released

    • C.

      The returned object is auto-released

    Correct Answer
    C. The returned object is auto-released
    Explanation
    When calling the "stringWithString:" method on an "NSString" object, you can assume that the returned object is auto-released. This means that you do not need to manually release the memory used by the returned object, as it will be taken care of by the autorelease pool.

    Rate this question:

  • 3. 

    What can you say about the memory addresses that will be printed by the following program? #import <Cocoa/Cocoa.h> int main( void ) { NSAutoreleasePool * pool; NSString * s1; NSString * s2; NSString * s3; NSString * s4; pool = [ [ NSAutoreleasePool alloc ] init ]; s1 = @"Hello world!"; s2 = @"Hello world!"; s3 = [ NSString stringWithString: @"Hello world!" ]; s4 = [ [ NSString alloc ] initWithString: @"Hello world!" ]; printf( "%p\n%p\n%p\n%p\n", s1, s2, s3, s4 ); [ s4 release ]; [ pool release ]; return 0; }

    • A.

      Each address will be different

    • B.

      Addresses 1 and 2 will be the same

    • C.

      Addresses 1, 2 and 3 will be the same

    • D.

      All addresses will be the same

    Correct Answer
    D. All addresses will be the same
    Explanation
    The program initializes four different NSString variables with the same string "Hello world!". However, the memory addresses of these variables will be the same because the NSString class uses a technique called string interning. String interning allows multiple variables to point to the same memory address if they have the same string value, in order to save memory. Therefore, in this program, all the addresses will be the same.

    Rate this question:

  • 4. 

    What will be printed by the following program? #include <stdio.h> int main( void ) { unsigned int array[ 2 ][ 2 ] = { { 0, 1 }, { 2, 3 } }; unsigned int i = 0; unsigned int sum = 0; int x = 0; int y = 0; for( i = 0; i < 4; ++i ) { x = i % 2; y = ( x ) ? 0 : 1; sum += array[ x ][ y ]; } printf( "%d\n", sum ); return 0; }

    • A.

      6

    • B.

      7

    • C.

      8

    • D.

      9

    • E.

      10

    Correct Answer
    E. 10
    Explanation
    The program initializes a 2x2 array with values {0, 1, 2, 3}. It then uses a for loop to iterate through the array. The variable x is set to the remainder of i divided by 2, which will alternate between 0 and 1. The variable y is set to 0 if x is 1, and 1 if x is 0. This means that the program will access the elements in the array in the order {0, 0}, {1, 1}, {0, 0}, {1, 1}. The sum variable is incremented by the value at each accessed element. Therefore, the final sum will be 0 + 3 + 0 + 7 = 10.

    Rate this question:

  • 5. 

    What happen when a floating point value is assigned to an integer variable?

    • A.

      The floating point value is truncated

    • B.

      The floating point value is rounded, then assigned as an integer

    • C.

      The assignment is not done, as it's invalid

    Correct Answer
    C. The assignment is not done, as it's invalid
    Explanation
    When a floating point value is assigned to an integer variable, the assignment is not done as it is considered invalid. This is because floating point values contain decimal places and can have a larger range of values compared to integers, which only store whole numbers. Therefore, the conversion from a floating point value to an integer would result in a loss of precision or information, making the assignment invalid.

    Rate this question:

  • 6. 

    Which of the following creates a class that conforms to a protocol?

    • A.

      @interface ClassName [ ProtocolName ]

    • B.

      @interface ClassName < ProtocolName >

    • C.

      @interface ClassName < ProtocolName

    • D.

      @interface ClassName < ProtocolName

    • E.

      @interface ClassName( ProtocolName )

    Correct Answer
    B. @interface ClassName < ProtocolName >
    Explanation
    The correct answer is "@interface ClassName < ProtocolName >". This syntax in Objective-C is used to create a class that conforms to a protocol. The angle brackets "< >" are used to specify the protocol that the class is conforming to.

    Rate this question:

  • 7. 

    What is the default visibility for instance variables?

    • A.

      @private

    • B.

      @protected

    • C.

      @public

    • D.

      @package

    • E.

      None of the above

    Correct Answer
    C. @public
    Explanation
    The default visibility for instance variables is @public. This means that instance variables can be accessed and modified by any class or method within the same package or in any subclass, regardless of the package.

    Rate this question:

  • 8. 

    After the execution of the following code, what is the retain count of the "s1"? NSMutableString * s1; NSMutableString * s2; s1 = [ [ [ NSMutableString alloc ] initWithString: @"Hello world!" ] autorelease ]; [ [ [ [ s1 retain ] retain ] retain ] release ]; s2 = [ s1 copy ]; [ s1 release ];

    • A.

      S1: 0

    • B.

      S1: 1

    • C.

      S1: 2

    • D.

      S1: 3

    • E.

      S1: 4

    Correct Answer
    C. S1: 2
    Explanation
    The retain count of "s1" is 2 because it is initially assigned a mutable string object with a retain count of 1. Then, it is retained three more times using the "retain" method, increasing the retain count to 4. However, it is also released once using the "release" method, decreasing the retain count to 3. Finally, "s1" is copied to "s2", which creates a new string object with a retain count of 1. After that, "s1" is released again, decreasing its retain count to 2.

    Rate this question:

  • 9. 

    Which of the following can be inherited?

    • A.

      Categories

    • B.

      Protocols

    • C.

      Classes

    • D.

      None of the above

    Correct Answer
    C. Classes
    Explanation
    Classes can be inherited in object-oriented programming. Inheritance allows a class to inherit properties and methods from another class, known as the superclass or parent class. This enables code reusability and promotes a hierarchical structure in the program. The child class, also known as the subclass, can inherit and extend the functionality of the parent class, adding its own unique properties and methods. Inheritance is a fundamental concept in object-oriented programming and is used to create relationships between classes, promoting code organization and modularity.

    Rate this question:

  • 10. 

    How do you throw an exception?

    • A.

      @raise e

    • B.

      @throw e

    • C.

      RAISE( e )

    • D.

      THROW( e )

    • E.

      None of the above

    Correct Answer
    B. @throw e
    Explanation
    To throw an exception, the correct syntax is "@throw e". This statement is used to raise an exception and pass it to the calling code. The "@" symbol is commonly used in programming languages to indicate that something is a keyword or a special instruction. Therefore, "@throw e" is the appropriate way to throw an exception. The other options, including "@raise e", "RAISE( e )", "THROW( e )", and "None of the above", are not the correct syntax for throwing an exception.

    Rate this question:

  • 11. 

    What class specifiers are supported by Objective-C?

    • A.

      @final

    • B.

      @static

    • C.

      @fast

    • D.

      @iterative

    • E.

      @enumeration

    • F.

      @singleton

    • G.

      There is no class specifier in Objective-C

    Correct Answer
    G. There is no class specifier in Objective-C
    Explanation
    Objective-C does not have any class specifiers. In Objective-C, class declarations are done using the @interface keyword and class implementations are done using the @implementation keyword. Class specifiers are used in other programming languages to provide additional information or restrictions on classes, but Objective-C does not have any built-in class specifiers.

    Rate this question:

  • 12. 

    What is the correct syntax for declaring the prototype of a block named "foo" returning an integer and taking a character pointer as argument?

    • A.

      ( int ) ^ foo( char * );

    • B.

      Int( ^ foo )( char * );

    • C.

      __block int( foo( char * ) );

    • D.

      ^ ( int foo( char * ) );

    • E.

      __block ( int foo( char * ) );

    • F.

      The Objective-C compiler does not support blocks

    Correct Answer
    F. The Objective-C compiler does not support blocks
  • 13. 

    What type of variable do you need to use to implement the singleton pattern?

    • A.

      Static

    • B.

      Auto

    • C.

      Const

    • D.

      Volatile

    • E.

      Extern

    • F.

      None of the above

    Correct Answer
    F. None of the above
    Explanation
    The singleton pattern is implemented using a static variable. However, in this question, the correct answer is "None of the above" because none of the options listed (static, auto, const, volatile, extern) specifically refers to the type of variable needed to implement the singleton pattern.

    Rate this question:

  • 14. 

    What is a category?

    • A.

      A namespace

    • B.

      A way to add instance variables to a class which already exists

    • C.

      A collection of classes

    • D.

      A way to add methods to a class which already exists

    • E.

      None of the above

    Correct Answer
    E. None of the above
    Explanation
    A category in programming refers to a way to add additional methods to an existing class without modifying its original implementation. It allows for extending the functionality of a class without subclassing or modifying the original class. Therefore, the correct answer is "None of the above" as none of the options accurately describe what a category is.

    Rate this question:

  • 15. 

    What does Objective-C not support?

    • A.

      Instance variables

    • B.

      Class variables

    • C.

      Static variables

    • D.

      Variable arguments

    • E.

      Private methods

    • F.

      Protected methods

    • G.

      Class methods

    Correct Answer
    D. Variable arguments
    Explanation
    Objective-C does not support variable arguments. Variable arguments allow a function or method to accept a varying number of arguments. In Objective-C, the number and types of arguments must be explicitly defined in the method signature. Variable arguments are commonly used in languages like C and C++, but Objective-C does not provide direct support for them.

    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
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Apr 16, 2012
    Quiz Created by
    Indianou

Related Topics

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.