1.
Which is the correct syntax to declare a function pointer named "foo" returning an integer and having an integer as argument?
Correct Answer
F. ( int * foo int )
2.
What can you assume when calling the "stringWithString:" method on a "NSString" object?
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.
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;
}
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.
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;
}
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.
5.
What happen when a floating point value is assigned to an integer variable?
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.
6.
Which of the following creates a class that conforms to a protocol?
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.
7.
What is the default visibility for instance variables?
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.
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 ];
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.
9.
Which of the following can be inherited?
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.
10.
How do you throw an exception?
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.
11.
What class specifiers are supported by 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.
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?
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?
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.
14.
What is a category?
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.
15.
What does Objective-C not support?
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.