#GESP425062. GESP25年6月四级判断题

GESP25年6月四级判断题

判断题(每题2分,共20分)

  1. 下面C++代码正确声明了一个返回int类型、接受两个int参数的函数。() {{ select(1) }}
  • 正确
  • 错误
    int add(int, int);
    
  1. 下面C++代码的输出是15。() {{ select(2) }}
  • 正确
  • 错误
    #include <iostream>
    using namespace std;
    
    void foo(int x) {
        x += 5;
    }
    
    int main() {
        int a = 10;
        foo(a);
        cout << a << endl;
        return 0;
    }
    
  1. 下面C++代码在一个结构体中又定义了别的结构体。这种结构嵌套定义的方式语法不正确。() {{ select(3) }}
  • 正确
  • 错误
    #include <string>
    #include <vector>
    using namespace std;
    
    struct Library {
        struct Book {
            struct Author {
                string name;
                int birthYear;
            };
            int year;
            string title;
            Author author;
        };
        string name;
        vector<Book> books;
    };
    
  1. 在C++中,相比于值传递,使用引用传递的优点是可以直接操作和修改原始变量,避免数据拷贝,提高效率。() {{ select(4) }}
  • 正确
  • 错误
  1. 下面这段代码不合法,因为每一行都必须显式初始化每个元素。() {{ select(5) }}
  • 正确
  • 错误
    int arr[2][3] = {{1, 2}, {3}};
    
  1. 以下程序中使用了递推方式计算阶乘((n! = 1 × 2 × ... × n)),计算结果正确。() {{ select(6) }}
  • 正确
  • 错误
    int factorial(int n) {
        int res = 1;
        for (int i = 0; i < n; ++i) {
            res *= i;
        }
        return res;
    }
    
  1. 无论初始数组是否有序,选择排序都执行(O(n^2))次比较。() {{ select(7) }}
  • 正确
  • 错误
  1. 以下C++代码,尝试对有n个整数的数组arr进行排序。这个代码实现了选择排序算法。() {{ select(8) }}
  • 正确
  • 错误
    void selectSort(int arr[], int n) {
        for (int i = 0; i < n - 1; ++i) {
            int minIndex = i;
            for (int j = i + 1; j < n; ++j) {
                if (arr[j] < arr[minIndex])
                    minIndex = j;
            }
            if (minIndex != i)
                swap(arr[i], arr[minIndex]);
        }
    }
    
  1. 如果一个异常在try块中抛出但没有任何catch匹配,它将在编译时报错。() {{ select(9) }}
  • 正确
  • 错误
  1. 下面C++代码实现将“Hello”写入data.txt。() {{ select(10) }}
  • 正确
  • 错误
    #include <fstream>
    using namespace std;
    
    int main() {
        ofstream out("data.txt");
        out << "Hello";
        out.close();
        return 0;
    }