#G083. GESP level4 2025年3月四级试卷
GESP level4 2025年3月四级试卷
GESP level4 - 2025年3月四级试卷
- 关于下述代码,说法错误的是( )。
{{ select(1) }}int multiply(int x, int y); int main() { int a = 4; int b = 5; int result = multiply(a, b); std::cout << "The result is: " << result << std::endl; return 0; } int multiply(int x, int y) { return x * y; }
- 函数multiply 的定义应该放到函数main 之前。
- 函数声明int multiply(int x, int y); 中明确指定了函数multiply() 的返回值为整数类型。
- 在main 函数中,函数multiply 通过multiply(a, b) 被调用,其中a 和b 是定义在main 函数中的变量,它们作为实参传递给了multiply 函数的形参x 和y 。
- 运行上述代码,将输出The result is: 20 。
- 执行下述代码将输出( )。
{{ select(2) }}int x = 10; void func() { int x = 20; std::cout << x; } int main() { func(); std::cout << x; return 0; }
- 2020
- 2010
- 1020
- 编译错误
- 执行下述代码后,变量a 的值为( )。
{{ select(3) }}int a = 10; int* p = &a; *p = 20;
- 10
- 20
- 随机值
- 编译错误
- 以下哪种参数传递方式可以避免拷贝大型对象? {{ select(4) }}
- 只能用值传递
- 只能用引用传递
- 只能用指针传递
- 引用传递和指针传递均可
- 执行下述代码,将输出( )。
{{ select(5) }}void swap(int a, int &b) { int temp = a; a = b; b = temp; } int main() { int x = 1, y = 2; swap(x, y); std::cout << x << y; return 0; }
- 12
- 21
- 22
- 11
- 下面的描述中,( )正确定义一个名为Person 的结构体并正确初始化了一个Person 结构体的变量p 。 {{ select(6) }}
- struct Person { string name; int age; }; Person p("Yang", 10);
- struct Person { string name, int age; }; Person p; p.age = 10; p.name = "Yang";
- struct Person { string name; int age; }; Person p = { "Yang", 10 };
- struct Person { string name; int age; }; Person p = new Person("Yang", 10);
- 给定如下代码,
下面描述错误的是( )。 {{ select(7) }}struct Person { std::string name; int age; struct Address { std::string street; std::string city; } address; };
- 结构Person 内嵌套结构Address
- Person 有一个Address 类型的 address 成员
- 一个Person 类型的变量p 的address 的初始化可以写成:p.address.street = "123 Main St"; p.address.city = "Anytown";
- 结构的嵌套可以减少命名冲突,因此可以不必控制嵌套层次
- 假设int arr[2][3] = {{1,2,3},{4,5,6}}; ,则arr[1][2] 的值是( )。 {{ select(8) }}
- 2
- 3
- 5
- 6
- 下面( )正确定义了二维数组。 {{ select(9) }}
- int arr[3,4];
- int arr[3][4];
- int arr(3,4);
- int a[3-4];
- 小杨正在爬楼梯,需要爬n阶才能到达楼顶。如果每次可以爬1个或2个台阶,下面代码采用递推算法来计算一共有多少种不同的方法可以爬到楼顶,则横线上应填写( )。
{{ select(10) }}int f(int n) { if (n == 1 || n == 2) return n; int f1 = 1; int f2 = 2; int res = 0; for (int i = 3; i <= n; i++) { // 在此处填入代码 } return res; }
- res += f1 + f2; f1 = f2; f2 = res;
- res = f1 + f2; f1 = f2; f2 = res;
- res += f1 + f2; f2 = res; f1 = f2;
- res = f1 + f2; f2 = res; f1 = f2;
- 给定如下算法,其时间复杂度为( )。
{{ select(11) }}bool f(int arr[], int n, int target) { for (int i = 0; i < (1 << n); i++) { int sum = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum += arr[j]; } } if (sum == target) return true; } return false; }
- O(n×2ⁿ)
- O(2ⁿ)
- O(n²)
- O(n)
- 下面关于排序稳定性的描述,正确的是( )。 {{ select(12) }}
- 稳定性指算法的时间复杂度恒定
- 稳定排序保证相同元素的相对顺序不变
- 选择排序是稳定排序
- 插入排序不是稳定排序
- 对数组arr[]={5, 3, 8, 1} 进行升序排序,执行第一轮冒泡排序后数组arr 中的内容为( )。 {{ select(13) }}
- 3, 5, 1, 8
- 3, 1, 5, 8
- 3, 5, 8, 1
- 5, 3, 8, 1
- 运行下面的代码,将出现( )。
{{ select(14) }}#include #include using namespace std; double hmean(double a, double b) { if (a == -b ) throw runtime_error("Runtime error occurred."); return 2.0ab/(a + b); } int main() { double x = 10; double y = -10; try { int result = hmean(x, y); cout << "hmean: " << result << endl; } catch (const runtime_error& e) { cout << "Caught: " << e.what() << endl; } catch (...) { cout << "Caught an unknown exception." << endl; } return 0; }
- 屏幕上输出Caught: Runtime error occurred.
- 屏幕上输出Caught an unknown exception.
- 程序调用 std::terminate()
- 编译错误
- 下面哪种方式不能实现将字符串"Happy Spring!" 输出重定向到文件log.txt ( )。 {{ select(15) }}
- freopen("log.txt", "w", stdout); cout << "Happy Spring!" << endl; fclose(stdout);
- std::ofstream outFile("log.txt"); outFile << "Happy Spring!" << endl; outFile.close();
- std::ofstream outFile("log.txt"); cout << "Happy Spring!" << endl; outFile.close();
- ofstream log_file("log.txt"); streambuf* org_cout = cout.rdbuf(); cout.rdbuf(log_file.rdbuf()); cout << "Happy Spring!" << endl; cout.rdbuf(org_cout);
- 函数是C++中的核心概念,用于封装可重用的代码块。 {{ select(16) }}
- 正确
- 错误
- 在C++中,函数的返回类型可以省略,默认为int 。 {{ select(17) }}
- 正确
- 错误
- 结构体的成员默认是public 访问权限。 {{ select(18) }}
- 正确
- 错误
- 假设整数数组arr[4]={0,1,2,3} 的第一个元素在内存中的地址为0x7ffee4065820 ,经过int* p = arr; p+=1;后,指针p 的值是1。 {{ select(19) }}
- 正确
- 错误
- 二维数组作为函数参数时,必须显式指定所有维度的大小。 {{ select(20) }}
- 正确
- 错误
- 递推是一种通过已知的初始值和递推公式,逐步求解目标值的算法。 {{ select(21) }}
- 正确
- 错误
- 考虑最坏情况下冒泡排序算法的时间复杂度,T(n) 为待排序数字的数目为n的复杂度,则其递推关系式为T(n)=T(n-1)+n,T(0)=1。 {{ select(22) }}
- 正确
- 错误
- 插入排序在最好情况(已有序)下的时间复杂度是O(n²) 。 {{ select(23) }}
- 正确
- 错误
- 对数组arr[]={4,3,1,5,2} 进行升序排序,执行第一轮选择排序后数组arr中的内容是{1, 4, 3, 5, 2} 。 {{ select(24) }}
- 正确
- 错误
- 未捕获异常会调用std::terminate终止程序。 {{ select(25) }}
- 正确
- 错误