DevPrep

All topics › What does it print? Interview puzzles

What prints to the console?

Senior PRO

Task

Objective-C, the project is built without ARC (MRC), the code runs on the main thread:

NSObject *object = [NSObject new];
dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"A %lu", (unsigned long)[object retainCount]);
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"B %lu", (unsigned long)[object retainCount]);
    });
    NSLog(@"C %lu", (unsigned long)[object retainCount]);
});
NSLog(@"D %lu", (unsigned long)[object retainCount]);

What prints, and in which order?

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store