#G087. GESP level4 函数
GESP level4 函数
GESP level4 - 函数
- 下面函数不能正常执行的是()
{{ select(1) }}0 #include 1 using namespace std; 2 int func() 3 { 4 //… 5 } 6 int main() 7 { 8 func(); 9 return 0; 10 }
- 函数定义在main函数之前
- 函数定义在main函数之后且无声明
- 函数定义在main函数之后但有声明
- 函数声明在main函数之前,定义在之后
- 下面程序输出的是()
{{ select(2) }}1 #include 2 using namespace std; 3 int func(); 4 int main() 5 { 6 int i=2; 7 cout<<i<<endl; 8 for(int x=0;x<1;x++) 9 { 10 int i=10; 11 cout<<i<<endl; 12 } 13 i=i+1; 14 cout<<i<<endl; 15 { 16 i=i*i; 17 cout<<i<<endl; 18 } 19 }
- 2 2 3 9
- 2 10 3 9
- 2 10 11 121
- 2 10 3 100
- 若函数声明为int f(int &x){ x+=3; return x; },则对声明的变量int a=3,下面哪个调用能够改变a的值( )。 {{ select(3) }}
- f(&a)
- f(*a)
- f(a)
- f(a-3)
- 执行下述代码将输出( )
{{ select(4) }}1 int x = 10; 2 void func() { int x = 20; std::cout << x; } 3 int main() { 4 func(); 5 std::cout << x; 6 return 0; 7 }
- 2020
- 2010
- 1020
- 编译错误
- 给定函数声明int multiply(int x, int y);,以下说法错误的是( )
{{ select(5) }}1 int multiply(int x, int y); 2 int main() { 3 int a = 4; 4 int b = 5; 5 int result = multiply(a, b); 6 std::cout << "The result is: " << result << std::endl; 7 return 0; 8 } 9 int multiply(int x, int y) { 10 return x * y; 11 }
- 函数定义应放在main之前
- 函数声明指定返回值为int
- a和b作为实参传递给形参x和y
- 输出结果为20
- 下面关于函数参数传递的说法正确的是( )。 {{ select(6) }}
- 值传递时子函数修改参数不影响主函数变量
- 值传递时子函数修改参数会影响主函数变量
- 地址传递时子函数修改参数不影响主函数变量
- 参数传递可不满足参数个数要求
- 以下哪个选项能正确定义一个计算浮点数平方的函数并调用( )。 {{ select(7) }}
- float square(float x) { return x * x; } float area = square(2);
- square(float x) { return x * x; } float area = square(2);
- void square(float x) { return x * x; } area = square(2.0);
- void square(float x) { *x; return; } area = square(2);
- 运行下面代码,变量a的值为( )
{{ select(8) }}1 void func(int& x) { 2 x = x * 2; 3 } 4 int a = 5; 5 func(a);
- 5
- 10
- 15
- 20
- 下面代码采用递推算法计算斐波那契数列,横线上应填写( )
{{ select(9) }}1 int fib(int n) { 2 if (n == 0 || n == 1) 3 return n; 4 int f1 = 0, f2 = 1, result = 0; 5 for (int i = 2; i <= n; i++) { 6 // 在此处填入代码 7 } 8 return result; 9 }
- result = f1 + f2; f1 = f2; f2 = result;
- result += f1 + f2; f1 = f2; f2 = result;
- result += f1 + f2; f2 = result; f1 = f2;
- result = f1 + f2; f2 = result; f1 = f2;
- 下面函数中采用的算法是( )
{{ select(10) }}1 int fib(int n) { 2 int i, f[n]={0, 1}; 3 for(int i=2; i<=n; i++) 4 f[i]=f[i-1]+f[i-2]; 5 return f[n]; 6 }
- 递推
- 递归
- 迭代
- 循环
- 下列关于函数的叙述正确的是( )。 {{ select(11) }}
- 函数必须有名字
- 函数必须有参数
- 函数必须有返回值
- 函数定义必须在调用前
- 在C++中,函数的参数默认以什么方式传递( )。 {{ select(12) }}
- 值传递
- 引用传递
- 指针传递
- 地址传递
- 以下哪个函数声明可以传递二维数组名作为参数( )。 {{ select(13) }}
- void BubbleSort(int a[][4]);
- void BubbleSort(int a[3][]);
- void BubbleSort(int a[][]);
- void BubbleSort(int ** a);
- 在下列代码横线处填写( ),使输出为“20 10”
{{ select(14) }}1 #include 2 using namespace std; 3 void xchg(__________) { 4 int t = x; 5 x = y; 6 y = t; 7 } 8 int main() { 9 int a = 10, b = 20; 10 xchg(a, b); 11 cout << a << " " << b << endl; 12 return 0; 13 }
- int x, int y
- int &x, int &y
- int a, int b
- int &a, int &b
- 下面代码实现插入排序,横线上应填写( )
{{ select(15) }}1 void insertion_sort(vector &nums) { 2 for (int i = 1; i < nums.size(); i++) { 3 int base = nums[i], j = i - 1; 4 { // 在此处填入代码 5 nums[j + 1] = nums[j]; 6 j--; 7 } 8 nums[j + 1] = base; 9 } 10 }
- while (j >= 0 && nums[j] > base)
- while (j > 0 && nums[j] > base)
- while (j >= 0 && nums[j] < base)
- while (j > 0 && nums[j] < base)
- 冒泡排序函数的时间复杂度为( )。 {{ select(16) }}
- O(n)
- O(n²)
- O(n log n)
- O(1)
- 下面关于排序算法稳定性的描述正确的是( )。 {{ select(17) }}
- 稳定性指时间复杂度恒定
- 稳定排序保证相同元素相对顺序不变
- 选择排序是稳定排序
- 插入排序不是稳定排序
- 函数参数传递时,若传常量值、常量引用和常量指针,能否修改实参( )。 {{ select(18) }}
- 都不能修改
- 常量值不能,引用和指针可以
- 常量值和引用不能,指针可以
- 都可以修改
- 函数不可以调用自己。( ) {{ select(19) }}
- 对
- 错
- 一个可能抛出异常的函数,必须在try子句里调用。( ) {{ select(20) }}
- 对
- 错