跳转至

综合冲刺练习#

覆盖全部已学内容的一次模拟测验,建议在完成前 6 篇后闭卷作答,限时 90 分钟。 对应总览:C# 复习总览


第一部分:选择题(每题 3 分,共 30 分)#

1. 下列属于值类型的是?

  • A. string
  • B. int[]
  • C. decimal
  • D. class 的实例

2. int x = 7; Console.WriteLine(x / 2); 输出?

  • A. 3.5
  • B. 3
  • C. 4
  • D. 编译错误

3. bool b = false; Console.WriteLine(!b); 输出?

  • A. True
  • B. False
  • C. 编译错误
  • D. null

4. 下列哪个循环至少执行一次循环体?

  • A. for
  • B. while
  • C. do-while
  • D. foreach

5. string[] arr = { "a", "b" }; arr.Length 的值是?

  • A. 1
  • B. 2
  • C. 3
  • D. 报错

6. Dictionary<string, int> 添加键值对用?

  • A. list.Add()
  • B. dict.Add(key, value)
  • C. array.Add()
  • D. dict.Push()

7. 方法重载依据什么区分?

  • A. 返回值
  • B. 参数列表
  • C. 方法名
  • D. 访问修饰符

8. sealed 修饰的类?

  • A. 只能被继承
  • B. 不能被继承
  • C. 必须实现接口
  • D. 是静态类

9. 事件只能在什么地方触发?

  • A. 任何地方
  • B. 订阅者类内部
  • C. 声明事件的类内部
  • D. Main 方法

10. Socket 客户端发起连接的方法?

  • A. Accept()
  • B. Listen()
  • C. Connect()
  • D. Bind()
参考答案
  1. C(decimal 是值类型)
  2. B(整数除法截断,7/2=3)
  3. A(!false = True)
  4. C(do-while 先执行后判断)
  5. B(数组长度 2)
  6. B(dict.Add(key, value))
  7. B(参数列表不同才是重载)
  8. B(sealed 密封类不能被继承)
  9. C(事件只能在声明类内部触发)
  10. C(客户端用 Connect)

第二部分:填空题(每题 2 分,共 20 分)#

1. C# 程序入口方法是 ==_== ==== ==_== ====。

2. 数组下标从 ==_== ==== ==_== ==== 开始。

3. foreach 是 ==_== ==== ==_== ==== 遍历(只读/可写)。

4. 抽象方法用 ==_== ==== ==_== ==== 关键字声明,子类用 ==_== ==== ==_== ==== 重写。

5. ref 参数传入前 ==_== ==== ==_== ==== 赋值,out 参数方法内 ==_== ==== ==_== ==== 赋值。

6. Func<int, string> 表示接收 ==_== ==== ==_== ==== 返回 ==_== ==== ==_== ====。

7. 文本文件逐行读取用 ==_== ==== ==_== ==== 类。

8. 跨线程更新控件用 ==_== ==== ==_== ==== 方法。

9. Socket 收发的是 ==_== ==== ==_== ==== 类型数据。

10. 面向对象三大特性:==_== ==== ==_== ====、==_== ==== ==_== ====、==_== ==== ==_== ====。

参考答案
  1. Main
  2. 0
  3. 只读
  4. abstract;override
  5. 必须;必须(out 在方法内必须赋值)
  6. int;string
  7. StreamReader
  8. BeginInvoke
  9. byte[](字节数组)
  10. 封装、继承、多态

第三部分:编程题(每题 10 分,共 50 分)#

1. 编写程序:输入 10 个整数存入数组,求最大值、最小值和平均值。

答案

```csharp

> int[] nums = new int[10]; > for (int i = 0; i < 10; i++) > nums[i] = Convert.ToInt32(Console.ReadLine()); > > int max = nums[0], min = nums[0], sum = 0; > foreach (int n in nums) > { > if (n > max) max = n; > if (n < min) min = n; > sum += n; > } > Console.WriteLine($"最大{max},最小{min},平均{sum / 10.0}"); > ```

2. 定义一个 Student 类(Name、Age、Score),创建 3 个学生对象存入 List,找出成绩最高者。

答案
class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
    public double Score { get; set; }
}

List`<Student>` list = new List`<Student>`()
{
    new Student { Name = "张三", Age = 18, Score = 85 },
    new Student { Name = "李四", Age = 19, Score = 92 },
    new Student { Name = "王五", Age = 18, Score = 78 },
};

Student best = list[0];
foreach (var s in list)
    if (s.Score > best.Score) best = s;
Console.WriteLine($"最高分:{best.Name} {best.Score}");

3. 用委托实现:给数组排序(升序)后,用 Find 找出第一个大于 50 的数。

答案
int[] nums = { 30, 55, 10, 80, 45 };
Array.Sort(nums);                         // 升序
int first = Array.Find(nums, n => n > 50); // 第一个大于50的
Console.WriteLine(first);                 // 55

4. 定义抽象类 Animal(抽象方法 Speak),DogCat 继承实现,循环调用各自的 Speak。

答案
abstract class Animal { public abstract void Speak(); }
class Dog : Animal { public override void Speak() => Console.WriteLine("汪汪"); }
class Cat : Animal { public override void Speak() => Console.WriteLine("喵喵"); }

List`<Animal>` animals = new List`<Animal>` { new Dog(), new Cat() };
foreach (var a in animals) a.Speak();   // 多态:汪汪 / 喵喵

5. WinForms:实现"添加学生"功能——TextBox 输入姓名,点击按钮加入 ListBox,统计人数显示在 Label。

答案
private void btnAdd_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(textBox1.Text))
    {
        listBox1.Items.Add(textBox1.Text);
        label1.Text = "人数:" + listBox1.Items.Count;
        textBox1.Clear();
    }
}

第四部分:代码改错(每题 5 分,共 10 分)#

1. 找出错误并改正。

string str = "123";
int n = str;   // 错误在哪?
答案

string 不能隐式转 int。应改为 int n = int.Parse(str);Convert.ToInt32(str),或安全版 int.TryParse(str, out int n);

2. 找出错误并改正(多播委托)。

public delegate void D();
D d = null;
d();   // 问题?
答案

d 为 null 直接调用会抛 NullReferenceException。应改为 d?.Invoke(); 或先判空 if (d != null) d();


评分参考#

分数段 评价 建议
90+ 掌握扎实 可直接开始 Vision Pro 学习
70-89 基本掌握 针对错题章节复习对应笔记
60-69 需要巩固 重做薄弱主题的复习篇
<60 需重点补习 建议按 7 天计划重新过一遍

相关笔记#