Functions and Arrays
Look at Table 6-1 in the textbook (page 338). Read the information on the toupper function. Make note of the parameter type, and the data type of the result. You can even try this out in Visual Studio. Based on that information, what is the output from the following statement: cout << toupper('a') << endl; A. 97 B. a C. 65 D. A
C. 65
What is the output of the following code: int list[10]={1,2};cout << list[7] << endl; A. 7 B. Error, you must initialize all 10 elements. C. The null character: \0 D. 0
D. 0
What is the output from the following: int xlist[]={1,2,10,100,200}; cout << xlist << endl; A. 1 B. Displays random data that was stored in that unknown memory space. C. Error - you cannot do an aggregate operation D. Displays the base address
D. Displays the base address
A value parameter (when you pass a variable by value) receives the address (memory location) of its corresponding actual parameter. True/False
False
Arrays are always passed by value in a function, unless you use the & operator in the formal parameter list. True/False
False
//Line 3 contains an aggregate operation and is not allowed in C++: int yourlist[5];int mylist[5]={1,2,3,4,5}; yourlist=mylist; //Line 3 True/False
True
A function can have more than one return statement. True/False
True
A function prototype includes (a) the function type, (b) the function name, (c) the number of parameters, and (d) the parameter types. True/False
True
A return statement returns only one value. True/False
True
C++ does not allow functions to return a value of type array. True/False
True
C++ provides the user a standard set of predefined functions (that you may or may not use). True/False
True
Consider the following function prototypes: int func1(int x, double y); double func2(string, int, double); char func3(int, int, double, char); string join(string, string); Based on the above prototypes, the following statement in the main body of the program is a valid statement (true or false): cout << func1(3, 2.1); True/False
True
Functions enable you to divide a program into manageable tasks. True/False
True
If MATRIX is a two-dimensional array, then the base address of MATRIXis the address of MATRIX[0][0]. True/False
True
In a function call statement, when passing an array as an actual parameter, you use only its name, as shown below: int myarray[3]={1,2,3};MYFUNCTION(myarray); True/False
True
Is the following a palindrome? So many Dynamos True/False
True
The following array definition is valid: char greeting[] = {'H', 'e', 'l', 'l', 'o'}; True/False
True
The scope of an identifier (variable) refers to those parts of the program where it is accessible. True/False
True
There are no aggregate operations on arrays, except for the input/output of character arrays (C-strings). True/False
True
Variables defined in the function heading are called formal parameters. True/False
True
Which of the following will output the elements of the array xlist in reverse order?
string xlist[]={"one","two", "three", "four"}; int i,x=0; for (i=3; i>=0; --i) cout << xlist[i] << " ";
In C++, an array index starts with the number ______.
0
Consider the following function prototypes: int func1(int x, double y); double func2(string, int, double); char func3(int, int, double, char); string join(string, string); How many parameters does the function func1 have?
2
Consider the following function prototypes: int func1(int x, double y); double func2(string, int, double); char func3(int, int, double, char); string join(string, string); How many parameters does the function func2 have?
3
What is the output of the following code: int xlist[]={1,2,10,100,200};cout << xlist[2] << endl; A. 10 B. Error. C. 0 D. 2
A. 10
What is the output of the following code: #include <iostream> #include <cmath> using namespace std; void mystery(int&, int); int main () { int y=3, a; a=mystery(y,3); cout << y << " " << a << endl; } void mystery(int& x, int y) { x=y*2; return y; } A. Error: You cannot return a value on a void function. B. 3 3 C. 0 3 D. 6 3
A. Error: You cannot return a value on a void function.
What is the purpose of the following code: int xlist[]={1,234,10,100,200}; int i,x=0; for (i=0; i<5; ++i) if (xlist[i] > x) x=xlist[i]; cout << x; A. It outputs the largest item of the array xlist (in this case 234). B. It sums the items of the array xlist. C. It outputs the smallest item of the array xlist ( in this case 1). D. It initializes the array xlist to all 0's.
A. It outputs the largest item of the array xlist (in this case 234).
What is the output of the following code: int i;char text[20] = "JJWatt" ; for (i=0; text[i] != '\0'; i++) cout << text[i] ; A. JJWatt B. /0 C. 6 D. Error
A. JJWatt
int myfunction(int array[]){ return 0;} Which of the following would be a correct prototype to the function defined above. A. int myfunction(int []); B. int myfunction(int array[]) C. int myfunction([]); D. int myfunction(int);
A. int myfunction(int []);
To declare the two-dimensional integer array MATRIX[3][2] as a formal parameter in a void function MYFUNCTION, you would do the following: A. void MYFUNCTION(int MATRIX[][2]) B. void MYFUNCTION(int MATRIX[][]) C. void MYFUNCTION(int MATRIX[2][3]) D. void MYFUNCTION(int MATRIX[3][])
A. void MYFUNCTION(int MATRIX[][2])
What is the output of the following code: #include <iostream>#include <cmath>using namespace std; int mystery(int&, int); int main () { int y=3, a; a=mystery(y,3); cout << y << " " << a << endl; } int mystery(int& x, int y) { x=y*2; return y; } A. 0 3 B. 6 3 C. 3 3 D. Error: You cannot pass by reference if the function is not void.
B. 6 3
What does the following code do? int xlist[]={1,2,3,4,5,6,7,8,9,10}; int i; for (i=0; i<10; ++i) if (i % 2 == 0) cout << endl << xlist[i] << " "; else cout << xlist[i] << " "; A. It doesn't run; there is an error. B. It outputs the elements of the array, two items per line. C. It only outputs the even numbers of the array. D. Only prints out the element of the array if it equals to 0.
B. It outputs the elements of the array, two items per line.
Consider the following function prototypes: int func1(int x, double y); double func2(string, int, double); char func3(int, int, double, char); string join(string, string); Based on the above prototypes, the following statement in the main body of the program is a valid statement (true or false): cout << func3(1, 2, 1.1, "A"); True/False
False
Consider the following function prototypes: int func1(int&, double); Based on the above prototype, the following statement in the main body of the program is a valid statement (true or false): cout << func1(3, 5.0);
False
Functions headers are terminated with a semicolon True/False
False
In C++, the names of the corresponding formal and actual parameters (the parameter names in the function heading and the function call) must be the same. True/False
False
The corresponding actual parameter in the function call when you pass by reference or value can be an expression, a variable, or a constant value. True/False
False
The following is a valid C-string declaration: char name[6]="JJWatt"; True/False
False
The function prototype is a copy of the function heading at the top of the program, without the body of the function. The prototype (like the function heading) does NOT have a semi-colon at the end of the line. True/False
False
The size of the following array is 6. char name[]="JJWatt"; True/False
False
There are two types of user-defined functions: value-returning and void. True/False
False
When you include a * after the data type of a formal parameter, the formal parameter becomes a reference parameter. True/False
False
Which of the following will correctly sum up all the numbers of the array, xlist, and display that total.
int xlist[]={1,2,10,100,200}; int i,x=0; for (i=0; i<5; ++i)x=x+xlist[i]; cout << x;