#4194. 基础复习

基础复习

1、{{ select(1) }} 下列关于函数定义的语句,正确的是:

  • int func() { return 0; }
  • function int() { return 0; }
  • def int func() { return 0; }
  • int = func() { return 0; }

2、{{ select(2) }} 函数的返回类型和参数类型共同决定了函数的:

  • 名称
  • 作用范围
  • 签名(函数特征)
  • 返回值

3、运行下面程序的输出是什么?

#include <iostream>
using namespace std;
int add(int a, int b) {
    return a + b;
}
int main() {
    cout << add(2, 3);
    return 0;
}

{{ select(3) }}

  • 2
  • 3
  • 5
  • 23

4、{{ select(4) }} 下面关于函数参数传递方式的说法中,错误的是:

  • 值传递不会改变原变量的值
  • 引用传递可以修改调用者的变量值
  • 所有函数默认都使用引用传递
  • 可以通过加 & 使用引用传递

5、运行以下程序后 x 的值是多少?

#include <iostream>
using namespace std;
void set(int &x) {
    x = 100;
}
int main() {
    int x = 1;
    set(x);
    cout << x;
    return 0;
}

{{ select(5) }}

  • 1
  • 100
  • 0
  • 编译错误

6、{{ select(6) }} 函数体内定义的变量属于什么作用域?

  • 全局作用域
  • 文件作用域
  • 函数作用域(局部作用域)
  • 无作用域

7、运行以下程序输出结果是?

#include <iostream>
using namespace std;
int func(int x) {
    if (x <= 1) return 1;
    return x * func(x - 1);
}
int main() {
    cout << func(3);
    return 0;
}

{{ select(7) }}

  • 3
  • 6
  • 9
  • 1

8、{{ select(8) }} 下列关于递归函数的说法中正确的是:

  • 递归函数不能有返回值
  • 递归只能用于打印
  • 递归必须有终止条件
  • 所有递归都会陷入死循环

9、{{ select(9) }} 若有字符串定义为 string s = "hello world";,则 s.size() 的返回值是:

  • 10
  • 11
  • 12
  • 9

10、运行以下程序的输出是?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "abcdefg";
    cout << s.substr(2, 3);
    return 0;
}

{{ select(10) }}

  • abc
  • bcd
  • cde
  • def

11、运行下面程序的输出是什么?

#include <iostream>
using namespace std;
int f(int n) {
    if (n == 0) return 0;
    return n + f(n - 1);
}
int main() {
    cout << f(3);
    return 0;
}

{{ select(11) }}

  • 3
  • 6
  • 9
  • 无法输出

12、{{ select(12) }} 若函数 int sum(int a, int b = 10) 定义存在,下列调用合法的是:

  • sum();
  • sum(5);
  • sum(5, 15, 20);
  • sum(5, "abc");

13、{{ select(13) }} 函数返回值类型为 void 表示:

  • 没有参数
  • 没有返回值
  • 返回值是0
  • 函数不能调用

14、运行以下程序,输出结果是?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "information";
    cout << s[4];
    return 0;
}

{{ select(14) }}

  • i
  • r
  • m
  • o

15、{{ select(15) }} 关于递归调用的最大风险是:

  • 编译错误
  • 死循环输出
  • 占用太多内存导致栈溢出
  • 不支持嵌套调用

16、{{ select(16) }} 若函数原型为 int max(int a, int b),则下列语句正确的是:

  • max(3, 4);
  • max("a", "b");
  • max();
  • int c = max;

17、运行以下代码后的输出是?

#include <iostream>
using namespace std;
void swap(int &a, int &b) {
    int t = a;
    a = b;
    b = t;
}
int main() {
    int x = 5, y = 8;
    swap(x, y);
    cout << x << " " << y;
    return 0;
}

{{ select(17) }}

  • 5 8
  • 8 5
  • 0 0
  • 编译错误

18、{{ select(18) }} 在一个函数中定义的变量在函数外部:

  • 仍然有效
  • 自动变成全局变量
  • 无法访问
  • 会被传递出去

19、运行以下程序输出为?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "abcdefg";
    s[1] = 'Z';
    cout << s;
    return 0;
}

{{ select(19) }}

  • abcdefg
  • aZcdefg
  • aZbdefg
  • Zbcdefg

20、{{ select(20) }} 在函数调用中,如果使用的是“引用传参”,可以实现:

  • 保证值不被改变
  • 在函数内部访问调用者变量
  • 拷贝参数更快
  • 跳过函数体

21、运行以下递归程序输出为?

#include <iostream>
using namespace std;
int f(int x) {
    if (x <= 1) return 1;
    return f(x - 1) + f(x - 2);
}
int main() {
    cout << f(4);
    return 0;
}

{{ select(21) }}

  • 5
  • 3
  • 4
  • 8

22、{{ select(22) }} 如果函数中调用自身,称为:

  • 回调函数
  • 链式函数
  • 递归函数
  • 静态函数

23、{{ select(23) }} 下面哪个字符串函数用于查找字符位置?

  • size()
  • find()
  • insert()
  • replace()

24、运行下面程序的输出是?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "123456";
    cout << s.substr(1, 3);
    return 0;
}

{{ select(24) }}

  • 123
  • 234
  • 345
  • 456

25、{{ select(25) }} 若一个递归函数缺少终止条件,将导致:

  • 编译错误
  • 只执行一次
  • 死循环或栈溢出
  • 输出为0

26、运行下面程序输出为?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "abc";
    s += "def";
    cout << s;
    return 0;
}

{{ select(26) }}

  • abc
  • abcdef
  • defabc
  • 错误

27、{{ select(27) }} 字符串s的第一个字符可通过哪种方式访问:

  • s[0]
  • s(0)
  • s.charAt(1)
  • s[1]

28、运行以下程序输出为?

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s = "abcdef";
    cout << s.find("cd");
    return 0;
}

{{ select(28) }}

  • 1
  • 2
  • 3
  • 4

29、{{ select(29) }} 以下字符串函数中,能返回字符串长度的是:

  • size()
  • push()
  • lengthof()
  • count()

30、{{ select(30) }} 若定义为 string s = "abc",执行 s[3] = 'x'; 后会:

  • 将末尾变为 x
  • 字符串长度变成4
  • 编译错误
  • 不影响原字符串

31、函数的返回值类型可以是 intfloatchar 等基本数据类型。 {{ select(31) }}

  • 正确
  • 错误

32、若函数参数使用引用传递方式,在函数内部对参数的修改会影响实参变量。 {{ select(32) }}

  • 正确
  • 错误

33、在C++中,void函数不能使用 return 语句。 {{ select(33) }}

  • 正确
  • 错误

34、递归函数必须包含一个能终止递归调用的条件。 {{ select(34) }}

  • 正确
  • 错误

35、递归调用次数过多可能导致程序崩溃。 {{ select(35) }}

  • 正确
  • 错误

36、在函数体外定义的变量叫局部变量。 {{ select(36) }}

  • 正确
  • 错误

37、可以在字符串中通过 s[0] 的方式访问第一个字符。 {{ select(37) }}

  • 正确
  • 错误

38、函数的参数只能是整型或浮点型,不能传字符串。 {{ select(38) }}

  • 正确
  • 错误

39、递归和循环在逻辑上可以实现相同的功能。 {{ select(39) }}

  • 正确
  • 错误

40、string 类型在 C++ 中属于 STL 的一部分。 {{ select(40) }}

  • 正确
  • 错误

41、在 C++ 中,一个函数内部不能调用另一个函数。 {{ select(41) }}

  • 正确
  • 错误

42、调用 s.size() 可以获取字符串 s 的长度。 {{ select(42) }}

  • 正确
  • 错误

43、递归函数可以嵌套调用自身多次。 {{ select(43) }}

  • 正确
  • 错误

44、函数中定义的变量,在函数结束后仍然有效。 {{ select(44) }}

  • 正确
  • 错误

45、string 类型的变量可以直接进行字符串拼接,如 s = s + "abc";。 {{ select(45) }}

  • 正确
  • 错误