32 #5187. [GESP202512四级]模拟测试-客观题

[GESP202512四级]模拟测试-客观题

一.单选题(每题2分,共30分)

  1. 在 C++ 中, ( ) 正确定义了⼀个返回整数值并接受两个整数参数的函数。

{{ select(1) }}

  • int add(int a, int b) { return a + b; }
  • void add(int a, int b) { return a + b; }
  • int add(a, b) { return a + b; }
  • void add(int a, int b) { return a - b; }
  1. 在C++中, 形参与实参的关系描述正确的是 ( )。

{{ select(2) }}

  • 形参在函数调⽤时指定, 实参在函数定义时传递
  • 形参在函数定义时指定, 实参在函数调⽤时传递
  • 形参和实参可以互换
  • 形参和实参必须是完全相同的类型, 不能有任何差异
  1. 运⾏以下代码, 屏幕上将输出

    #include <iostream>
    using namespace std;
    
    int var = 100;
    
    void function() {
    	int var = 200;
    
    	cout << var << " ";
    	cout << ::var << " ";
    }
    
    int main() {
    	cout << var << " ";
    
    	function();
    
    	var += 100;
    	cout << var << " ";
    
        return 0;
    }
    

{{ select(3) }}

  • 100 200 100 200
  • 100 200 100 300
  • 100 200 200 200
  • 100 200 200 300
  1. 运⾏下⾯代码, 屏幕上输出是 ( )。

    int arr[3] = {24, 9, 7};
    int* p = arr;
    p++;
    cout << *p << endl;
    

{{ select(4) }}

  • 2424
  • 99
  • 77
  • 不确定
  1. 运⾏下⾯代码⽚段的结果是 ( )。

    int x = 20;
    int y = 24;
    int* p = &x;
    int* q = &y;
    p = q;
    

{{ select(5) }}

  • 将x赋值为24
  • 将y赋值为20
  • 将q指向x的地址
  • 将p指向y的地址
  1. 下⾯的描述中,( )不能正确定义⼀个名为Student 的结构体以及⼀个包含20个元素的结构数组。

{{ select(6) }}

  • struct Student {
      	 string name;
        	int age;
      	 float score;
     };
     struct Student  students[20];
    
  • struct Student {
      	 string name;
      	 int age;
      	 float score;
     };
     Student  students[20];
    
  • struct Student {
     	string name;
     	int age;
      	 float score;
     };
     Student*  students = new Student[20];
    
  • struct Student {
     	string name;
     	int age;
     	float score;
     };
     Student  students = new Student[20];
    
  1. 给定如下代码,下面描述错误的是( )。
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 类型的变量 paddress 的初始化可以写成:p.address.street = "123 Main St"; p.address.city = "Anytown";
  • 结构的嵌套可以减少命名冲突,因此可以不必控制嵌套层次
  1. ⼀个⼆维数组定义为 int arr[3][4]; (假设⼀个int变量占4个字节) , 则 int arr[0] 占⽤( ) 个字节 的内存( )

{{ select(8) }}

  • 33
  • 44
  • 1212
  • 1616
  1. 下⾯代码采⽤递推算法来实现整数 的阶乘( ) , 则横线上应填写

    int factorial(int n) {
    	int result = 1;
    	for (int i = 2; i <= n; i++) {
    		________________________________ // 在此处填入代码
    	}
    	return result;
    }
    

{{ select(9) }}

  • result *= i;
  • result += i;
  • result *= result;
  • result += result;
  1. 下⾯关于排序算法(冒泡排序、插⼊排序和选择排序)的描述中,不正确的是

{{ select(10) }}

  • 冒泡排序基于元素交换实现,需借助临时变量,共涉及3个单元操作;⽽插⼊排序基于元素赋值实现,仅需1个单元操作。因此冒泡排序的计算开销通常⽐插⼊排序更⾼。
  • 选择排序在任何情况下的时间复杂度都为 O(n2)。
  • 冒泡排序在任何情况下的时间复杂度都为 O(n2)。
  • 如果给定数据部分有序,插⼊排序通常⽐选择排序效率更⾼。
  1. 冒泡排序的第⼀轮操作是从左到右遍历数组,通过两两⽐较相邻元素,将当前最⼤的元素移动到末尾。给 定数组 arr[]={4, 1, 3, 1, 5, 2} ,执⾏第⼀轮冒泡排序后数组 arr 中的内容为( )。

{{ select(11) }}

  • 1, 4, 3, 1, 5, 2
  • 1, 3, 1, 4, 2, 5
  • 1, 4, 3, 1, 2, 5
  • 4, 1, 3, 1, 5, 2
  1. 冒泡排序算法的平均时间复杂度为

{{ select(12) }}

  • O(n2)O(n^2)
  • O(2n)O(2^n)
  • O(1)O(1)
  • O(n)O(n)
  1. 下⾯代码实现了插⼊排序函数(升序) , 则横线上应填写

    void insertion_sort(vector<int> &nums) {
    	for (int i = 1; i < nums.size(); i++) {
    			int base = nums[i], j = i - 1;
    			________________________________ { // 在此处填入代码
    			nums[j + 1] = nums[j];
    			j--;
    		}
    		nums[j + 1] = base;
    	}
    }
    

{{ select(13) }}

  • while (j >= 0 && nums[j] > base)
  • while (j > 0 && nums[j] > base)
  • while (j >= 0 && nums[j] < base)
  • while (j > 0 && nums[j] < base)
  1. ⼩杨⽤⽂件重定向实现在 log.txt ⽂件中输出⽇ 志, 则下⾯横线上应填写

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main() {
    	ofstream log_file("log.txt");
    	streambuf* original_cout = cout.rdbuf();
    	cout.rdbuf(log_file.rdbuf());
    	___________________________________ // 在此处填入代码
    	cout.rdbuf(original_cout); // 恢复原始的标准输出缓冲区
    	return 0;
    }
    

{{ select(14) }}

  • cout << "This output will go to the log file." << endl;
  • log_file << "This output will go to the log file." << endl;
  • cout >> "This output will go to the log file." >> endl;
  • log_file >> "This output will go to the log file." >> endl;
  1. 下面哪种方式不能实现将字符串 "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分)

  1. 函数是C++中的核心概念,用于封装可重用的代码块。( )
    {{ select(16) }}
  1. 在C++中,函数的返回类型可以省略,默认为 int。( )
    {{ select(17) }}
  1. 结构体的成员默认是 public 访问权限。( )
    {{ select(18) }}
  1. ⼆维数组的⾏的⼤⼩的必须在定义时确定, 列的⼤⼩可以动态变化。

{{ select(19) }}

  • 正确
  • 错误
  1. 递推算法通过逐步求解当前状态和前⼀个或⼏个状态之间的关系来解决问题。

    {{ select(20) }}

  • 正确
  • 错误
  1. 选择排序是稳定的排序算法。

    {{ select(21) }}

  • 正确
  • 错误
  1. 插⼊排序的时间复杂度总是⽐冒泡排序低

{{ select(22) }}

  • 正确
  • 错误
  1. 冒泡排序和插⼊排序都是稳定的排序算法。

    {{ select(23) }}

  • 正确
  • 错误
  1. 函数不可以调用自己。

{{ select(24) }}

  • 正确
  • 错误
  1. 执⾏下⾯C++代码后, 输出的是20。

    int point(int* p){
    	return *p * 2;
    } 
    int main() {
    	int a = 10;
    	int* p = &a;
    	*p = point(p);
    	cout << *p << endl;
    }
    

    {{ select(25) }}

  • 正确
  • 错误