小杨正在编写一个“数字交换器”程序,他希望通过函数交换两个变量的值。请问运行以下代码后,屏幕上输出的是( )。
01void exchange(int *a, int &b) { 02 int t = *a; 03 *a = b; 04 b = t; 05} 06 07int main() { 08 int x = 100, y = 200; 09 exchange(&x, y); 10 cout << x << " " << y; 11 return 0; 12}
下面程序想通过函数计算三门课总分,横线处应填入的是( )。
01int sumScore(int a, int b, int c) { 02 return a + b + c; 03} 04 05int main() { 06 int chinese = 88, math = 95, english = 90; 07 int total = __________; 08 cout << total; 09 return 0; 10}
下面程序输出结果是( )。
01int addOne(int x) { 02 return x + 1; 03} 04 05int main() { 06 int a = 6; 07 cout << addOne(a) + addOne(3); 08 return 0; 09}
关于下面程序,说法正确的是( )。
01void show() { 02 int stars = 5; 03} 04 05int main() { 06 cout << stars; 07 return 0; 08}
小杨在调试一个“等级提升”系统,代码逻辑如下,执行后 *p 的值是( )。
01int lv = 5, next_lv = 6; 02int *p = &lv; 03*p = *p + 1; 04p = &next_lv;
小杨正在开发一款名为“星际网格”的游戏,他用二维数组 int map[5][4]; 来表示地图。已知 int 占 字节,如果 map 的内存地址是 0x2000,则表达式 &map + 1 的地址值是( )。
执行完下面代码后,变量 val 的值是( )。
01int data[] = {10, 20, 30, 40, 50}; 02int *ptr = data + 2; 03int val = *(ptr - 1) + *(ptr + 1);
某班 个小组、每组 名同学的分数存入下面的二维数组 score,则 score[1][2] 的值是( )。
01int score[3][4] = { 02 {80, 81, 82, 83}, 03 {90, 91, 92, 93}, 04 {70, 71, 72, 73} 05};
小杨定义了一个结构体 Hero 来表示游戏角色,下面哪种初始化方式会由于语法错误导致编译失败?( )。
01struct Hero { 02 string name; 03 int hp; 04};
下面程序输出结果是( )。
01struct Book { 02 string title; 03 int pages; 04}; 05 06int main() { 07 Book books[2] = {{"Math", 120}, {"Science", 150}}; 08 cout << books[1].title; 09 return 0; 10}
小杨在对“能量晶石”按亮度进行排序。如果两块晶石亮度相同,他希望保持它们在原始序列中的相对顺序。下列关于排序算法稳定性的说法,错误的是( )。
小杨的机器人正在能量踏板上跳跃,踏板编号为 。跳到第 块踏板的方案数满足递推式 f(n) = f(n - 1) + f(n - 2)。若 f(1) 为 1,f(2) 为 2,则运行以下代码计算 jump(5) 的结果是( )。
01int jump(int n) { 02 if (n <= 2) 03 return n; 04 int a = 1, b = 2, c = 0; 05 for (int i = 3; i <= n; i++) { 06 c = a + b; 07 a = b; 08 b = c; 09 } 10 return c; 11}
在“模拟实验室”程序中,为了防止除以 导致崩溃,小杨使用了异常处理机制。执行以下代码将输出( )。
01try { 02 int x = 10, y = 0; 03 if (y == 0) throw "Zero Error"; 04 cout << x / y; 05} catch (int e) { 06 cout << "Error Code: " << e; 07} catch (const char* msg) { 08 cout << "Caught: " << msg; 09}
下面代码使用某种排序算法,将数组中的元素按从小到大排序。这段代码使用的排序算法是( )。
01void mystery_sort(double arr[], int n) { 02 for (int i = 0; i < n - 1; i++) { 03 int minPos = i; 04 for (int j = i + 1; j < n; j++) { 05 if (arr[j] < arr[minPos]) { 06 minPos = j; 07 } 08 } 09 double temp = arr[i]; 10 arr[i] = arr[minPos]; 11 arr[minPos] = temp; 12 } 13}
小杨正在读取“冒险日志”文件 quest.txt。若文件内容为 Level 10,执行以下程序后输出为( )。
01ifstream fin("quest.txt"); 02string s; 03int v; 04fin >> s >> v; 05cout << s.length() * v;
运行以下程序后,变量 a 的值最终会变为 。
01void modify(int *p) { 02 *p = *p + 10; 03} 04 05int main() { 06 int a = 10; 07 modify(&a); 08 return 0; 09}
在 C++ 中,引用一旦初始化并绑定到某个变量后,可以通过赋值语句将其重新绑定到另一个变量。
下面程序可以正确计算并输出 名学生的平均成绩。
01struct Student { 02 int id; 03 int score; 04}; 05 06int main() { 07 Student students[3] = { 08 {1, 90}, 09 {2, 80}, 10 {3, 100} 11 }; 12 13 int sum = 0; 14 for (int i = 0; i < 3; i++) { 15 sum += students[i].score; 16 } 17 double average = sum / 3.0; 18 cout << average << endl; 19 return 0; 20}
选择排序算法在寻找每一轮最小值时,如果遇到相等的元素不进行交换,则选择排序是一种稳定的排序算法。
如果使用带 flag 的冒泡排序,且待排序数组一开始就是有序的,那么算法只需一轮扫描即可结束,时间复杂度为 O(n)。
在 C++ 中定义二维数组并初始化时,可以省略第一维,但不能省略第二维。因此 int a[][2] = {{1, 2}, {3, 4}}; 是合法的,而 int a[][] = {{1, 2}, {3, 4}}; 是不合法的。
下面代码的时间复杂度是 O(2^n)。
01int cnt = 0; 02for (int i = 1; i <= n; i++) { 03 for (int j = 1; j <= i; j++) { 04 cnt++; 05 } 06}
假设文件 output.txt 能正常打开,下面代码通过 rdbuf 将 cout 的输出重定向到了文件中。
ofstream fout("output.txt");
streambuf* old_buf = cout.rdbuf();
cout.rdbuf(fout.rdbuf());
cout << "GESP Exam";
cout.rdbuf(old_buf);
小杨想通过下面程序给饭卡充值,程序会输出 70。
01void recharge(int money) { 02 money += 20; 03} 04 05int main() { 06 int card = 50; 07 recharge(card); 08 cout << card; 09 return 0; 10}
下面代码可以通过编译。
01int a[5]; 02a++;