收藏 分销(赏)

C#基础试题(附答案).doc

上传人:仙人****88 文档编号:9408189 上传时间:2025-03-25 格式:DOC 页数:16 大小:126.54KB
下载 相关 举报
C#基础试题(附答案).doc_第1页
第1页 / 共16页
C#基础试题(附答案).doc_第2页
第2页 / 共16页
点击查看更多>>
资源描述
一、选择题 1、能作为C#程序的基本单位是( )。 A. 字符 B. 语句 C. 函数 D. 源程序文件 答案:B 2、将变量从字符串类型转换为数值类型可以使用的类型转换方法是( )。 A.Str() B.Cchar C.CStr() D.int.Parse(); 答案:D 3、数据类型转换的类是( )。 A.Mod B.Convert C. Const D. Single 答案:B 4、字符串连接运算符包括&和( )。 A. + B. - C. * D. / 答案:A 5、先判断条件的当循环语句是( )。 A. do...while B. while C. while...do D. do ...loop 答案:B 6、下面几个函数,()是重载函数. 1.void f1(int) 2.int f1(int) 3.int f1(int,int) 4.float k(int) A.四个全 B.1 和 4 C.2 和 3 D.3和4 答案:C 8、以下的C#代码: static void Main(string[] args) {   Console.WriteLine("运行结果: {0}",Console.ReadLine());   Console.ReadLine(); }        代码运行结果为( )。 A.在控制台窗口显示“运行结果:” B.在控制台窗口显示“运行结果:{0}” C.在控制台窗口显示“运行结果:,Console.ReadLine” D.如果用户在控制台输入“ A”,那么程序将在控制台显示“运行结果:A” 答案:D 9、在C#中定义一个数组,正确的代码为( )。 A.int arraya = new int[5]; B.int[] arraya = new int[5]; C.int arraya = new int[]; D.int[5] arraya = new int; 答案:B 10、在C#中,下列代码运行后,变量Max的值是( )(选择一项)Int a=5,b=10,c=15,Max=0;        Max = a>b?a:b;        Max = c<Max?c:Max; A.0 B.5 C.10 D.15 答案:C 11、在C#中,关于continue和break,以下说法正确的是(   ) A break是中断本次循环 B continue是中断本次循环,进入一下次的循环 C break是中断本次循环,进入一下次的循环 D continue是中断整个循环 答案:A 12、在C#中,关于while和do…while,以下说法正确的是(    ) A while先执行然后判断条件是否成立 B while最少的循环次数是1次 C do…while先执行然后判断条件是否成立 D do…while最少的循环次数是0次 答案:C 13、在C#中,下列变量定义与赋值正确的是(      ) A int a=同学   B float a=老师 C double a=教室    D char a=’学’ 答案:D 14、表达式 “abcde”= =”abcde”+”2006”的值为( ) A.True2006 B. true C. false D. 0 答案:C 15、在C#中定义类时,使用的关键字是( ) A、interface B、int C、class D、overrides 答案:C 二.写出下列程序的结果 1、 { int y = 1,x; if (y!=0) { x = 5; } else if (y < 0 ) { x = 4; } else { x = 3; } Console.WriteLine("x={0}”, x); } 答案:5 2、 { int x, y = 0; do { x = y++; Console.WriteLine(x); } while (y < 6); } 答案:15 3、 class Test { static void Main( ) { int x=5; int y=x- -; Console.WriteLine("y={0}", y); y=- -x; Console.WriteLine("y={0}", y); } } 答案:5,5 4、 class Test {     public static void Main()   {  static int[] a = { 1, 2, 3, 4, 5, 6, 7, 8 }; int s0, s1, s2;      s0=s1=s2= 0;      for (int i = 0; i < 8; i++)      {  switch (a[i] % 3)       { case 0: s0 += Test.a[i]; break;        case 1: s1 += Test.a[i]; break;        case 2: s2 += Test.a[i]; break;   } }      Console.WriteLine(s0 + " " + s1 + " " + s2); } } 答案:9+12+13 5、 using System; class Test {    public static void Main () {  int s=0, i=1;   for (;  ; i++) {     if (s>50)  break;     if (i%2==0)  s+=i; } Console.writeLine ("i, s=" + i + "," + s); }   } 答案:14,S=2+4+6...+14,56 6、写出下列函数的功能。 static float FH() {     float y=0,n=0;     int x = Convert.ToInt32(Console.ReadLine()); //从键盘读入整型数据赋给x     while (x!=-1) {       n++; y+=x;       x = Convert.ToInt32(Console.ReadLine());     } if (n==0) { return y; } else { return y/n; } } 答案:从键盘中输入输入整型数只要不是-1就求和,不输入数就返回0,否则求这几个数的的平均值。 7、 using System; class Test { public static void Main () {     int[ ] a ={2,4,6,8,10,12,14,16,18};     for (int i=0; i<9; i++) {         Console.write(“ ”+a[i]);         if ((i+1)%3==0) Console.writeLine();     } } } 答案:2 4 6 8 10 12 14 16 18 三、编程题 (全部把功能写到自定义函数里) 1、编一个程序,从键盘上输入三个数,用三元运算符(? :)把最大数找出来。 答案:{ Console.Write("请输入三个数:"); int a=int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int c = int.Parse(Console.ReadLine()); int max = a; int s= Max(a,b,c); Console.WriteLine("结果为:{0}",s); Console.ReadKey(); } static int Max(int a,int b,int c) { int max; max= a > b ? a : b; max = max> c ? max : c; return max; } 2、编一个程序,输入一个字符,如果是大写字母,就转换成小写字母,否则不转换。 答案: { Console.Write("输入字符:"); char c = char.Parse(Console.ReadLine()); char s = Change(c); Console.WriteLine("{0}", s); Console.ReadKey(); } static char Change(char c) { if (c >= 'A' && c <= 'Z') { c = Convert.ToChar(c + 32); } return c ; } 3、输入一个字符,判定它是什么类型的字符(大写字母,小写字母,数字或者其它字符) 答案: { Console.Write("输入字符"); char c = char.Parse(Console.ReadLine()); Console.WriteLine("{0}", Judge(c)); Console.ReadKey(); } static int Judge(char c) { int b; if (c >= 'A' && c <= 'Z') { b = 0; } else if (c >= 'a' && c <= 'z') { b = 1; } else if (c >= 48 && c <= 57) { b = 2; } else { b = 3; } return b; } 4、编一个程序,输入一个正数,对该数进行四舍五入到个位数的运算。 例如,实数12.56经过四舍五入运算,得到结果13;而12.46经过四舍五入运算, 得到结果12。 答案:{ Console.Write("输入一个正数:"); double a = double.Parse(Console.ReadLine()); Console.WriteLine("{0}",Count(a)); Console.ReadKey(); } static int Count(double a) { int b = (int)a; if (a >= 0) { if (a - b >= 0.5) { b = b + 1; } } return b; } 5、编一个程序,输入0—100之间的一个学生成绩分数,用switch语句输出成绩等第 (成绩优秀(90-100),成绩良好(80-89),成绩及格(60-79)和成绩不及格 (59以下))。 答案:static void Main(string[] args) { Score(); Console.ReadKey(); } static void Score() { int score = int.Parse(Console.ReadLine()); int s = score / 10; int a; if (s >= 9 && s <= 10) { a = 0; } else if (s >= 8&& s < 9) { a = 1; } else if (s >= 6 && s < 8) { a = 2; } else { a = 3; } switch (a) { case 0: Console.WriteLine("成绩优秀"); break; break; case 1: Console.WriteLine("成绩良好"); break; case 2: Console.WriteLine("成绩及格"); break; default: Console.WriteLine("成绩不及格"); break; } 6、 编一个程序,用while循环语句来计算1+1/2+2/3+3/4+...+99/100之和。 答案:{ Console.Write("sum="); double sum = 1; Console.WriteLine("{0}", Sum()); Console.ReadKey(); } static double Sum() { double sum = 1; double i=1; while ( i <= 100) { sum += i / (i+1); i=i+1; } return sum; } 7、编写一个程序,用while语句,求出1+(1+2)+(1+2+3)+...+(1+2+3+...+10)之和。 答案: { int sum = 0; Console.Write("和为:"); Console.WriteLine("{0}",Sum()); Console.ReadKey(); } static int Sum() { int sum = 0; int i = 1; int a = 0; while (i <= 10) { a = a+i; sum= sum+ a; i = i + 1; } return sum; } 8、有关系式1*1+2*2+3*3+...+k*k<2000,编一个程序,求出满足此关系式的k的最大值。 答案:{ int sum=0; Console.WriteLine("最大值为:{0}" ,Max()); Console.ReadKey(); } static int Max() { int sum = 0; int k=1; while(sum<2000) { sum+=k*k; k++; } return k-1; } 9、编写一个程序,要求用while循环语句,打印1到100的正整数,每行打印5个数,每列右对齐。 答案: { Quee(); Console.ReadKey(); } static void Quee() { for (int i = 1; i <= 100; i++) { Console.Write("{0} ",i); if (i % 5 == 0) { Console.WriteLine(); i++; } } return; } 10、编一个程序,利用二重for循环语句,打印出九九乘法口诀表。 答案: { Chengfabiao(); Console.ReadKey(); } static void Chengfabiao() { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { Console.Write("{0}*{1}={2} ", j, i, i * j); } Console.WriteLine(); } return; } 11、编一个程序,利用for循环语句,求出1!+2!+3!+...+10!的和。 答案:{ int sum = 0; Console.WriteLine("{0}", Sum()); Console.ReadKey(); } static int Sum() { int sum = 0; int a = 1; for (int i = 1; i <= 10; i++) { a = a * i; sum = sum + a; } return sum; } 13、编一个程序,定义一个字符串变量,输入字符串,然后再输入一个字符,在字符串中查找该字符出现的次数。 答案: { Console.Write("请输入字符串:"); string str = Console.ReadLine(); Console.Write("请输入字符:"); char c = char.Parse(Console.ReadLine()); int s = Str(str, c); Console.WriteLine("{0}", s); Console.ReadKey(); } static int Str(string str, char c) { int a = 0; for (int i = 0; i < str.Length; i++) { if(str[i]==c) { a++; } } return a; } 15、编一个程序,输入一个整数,判定它为几位数。例如,99是2位数,-100是3位数。 答案: { Console.WriteLine("{0}", Math()); Console.ReadKey(); } static int Math() { int a = int.Parse(Console.ReadLine()); int b=0; int c = 1; if (a < 0) { b = -a; } if (a > 0) { b = a; } while (b >= 10) { b = b / 10; c++; } return c; } 四、面向对象 1、定义一个车辆(Vehicle)基类,具有Run、Stop等方法,具有Speed(速度)、 MaxSpeed(最大速度)、Weight(重量)等域。然后以该类为基类,派生出Bicycle、Car等类。并编程对该派生类的功能进行验证。 答案: 基类: class Vehicle { public double Speed; public double Maxspeed; public double Weight; public void Run() { Console.WriteLine("我跑。。。。。"); } public void Stop() { Console.WriteLine("我停。。。。。"); } 派生类: class Bicycle:Vehicle { } class Car:Vehicle { } 对派生类功能的验证: static void Main(string[] args) { Bicycle p = new Bicycle(); p.Run(); Car c = new Car(); c.Stop(); Console.ReadKey(); } 2、写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度。 2)输出字符串中第一个出现字母a的位置。 3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。 4)将字符串“hello”替换为“me”,输出新字符串。 5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。 答案: { Console.Write("输入字符:"); string str=Console.ReadLine(); while (str.Length<= 3) { str = Console.ReadLine(); } Console.Write("字符串的个数:"); Console.WriteLine("{0}", str.Length); int index = str.IndexOf('a'); if(index>=0) { Console.Write("出现字符a的位置:"); Console.WriteLine("{0}", index); } else { Console.WriteLine("字符串中不含字符a"); } string str1 = "hello" + str.Substring(3); Console.Write("出现新字符串:"); Console.WriteLine(str1); str1 = str1.Replace("hello","me"); Console.Write("替换后的字符串:"); Console.WriteLine(str1); string []str2 = str1.Split('m'); for (int i = 0; i < str2.Length; i++) { Console.Write("分割后的字符串:"); Console.WriteLine(str2[i]); } Console.ReadKey(); } 3、编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。 1)创建一个类A,在A中编写一个可以被重写的带int类型参数的方法MyMethod, 并在该方法中输出传递的整型值后加10后的结果。 2)再创建一个类B,使其继承自类A,然后重写A中的MyMethod方法,将A中接收的整型值加50, } 3)在Main方法中分别创建A和类B的对象,并分别调用MyMethod方法。 答案: 并输出结果。 class A { public virtual void MyMethod(int num) { num+=10; Console.WriteLine("{0}",num); } class B:A { public override void MyMethod(int num) { num += 50; Console.WriteLine("{0}",num); } { A a = new A(); a.MyMethod(3); B b = new B(); b.MyMethod(8); Console.ReadKey(); 4 编写一个类Calculate1,实现加、减两种运算,然后,编写另一个派生类Calculate2,实现乘、除两种运算。 class Calculate1 { public virtual void Mix(int a,int b) { Console.Write("运算符为:"); string s=Console.ReadLine(); int c; if (s == "+") { c = a + b; Console.WriteLine("{0}", c); } else if (s == "-") { c = a - b; Console.WriteLine("{0}", c); } } public override void Mix(int a, int b) { Console.Write("运算符为:"); string s = Console.ReadLine(); int c; if (s == "*") { c = a * b; Console.WriteLine("{0}",c); } else if (s == "/") { c = a / b; Console.WriteLine("{0}",
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服