Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

Exercises

  1. Type in and run the five programs presented in this chapter. Compare the output produced by each program with the output presented after each program.
  2. Write a program that displays the following text:

    In Objective-C, lowercase letters are significant.
    main is where program execution begins.
    Open and closed braces enclose program statements in a routine.
    All program statements must be terminated by a semicolon.
  3. What output would you expect from the following program?

    #import <Foundation/Foundation.h>
    
    
    int main (int argc, const char *argv[])
    {
       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init;
       int i;
    
       i = 1;
       NSLog (@"Testing...");
       NSLog (@"....%i", i);
       NSLog (@"...%i",  i + 1);
       NSLog (@"..%i", i + 2);
    
       [pool drain];
       return 0;
    }
  4. Write a program that subtracts the value 15 from 87 and displays the result, together with an appropriate message.
  5. Identify the syntactic errors in the following program. Then type in and run the corrected program to make sure you have identified all the mistakes:

    #import <Foundation/Foundation.h>
    
    
    int main (int argc, const char *argv[]);
    (
       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
       INT sum;
       /* COMPUTE RESULT //
       sum = 25 + 37 - 19
       / DISPLAY RESULTS /
       NSLog (@'The answer is %i' sum);
    
       [pool drain];
       return 0;
    }
  6. What output would you expect from the following program?

    #import <Foundation/Foundation.h>
    
    
    int main (int argc, const char *argv[]))
    {
       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
       int answer, result;
    
       answer = 100;
       result = answer - 10;
    
       NSLog (@"The result is %i\n", result + 5);
    
       [pool drain];
       return 0;
    }
  • + Share This
  • 🔖 Save To Your Account