1.
When the retain count on an object reaches 0, the object gets sent a dealloc message.
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.
2.
In general, when you add an object to a Foundation array, its retain count gets increased by 1.
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.
3.
You can add more than one category to a class.
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.
4.
A newly allocated object should always be initialized
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.
5.
If you override dealloc, you should first send a dealloc message to super before you release your instance variables.
Correct Answer
B. False
Explanation
When overriding the dealloc method in Objective-C, it is not necessary to send a dealloc message to the super class before releasing instance variables. The dealloc method is automatically called when an object is deallocated, and it is responsible for releasing any resources that the object may have acquired during its lifetime. However, it is important to note that when overriding dealloc, it is still good practice to release any retained or owned instance variables before calling [super dealloc] to ensure proper memory management.
6.
If you write this outside of any method or function:
int gRecordCount;
Correct Answer
D. All of the above
Explanation
The given code snippet declares a global variable called "gRecordCount". A global variable can be accessed from other files by using an external declaration, making it accessible throughout the program. Additionally, since the variable is declared outside of any method or function, it is considered a global variable. Lastly, if no initial value is assigned to the variable, it will have a default initial value of 0. Therefore, all of the given options are correct.
7.
The statement
if ( x < 10 && x > 50)
y = 100;
else
y = 50;
Correct Answer
B. Will always assign 50 to y
Explanation
The given statement uses the logical operator "&&" which represents the logical AND. In this case, the condition "x < 10 && x > 50" can never be true because it is impossible for a number to be both less than 10 and greater than 50 at the same time. Therefore, the else statement will always be executed, assigning the value 50 to y.
8.
In general, when you use the removeObjectAtIndex: method to remove an object from an array, the object's retain count will remain unchanged.
Correct Answer
B. False
Explanation
When you use the removeObjectAtIndex: method to remove an object from an array, the object's retain count will decrease by 1. This is because removeObjectAtIndex: not only removes the object from the array but also releases its ownership. Therefore, the correct answer is False.
9.
In order to make sure you release any instance variables you own, you should override the release method in your class.
Correct Answer
B. False
Explanation
Overriding the release method in a class is not necessary to release instance variables. The release method is used in Objective-C to release memory allocated by an object, but in modern programming languages like Swift, memory management is handled automatically using ARC (Automatic Reference Counting). Therefore, there is no need to override the release method in Swift or other languages that use automatic memory management.
10.
What is the value of z after executing this sequence (check indentation)?
int x = 100, y = 50, z = -1;
if ( x + y > 50)
if ( x + y < 100)
z = 0;
else
z = 1;
Correct Answer
C. 1
Explanation
The value of z is 1 because the condition x + y > 50 is true (150 > 50) and the condition x + y < 100 is also true (150 < 100). Therefore, the statement z = 0 is executed, making the value of z equal to 0.
11.
When you drain an autorelease pool all objects that have been sent autorelease messages will get deallocated
Correct Answer
B. False
Explanation
When you drain an autorelease pool, only the objects that have been sent autorelease messages will have their retain count decreased by 1. However, they will not necessarily get deallocated immediately. The objects will be deallocated only when the retain count reaches 0. Therefore, the statement is false.
12.
NSInteger is
Correct Answer
D. All of the above
Explanation
The correct answer is "All of the above" because NSInteger is a type in Objective-C that can either be an int or a long int depending on the compiler options. It is not an object, but rather a primitive data type that represents an integer. Therefore, all three statements are true.
13.
What is wrong with this line of code?
Int Sum;
Correct Answer
B. The data type
Explanation
The given correct answer suggests that the issue with the line of code "Int Sum;" is related to the data type. However, without further context, it is not possible to determine the exact problem with the code. It could be that "Int" is not a valid data type in the programming language being used, or it could be a typo and should be "int" instead.
14.
The difference between enclosing a file name in <...> and "..." when using #import is by default
Correct Answer
B. The former does not search the current source directory first
Explanation
When using #import, enclosing a file name in means that the compiler will search for the file in the system's standard include directories first, before searching the current source directory. On the other hand, enclosing the file name in "..." means that the compiler will search for the file in the current source directory first, before searching the standard include directories. Therefore, the correct answer is that the former (enclosing the file name in ) does not search the current source directory first.
15.
An instance of a class is known as
Correct Answer
C. 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.
16.
Once you create a number object, you can't change its value
Correct Answer
A. True
Explanation
The statement is true because once a number object is created, its value cannot be changed. In programming, number objects are typically immutable, meaning that their values cannot be modified after they are created. This is different from variables, which can be reassigned to different values. Therefore, if you create a number object with a certain value, you cannot change that value later on.
17.
Can you send a message to self from inside main?
Correct Answer
B. False
Explanation
In most programming languages, including Java, it is not possible to send a message to oneself from inside the main method. The main method is the entry point of the program and is executed only once when the program starts. It is not designed to send messages or communicate with itself. Therefore, the correct answer is False.
18.
The following:
#define kStudents 50
Correct Answer
B. Defines an identifier kStudents with associated text 50
Explanation
The given code snippet uses the #define preprocessor directive to create a constant identifier called kStudents and associates it with the value 50. This means that wherever the identifier kStudents is used in the code, it will be replaced with the value 50 during the preprocessing stage. Therefore, the correct answer is "defines an identifier kStudents with associated text 50".
19.
If you want to return an object back to the caller of your method and you want to mark the object for later release, you should
Correct Answer
D. Copy it before returning
Explanation
When returning an object back to the caller of a method and wanting to mark it for later release, the correct approach is to copy it before returning. This ensures that the caller receives a new instance of the object, independent of the original one, and takes ownership of it. By creating a copy, any modifications made to the returned object by the caller will not affect the original object, and both can be released separately when appropriate.
20.
In order to make sure that an object is not inadvertently released someplace else, you
Correct Answer
C. Retain it
Explanation
Retaining an object ensures that its reference count is increased, preventing it from being deallocated prematurely. By retaining an object, you indicate that you want to keep using it and that it should not be released until you explicitly release it. This is important to avoid accessing deallocated memory and potential crashes in your program. Therefore, retaining an object is necessary to ensure its availability and prevent accidental release elsewhere in the code.
21.
What is wrong with this code?
int i, j, sum;
i = 100;
sum = i + j;
Correct Answer
C. 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.
22.
Sending a release message to an object always causes the memory associated with that object to be released.
Correct Answer
B. False
Explanation
Sending a release message to an object does not always cause the memory associated with that object to be released. It depends on the memory management system being used. In some systems, releasing an object simply decrements its reference count, and the memory is only released when the reference count reaches zero. In other systems, releasing an object may not immediately release the memory, but instead mark it as available for reuse in the future. Therefore, it is not always guaranteed that sending a release message will immediately release the memory associated with an object.
23.
A leading + sign at the start of a method indicates
Correct Answer
C. 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.
24.
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. However, it is not necessary for all methods in a category to be implemented. Categories allow for the organization and grouping of methods, but the implementation of individual methods within a category is optional. Therefore, the statement that all of the methods in a category must be implemented is false.
25.
If s is an NSString object, the expression
[s length] - 1
Correct Answer
B. 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.
26.
The iPod Touch supports garbage collection
Correct Answer
B. False
Explanation
The iPod Touch does not support garbage collection. Garbage collection is a feature in programming languages that automatically frees up memory by removing objects that are no longer needed. Since the iPod Touch runs on iOS, which is based on the Objective-C programming language, it uses manual memory management instead of garbage collection. This means that developers need to manually allocate and deallocate memory in their code. Therefore, the correct answer is False.
27.
What can you say about the indicated statement?
Fraction *f =
[[Fraction alloc] init], *g;
...
g = f;
[g release]; <==
...
Correct Answer
B. It could be replaced by [f release] with the same effect
28.
Given the following method declaration in the interface section, what can you say about area?
-(Rectangle *) area;
Correct Answer
D. 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.
29.
A category can be used to add new instance variables to a class.
Correct Answer
B. False
Explanation
A category in programming is a way to add new methods to an existing class, not instance variables. Instance variables are defined within the class itself, while categories are used to extend the functionality of a class by adding new methods. Therefore, the statement that a category can be used to add new instance variables to a class is false.
30.
Sending an autorelease message to an object has the effect of increasing the object's retain count by 1.
Correct Answer
B. 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.
31.
What is wrong with this sequence of code?
sum = 100;
int sum;
Correct Answer
D. 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.
32.
What is the output from this code?
int i, x = 0;
for (i=5; i>0; --i)
x -= i;
NSLog (@"%i", x);
Correct Answer
C. -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.
33.
A web-based app runs the same way as a native iPhone apps
Correct Answer
B. 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.
34.
If x is an integer, the expression
! x
Correct Answer
D. All of the above
Explanation
The expression "! x" represents the logical negation of x. If x is non-zero, the logical negation of a non-zero value is 0. If x is zero, the logical negation of zero is 1. Therefore, the expression has the value 0 if x is non-zero and has the value 1 if x is zero. Thus, the correct answer is "All of the above" because all three statements are true.
35.
What is the value of sum after executing this loop:
for (int i = 0, sum = 0;
i < 10; i += 2)
sum += i;
Correct Answer
D. 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.
36.
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.
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.
37.
In general, you are responsible for releasing an object created with the copy method.
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.
38.
If you create a new NSString object with the stringWithString: method, you can assume
Correct Answer
C. The newly-created string has been autoreleased
Explanation
When creating a new NSString object with the stringWithString: method, the newly-created string is assumed to have been autoreleased. This means that the memory management of the string is handled automatically by the autorelease pool, and it will be released at some point in the future when the pool is drained. This allows the programmer to not worry about manually releasing the string and reduces the chances of memory leaks.
39.
The difference between #import and #include is
Correct Answer
C. The former prevents a file from being included multiple times
Explanation
The correct answer is that the former (#import) prevents a file from being included multiple times. This is because when using #import, the preprocessor checks if the file has already been included and if so, it skips the inclusion. This ensures that the file is only included once, avoiding duplication and potential errors. On the other hand, #include does not have this built-in mechanism to prevent multiple inclusions, so it is possible to include the same file multiple times, leading to duplicate code and potential issues.
40.
Given that i = 11, what is the value of the expression
i % 2.0
Correct Answer
D. The modulus operator can only be used with integers
41.
When you use alloc to create a new object, you should release the object when you're done using it
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.
42.
What is true about the local variable counter if this statement is executed inside a method?
static int counter = 0;
Correct Answer
B. Its value will be set to 0 only once when the program begins execution
Explanation
The given correct answer states that the value of the local variable "counter" will be set to 0 only once when the program begins execution. This means that every time the method is called, the value of "counter" will not be reset to 0. Instead, it will retain its value from previous method calls. Therefore, the correct answer suggests that the local variable "counter" will have a persistent value throughout the program's execution.
43.
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 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.
44.
The line
id <Painting> myObject;
Correct Answer
C. 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.
45.
This directive
@interface Fraction (MathOps) <NSCopying, NSCoding>
Correct Answer
C. Both of the above
Explanation
This directive "@interface Fraction (MathOps) " specifies a set of methods that conforms to 2 protocols and starts a category definition. By using the parentheses after the interface name, it indicates that the methods declared within this category are an extension of the Fraction class. The "" part indicates that the methods in this category conform to the NSCopying and NSCoding protocols. Therefore, the correct answer is "Both of the above".
46.
A respondsToSelector: message can be sent to an object to see if it can respond to a particular method.
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.
47.
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];
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.
48.
The sequence
#ifndef MIN
#define MIN(x,y) ((x)<(y) \
? (x) : (y))
#endif
Correct Answer
B. Defines a macro MIN if MIN is not previously defined
Explanation
The given code snippet is a preprocessor directive that defines a macro MIN only if it is not previously defined. This means that if the macro MIN has already been defined in the code before this snippet, the code will not redefine it. If MIN is not previously defined, the code will define it using the provided implementation.
49.
Given
int a = 10, b = 20, c;
b *= a + 5;
What is the resulting value of b?
Correct Answer
B. 300
Explanation
The expression "b *= a + 5" is equivalent to "b = b * (a + 5)". So, first, the value of "a + 5" is calculated, which is 10 + 5 = 15. Then, the value of "b * (a + 5)" is calculated, which is 20 * 15 = 300. Therefore, the resulting value of b is 300.
50.
Given
int a = 25, b = 6, c = 10;
The expression
a / b * c + a % c
has the value
Correct Answer
D. 45
Explanation
The expression first divides 25 by 6, which results in 4. Then it multiplies the result by 10, giving 40. Next, it calculates the remainder of dividing 25 by 10, which is 5. Finally, it adds the two previous results together, resulting in a final value of 45.