#4930. [GESP202503四级]客观题
[GESP202503四级]客观题
一.单选题(每题2分,共30分)
- 关于下述代码,说法错误的是( )。
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;
}
{{ select(1) }}
- 函数 multiply 的定义应该放到函数 main 之前。
- 函数声明 int multiply(int x, int y); 中明确指定了函数 multiply() 的返回值为整数类型。
- 在 main 函数中,函数 multiply 通过 multiply(a, b) 被调用,其中 a 和 b 是定义在 main 函数中的变量,它们作为实参传递给了 multiply 函数的形参 x 和 y 。
- 运行上述代码,将输出 The result is: 20 。
- 执行下述代码将输出( )。
int x = 10;
void func() {
int x = 20;
std::cout << x;
}
int main() {
func();
std::cout << x;
return 0;
}
{{ select(2) }}
- 2020
- 2010
- 2005
- 编译错误
- 执行下述代码后,变量 a 的值为( )。
int a = 10;
int* p = &a;
*p = 20
{{ select(3) }}
- 10
- 20
- 随机值
- 编译错误
4.以下哪种参数传递方式可以避免拷贝大型对象?( )
{{ select(4) }}
- 只能用值传递
- 只能用引用传递
- 只能用指针传递
- 引用传递和指针传递均可
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;
}
{{ select(5) }}
- 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.name = "Yang"; p.age = 10;
-
struct Person { string name; int age; }; Person p = { "Yang", 10 };
-
struct Person { string name; int age; }; Person p = new Person("Yang", 10);
- 给定如下代码,
struct Person {
std::string name;
int age;
struct Address {
std::string street;
std::string city;
};
Address address;
};
下面描述错误的是( )。
{{ select(7) }}
- 结构 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];
- 小杨正在爬楼梯,需要爬 阶才能到达楼顶。如果每次可以爬 个或 个台阶,下面代码采用递推算法来计算 一共有多少种不同的方法可以爬到楼顶,则横线上应填写( )。
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;
}
{{ select(10) }}
-
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;
- 给定如下算法,其时间复杂度为()
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;
}
{{ select(11) }}
12.下面关于排序稳定性的描述,正确的是()
{{ 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
- 运行下面的代码,将出现()。
double hmean(double a, double b) {
if (a == -b )
throw runtime_error("Runtime error occurred.");
return 2.0*a*b/(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;
}
{{ select(14) }}
- 屏幕上输出 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);
二.判断题(每题2分,共20分)
16.函数是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) }}
- 正确
- 错误
- 考虑最坏情况下冒泡排序算法的时间复杂度,为待排序数字的数目为 的复杂度,则其递推关系式为。
{{ select(22) }}
- 正确
- 错误
- 插入排序在最好情况(已有序)下的时间复杂度是。
{{ select(23) }}
- 正确
- 错误
- 对数组 arr[]={4, 3, 1, 5, 2} 进行升序排序,执行第一轮选择排序后数组arr中的内容是 {1, 4, 3, 5,2} 。
{{ select(24) }}
- 正确
- 错误
- 未捕获异常会调用std::terminate终止程序。
{{ select(25) }}
- 正确
- 错误