1. Are the following function definition statements correct?
(a)
void fun1(int i,int j); { cout<<i+j; }
(b)
void fun1(int i,int j) { void fun2() { cout<< "This is fun2. "<<endl; } cout<<i+j; }
(c )
void fun1(int i,int j) { cout<<i+j; return 1; }
(d)
void fun1(int i,int j) { cout<<i+j; }
Answer: answer: d
One semicolon is added in a;
b it is wrong to define a function inside a function. Functions can be called nested but cannot be defined nested;
c defines a void function. It is wrong to have a return value. If the function type is defined as void, the function has no return value.
2. In the following description of C + + functions, what is correct?
(a) Each function must have at least one parameter
(b) Each function must return a value
(c) the function must be declared or defined before being called
(d) A function cannot call itself
Answer: answer: c
a error: the function can have parameters or no parameters (parameterless function).
b error: a function can have a return value or no return value. When there is no return value, this function is defined as void.
d error: a function call itself is called a recursive call, which is allowed.
3. The following function declaration statement is correct?
(a) int fun(int var1=1,char* var2= "Beijing ",double var3);
==(b) int fun(int,char* = "Beijing ",double =3.14159); ==
(c ) int fun(int var1=1,char* var2= "Beijing ",double var3=3.14159);
int fun(int,char*,double var3= 12.34);
(d) int fun(int var1=1,char*,double var3=3.14159);
Answer: b
When a function is called, the real participation formal parameters are matched from left to right. When defining the default value, it should be defined from right to left. Options a and d do not follow the principle that the default value should be defined from right to left, that is, for the first parameter with default value, there are parameters behind it that do not define default value, which is wrong.
c: The third parameter in the function is defined twice, which is an error.
4. Run the following program and the result is?
#include <bits/stdc++.h> using namespace std; int f(int[ ],int); void main() { int a[]={-1,3,5,-7,9,-11}; cout<<f(a,6)<<endl; } int f(int a[],int size) { int i,t=1; for(i=0;i<size;i++) if(a[i]>0) t*=a[i]; return t; }
Answer: 135
5. Run the following program and the result is?
#include <bits/stdc++.h> using namespace std; int main() { char str[][10] = { "vb","pascal","c++" }, s[10]; strcpy_s(s, (strcmp(str[0], str[1]) < 0 ? str[0] : str[1])); if (strcmp(str[2], s) < 0) strcpy_s(s, str[2]); cout << s << endl; return 0; }
Answer: c++
6. What is the result of running the following program?
#include <bits/stdc++.h> using namespace std; void fun1(const double& i) { cout << i << endl; } void fun2(double& j) { cout << j << endl; } void fun3(double k) { cout << k << endl; } void main() { fun1('a'); fun2('a'); fun3('a'); }
(a) 97 (b) 97 a 97 (c) a (d) program error, unable to run.
Answer: answer: d.
Literals, constants, and parameters requiring type conversion can be passed to const & parameters, but not to non const reference parameters. That is, type conversion is not allowed for non const reference parameters. In this problem, the parameters of fun1 and fun3 are implicitly data type converted when calling, while the parameters of fun2 are non const reference parameters, and type conversion is not allowed, so the call to fun2 is wrong. Therefore, the program has an error and cannot run.
7. What is the result of running the following program?
#include <bits/stdc++.h> using namespace std; int findmax(int Iarg[]); float findmax(float Farg[]); double findmax(double Darg[]); int main() { int Iarg[6] = { 15,88,34,12,31,10 }; float Farg[6] = { 145.5,32.3,363.2,19.3,70.1,35.4 }; double Darg[6] = { 15.54323,2.47763,63.29876,19.67863,78.34541,35.44009 }; cout << "largest value in the Iarg is " << (findmax(Iarg)) << "\n"; cout << "largest value in the Farg is " << (findmax(Farg)) << "\n"; cout << "largest value in the Darg is " << (findmax(Darg)) << "\n"; return 0; } int findmax(int Iarg[]) { int max = 0; for (int i = 0; i < 6; i++) { if (Iarg[i] > max) { max = Iarg[i]; } } return max; } float findmax(float Farg[]) { float max = 0; for (int i = 0; i < 6; i++) { if (Farg[i] > max) { max = Farg[i]; } } return max; } double findmax(double Darg[]) { double max = 0; for (int i = 0; i < 6; i++) { if (Darg[i] > max) { max = Darg[i]; } } return max; }
Resolution:
This question mainly examines function overloading. This program is used to solve and output the maximum element of an integer array, the maximum element of a floating-point array and the maximum element of a double precision array. In the main function, the three time findmax() function is invoked. According to the different array types of the transfer arguments, different functions are automatically invoked to complete the solution operation. Here, the findmax() function implements the overloading.
8. What is the result of running the following program?
#include<bits/stdc++.h> using namespace std; int fun(int, int); void main() { cout << "n=" << fun(0, 0) << endl; } int fun(int n, int s) { int s1, n1; s1 = s + n * n; if (s1 < 100) { n1 = n + 1; fun(n1, s1); } else return n - 1; }
The stack pressing is shown in the figure below. When f(7,91) is called, 7-1 = 6 is returned, and the program ends.