NSMutableString *s = [NSMutableString stringWithString: @"123"];
[s appendString: @"456"];
- 123456
- 123
- 456
- This code contains an error.
NSString *str = nil;
NSInteger i = str.integerValue;
- nil
- 0 (technically
nil
== 0 but i will have a literal value of0
and not thevoid*
value ofnil
) - -1
- This code crashes.
NSString str = "test" + " " + "more";
- This code contains an error
- test
- nil
- test more
NSPredicate *p2 = [NSPredicate predicateWithBlock:^BOOL(NSString* evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return evaluatedObject.intValue % 2 == 0;
}];
NSArray *vals = @[@"1", @"2", @"3"];
NSArray *n2 = [vals filteredArrayUsingPredicate:p2];
NSLog(@"%@", n2.firstObject);
- 2
- 1,2,3
- 1,2
- Nothing, since this code contains an error.
- atomic/strong
- atomic/weak
- nonatomic/weak
- nonatomic/strong
- NSMutableDictionary's values can change
- NSMutableDictionary has not initializers.
- NSDictionary can't be copied.
- NSDictionary's values can change.
-(float)foo;
- A function with a return type of float.
- This code contains an error.
- A variable declaration of type float.
- A property of type float.
#import "NSString+NameHelper.h"
- NameHelper is a category of NSString.
- NameHelper is a subclass of NSString.
- NSString implements the NameHelper protocol.
- NSString has a helper class.
float x = 5.;
- Nothing is wrong with this code.
- Declarations do not need semicolons.
- x=5 is an invalid float.
- Variables can't be declared and initialized in the same state.
for (int x=0; x<100; x++) {
x = x + 1;
}
- 50
- 99
- 100
- This code contains an error.
[self addObserver: self forKeyPath: @"val" options:0 context: nil];
- Key-Value Observing
- Class Value Observing
- Key-Data Observing
- KeyPath Observing
- Automatic Reference Counting
- Automatic Retain Checking
- Async Retain Cycles
- Automatic Release Code
int val = 0;
val = 1.5;
printf("%d", val);
- 1
- 2
- 0
- This code contains an error.
- single inheritance but multiple protocol implementation
- Objective-C doesn't support inheritance
- dual class inheritance
- unlimited class inheritance and protocol adherence
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: @"b", @"e", @"a", @"r", nil];
- 2
- 4
- 5
- This code contains an error.
NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithCapacity:5];
[dict1 setValue:@"key" forKey:@"value"];
- The key and value items are mixed
- Nothing is wrong with it
- You can't set the capacity of a dictionary
- NSMutableDictionary doesn't have a :setValue:forKey function.
NSData *data = [@"print" dataUsingEncoding:NSASCIIStringEncoding];
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
- This code is invalid
- Nothing is printed from this code.
- nil
+(void)doSomething;
- It is static
- It is abstract.
- It is inline.
- This code contains an error.
- functions
- initializers
- fields
- all of these answers
@interface MyClass : NSObject
@property (strong, nonatomic, readonly) NSString *name;
@end
- There is nothing wrong with this code.
- There is not read-only directive.
- MyClass doesn't implement NSObject.
- Properties are declared in the implementation.
typedef enum { Foo1, Foo2} Foo;
- There is no base type.
- NSObject
- int
- NSNumber
Q22. If you want to store a small amount of information (e.g., user settings), whats the best, built-in way to go?
- UserDefaults
- plist file
- CoreData
- TextFile
- to extend other classes
- to manage access control
- to coordinate objects
- to group classes
if ([keyPath isInstanceOf:[NSString class]]) {
}
- This code contains an error
- if keyPath is an instance of NSString
- if keyPath's baseclass is the same as NSString's baseclass
- if keyPath implements the same methods as NSString
int(^foo)(int);
- an Extension
- a Generic
- a block of code
- an abstract class
Q26. For observing changes to a property, which of these two statements cause the related method to be called and why?
1. _val = 1;
2. self.val= 100;
- Statement 2, since it calls the auto-created setter on the property.
- Statement 1, since it uses the property directly.
- Statement 2, since it specifies the class instance to use.
- Statement 1, since it calls the auto-created setter on the property.
float x = 2.0;
int(^foo)(int) = ^(int n1) {
return (int)(n1*x);
};
foo(5);
- Ints and floats can't be multiplied.
- The parameter isn't declared correctly.
- x is not in the right scope.
- Nothing is wrong with this code.
- Arrays are ordered, non-unique values.
- Arrays are stored sorted.
- Sets are ordered, unique values.
- Sets can contain nils.
- nothing, as they're never used in Objective-C
- function calls only
- property getter/setter
- parameter delimiters
NSArray *vals = @[@"1", @"2", @"3"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF.intValue > 1"];
NSArray *newVals = [vals filteredArrayUsingPredicate:pred];
- 2,3
- nil
- This code contains an error
- 2,"3"
-(int)foo:(int)a b:(int)c;
- self.foo(5, b:10);
- This code contains an error.
- [self foo:5:10:20];
- [self foo:5 b:10];
NSError *error;
NSData *data;
id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
- NSString
- NSArray
- id
- NSDictionary
-(void)testFunc:(NSString**)str;
- The parameter is passed by value and can not be changed
- ** is not allowed on a parameter
- The parameter may be nil
- The parameter is passed by reference and may be changed
typedef enum {
thing1,
thing2,
thing3
} Thing;
-(void) enumStuff {
NSLog(@"%d", thing2);
}
- 0
- 1
- thing2
- This code does not print anything
Q35. You are worried about threaded access to a property and possible collision in writing. What directive should you use on the property?
- non-atomic
- strong
- weak
- atomic
int temp = 1==1;
-
temp
is a keyword. - 1==1 is invalid.
- 1==1 evaluates to a Boolean.
- Nothing is wrong with it.
dispatch_async(dispatch_get_main_queue(), ^{
// code
});
- It executes on the main queue.
- It is the last code to run before the app goes inactive.
- It executes on a background thread.
- It is queued to execute in the background.
NSMutableSet *set1 = [NSMutableSet setWithObjects: @1,@2, @3, @4, @5, nil];
[set1 add0bject:@3];
- zero
- six
- one
- five
NSDictionary *d1 = @[@"v1", @4, @"v2", @5.6, @"v3"];
NSlog(@"d1: %@", d1);
- NSDictionary cannot be printed this way.
- The last key is missing a value.
- Dictionaries cannot have mixed types as values.
- d1 is assigned an NSArray of values.
@property (nonatomic, readonly) int val;
- 8
- nil
- -1
- undefined
- UI
- dispatch
- background
- main