#6087. gesp四级真题分类四:结构体
gesp四级真题分类四:结构体
四、结构体(共12题)
1. 在C++中,()正确定义一个名为 student 的结构体,其中包含一个 name 字符数组和一个 age 整数?
{{ select(1) }}
struct student { char name[20]; int age; };student struct { char name[20]; int age; };student struct { string name; int age; };struct student { char[20] name; int age; };
2. 下面的描述中,()不能正确定义一个名为 Student 的结构体以及一个包含20个元素的结构数组。
{{ select(2) }}
-
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];
3. 下面的描述中,()正确定义一个名为 Person 的结构体并正确初始化了一个 Person 结构体的变量 p。
{{ select(3) }}
-
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);
4. 给定如下代码,下面描述错误的是( )。
struct Person {
std::string name;
int age;
struct Address {
std::string street;
std::string city;
};
Address address;
};
{{ select(4) }}
- 结构 Person 内嵌套结构 Address
- Person 有一个 Address 类型的 address 成员
- 一个 Person 类型的变量 p 的 address 的初始化可以写成:p.address.street = "123 Main St"; p.address.city = "Anytown";
- 结构的嵌套可以减少命名冲突,因此可以不必控制嵌套层次
5. 关于结构体初始化,以下哪个选项中正确的是( )。
struct Point { int x, y; };
{{ select(5) }}
Point p = (1,2);Point p = {1,2};Point p = new {1,2};Point p = <1,2>;
6. 运行如下代码会输出( )。
struct Cat {
string name;
int age;
};
void birthday(Cat& c) { c.age++; }
int main() {
Cat kitty{"Mimi", 2};
birthday(kitty);
cout << kitty.name << " " << kitty.age;
}
{{ select(6) }}
- Mimi 2
- Mimi 3
- kitty 3
- kitty 2
7. 运行如下代码会输出( )。
struct Point { int x, y; };
struct Rectangle {
Point topLeft;
Point bottomRight;
};
int main() {
Rectangle rect = {{10, 10}, {20, 20}};
rect.topLeft.x = 5;
Point* p = &rect.bottomRight;
p->y = 5;
cout << rect.topLeft.x + rect.bottomRight.y;
}
{{ select(7) }}
- 10
- 30
- 15
- 20
8. 下面C++代码在一个结构体中又定义了别的结构体。这种结构嵌套定义的方式语法不正确。(判断题)
#include <string>
#include <vector>
using namespace std;
struct Library {
struct Book {
struct Author {
string name;
int birthYear;
};
string title;
int year;
Author author;
};
string name;
vector<Book> books;
};
{{ select(8) }}
- 正确
- 错误
9. 结构体的成员默认是 public 访问权限。(判断题)
{{ select(9) }}
- 正确
- 错误
10. 下面C++代码合法。(判断题)
struct Student {
string name;
int age;
float score;
};
Student* students = new Student[20];
{{ select(10) }}
- 正确
- 错误
11. 下面代码没有语法错误。(判断题)
struct GameCharacter {
string name;
int level;
float position_x, position_y;
struct Equipment {
string weapon;
int attack_bonus;
int defense_bonus;
} equipment;
struct Skill {
string name;
int damage;
} skills[8];
int skill_count;
};
{{ select(11) }}
- 正确
- 错误
12. 下面程序执行后输出 2010。(判断题)
int x = 10;
void f() { int x = 20; cout << x; }
int main() { f(); cout << x; }
{{ select(12) }}
- 正确
- 错误