Objective C & OOP Examination 1

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 Indianou
I
Indianou
Community Contributor
Quizzes Created: 4 | Total Attempts: 2,205
| Attempts: 1,454
SettingsSettings
Please wait...
  • 1/100 Questions

    The primary root object for the Foundation framework is called NSObject

    • True
    • False
Please wait...
Object Oriented Programming Quizzes & Trivia
About This Quiz

This examination assesses knowledge in Objective C and fundamental Object-Oriented Programming (OOP) concepts. It covers memory management, class categories, and initialization protocols, essential for developers to build robust software applications efficiently.


Quiz Preview

  • 2. 

    Given an object with an instance variable called numerator and synthesized accessor methods the following two lines of code achieve the same purpose: fract.numerator = 10; [fract setNumerator: 10];

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The given answer is true because both lines of code are setting the value of the numerator instance variable to 10. The first line uses the synthesized accessor method to directly set the value, while the second line uses a custom setter method called setNumerator to achieve the same purpose. Therefore, both lines of code have the same effect and achieve the same purpose.

    Rate this question:

  • 3. 

    The id data type can be used for any object type; it is a generic object data type.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The statement is true because the id data type in Python can be used to represent any object type. It is a generic object data type that can hold the unique identifier of any object in memory. This allows for flexibility in programming as the id data type can be used to store references to objects of different types without needing to specify the specific type beforehand.

    Rate this question:

  • 4. 

    When the retain count on an object reaches 0, the object gets sent a dealloc message.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    When the retain count on an object reaches 0, it means that there are no more references to that object and it is no longer needed. At this point, the object gets sent a dealloc message, which is a method that releases any resources or memory that the object was holding onto. This allows the object to clean up after itself and free up any allocated memory, making it available for other objects to use. Therefore, the statement "When the retain count on an object reaches 0, the object gets sent a dealloc message" is true.

    Rate this question:

  • 5. 

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

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The conformsToProtocol: method is a built-in method in Objective-C that allows us to check if an object belongs to a class that implements a specific protocol. It returns a Boolean value, true if the object conforms to the protocol and false otherwise. This method is useful when we want to ensure that an object meets certain requirements defined by a protocol before performing certain operations or accessing specific methods or properties. Therefore, the given statement is true.

    Rate this question:

  • 6. 

    A respondsToSelector: message can be sent to an object to see if it can respond to a particular method.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The statement is true because in Objective-C, the respondsToSelector: message can be used to check if an object can respond to a specific method. This is commonly used when you want to avoid calling a method on an object that may not have implemented it, preventing runtime errors. By using respondsToSelector:, you can safely check if the object can respond to the method before actually calling it.

    Rate this question:

  • 7. 

    You own an object that you copy

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The statement "You own an object that you copy" is true. When you copy an object, you create a new instance of that object which you then own. This means that you have the ability to modify or manipulate the copied object without affecting the original object. Therefore, the answer is true.

    Rate this question:

  • 8. 

    You can add more than one category to a class.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    In object-oriented programming, it is possible to assign a class to multiple categories or classifications. This is known as multiple inheritance or multiple classification. It allows a class to inherit properties and behaviors from more than one parent class. This can be useful in situations where a class needs to have characteristics from different categories. Therefore, the statement "You can add more than one category to a class" is true.

    Rate this question:

  • 9. 

    In general, you are responsible for releasing an object created with the copy method.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    When an object is created using the copy method, it creates a new instance of the object that is a copy of the original object. This means that the new object has its own memory space and is independent of the original object. Therefore, it is the responsibility of the programmer to release the memory occupied by the copied object when it is no longer needed. This can be done by explicitly calling the appropriate release or deallocation method. Hence, the statement "In general, you are responsible for releasing an object created with the copy method" is true.

    Rate this question:

  • 10. 

    In general, when you add an object to a Foundation array, its retain count gets increased by 1.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    When an object is added to a Foundation array, its retain count is increased by 1. This means that the object is being held in memory by the array and will not be deallocated until the retain count reaches 0. This is important for memory management in Objective-C, as it ensures that objects are not prematurely released while they are still being used by other parts of the program. Therefore, the statement "True" is correct.

    Rate this question:

  • 11. 

    In general, when you add an object to a Foundation array, its retain count gets increased by 1.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    When an object is added to a Foundation array, its retain count is increased by 1. This means that the array is now holding a reference to the object, preventing it from being deallocated from memory. This is a fundamental concept in memory management in Objective-C and ensures that objects are kept alive as long as they are needed. By increasing the retain count, the array is indicating that it has ownership of the object and will release it when it is no longer needed. Therefore, the statement "True" is correct.

    Rate this question:

  • 12. 

    In the general form of the for statement for ( e1; e2; e3 )    statement; e2 is evaluated before statement is executed.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    In the general form of the for statement, e2 is evaluated before the statement is executed. This means that the condition specified in e2 is checked first, and if it evaluates to true, then the statement is executed. If e2 evaluates to false, the loop is not executed at all. Therefore, the correct answer is true.

    Rate this question:

  • 13. 

    When you use alloc to create a new object, you should release the object when you're done using it

    • True

    • False

    Correct Answer
    A. True
    Explanation
    When using the "alloc" method to create a new object in programming, memory is allocated for that object. To prevent memory leaks and optimize memory usage, it is necessary to release the object when it is no longer needed. This can be done by using the "release" or "dealloc" method. Therefore, the statement "When you use alloc to create a new object, you should release the object when you're done using it" is true.

    Rate this question:

  • 14. 

    The @private directive restricts access to instance variables to methods in the class; subclasses cannot access the variables directly.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The @private directive is used in object-oriented programming languages to restrict access to instance variables. When applied to a variable, it means that only methods within the class can access it, while subclasses cannot access the variable directly. This helps maintain encapsulation and prevents subclasses from directly modifying or accessing the instance variables of the superclass. Therefore, the statement "The @private directive restricts access to instance variables to methods in the class; subclasses cannot access the variables directly" is true.

    Rate this question:

  • 15. 

    What file name extension is used for Objective-C code?

    • .cpp

    • .m

    • .pl

    • .c

    Correct Answer
    A. .m
    Explanation
    The correct answer is .m. In Objective-C, the file name extension used for Objective-C code is .m. This extension is commonly used to indicate that the file contains Objective-C code, similar to how .cpp is used for C++ code and .c is used for C code. The .m extension helps developers and compilers identify the file as containing Objective-C code and allows for proper compilation and execution of the code.

    Rate this question:

  • 16. 

    A newly allocated object should always be initialized

    • True

    • False

    Correct Answer
    A. True
    Explanation
    When a new object is allocated in a program, it means that memory is set aside for that object. However, this memory is not automatically filled with any specific values. Initialization is the process of assigning initial values to the object's variables or attributes. It is important to initialize a newly allocated object to ensure that all its variables have valid values before they are used in the program. This helps prevent any unexpected behavior or errors that may occur due to uninitialized variables. Therefore, the statement that a newly allocated object should always be initialized is true.

    Rate this question:

  • 17. 

    A class gets sent an initialize message when the class gets referenced for the first time.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    When a class gets referenced for the first time, it receives an initialize message. This message is sent to the class to perform any necessary setup or initialization tasks before it can be used. Therefore, the statement "A class gets sent an initialize message when the class gets referenced for the first time" is true.

    Rate this question:

  • 18. 

    An instance of a class is known as

    • A message

    • A method

    • An object

    • None of the above

    Correct Answer
    A. An object
    Explanation
    An instance of a class is known as an object. In object-oriented programming, a class is a blueprint for creating objects. An object is an instance of a class, meaning it is a specific realization or occurrence of the class. Objects have their own unique set of properties and behaviors, which are defined by the class they belong to. Therefore, an object is the correct term for an instance of a class.

    Rate this question:

  • 19. 

    What is wrong with this sequence of code? sum = 100; int sum;

    • Nothing is wrong

    • Everything is wrong! I hate Objective-C!

    • "sum" is a reserved word

    • A variable must be declared before it can be used

    Correct Answer
    A. A variable must be declared before it can be used
    Explanation
    The code is incorrect because the variable "sum" is being assigned a value before it is declared. In programming, variables must be declared before they can be used.

    Rate this question:

  • 20. 

    In general, you are responsible for releasing an object created with the copy method.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    When an object is created using the copy method, it creates a new instance of the object with the same values as the original object. In general, it is the responsibility of the programmer to release or deallocate the memory used by the copied object when it is no longer needed. Therefore, the statement "In general, you are responsible for releasing an object created with the copy method" is true.

    Rate this question:

  • 21. 

    It's okay to override init, but not considered wise to override alloc.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    Overriding the `init` method in a class allows customization of the initialization process for instances of that class. This can be useful to set default values or perform additional setup tasks. However, overriding the `alloc` method, which is responsible for memory allocation and initialization of an object, can lead to unexpected behavior and potential memory leaks. It is generally recommended to avoid overriding `alloc` unless there is a specific need for it. Therefore, the statement that it's okay to override `init` but not considered wise to override `alloc` is true.

    Rate this question:

  • 22. 

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

    • Defines a category called MathOps

    • Defines a protocol called MathOps

    Correct Answer
    A. Defines a category called MathOps
    Explanation
    This code snippet is defining a category called MathOps for the Fraction class. A category allows you to add additional methods to an existing class without subclassing it. By using this category, you can add new methods to the Fraction class specifically for mathematical operations.

    Rate this question:

  • 23. 

    Web-based apps require users to connect to the website that host the application in order to run them.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    Web-based apps are designed to run on a web browser, which means users need to connect to the website hosting the application to access and use it. Unlike traditional software that is installed directly on a device, web-based apps rely on an internet connection to function. This allows users to access the app from any device with a web browser, making it convenient and accessible from anywhere. Therefore, the statement "Web-based apps require users to connect to the website that hosts the application in order to run them" is true.

    Rate this question:

  • 24. 

    The method names print and print: refer to two different methods.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The statement is true because the method names "print" and "print:" have different syntaxes. In many programming languages, including Java and Python, the presence of a colon (:) in a method name indicates that it takes parameters or arguments. Therefore, "print" and "print:" are considered as two distinct methods with potentially different functionalities.

    Rate this question:

  • 25. 

    The dealloc method is automatically called when an object's memory can be freed.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The dealloc method in object-oriented programming is responsible for freeing the memory occupied by an object. When an object is no longer needed or goes out of scope, the dealloc method is automatically called to release the memory allocated to that object. Therefore, the statement "The dealloc method is automatically called when an object's memory can be freed" is true.

    Rate this question:

  • 26. 

    If s is an NSString object, the expression [s length] - 1

    • Gives the number of characters in the string

    • Gives the number of characters in the string minus 1

    • Gives an error as there is no length method

    • None of the above

    Correct Answer
    A. Gives the number of characters in the string minus 1
    Explanation
    The expression [s length] - 1 gives the number of characters in the string minus 1. The [s length] method returns the length of the NSString object, and subtracting 1 from it gives the number of characters in the string minus 1.

    Rate this question:

  • 27. 

    If you omit a break statement at the end of a case in a switch statement, then execution will fall down into the next case.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    If a break statement is omitted at the end of a case in a switch statement, it means that the code will not exit the switch statement and will continue executing the code in the next case. This is known as "falling through" to the next case. It can be useful in certain situations where you want to execute multiple cases sequentially without having to repeat the code. However, it can also lead to unexpected behavior if not used carefully.

    Rate this question:

  • 28. 

    A static variable will retain its value from one method call to the next.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    A static variable is a variable that is shared by all instances of a class. It is not tied to a specific object and retains its value across method calls. This means that when a method is called multiple times, the static variable will maintain the same value throughout those calls. Therefore, the given answer "True" is correct.

    Rate this question:

  • 29. 

    What is the value assigned to m? #define M(x,y) (((x)>(y)) ?   (x) : (y)) ... int x = 10, y = 20, m; m = M (x, y);

    • 10

    • 20

    • 30

    • 40

    Correct Answer
    A. 20
    Explanation
    The value assigned to m is 20. In the macro definition M(x,y), it compares the values of x and y using the greater than operator. If x is greater than y, it returns x. Otherwise, it returns y. In this case, since x is not greater than y (10 is not greater than 20), the value assigned to m is y, which is 20.

    Rate this question:

  • 30. 

    The first character in a string has an index number of 1

    • True

    • False

    Correct Answer
    A. False
    Explanation
    The given statement is false. In most programming languages, the index of the first character in a string starts at 0, not 1. This means that the first character in a string would have an index number of 0, not 1.

    Rate this question:

  • 31. 

    A class definition does not contain

    • An interface section

    • Instance variables

    • Methods

    • A main routine

    Correct Answer
    A. A main routine
    Explanation
    A class definition does not contain a main routine because the main routine is typically found in the program that uses the class, rather than within the class itself. The main routine is the entry point of the program and is responsible for starting the execution of the program. It is separate from the class definition and is used to create objects of the class and call its methods.

    Rate this question:

  • 32. 

    If x is an integer, the expression (x > 0) ? 1 :    ((x == 0) ? 0 : -1)

    • Has the value 1 if x is greater than 0

    • Has the value 0 if x is equal to 0

    • Has the value -1 if x is less than 0

    • All of the above

    Correct Answer
    A. All of the above
    Explanation
    The given expression is a conditional (ternary) operator that checks the value of x. If x is greater than 0, it returns 1. If x is equal to 0, it returns 0. If x is less than 0, it returns -1. Therefore, the expression has the value 1 if x is greater than 0, has the value 0 if x is equal to 0, and has the value -1 if x is less than 0. Hence, the correct answer is "all of the above."

    Rate this question:

  • 33. 

    The main difference between NSString and NSMutableString objects is

    • The contents of the former can be changed while the latter cannot

    • The contents of the former cannot be changed while the latter can

    • There really is no significant difference

    • They both can be used to store strings

    Correct Answer
    A. The contents of the former cannot be changed while the latter can
    Explanation
    The main difference between NSString and NSMutableString objects is that the contents of NSString objects cannot be changed once they are set, while the contents of NSMutableString objects can be modified. This means that NSString objects are immutable, while NSMutableString objects are mutable. Therefore, the correct answer is "The contents of the former cannot be changed while the latter can."

    Rate this question:

  • 34. 

    What is the value of sum after executing this loop: for (int i = 0, sum = 0;      i < 10; i += 2)    sum += i;

    • 30

    • 55

    • 17

    • 20

    Correct Answer
    A. 20
    Explanation
    The loop starts with i = 0 and sum = 0. In each iteration, i is incremented by 2 and added to sum. The loop stops when i reaches 10. Therefore, the loop will execute 5 times with i taking the values 0, 2, 4, 6, and 8. The sum of these values is 20, which is the final value of sum after executing the loop.

    Rate this question:

  • 35. 

    In general, this sequence [foo retain]; [pool drain]; guarantees the existence of the object foo past the draining of the autorelease pool pool.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The given code sequence includes a retain statement for the object foo and a drain statement for the autorelease pool pool. The retain statement increases the retain count of the object, ensuring that it will not be deallocated immediately. The drain statement releases all objects in the autorelease pool, which would normally result in the object foo being released as well. However, since the retain statement was used, the retain count of foo is still greater than zero, so it will not be deallocated. Therefore, the given code sequence guarantees the existence of the object foo past the draining of the autorelease pool pool.

    Rate this question:

  • 36. 

    Data encapsulation means

    • Putting all the data inside the program code

    • Archiving data to a file so it can later be read back in

    • Storing an object's data in its instance variables with access through its methods

    • Putting some information in a time capsule and burying it in your backyard

    Correct Answer
    A. Storing an object's data in its instance variables with access through its methods
    Explanation
    Data encapsulation refers to the practice of bundling data and the methods that operate on that data into a single unit, known as an object. This answer correctly states that data encapsulation involves storing an object's data in its instance variables, which are the variables that hold the object's data, and providing access to that data through its methods. By encapsulating the data within the object, it ensures that the data is protected and can only be accessed and modified through the defined methods, promoting data integrity and security.

    Rate this question:

  • 37. 

    A web-based app runs the same way as a native iPhone apps

    • True

    • False

    Correct Answer
    A. False
    Explanation
    A web-based app does not run the same way as a native iPhone app. Native iPhone apps are specifically developed for the iOS operating system and are installed directly on the device, utilizing its full capabilities. On the other hand, web-based apps run within a web browser and are platform-independent, meaning they can be accessed from any device with a compatible browser. Therefore, the statement is false.

    Rate this question:

  • 38. 

    What is the value of sum after executing this loop: for (int i = 0, sum = 0;      i < 10; i += 2)    sum += i;

    • 30

    • 55

    • 17

    • 20

    Correct Answer
    A. 20
    Explanation
    The loop starts with i = 0 and sum = 0. It iterates as long as i is less than 10, incrementing i by 2 in each iteration. Inside the loop, the value of i is added to sum.

    The loop will iterate 5 times, with i taking the values 0, 2, 4, 6, and 8.

    The sum of these values is 20, so the value of sum after executing the loop is 20.

    Rate this question:

  • 39. 

    If you override a method in a subclass there is no way to invoke the method of the same name in the parent class

    • True

    • False

    Correct Answer
    A. False
    Explanation
    When you override a method in a subclass, you can still invoke the method of the same name in the parent class by using the "super" keyword. This allows you to access and execute the parent class's implementation of the method. Therefore, the statement that there is no way to invoke the method of the same name in the parent class is false.

    Rate this question:

  • 40. 

    In general, a class should never own its own objects

    • True

    • False

    Correct Answer
    A. False
    Explanation
    This statement is false because it is not a general rule that a class should never own its own objects. In object-oriented programming, it is common for a class to have ownership of its own objects. Ownership allows the class to control the lifecycle and behavior of its objects, ensuring proper initialization, cleanup, and encapsulation. Without ownership, it would be difficult to manage the relationships and dependencies between objects in a system. Therefore, it is incorrect to say that a class should never own its own objects.

    Rate this question:

  • 41. 

    A class can have more than one parent class.

    • True

    • False

    Correct Answer
    A. False
    Explanation
    In object-oriented programming, a class can only have one parent class, which is known as single inheritance. This means that a class can inherit the properties and methods of only one other class. If a class needs to inherit from multiple classes, it can achieve this through interfaces or by using multiple inheritance, which is not supported in all programming languages. Therefore, the statement that a class can have more than one parent class is false.

    Rate this question:

  • 42. 

    What is wrong with this code? int i, j, sum; i = 100; sum = i + j;

    • Nothing is wrong

    • You can't initialize the variable i to 100

    • The value of j is not initialized

    • You can't declare and initialize a variable in the same line of code

    Correct Answer
    A. The value of j is not initialized
    Explanation
    The code is incorrect because the variable j is not initialized before it is used in the expression "sum = i + j". In order to perform addition, both variables i and j need to have a value assigned to them.

    Rate this question:

  • 43. 

    A leading + sign at the start of a method indicates

    • An initialization method

    • An instance method

    • A class method

    • None of the above

    Correct Answer
    A. A class method
    Explanation
    A leading + sign at the start of a method indicates that it is a class method. Class methods are associated with the class itself rather than with instances of the class. They can be called directly on the class without the need for an instance. Class methods are often used to perform operations that are related to the class as a whole rather than to specific instances.

    Rate this question:

  • 44. 

    Given the following method declaration in the interface section, what can you say about area? -(Rectangle *) area;

    • It takes no arguments

    • It returns a Rectangle object

    • A corresponding method definition should appear in the implementation section

    • All of the above

    Correct Answer
    A. All of the above
    Explanation
    The given method declaration in the interface section indicates that the "area" method takes no arguments, as there are no parameters specified in the declaration. It also states that the method returns a "Rectangle" object, as the return type is specified as "Rectangle *". Lastly, it suggests that a corresponding method definition should appear in the implementation section, as the declaration in the interface section is typically followed by its implementation in the implementation section. Therefore, all of the given statements are correct.

    Rate this question:

  • 45. 

    Sending an autorelease message to an object has the effect of increasing the object's retain count by 1.

    • True

    • False

    Correct Answer
    A. False
    Explanation
    Sending an autorelease message to an object does not increase its retain count. In fact, it has the opposite effect. When an object is sent an autorelease message, it is added to the current autorelease pool. At the end of the autorelease pool's scope, the pool sends a release message to each object in the pool, effectively decreasing their retain count by 1. Therefore, sending an autorelease message does not increase the object's retain count.

    Rate this question:

  • 46. 

    The removeObject: method

    • Can be used on mutable arrays

    • Lets you supply an isEqual: method to test for equality

    • Can be used on mutable sets

    • All of the above

    Correct Answer
    A. All of the above
    Explanation
    The removeObject: method can be used on mutable arrays, as well as on mutable sets. It allows the user to supply an isEqual: method to test for equality. Therefore, the correct answer is "All of the above".

    Rate this question:

  • 47. 

    Which is an example of an invalid variable name?

    • _Rectangle

    • Two_By_Four

    • X123$

    • Floats

    Correct Answer
    A. X123$
    Explanation
    The variable name "x123$" is an example of an invalid variable name because it starts with a number, which is not allowed in most programming languages. Variable names should typically start with a letter or an underscore.

    Rate this question:

  • 48. 

    What is the output from this code? int i, x = 0; for (i=5; i>0; --i)    x -= i; NSLog (@"%i", x);

    • -20

    • 15

    • -15

    • 12

    Correct Answer
    A. -15
    Explanation
    The code initializes two variables, i and x, with i being initially set to 5 and x set to 0. It then enters a for loop that decrements i by 1 each iteration until i is no longer greater than 0. Inside the loop, x is subtracted by the current value of i.

    The loop will run for 5 iterations, decrementing i by 1 each time. Therefore, x will be subtracted by 5, then 4, then 3, then 2, and finally 1.

    So the final value of x will be 0 - 5 - 4 - 3 - 2 - 1 = -15.

    Therefore, the output from this code will be -15.

    Rate this question:

  • 49. 

    The line id <Painting> myObject;

    • Says that myObject is part of the Painting category

    • Says that myObject conforms to the Painting category

    • Says that myObject conforms to the Painting protocol

    • Is not valid Objective-C syntax

    Correct Answer
    A. Says that myObject conforms to the Painting protocol
    Explanation
    The line "id myObject;" suggests that myObject conforms to the Painting protocol. In Objective-C, the "" syntax is used to indicate protocol conformance. Therefore, this line of code is stating that myObject is an object that conforms to the Painting protocol.

    Rate this question:

Quiz Review Timeline (Updated): Feb 9, 2024 +

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

  • Current Version
  • Feb 09, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Apr 12, 2012
    Quiz Created by
    Indianou
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.