Professional Documents
Culture Documents
Objective-C and IOS Programming A Simplified Approach To Developing Apps For The Apple Iphone and Ipad 1st Edition Arshia Khan Test Bank 1
Objective-C and IOS Programming A Simplified Approach To Developing Apps For The Apple Iphone and Ipad 1st Edition Arshia Khan Test Bank 1
TRUE/FALSE
1. A common rationale for the concept of “functions” is that they can be used to “divide and
conquer” code.
2. One of the negative aspects of using functions is that they make debugging more difficult.
3. A function declaration has two main parts: the return type and the parameters list.
7. In an Objective-C program, a function declaration can be placed either before or after the
declaration of another function that invokes it.
1 #import <Foundation/Foundation.h>
2 int main (int argc, const char * argv[])
3 {
4 @autoreleasepool {
5 welcome();
6 }
7 return 0;
8 }
9 void welcome()
10{
11 NSLog(@"\nGood Evening, how are you doing tonight?");
12}
10. A function creates a group of lines of code that can be called multiple times to perform a task that
is done repeatedly.
11. The call to a function can be made inside of the main function, but the actual code for the body of
the function is never placed inside of the main function.
12. A function can return a basic data type such as an int, char, double, or float or a derived
data type such as a pointer, structure, or enum.
16. The scope of a local variable is within the set of curly braces in which it was declared.
MULTIPLE CHOICE
3. If a function returns no value, the <return_data_type> specified in the function declaration should
be ____.
a. null c. int
b. void d. false
4. If no return type is specified for a function, the function is assumed to return a(n) ____.
a. null c. int
b. void d. false
7. In the following function definition, to what does the first word, void, on line 1 refer?
1 void welcome(void)
2 {
3 NSLog(@"\nGood Evening, how are you doing tonight?");
4 }
1 #import <Foundation/Foundation.h>
2 void welcome(void)
3 {
4 NSLog(@"\nGood Evening, how are you doing tonight?");
5 }
6
7 int main (int argc, const char * argv[])
8 {
9 @autoreleasepool {
10 welcome();
11 }
12 return 0;
13 }
a. 2 c. 7
b. 4 d. 10
9. Which line of the following example contains the body of the function welcome?
1 #import <Foundation/Foundation.h>
2 void welcome(void)
3 {
4 NSLog(@"\nGood Evening, how are you doing tonight?");
5 }
6
7 int main (int argc, const char * argv[])
8 {
9 @autoreleasepool {
10 welcome();
11 }
12 return 0;
13 }
a. 2
a. 2 c. 7
b. 4 d. 10
10. A ____ is a declaration of a function which contains the return type, the name of the function, and
the parameters that are being passed to the function.
a. formal parameter c. function call
b. main function d. prototype
11. The ____ is the type of data the function will return when executed.
a. actual type c. scope
b. return type d. parameter type
12. If a function does not return a value, then its return type is defined as ____.
a. void c. false
b. null d. 0
13. The parameters listed in a function definition are called the ____ parameters.
a. data c. actual
b. formal d. definition
ANS: B PTS: 1 REF: 107
14. The parameters that are passed to a function when the function is invoked are called ____
parameters.
a. data c. actual
b. formal d. definition
Figure 5-1
1 #import <Foundation/Foundation.h>
2 double average(double n1, double n2, double n3);
3 int main (int argc, const char * argv[])
4 {
5 @autoreleasepool {
6 double n1, n2, n3, avg;
7 NSLog(@"Enter 3 numbers");
8 scanf("%lf" "%lf" "%lf", &n1, &n2, &n3);
9 avg = average(n1, n2, n3);
10 NSLog(@"The average of %lf %lf %lf is: %lf", n1, n2,
11 n3, avg);
12 }
13 return 0;
14 }
15 double average(double n1, double n2, double n3)
16 {
17 return (n1 + n2 + n3)/3;
18 }
15. In Figure 5-1, the return type of the average function is ____.
a. float c. char
b. int d. double
16. In Figure 5-1, the parameters passed to the average function in line 15 are the ____ parameters.
a. range c. actual
b. formal d. global
17. In Figure 5-1, the parameters passed to the average function in line 9 are the ____ parameters.
a. range c. actual
b. formal d. global
20. ____ variables are declared outside of all the functions in the program, right after the import
declarations.
a. Local c. Global
b. Scope d. Range
21. ____ variables are seen only inside of the function in which they are declared and are invisible to
the rest of the program.
a. Local c. Global
b. Scoped d. Range
{
return totalgrowth/years;
}
a. double c. void
b. int d. float
Figure 5-2
#import <Foundation/Foundation.h>
2 double diff (double n1, double n2);
3 double avg (double totalgrowth, double years);
4 double totalGrowth; //growth in five years
5 int main(int argc, const char * argv[])
6 {
7 @autoreleasepool {
8 //declare variables
9 double initialHeight; //initial height of a tree
11 double finalHeight; //final height of the tree
13 double avgGrowthPerYear; //Average growth of a tree
15 //prompt for the initial height of the tree
16 NSLog(@"\nInitial height of the tree in feet?");
17 scanf("%lf",&initialHeight);
18 NSLog(@"\nFinal height of the tree?");
20 scanf("%lf", &finalHeight);
21 totalGrowth = diff(initialHeight, finalHeight);
22 avgGrowthPerYear = avg(totalGrowth, 5.0);
23 NSLog(@"\nAverage growth is %lf", avgGrowthPerYear);
25 }
26 return 0;
27 } // end of main function
28
29 double diff (double n1, double n2)
30 {
31 double totalGrowth;
32 totalGrowth = n2 - n1;
33 return totalGrowth;
34 }
35
36 double avg(double totalgrowth, double years)
37 {
38 return totalgrowth/years;
39 }
29. A variable that holds a dollar amount such as 5645.89 can be declared as a(n) ____.
a. int c. char
b. double d. string
31. In the following function, what is the scope of the variable, amt, on line 8?
32. What is the prototype for a function, check, that takes an integer parameter, num, and does not
return anything?
a. check (integer num);
b. void check (int num);
c. void check (integer num);
d. null check (int num);
33. What is the prototype for a function, getCoins, that takes a double parameter, amt, and returns
an integer value?
a. double getCoins (integer amt);
b. int getCoins(double amt);
c. integer getCoins (dbl amt);
d. double getCoins(int amt);