c#综合练习附答案

更新时间:2023-10-26 04:28:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

一、选择题

1. 下列程序所计算的数学式是( ) int a=0, i=2; while(i<100) {

a+=i; i+=2; }

A. a=1+2+4+…+98 B. a=1+2+4+…+100 C. a=2+4+6+…+98 D. a=2+4+6+…+100

2、下面是几条动态初始化一维数组的语句,其中正确的是( ) A、int[ ] arr2=new int[ ]; B、int[ ] arr2=new int[4 ];

C、int[ ] arr2=new int[i]{ 6,5,1,2,3}; D、int[ ] arr2=new int[4]{ 6,5,1,2,3};

3、用static关键字定义的静态方法,属于整个类而不属于( )。 A、类的某一个具体实例 B、类的其他方法 C、类的任何成员 D、常量 4、值参数是 参数( )。

A、按值传递 B、按地址传递 C、按引用传递 D、什么也不传递

5、类中两个以上的同名方法,只要 不同,编译器就知道调用哪个方法(A、参数类型 B、参数类型或参数个数 C、参数与顺序个数 D、返回类型 6、加载窗体时触发的事件是( )。

A、 Click B、Load C、GotFoucs D、DoubleClick 7、改变窗体的标题,需修改的窗体属性是( )。 A、Text B、Name C、Title D、Index

8、控件组合了Textbox控件和Listbox控件的功能( ) A、Label B、ComboBox C、StatusBar D、PictureBox 9.Textbox控件的PasswordChar属性的作用是( )。 A、该属性是Boolean类型,表示是否使用*号隐藏输入信息。 B、 该属性是String类型,表示输入的隐藏信息的实际内容

。 ) C、 该属性是Char类型,表示用哪个字符的隐藏输入的信息 ( ) D、 该属性在C#中未使用

9、 Timer的 事件在每个时间间隔内被重复激发。( )。 A、Click

B、Tick

C、ServerTick

D、Server Click

10、用户点击消息框按钮时返回值( )。

A、DialogValue B、DialogBox C、DialogResult D、DialogBox

二、填空题

1、 C#数组类型是一种引用类型,所有的数组都是从System命名空间的 类继承而来

的引用对象。 答案:object

2、一般将类的构造方法声明为 访问权限。如果声明为private,就不能创建该类的对象。

答案:public或公有

3、类中声明的属性往往具有get()和 两个函数。 答案:set()

4、对于方法,参数传递分为值传递和 两种。 答案:引用传递

5、传入某个属性的SET方法的隐含参数的名称是 。 答:value

6、C#提供一个默认的无参构造函数,当我实现了另外一个有一个参数的构造函数时,还想保留这个无参数的构造函数。这样我应该写 构造函数。 答:两个

7.装箱是把值类型转换到 类型。 答:引用类型

8.拆箱是引用类型返回到 类型。 答:值类型

9.C#的值类型包括 、 和 三种。 答:整数类型、浮点类型以及布尔类型

三、简答题

1.如何区别重载方法?

不同的参数类型,不同的参数个数,不同的参数顺序

2.C#用多种修饰符来表达类的不同性质。根据其保护级C#的类有五种不同的限制修饰符,请写出并指出它们之间的区别是什么? 答:

public 可以被任意存取

protected只可以被本类和其继承子类存取

internal只可以被本组合体(Assembly)内所有的类存取,组合体是C#语言中类被组合后的逻辑单位和物理单位,其编译后的文件扩展名往往是“.DLL”或“.EXE”。

protected internal唯一的一种组合限制修饰符,它只可以被本组合体内所有的类和这些类的继承子类所存取。

private只可以被本类所存取。

3. 编写一个类,利用方法重载完成二个整数、三个整数之和,在主函数中分别调用并输出和。

class Program {

public static int Add(int x, int y) {

return x + y; }

public int Add(int x, int y, int z) {

return x + y + z; }

static void Main(string[] args) {

Program program = new Program(); //实例化类对象 int x = 3; int y = 5; int z = 7;

//根据传入的参数类型及参数个数的不同调用不同的Add重载方法 Console.WriteLine(x + \ + y + \ + Program.Add(x, y));

Console.WriteLine(x + \ + y + \ + z + \ + program.Add(x, y, z)); Console.ReadLine(); } } }

4、设计一个成绩类,此类能够记录学生姓名、学号、成绩和科目,并且定义学生学号属性

class student {

public string name; public int no; public string sex; public float score; public string subject;

public string NO //创建学号属性

{

get {

return no; } set {

no = value; } } }

四、程序阅读题

1、using System; class Arraysort {

static void Main( ) {

int[] nums=new int[]{10,8,36,12,24}; foreach(int j in nums) {

Console.Write(“{0}\\0”, j); }

Console.WriteLine();

for(int j=nums.Length-1;j>=0;j--)

Console.Write (“{0}\\0”, nums[j]); Console.WriteLine();

} }

答案:10 8 36 12 24 24 12 36 8 10 2、 class Fruit {

public string color; public string shape;

public Fruit(string c, string s) {

color = c; shape = s; } }

class Test {

public static void Main() {

Fruit Orange = new Fruit(\

Console.WriteLine(\ }

}

答案:orange, round

3、using System; class Point {

public int x,y;

public Point(int x ,int y) {

this.x=x; this.y=y; } }

class Test { static void Main() { Point p=new Point(5,6); Console.WriteLine(\ Console.WriteLine(\ }

}

答案:x=5 y=6

4、class Decon1 {

public Decon1( )

{ Console.WriteLine(“调用构造函数Decon1”); }

~Decon1( )

{ Console.WriteLine(“调用析构函数Decon1”); } }

class Decon2 {

public Decon2( )

{ Console.WriteLine(“调用构造函数Decon2”); }

~Decon2( )

{ Console.WriteLine(“调用析构函数Decon2”); } }

class Test {

public static void Main()

{ Decon1 dec1=new Decon1( ); Decon2 dec2=new Decon2( ); } } 答案:调用构造函数Decon1 调用构造函数Decon2 5、 using System; class Test {

public void myMeth( ) {

int j;

for(j=1;j<10;j++) {

if(j%3==0) continue;

Console.WriteLine( “{0}\\t”,j); } }

static void Main( ) {Test lei=new Test( );

lei.myMeth( ); }

} 答案: 1

2 4 5 7 8

6、using System; class Test

{ public void myMeth( ) { int j= 8; if(j>=5) { j=j*2;

Console.WriteLine(j ); return; } else

{j=j*3;

Console.WriteLine(j ); return; } }

static void Main( ) { Test lei=new Test( ); lei.myMeth( ); }

} 答案:16

7、using System; class Test {

public void Swap(int x,int y) {

int k; k=x; x=y; y=k; }

static void Main() {

int a=8, b=68;

Console.WriteLine(\ Test sw=new Test(); sw.Swap(a, b);

Console.WriteLine(\} }

答案:a=8,b=68 a=8,b=68 8、 using System;

class TestoverLoad {

public void print(int i) {

Console.WriteLine(\输出的整数={0}\,i); }

public void print(string s) {

Console.WriteLine(\输出的字符串={0}\ s); }

public void print(double d) {

Console.WriteLine(\输出的双精度数={0}\,d); } }

class test {

public static void Main( ) {

TestoverLoad app=new TestoverLoad( ); app.print(6);

app.print(\理解方法重载了吗?\); app.print(3.14); } }

答案:输出的整数=6

输出的字符串=理解方法重载了吗?

输出的双精度数=3.14 9、class A {

public int count; public A() {

count = -1; }

public A(int n) {

count = n; } }

class Test {

static void Main() {

A a = new A();

Console.WriteLine(\ A b = new A(5);

Console.WriteLine(\ }

}

答案:count=-1

count=5

10、有以下程序,计算结果

static void Main(string[] args)

{ Console.Write(\输入一整数\ String s = Console.ReadLine(); int num = int.Parse(s); if (num++ > 5)

Console.WriteLine(num); else

Console.WriteLine(- -num); Console.Read(); }

运行时,从键盘输入5,则输出结果是:5

本文来源:https://www.bwwdw.com/article/0ms2.html

Top