You are on page 1of 10

Objective-C and iOS Programming A

Simplified Approach To Developing


Apps for the Apple iPhone and iPad
1st Edition Arshia Khan Solutions
Manual
Full download at link:

Solution Manual: https://testbankpack.com/p/solution-manual-


for-objective-c-and-ios-programming-a-simplified-approach-to-
developing-apps-for-the-apple-iphone-and-ipad-1st-edition-
arshia-khan-1285187059-9781285187051/

Test Bank: https://testbankpack.com/p/test-bank-for-objective-


c-and-ios-programming-a-simplified-approach-to-developing-
apps-for-the-apple-iphone-and-ipad-1st-edition-arshia-khan-
1285187059-9781285187051/
Chapter 5: Functions

TRUE/FALSE

1. A common rationale for the concept of “functions” is that they can be used to “divide and
conquer” code.

ANS: T PTS: 1 REF: 102

2. One of the negative aspects of using functions is that they make debugging more difficult.

ANS: F PTS: 1 REF: 102

3. A function declaration has two main parts: the return type and the parameters list.

ANS: F PTS: 1 REF: 102

4. By default, a function is assumed to return a null value.

ANS: F PTS: 1 REF: 102


5. When a function does not need any parameters, the function declaration must contain the
expression (void) immediately following the function name.

ANS: F PTS: 1 REF: 103

6. The body of a function can be as long as you want it to be.

ANS: T PTS: 1 REF: 103

7. In an Objective-C program, a function declaration can be placed either before or after the
declaration of another function that invokes it.

ANS: T PTS: 1 REF: 103

8. A compiler scans a program from top to bottom.

ANS: T PTS: 1 REF: 103

9. The following program will compile correctly.

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}

ANS: F PTS: 1 REF: 104

10. A function creates a group of lines of code that can be called multiple times to perform a task that
is done repeatedly.

ANS: T PTS: 1 REF: 104

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.

ANS: T PTS: 1 REF: 105-106

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.

ANS: T PTS: 1 REF: 107

13. In Objective-C, functions can only return one value.

ANS: T PTS: 1 REF: 107


14. The parameters passed to a function must only be of basic data types such as int, char,
double, or float.

ANS: F PTS: 1 REF: 107

15. A function cannot call another function.

ANS: F PTS: 1 REF: 109

16. The scope of a local variable is within the set of curly braces in which it was declared.

ANS: T PTS: 1 REF: 111

17. In the following code section, getCoins(amount) is a function call.


if (num == 4
{
NSLog(@" %i quarters dispensed", getCoins(amount));
}

ANS: T PTS: 1 REF: 116

MULTIPLE CHOICE

1. A function consists of a declaration and a(n) ____.


a. body c. initialization
b. loop d. return type

ANS: A PTS: 1 REF: 102

2. If the ____ of a program is missing, then nothing can be executed.


a. program declaration c. program initialization
b. main function d. initialize function

ANS: B PTS: 1 REF: 102

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

ANS: B PTS: 1 REF: 102

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

ANS: C PTS: 1 REF: 102


5. The maximum number of variables that can be passed to a function is ____.
a. 3 c. 10
b. 5 d. unlimited

ANS: D PTS: 1 REF: 102

6. In the following program, on what line is the function welcome declared?


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

ANS: A PTS: 1 REF: 103

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 }

a. body type c. function type


b. argument type d. return type

ANS: D PTS: 1 REF: 103

8. In the following example, on which line is the function welcome called?

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

ANS: D PTS: 1 REF: 103

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

ANS: B PTS: 1 REF: 103

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

ANS: D PTS: 1 REF: 103

11. The ____ is the type of data the function will return when executed.
a. actual type c. scope
b. return type d. parameter type

ANS: B PTS: 1 REF: 107

12. If a function does not return a value, then its return type is defined as ____.
a. void c. false
b. null d. 0

ANS: A PTS: 1 REF: 107

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

ANS: C PTS: 1 REF: 107

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

ANS: D PTS: 1 REF: 107

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

ANS: B PTS: 1 REF: 107

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

ANS: C PTS: 1 REF: 107


18. The visibility range of a function is called its ____.
a. locality c. range
b. currency d. scope

ANS: D PTS: 1 REF: 111

19. A ____ variable can be accessed by all functions within a program.


a. local c. global
b. scope d. range

ANS: C PTS: 1 REF: 111

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

ANS: C PTS: 1 REF: 111

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

ANS: A PTS: 1 REF: 111

22. The parameters of a function are ____ to the function.


a. invisible c. local
b. prototyped d. formalized

ANS: C PTS: 1 REF: 111

23. What is the prototype for the following function?


double diff (double n1, double n2)
{
double totalGrowth;
totalGrowth = n2 - n1;
return totalGrowth;
}
a. double diff (n1, n2);
b. diff (double n1, double n2);
c. diff (n1, n2);
d. double diff (double n1, double n2);

ANS: D PTS: 1 REF: 112


24. What return type should be specified for a function, avg, that takes two double parameters,
totalgrowth and years, and has the following body:

{
return totalgrowth/years;
}
a. double c. void
b. int d. float

ANS: A PTS: 1 REF: 112

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 }

25. In Figure 5-2, the variable, totalGrowth, on Line 4 is ____.


a. global c. local to diff
b. local to main d. local to avg
ANS: A PTS: 1 REF: 112

26. In Figure 5-2, the variable totalGrowth on Line 33 is ____.


a. global c. local to diff
b. local to main d. local to avg

ANS: C PTS: 1 REF: 113

27. In Figure 5-2, the variable, totalGrowth, on Line 38 is ____.


a. global c. local to diff
b. local to main d. local to avg

ANS: D PTS: 1 REF: 113

28. In Figure 5-2, the variable, initialHeight, on Line 9 is ____.


a. global c. local to diff
b. local to main d. local to avg

ANS: B PTS: 1 REF: 113

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

ANS: B PTS: 1 REF: 113

30. A ____ variable is live during the entire execution of a program.


a. scoped c. global
b. ranged d. local

ANS: C PTS: 1 REF: 113

31. In the following function, what is the scope of the variable, amt, on line 8?

1 void selection(int num){


2 int account;
3 NSLog(@"Enter an account number");
4 scanf("%i", &account);
5 if (account == account1 || account == account2 ||
6 account == account3) {
7 if (num == 1) {
8 double amt;
9 NSLog(@"Enter the amount to deposit");
10 scanf("%lf", &amt);
11 deposit(account, amt);
12 }
13 else if (num == 2) {
14 double amt;
15 NSLog(@"Enter the amount to withdraw");
16 scanf("%lf", &amt);
17 withdrawal(account, amt);
18 }
19 else if (num == 3) {
20 NSLog(@"The account balance is $%.2f",
21 checkBalance(account));
22 }
23 }
24 }
a. global
b. the selection function
c. between the curly braces on lines 7 and 12
d. between the curly braces on lines 6 and 23

ANS: C PTS: 1 REF: 115

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);

ANS: B PTS: 1 REF: 117

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);

ANS: B PTS: 1 REF: 117

You might also like