Computing I: Homework Packet 4 - File (now you can have fun, go to a game or two)
7) What symbol is used to signify that a parameter is a pointer?
*
5) Given the following function definition fragment, for which values of myInt should the function be tested? int doSomething(int myInt) { if(myInt < 0) { //do some stuff here } return -1; }
-1, 0, and 1
37 ) What is the value of chice after the following statements? Void getChoice(&choice, count); { if(par_ **** < 0) *par_choice = 0; if(par_count = 0); *par_choice = -1; else *par_choice = 99; return; } A) 99 B) 0 C)3 D) -1
A) 99
23) A simplified version of a function which is used to test the main program is called. A) a stub B) abstraction. C) a driver D) polymorphism
A) a stub
40) Testing a function or program using test value that are at or near the value that change the outcome of the program is known as using A) boundary value B) a black-box C) parameter D) functional decomposition
A) boundary value
21) Given the following function declaration and local variable declarations, which of the following is not a correct function call? int myInt; float myFloat; char ch; void someFunction(int* first, float second, char third); A) someFunction(1,2.0,ch); B) someFunction(&myInt, myFloat, ch); C) someFunction(&myInt, myFloat, '1'); D)someFunction(&myInt, 2.0,'c');
A) someFunction(1,2.0,ch);
26) To mimic Call-by-reference parameter passing, parameters are passed. A) the address of the argument B) A copy of the value in the actual argument. C) the value in the actual argument. D) nothing
A) the address of the argument
2) The & operator is called the ______ Operator
Address of
13) The items passed to a function are called_______.
Arguments
19) What is the output of the following code fragments? int trail(int* a, int b) { if (b > *a) { *a = b return -*a; { else { return 0; } } int x = 0, y= 10,z; z = trial(&y,x); printf("%d%d%d\n". z,x,y); A) 10 0 0 B) 0 0 10 C) 0 10 0 D) -10 0 0
B) 0 0 10
33) Given the function definition void something(int a, int* b) { inc; c = a + 2; a = a * 3; *b = c + a } what is the output after the following code fragment that invokes something int r = 1; int s = 2; int t = 3; printf("%d %d %d\n", r, s, t); A) 1 10 3 B) 1 14 3 C) 1 14 9 D) 5 14 3
B) 1 14 3
18) Which of the following are true? A) As long as the function is define anywhere in your program, it can be used anywhere else. B) A function can call another function. C) A function definition can contain another function definition. D) if you have function prototypes (declarations), the order in which you define your function is important.
B) A function can call another function.
24) What is the output of the following function and function call? void calculateCost(int count, float* subTotal, float* taxCost); float tax = 0.0 subtotal = 0.0 calculateCost(15, &subtotal, &tax); printf( "The cost for 15 items is %.2f, and tax for %.2f is %.2f", subtotal, subtotal, tax); //end of fragment void calculateCost(int count, float* subTotal, float* taxCost); { if( count < 10) { *subTotal = count * 0.50; } else } *subTotal = count * 0.20; } *taxCost = 0.1 * *subTotal; } A) The cost for 15 items is 3.00, and the tax for 3.00 is 0.00 B) The cost for 15 items is 3.00, and the tax for 3.00 is 0.30 C) The cost for 15 items is o.00, and the tax for 3.00 is 0.00 D) The cost for 15 items is 0.00, and the tax for 0.00 is 0.00
B) The cost for 15 items is 3.00, and the tax for 3.00 is 0.30
43) What is the output of the following function and function call? void calculateCost(int count, float* subTotal, float taxCost); float tax = 0.0, subtotal = 0.0; calculateCost(15, &subtotal, tax); printf("The cost for 15 items is %.2f, and the tax for %.2 is %.2", subtotal, subtotal, tax); // end of fragment void calculateCost(int count, float* subTotal, float taxCost) { if( count < 10) { *subTotal = count * o.50; } else { *subTotal = count * 0. 20; } taxCost = 0.1* *subTotal; } A) The cost for item is 3.00, and the tax for 3.00 is 0.30; B) The cost for item is 3.00, and the tax for 3.00 is 0.00; C) The cost for item is 0.00, and the tax for 3.00 is 0.00; D) The cost for item is 0.00, and the tax for 3.00 is 0.30;
B) The cost for item is 3.00, and the tax for 3.00 is 0.00;
22) The deference operator, *, can be applied to. A) only pointer variable B) any expression that evaluates as an address. C) all variables. D) parameters only in void functions.
B) any expression that evaluates as an address.
35) Testing your program should be done A) only if your instructor requires it. B) as each function is developed. C) at the end of the coding. D) only if there appear to be problems.
B) as each function is developed.
42) What is wrong with the following function body? void calculate(int count, float price, float* cost) { if(count < 0) *cost = 0.0; else *cost = count * price; return; } A) Void function must return a value. B) nothing C) V0id function may not have a return statement. D) Cannot mix reference style and value parameters.
B) nothing
27) If you need a function to get both the number of items and the cost per item from a user, which would be a good function declaration to use? A) void getData(int count, float cost); B) void getData(int* count, float* cost); C) int getData(float cost); D) int,float getData(void);
B) void getData(int* count, float* cost);
28) You should make a parameter a reference style parameter by making it a pointer if A) you need to be able to change the value of the parameter in the function, but not the value of argument. B) you need the function to change the value of the argument passed to the function. C) always D) any of the other parameters are reference style parameters.
B) you need the function to change the value of the argument passed to the function.
10) Pre and post conditions for a function should be written (before/after) the function definition is written.
Before
8) Testing a program with values that are close to value that will change the execution of a program is called_______.
Boundary value testing
20) Which of the following comments would be the best post- condition for this swap function? void swap (int*left, int* right); A) //Postcondition: None B) //Postcondition: the value of left and right are exchanged C) // Postcondition: the value at the addresses held in left and right are exchanged D) // Postcondition: left has the value of right
C) // Postcondition: the value at the addresses held in left and right are exchanged
32) Given the following function definition, void shift(int*a, int*b) ( *a = b *b = *a; } what is the output after the following function call int first = 0, second = 10; shift(7first, &second); printf('%d %d \n", first, secon); A) 10 0 B) 0 0 C) 10 10 D) 0 10
C) 10 10
25) When a void function is called, it is known as A) a returned value. B) a comment. C) an executable statement. D) an output function.
C) an executable statement.
39) The postcondition of a function A) declares the function for the complier B) determines how the function will complete its job C) tells what will be true after the function executes. D tells what must be true before the function executes
C) tells what will be true after the function executes.
30) If you were to write a function for displaying the cost of an item to the screen, which function prototype would be most appropriate? A) float display( void ) ; B) void display( void ); C) void display(float myCost); D) int display (float myCost);
C) void display(float myCost);
Given the following function definitions and program fragments, what is the output? void f1 (int* z, int *q) { int temp temp = *q; *q = *z; *z = temp; } void f2( int* a, int* b) { if(*a < *b) f1(a,b); else *a = *b; } int x = 3, y = 4 f2(&y, &x); print("%d %d\n", x, y); A) 3 4 B) 4 3 C) 4 4 D) 3 3
D) 3 3
Which of the following is a legal call to the displayOutput function assuming myTotal is an integer variable? void displayOutput( int total); A) void displayOutput(myTotal); B) displayOutput(int myTotal); C) printf("%d\n", displayOutput(myTotal)); D) DisplaysOutput(myTotal);
D) DisplaysOutput(myTotal);
17) In the following function, what is passed to the first parameter? void f1(int* value1, int value2); int x,y; f1(&x,y); A) the value of x B) the value of y C) Nothing, it is a void function. D) The memory location of the variable x.
D) The memory location of the variable x.
31) A simplified main program used to test functions is called A) abstraction B) a stub C) polymorphism D) a driver
D) a driver
34) If a function needs to modify more than one variable, it must. A) be a void function B) use pass by value parameter passing C) return all values needed D) mimic pass by reference parameter passing by passing pointers.
D) mimic pass by reference parameter passing by passing pointers.
36) The precondition(s) for the function describe A) how to call the function. B) what the function does. C) what is true after the function is executed. D) what must be true before the function is executes.
D) what must be true before the function is executes.
15) If we want to test if a given function works correctly, we would write a _____ to test it
Driver
3) A ______ is a main program that only checks that function execute correctly.
Driver
41) Which of the following is true for a void function? A) There cannot be a return statement. B) The value of 0 should be returned. C) The value of void should be returned. D) Nothing is returned. E) A and D
E) A and D
44) Which of the following function prototypes are not valid? A) void doSomething(int x, int y); B) void doSomething(int* x, int* y); c) void doSomething(int* x, int y); D) All are not valid. E) All are valid
E) All are valid
38) what is wrong with the following code? int f1( int x, int y) { x = y *y; return x; int f2 ( float a, float* b ) { if (a < b* ) *b = a else a = *b return; } } A) Neither function should return a value B) In f2, a cannot be assigned *b. C) Both parameters to f2 should mimic pass-by reference and be pointers. D) Nothing is wrong E) Function definitions may not be nested
E) Function definitions may not be nested
46) It is illegal to call other functions from inside a function definition.
False
47) A void function can be used in an assignment.
False
52) A stub is a function that is completely defined an well tested.
False
55) a void function can return any value.
False
16) The value or variable listed in the function declaration are called______ to the function.
Formal paramater
9) What type of value does a void function return?
None/Nothing
1) Using Functions in a program is called
Procedural Abstraction
11) what is the correct way to call the following function? void setDisplay(void);
Set Display();
4) A ____ is a simplified version of a function used to test main programs.
Stub
48) It is acceptable to have both normal variable and pointer variable parameters in the same function declaration.
True
49) The dereference operator * is a unary operator meaning it only has one operand.
True
50) The following is legal in a void function return;
True
51) In the function mimicing pass-by-reference parameters, the value of the actual arguments passes to the function are addresses.
True
53) Functions can return at most one value.
True
54) A function that passes the address of a variable as a parameter is allowed to make changes to the argument through the dereference operator.
True
6) A function that does not return a value is known as a _____ function
Void
12) what is the most appropriate way to call the doThings function? Assume that you have two variables named var1 and var2 defined as follows. int var1; float var2;
doThing(var1, var2)
14) When the address of the actual argument is passed to the formal parameter, this allows us to mimic a parameter passing mechanism called______.
passed by reference