C#_基础题1-10套答案

one

 

1.用户输入一个整数,用if...else判断是偶数还是奇数
            Console.WriteLine("请输入一个整数:");
            int a = Convert.ToInt32(Console.ReadLine());
            if (a / 2 == 0)
            {
                Console.WriteLine("偶数");
            }
            else
            {
                Console.WriteLine("奇数");
            }
 
2.输入一个字母,判断是大写还是小写字母
            Console.WriteLine("请输入一个字母:");
            char ch = char.Parse(Console.ReadLine());
            if (ch > 'a' && ch < 'z')
            {
                Console.WriteLine("小写");
            }
            else
                Console.WriteLine("大写");
 
3.求1~99所有奇数的和,用while语句
            int i = 0, sum = 0;
            while (i<100)
            {
                sum += i;
                i++;
            }
            Console.WriteLine(sum);
 
4.用户输入三个整数,将最大数和最小数输出
            Console.WriteLine("请输入第1个数:");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第2个数:");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第3个数:");
            int c = Convert.ToInt32(Console.ReadLine());
            int max = Math.Max(Math.Max(a, b), c);
            int min = Math.Min(Math.Min(a, b), c);
            Console.WriteLine("max={0},min={1}",max,min);
      
5.输入三个数,按从小到大的顺序排列
  比较特殊的做法:
            int[] num = new int[3];
            Console.WriteLine("请输入第1个数");
            num[0] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第2个数");
            num[1] = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第3个数");
            num[2]= Convert.ToInt32(Console.ReadLine());
            //int min = num[0] < num[1] ? (num[0] < num[2] ? num[0] : num[2]) : (num[1] < num[2] ? num[1] : num[2]);
            int min =Math.Min(Math.Min(num[0],num[1]),num[2]);
            int max = Math.Max(Math.Max(num[0],num[1]),num[2]);
            for (int i = 0; i < 3; i++)
            {
                if (num[i]<max&&num[i]>min)
                {
                    Console.WriteLine("{0},{1},{2}",min,num[i],max);
                }
            }
 
   一般的做法:
            Console.WriteLine("请输入第1个数:");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第2个数:");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第3个数:");
            int c = Convert.ToInt32(Console.ReadLine());
            int temp;
            if (a > b) { temp = a; a = b; b = temp; }
            if (a > c) { temp = a; a = c; c = temp; }
            if (b > c) { temp = b; b = c; c = temp; }
            Console.WriteLine("{0},{1},{2}",a,b,c);
 
6.将1~200末位数为5的整数求和
            int sum = 0;
            for (int i = 1; i <= 200; i++)
            {
                if (i % 5==0)
                {
                    sum += i;
                }
            }    
            Console.WriteLine(sum);
 
7.计算2.5的3次方
           方法一:
            double sum =1;
            for (int i = 0; i < 3; i++)
            {
               sum *= 2.5;
            }
            Console.WriteLine(sum);
           方法二:           
           Console.WriteLine(Math.Pow(2.5, 3));
    
 
8.将24的所有因子求积
            int sum = 0;
            for (int i = 1; i <= 24; i++)
            {
                if (24%i==0)
                {
                    sum += i;
                }
            }
            Console.WriteLine(sum);
 
9.输入一个年份看是否为闰年
                int i = Convert.ToInt32(Console.ReadLine());
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
                {
                    Console.WriteLine("闰年");
 
                }
                else
                    Console.WriteLine("平年");
 
 
10.输入一段字符判断是大写,还是小写。若是小写,转换为大写,若是大写,转换为小写
            string a = Convert.ToString(Console.ReadLine());
            char b = char.Parse(a);
            if (b >= 'a' && b <= 'z')
            {
                Console.WriteLine(a.ToUpper());
            }
            else
                Console.WriteLine(a.ToLower());
 
 
11.判断一个数是否为素数(质数)
            int b = 1;
            int a = Convert.ToInt32(Console.ReadLine());
            for (int i = 2; i <= a/2; i++)
            {
                if (a % i == 0)
                {
                    b = 0;
                    break;
                }
 
            }
            if (b == 0)
            {
                Console.WriteLine("不是素数");
            }
            else
                Console.WriteLine("是素数");
 
12.一个名为Circle类包含一个字段(半径),两个方法,一个方法求面积,另一个方法求周长,并在Main函数中传递相应的数据,来显示他的周长和面积
    class Circle
    {
        public double sq(double r)
        {
            return Math.PI * r * r;
        }
        public double zc(double r)
        {
            return 2 * Math.PI * r;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Circle cir = new Circle();
            Console.WriteLine("sq={0},zc={1}",cir.sq(3),cir.zc(3));
        }
    }
 
13.设计一个形状为figure,字段的个数和构造函数的设计自己考虑,另外设计一个虚方法
  设计一个圆的类Circle,继承figure类,并重写求面积的方法
  设计完成后在Main函数中定义square和circle的实例,并调用求面积的方法,输出他们的面积
    class Figure
    {
        public virtual double sq(int a,int b)
        {
            return a + b;
        }
        public virtual double cq(double r)
        {
            return r;
        }
    }
     
    class Circle:Figure
    {
        public override double  cq(double r)
        {
            return Math.PI * r * r;
        }
    }
    class Square:Figure
    {
        public override double sq(int a, int b)
        {
            return a * b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Circle cir = new Circle();
            Console.WriteLine("cq={0}",cir.cq(3));
            Square sq = new Square();
            Console.WriteLine("sq={0}", sq.sq(1,2));
        }
    }
 
14.设计一个类,包含两个字段(double类型)和一个构造函数,初始化这两个字段,另外还包含一个虚方法(输出这两个字段的和)
构造另外一个类,此类继承设计的那个类要求重写虚方法(输出这两个数的积)
在Main函数中定义上面类的实例,传入适当的参数,使得他们在屏幕上显示。
    class FatherClass
    {
        public double a;
        public double b;
        public FatherClass()
        {
            this.a = a; this.b = b;
        }
        public virtual double Sum(double a,double b)
        {
            return a + b;
        }
    }
    class SonClass:FatherClass
    {
        //public SonClass() : base() { }构造函数
        public override double  Sum(double a, double b)
        {
             return a * b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            FatherClass fs = new FatherClass();
            SonClass ss = new SonClass();
            Console.WriteLine(ss.Sum(2.2,3.3));
        }
    }

 

two

 

1.  输入一个十进制的数,输出它的十六进制数。
            Console.WriteLine("请输入一个十进制的数:");
            int i = int.Parse(Console.ReadLine());
            Console.WriteLine("它的十六进制数是:{0:x}", i);
 
 
2.  输入一个英文小写字母,将它转换成大写字母。
            //第一种方法:
            Console.WriteLine("请输入一个英文字母:");
            char a = Convert.ToChar(Console.ReadLine());
            if (a > 'a' && a < 'Z')
            {
                Console.WriteLine(a.ToString());
            }
            else
                Console.WriteLine(a.ToString().ToUpper());
 
            // 第三种方法:(int类型的转换)
            int c = Convert.ToInt32(Console.Read());
            char x = (char)c;
            Console.WriteLine(x.ToString().ToUpper());
 
            //第二种方法(char类型)
            Console.WriteLine("请输入一个英文字母:");
            char a = Convert.ToChar(Console.Read());
            if (a > 65 && a < 90)
            {
                Console.WriteLine(a);
            }
            else
            {
                Console.WriteLine(a.ToString().ToUpper());
            }
              
            //第四种方法:(string类型)
            Console.WriteLine("请输入一个英文小写字母:");
            string a = Convert.ToString(Console.ReadLine());
            Console.Write(a.ToUpper());
 
 
 
3.  输入三个整数,求三个数的和。
            Console.WriteLine("请输入三个整数:");
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());
            int c = int.Parse(Console.ReadLine());
             
            //方法一:
            int sum = a + b + c;
            Console.WriteLine("这三个整数的和是:{0}", sum);
 
            //方法二:
            Console.WriteLine("这三个整数的和是:{0}", a + b + c);
 
 
 
 
 
4.  输入一个DOUBLE类型数,输出它的整数部分。
            Console.WriteLine("输入一个DOUBLE类型数:");
            double a = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("{0:#}", a);
 
5.  输入两个数输出其中最大的一个。(使用三目运算符)。
 
            Console.WriteLine("请输入两个数:");
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());
            int max;
            max = a > b ? a : b;
            Console.WriteLine("max={0}", max);
6.  求1+2+3+……+10的和。
 
            int a;
            int sum = 0;
            for (a = 1; a <= 10; sum += a++) ;
            Console.WriteLine("sum={0}", sum);
 
7.  输入一个圆的直径,求它的周长和面积。
 
            Console.WriteLine("请输入一个圆的直径:");
            double d = Convert.ToDouble(Console.ReadLine());
            double c = 2 * Math.PI * d / 2;
            double s = Math.PI * d / 2 * d / 2;
            Console.WriteLine("周长={0},面积={1}", c, s);
 
8.  输入一个数,判断它是否同时被3和5整除。
 
            Console.WriteLine("请输入一个数:");
            int s = int.Parse(Console.ReadLine());
            if (s % 3 == 0 && s % 5 == 0)
            {
                Console.WriteLine("{0}能够同时被3和5整除", s);
            }
            else
                Console.WriteLine("{0}不能够同时被3和5整除", s);
9.  说出console.write()与console.writeline();console.read()与console.readline();constreadonly ;%与/的区别。
            /* console.write()与console.writeline();
             * console.writeLine()换行,而console.write()不换行
             * console.read()与console.readline();
             * console.readline()是读取输入的一行的数据,而console.read()是读取首字母的ASC的值
             * constreadonly
             * const 编译常量 ,是静态的 ,不能和static一起用 ;readonly 执行常量
             * %与/的区别*
             * %是取余,/是取整
             * /
 
10. 打印出:   NAME    SEX     ADDRESS
             SA        16       BEIJING
         TT        20       WUHAN
 
 
            Console.WriteLine("NAME\tSEX\tADDRESS");
            Console.WriteLine("SA\t16\tBEUING");
        Console.WriteLine("TT\t20\tWUHAN");

 

three

 

 

 

four

 

1.  输入一个邮箱地址,如果正确则显示success否则显示error(正确的邮箱地址包含@)。
Console.WriteLine("请输入一个邮箱地址:");
            bool b =true;
            while (b)
            {
                string str = Convert.ToString(Console.ReadLine());
                if (str.Contains("@") && str.Contains(".com"))
                {
                    Console.WriteLine("Success");
                }
                else
                    Console.WriteLine("error");
            }
2.  输入一个小写字母,要求输出它的大写字母。
Console.Write("请输入一个小写字母:");
            string a = Convert.ToString(Console.ReadLine());
       Console.WriteLine(a.ToUpper());
      //字符串大小写相互转换
       //String类型
            while (true)
            {
                string str = Convert.ToString(Console.ReadLine());
                char ch = char.Parse(str);
                if (ch >= 'a' && ch <= 'z')
                {
                    Console.WriteLine(str.ToUpper());
                }
                else
                    Console.WriteLine(str.ToLower());
            }
============================================================================================
       //Char类型
            while (true)
            {
                char ch = char.Parse(Console.ReadLine());
                //if (ch >= 'a' && ch <= 'z')
                if (ch >= 97 && ch <= 122)
                {
                    Console.WriteLine((char)(ch - 32));
                }
                else
                    Console.WriteLine((char)(ch + 32));
            }
 
3.  输入一个5位数整数,将其倒向输出。(例如:输入12345,输出 54321). 
            while (true)
            {
                Console.WriteLine("输入整数:");
                int a = Convert.ToInt32(Console.ReadLine());
                int length = a.ToString().Length;
 
                int[] b = new int[length];
 
                for (int i = 0; i <= length - 1; i++)
                {
                        int num = length - i;
                        int num1 = Convert.ToInt32(Math.Pow(10, num));
                        int num2 = Convert.ToInt32(Math.Pow(10, num - 1));
                        int cout1 = a % num1;
                        b[i] = cout1 / num2; 
                        //Console.Write(b[num - 1]);
                }
==================================================================
方法一:使用Array类的Reverse()方法
                Array.Reverse(b);
                foreach (int m in b)
                {
                     Console.Write(m);
                }
                Console.Write("\n");  
            }
==================================================================
方法二:使用堆栈
                Stack numbers = new Stack();
                //填充堆栈
                foreach (int number in b)
                {
                    numbers.Push(number);
                    //Console.Write(number);
                }
                //遍历堆栈
                foreach (int number in numbers)
                {
                    //Console.Write(number);
                }
                //清空堆栈
                while (numbers.Count != 0)
                {
                    int number = (int)numbers.Pop();
                    Console.Write(number);
                }
                Console.Write("\n");
 
==================================================================
 
输入几个数按照1,2,3的格式输出(简单的说就是每两个数字之间必须用逗号隔开)
            string str = Convert.ToString(Console.ReadLine());
            string[] arraystr = str.Split(',');//数组的动态存储,用Realdine读取。
            Array.Reverse(arraystr);//逆着输出
            foreach (string i in arraystr)
            {
                Console.Write("{0}\0",i);
            }
4.  从界面输入5个数, 然后根据这5个数,输出指定数目的”*”,如有一个数为5,则输出”*****”.
            while (true)
            {
                int n = int.Parse(Console.ReadLine());
                for (int i = 0; i < n; i++)
                {
                    Console.Write("*");
                }
                Console.Write("\n");
            }
 
5.  将如下字符串以”.”分割成3个子字符串. ” www.softeem.com”。
           string a = "www.softeem.com";
            string[] str = a.Split('.');
            Console.WriteLine("{0}\0{1}\0{2}",str[0],str[1],str[2]);
 
算法:不管输入多长如例子a.b.c.e.f.g.h.i 输出为 a b c d e f g h有点则分割,没点就输出
            bool b = true;
            while (b)
            {
                Console.Write("输入数据:");
                string a = Convert.ToString(Console.ReadLine());
                if (a.Contains("."))
                {
                    Console.WriteLine(a.Replace('.',' '));//此处用到了替换,直接将‘.’替换为 '';
                }
                else
                    Console.WriteLine(a);
            }
6.  1 2 3 5          3 5 7 9
4 5 6 2          4 0 3 0
5 6 2 3    +     2 3 0 8        =?
8 5 3 1          9 7 5 1
int[,] num1 = { { 1, 2, 3, 5 }, { 4, 5, 6, 2 }, { 5, 6, 2, 3 }, { 8, 5, 3, 1 } };
            int[,] num2 = { { 3, 5, 7, 9 }, { 4, 0, 3, 0 }, { 2, 3, 0, 8 }, { 9, 7, 5, 1 } };
            int[,] sum = new int[4, 4];
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    sum[i, j] = num1[i, j] + num2[i, j];
                }
            }
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.Write("{0}\t", sum[i, j]);
                }
                Console.Write("\n");
            }
         }
7.    2   4   9            1  2
      1   3   5     *      4  5          =?
                           1  0                               
          int[,] num1 = new int[2, 3] { { 2, 4, 9 }, { 1, 3, 5 } };
            int[,] num2 = new int[3, 2] { { 1, 2 }, { 4, 5 }, { 1, 0 } };
            int[,] sum = new int[2, 3];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    sum[i, j] = num1[i, j] * num2[j, i];
                }
            }
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write("{0}\t", sum[i, j]);
                }
                Console.Write("\n");
            }
         }
 
8   输入a,b,c的值。求  ax*x+bx+c=0的根。
            while (true)
            {
                Console.WriteLine("请输入三个数:");
                double a = Convert.ToInt32(Console.ReadLine());
                double b = Convert.ToInt32(Console.ReadLine());
                double c = Convert.ToInt32(Console.ReadLine());
                double d = b * b - 4 * a * c;
                double x1 = (Math.Sqrt(d) - b) / (2 * a);
                double x2 = (-b - (Math.Sqrt(d))) / (2 * a);
                if (b * b - 4 * a * c < 0)
                {
                    Console.WriteLine("该方程没有根");
                }
                else if (x1==x2)
                {
                    Console.WriteLine("该方程的根是:x1=x2={0}", x1);
                }
                else
                    Console.WriteLine("该方程的根是:x1={0},x2={1}", x1, x2);
            }
9   输入5个数,将其重小到大排列输出。(使用冒泡)
            非冒泡排序
Console.WriteLine("请输入五个数");
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());
            int c = int.Parse(Console.ReadLine());
            int d = int.Parse(Console.ReadLine());
            int e = int.Parse(Console.ReadLine());
            int min1 = a < b ? (a < c ? a : c) : (b < c ? b : c); //比较三个数的大小
            int min2 = d < e ? d : e;
            int min = min1 < min2 ? min1 : min2;
            Console.WriteLine(min);
             
            冒泡排序:
            Console.Write("请按1,2,3的格式输入几个数字:");
            string str = Convert.ToString(Console.ReadLine());
            string[] arraystr = str.Split(',');
            int arraylength = arraystr.Length;
            int[] num = new int[arraylength];
            int count = arraylength - 1;
            for (int i = 0; i <arraylength; i++)
            {
                num[i] = int.Parse(arraystr[i]);
            }
            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < count - i; j++)
                {
                    if (num[j] > num[j + 1])
                    {
                        int temp;
                        temp = num[j];
                        num[j] = num[j + 1];
                        num[j + 1] = temp;
                    }
                }
            }
            foreach (int i in num)
            {
                Console.Write("{0}\0",i);
            }
 
 
10  输入一个数n,求n!。(使用递归)
bool b = true;
            while (b)
            {
                Console.Write("输入数:");
                int n = Convert.ToInt32(Console.ReadLine());
                int sum = 1;
                for (int i = 1; i <= n; i++)
                {
                    sum *= i;
                }
                Console.WriteLine(sum);
            }
}

 

five

 

1.  创建一个Employ类, 实现一个计算全年工资的方法.
    class Employ
    {
        public int Getyearmoney(int monthmoney)
        {
            return 12 * monthmoney;
        }
        static void Main(string[] args)
        {
            Employ employ = new Employ();
            Console.Write("请输入月工资:");
            int monthmoney = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("年工资为:{0}",employ.Getyearmoney(monthmoney));
        }
    }
 
         
2.  定义一个方法, 既可以求2个整数的积,也可以求3或4个整数的积.
        public static int Cj(params int[] nums)
        {
            int Mul = 1;
            foreach (int num in nums)
            {
                Mul *= num;
            }
            return Mul;
        }
        static void Main(string[] args)
        {
            ///将数字按照字符串的格式输出
            Console.WriteLine("请输入数据:");
            string a = Convert.ToString(Console.ReadLine());
            string[] str = a.Split(',');
            int[] num = new int[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                num[i]=int.Parse(str[i]);//将string[]转换为int[]类型;
            }
            int c = 0;
            foreach (int i in num)//遍历数组乘积累积
            {
                c = Cj(num);
            }
            Console.WriteLine(c);
        }
 
 
 
3.  编写一个方法,求两点(x1,y1)与(x2,y2)之间的距离(用户自己输入点的坐标);
    class Program
    {
        public int Mul(int x1, int y1, int x2, int y2)
        {
            return Convert.ToInt32(Math.Sqrt(Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
        }
        static void Main(string[] args)
        {
            Program pr = new Program();
            Console.Write("第一个点的横坐标:");
            int x1 =Convert.ToInt32(Console.ReadLine());
            Console.Write("第一个点的纵坐标:");
            int y1 =Convert.ToInt32(Console.ReadLine());
            Console.Write("第二个点的横坐标:");
            int x2 =Convert.ToInt32(Console.ReadLine());
            Console.Write("第二个点的纵坐标:");
            int y2 =Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("这两个点之间的距离是:{0}",pr.Mul(x1, y1, x2, y2));
        }
     }
 
 
    class Program
    {
        /// <summary>
        /// 求两点间的距离
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public int Fartwopoint(int[,] point)
        {
            Console.Write("第一个点的横坐标:");
            int x1 = int.Parse(Console.ReadLine());
            Console.Write("第一个点的纵坐标:");
            int y1 = int.Parse(Console.ReadLine());
            Console.Write("第二个点的横坐标:");
            int x2 = int.Parse(Console.ReadLine());
            Console.Write("第二个点的纵坐标:");
            int y2 = int.Parse(Console.ReadLine());
            int[,] a = new int[2, 2] { { x1, y1 }, { x2, y2 } };
            double d = Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            int c = Convert.ToInt32(Math.Sqrt(d));
            return c;
        }
        static void Main(string[] args)
        {
            Program pr = new Program();
            int[,] point = new int[2, 2];
            Console.WriteLine(pr.Fartwopoint(point));
        }
    }
方法二:
    class Program
    {
        public int Mul(int x1, int y1, int x2, int y2)
        {
            return Convert.ToInt32(Math.Sqrt(Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
        }
        static void Main(string[] args)
        {
            Program pr = new Program();
            Console.Write("第一个点的横坐标:");
            int x1 =Convert.ToInt32(Console.ReadLine());
            Console.Write("第一个点的纵坐标:");
            int y1 =Convert.ToInt32(Console.ReadLine());
            Console.Write("第二个点的横坐标:");
            int x2 =Convert.ToInt32(Console.ReadLine());
            Console.Write("第二个点的纵坐标:");
            int y2 =Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("这两个点之间的距离是:{0}",pr.Mul(x1, y1, x2, y2));
        }
     }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    方法三:
    class Point
    {
        private int x1;
        private int y1;
        private int x2;
        private int y2;
        public int Jl(int x1,int y1,int x2,int y2)
        {
            int z = Convert.ToInt32(Math.Sqrt(Convert.ToInt32((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
            return z;
        }
 
        static void Main(string[] args)
        {
            Point poi = new Point();
            Console.WriteLine(poi.Jl(poi.x1,poi.x2,poi.y1,poi.y2));
         }       
   }
4.  创建一个Student类, 定义5个字段和2个方法,上述类里, 定义两个同名方法GetAge, 一个返回年龄,一个根据传入的出生年月,返回年龄。
    class Student
    {
        private int id;
        private int age;
        private string name;
        private string sex;
        private DateTime birthday;
 
        public int GetAge(int age)
        {
            return age;
        }
        public int GetAge(DateTime birthday)
        {
            return DateTime.Now.Year - birthday.Year + 1;
 
        }
        static void Main(string[] args)
        {
            Console.WriteLine("请输入出生日期:");
            string stubirthday = Convert.ToString(Console.ReadLine());
            Student stu = new Student();
            stu.birthday = Convert.ToDateTime(stubirthday);
            Console.WriteLine("年龄:{0}",stu.GetAge(stu.birthday));
        }
 
    }
 
5.  编写一个方法,输入一个整数,然后把整数一反序返回(比如输入8976,返回6798);
8976 = 8 x 1000 +9 x 100 +7 x 10 +6 x 1;
6798 = 6 x 1000 +7 x 100 +9 x10 +8 x 1;
1000 = Math.Pow (10, a.Length); a-length --;
6 = a %10 ,7 = a /1000 - a/100,9 = a/100 -a/10
6.  定义一个类,完成一个方法,以数字表示的金额转换为以中文表示的金额, 如下24000,098.23贰千肆佰万零玖拾捌元贰角八分,1908.20 壹千玖佰零捌元贰角
 
 
7.  static ,viod 是什么意思,getsetrefoutpublicprivate的区别是什么。
Static静态类
Void 无返回值
get 可读,set 可写
ref开始必须赋值
out开始不须直接赋值
public 公共可访问的类
private私有的仅在内部类中使用

 

six

 

1.  封装一个N!,当输入一个数时,输出它的阶层。
 
    sealed class Data//密封类
    {
        public int N(int a)
        {
            int sum =1;
            for(int i =1;i<=a;i++)
            {
                sum *= i;
            }
            return sum;
        }
}
 
   private void button1_Click(object sender, EventArgs e)
   {
        Data dt = new Data();
        MessageBox.Show(dt.N(3).ToString());
}
 
 
2.  定义一个索引,访问其相关的内容。
==定义Student 和Students 两个类
    public class Student
    {
        public Student(int id, string name, int age, string sex)
        {
            this.id = id;
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        private int id;
 
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
 
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int age;
 
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        private string sex;
 
        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
    }
    public class Students
    {
        Student[] stu = new Student[3];
        public Students()
        {
            Student stu1 = new Student(1,"a",23,"men");
            Student stu2 = new Student (2,"b",23,"women");
            Student stu3 =new Student (3,"c",21,"men");
            stu[0]=stu1;
            stu[1]=stu2;
            stu[2]= stu3;
        }
        public Student this[int index]
        {
            get
            {
                return stu[index];
            }
            set
            {
                stu[index] =value;
            }
        }
}
 
        private void button1_Click(object sender, EventArgs e)
        {
            Students stu = new Students();
            MessageBox.Show(stu[0].Name+" "+stu[1].Sex+" "+stu[2].Id);
    }
 
3.  定义一个抽象类,并在子类中实现抽象的方法.
 
//定义抽象类Abtr
    abstract class Abtr
    {
        public abstract int Num(int a,int b,int c);// 抽象类的抽象方法
 
        public virtual int Max(int a,int b,int c) //抽象类的虚方法
        {
            int max1 = Math.Max(a, b);
            int max = Math.Max(max1, c);
            return max;
        }
}
 
    //子类是抽象类
    abstract class CAbtr : Abtr
    {
        public abstract override int Num(int a, int b, int c);
    }
 
    //子类不是抽象类时候
    class CAbtr : Abtr
    {
        public override int Num(int a, int b, int c)
        {
            return Convert.ToInt32(Math.Sqrt(Convert.ToDouble(a * b * c)));
        }
        public override int Max(int a, int b, int c)
        {
            int max1 = Math.Max(a, b);
            int max = Math.Max(max1, c);
            return 100*max;
        }
 
 
4.  有三角形,矩形,正方形,梯形,圆,椭圆,分别求出各自的周长,面积(abstract).
 
    abstract class Refent
    {
        public abstract int Sanjiaozc(int d,int h,int a,int b);
        public abstract int Sanjiaomj(int d, int h,int a,int b);
        public abstract int Juxizc(int l, int h);
        public abstract int Juximj(int l, int h);
        public abstract int Zhengzc(int a);
        public abstract int Zhengmj(int a);
        public abstract int Tixingzc(int a, int b , int h,int c,int d);
        public abstract int Tixingmj(int a, int b, int h,int c,int d);
        public abstract int Cirzc(int r);
        public abstract int Cirmj(int r);
}
 
    class CRent:Refent
    {
        int zc; int mj;
        public override int Sanjiaozc(int d,int h,int a,int b)
        {
            zc = d+ a + b;
            return zc;
        }
        public override int Sanjiaomj(int d, int h, int a, int b)
        {
            mj = d * h / 2;
            return mj;
        }
        public override int Juxizc(int l, int h)
        {
            zc = 2*(l + h);
            return zc;
        }
        public override int Juximj(int l, int h)
        {
            mj = 2 * l * h;
            return mj;
        }
 
        public override int Zhengzc(int a)
        {
            zc = 4 * a;
            return zc;
        }
        public override int Zhengmj(int a)
        {
            mj = a * a;
            return mj;
        }
        public override int  Tixingzc(int a, int b, int h, int c, int d)
        {
            zc = a + b +c +d;
            return mj;
        }
        public override int Tixingmj(int a, int b, int h, int c, int d)
        {
            mj = (a + b) * h / 2;
            return mj;
        }
        public override int Cirmj(int r)
        {
            mj = Convert.ToInt32(Math.PI * r * r);
            return mj;
        }
        public override int Cirzc(int r)
        {
            zc = Convert.ToInt32(2 * Math.PI * r);
            return zc;
        }
}
 
 
5.  分析:为什么String类不能被继承.
string类是密封类
 
 
6.  定义一个接口,接口必须含有一个方法, 用一个实现类去实现他.
    interface Inte
    {
        int GetAge(string date);
}
 
public class Employee:Inte
    {
        public int GetAge(string date)
        {
            DateTime dt = DateTime.Parse(date);
            int age =dt.Year - DateTime.Now.Year;
            return age;
        }
    }
 
7.  说明接口和抽象类的区别.
1.  接口不能有构造函数,抽象类有构造函数
2.  接口允许方法的实现,抽象方法是不能被实现的
3.  接口里面不须override来重写,抽象类一定要override重写
 
 
8.  如果从TextBox输入一个参数, 就求圆的周长, 两个参数就求矩形的周长,三个参数就求三角形的周长.
***对于params的用法还不是很了解?ArrayList, 还有HashTable
    class ZC
    {
        public int C(params int[] a)
        {
            int sum = 0;
            foreach (int i in a)
            {
                if (a.Length == 1)
                {
                    sum += Convert.ToInt32(2 * Math.PI * i);
                }
                if (a.Length == 2)
                {
                    sum += 2* i;
                }
                if (a.Length==3)
                {
                    sum += i;
                }
            }
            return sum;
        }
    }
 
WinFrom中的代码:
        private void button1_Click(object sender, EventArgs e)
        {
            ZC zc = new ZC();
            string s = textBox1.Text.Trim();
            string[] str = s.Split(',');
            int[] a = new int[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                a[i] = int.Parse(str[i]);
            }
            MessageBox.Show(zc.C(a).ToString());
        }
 
 
9.  说明重载与重写的区别。
 
1.重载函数之间的参数类型或个数是不一样的.重载方法要求在同一个类中.
(解释:例如原来有一个构造函数
public Student(){},
现在有重载了这个构造函数
public Student(int age ,int id,string name)
{
   this.age = age ;
   this.name = name ;
   this.id = id;
})
 
2.重写要求返回类型,函数名,参数都是一样的,重写的方法在不同类中.
 
10. 完成一个小型的计算器,不熟悉的功能可以不完成
11. 
12. Public private internal protected的使用范围。
 大到小:Public protected internal private
  Public 所有类,protected 子父类访问internal当前项目,private当前类

 

seven

 

1.定义一个委托,委托一个去借东西的方法Send,如果借到了返回GOOD!,否则NO!
class Borrow
    {
        public delegate string Send();
        public string Br(Send send)
        {
            return send();
        }
        public string OK()
        {
            return "GOOD!";
        }
        public string NO()
        {
            return "NO!";
        }
}
调用:
Borrow b = new Borrow();
            MessageBox.Show(b.Br(b.OK));
 
2.定义一个事件,当我激发它时,返回一个结果给我.
class Event1
        {
            public delegate string Eve();
            public event Eve eve;    //事件
            public string Trigger()
            {
                if (eve != null)
                    return eve();
                else
                    return "";
            }
            public string A1()
            {
                return "A1";
            }
            public string A2()
            {
                return "A2";
            }
        }
调用:
Event1 event1 = new Event1();
            event1.eve += event1.A1;
            MessageBox.Show(event1.Trigger());
 
3.定义一个拿100元钱去买东西的委托,书的价格是58元,本的价格是20元,返回剩余的钱.
class Event1
        {
            public delegate int Eve(int sum);
            public event Eve eve;    //事件
            public int Trigger(int sum)
            {
                if (eve != null)
                    return eve(sum);
                else
                    return 0;
            }
            public int Book(int sum)
            {
                return sum-=58;
            }
            public int Text(int sum)
            {
                return sum-=20;
            }
        }
调用:
Event1 event1 = new Event1();
            int sum = 100;
            event1.eve += event1.Book;
            sum = event1.Trigger(sum);
            event1.eve += event1.Text;
            sum = event1.Trigger(sum);
            MessageBox.Show(sum.ToString());
 
4.说出委托与事件的区别.
事件属于委托,委托不属于事件.事件是委托的一个变量.

 

 

eight

 

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(Max(1, 4, 6, 8, 3).ToString());
}
public int Max(params int[] array)
{
    int max = Int32.MinValue;
    foreach (int i in array)
    {
        max = Math.Max(i, max);
    }
    return max;
}
 
private void button2_Click(object sender, EventArgs e)
{
    string str = "";
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 2*i+1; j++)
        {
            str += "* ";
        }
        str += "\r\n";
    }
 
    for (int i = 0; i < 3; i++)
    {
        for (int j = 5; j > 2*i; j--)
        {
            str += "* ";
        }
        str += "\r\n";
    }
    richTextBox1.Text = str;
}
 
private void button3_Click(object sender, EventArgs e)
{
    DateTime date1 = DateTime.Parse("1998-1-1 00:00:00");
    DateTime date2 = DateTime.Parse("2009-3-13 00:00:00");
    TimeSpan time = date2 - date1;
    int days = time.Days+1;
    if (days % 5 == 0)
    {
        MessageBox.Show("晒网");
    }
    else
    {
        if (days % 5 > 3)
        {
            MessageBox.Show("晒网");
        }
        else
        {
            MessageBox.Show("打鱼");
        }
    }
}
 
/// <summary>
/// 求200小孩。。。。。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
    int length = 200;
    int[] array = new int[length];
    for (int i = 0; i < length; i++)
    {
        array[i] = i + 1;
    }
    string str = null;
 
    for (int i = 1; i <= length; i++)
    {
        str += i.ToString() + ",";
    }
    Out(str, array);
     
}
public void Out(string str,int []array)
{
    if (array.Length == 3)
    {
        MessageBox.Show(array[1].ToString());
    }
    else
    {
        int lit = array.Length % 4;
        string st = "";
        for (int i = lit; i>0; i--)
        {
            st += array[array.Length - i].ToString()+",";
        }
         
        for (int i = 1; i <= array.Length; i++)
        {
            if (i % 4 == 0)
            {
                str=str.Replace(","+array[i - 1].ToString() + ",", ",");
            }
        }
        int length = str.Length;
        if (st == "")
        {
            str = str;
        }
        else
        {
            str = str.Replace(st, "");
            str = st + str;
        }
       
        string[] arraystring = str.Split(',');
        int[] arrayint = new int[arraystring.Length-1];
        for (int i = 0; i <arraystring.Length-1; i++)
        {
            arrayint[i] = int.Parse(arraystring[i]);
        }
        Out(str, arrayint);
    }
 
}
 
private void button5_Click(object sender, EventArgs e)
{
    int sum = 0;
    Random rand = new Random();
    for (int i = 0; i < 100; i++)
    {
         
        int x = rand.Next(65, 91);
        char y = Convert.ToChar(x);
        if (y == 'A' || y == 'O' || y == 'E' || y == 'I' || y == 'U')
        {
            sum++;
        }
    }
    MessageBox.Show(sum.ToString());
 
}
 
private void button6_Click(object sender, EventArgs e)
{
    string str = "2,";
    for (int i = 3; i <= 100; i++)
    {
        int j;
        for ( j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                break;
 
            }
           
        }
        if (i==j)
        {
            str += i.ToString()+",";
             
        }
    }
    MessageBox.Show(str);
}
 
private void button7_Click(object sender, EventArgs e)
{
    int i = 27863654;
    string str = Convert.ToString(i, 2);
    string sumstr = str.Replace("0", "");
    MessageBox.Show(sumstr.Length.ToString());
}
 
private void button8_Click(object sender, EventArgs e)
{
    string str = "";
    for (int i = 100; i < 1000; i++)
    {
        int a = i % 10;//个位
        int b = (i / 10) % 10;//十位
        int c = i / 100;//百位
        int sum = a * a * a + b * b * b + c * c * c;
        if (sum == i)
        {
            str += i.ToString() + ",";
        }
    }
    MessageBox.Show(str);
}
 
private void button9_Click(object sender, EventArgs e)
{
    int sum = 0;
    string str = "";
    for (int i = 2; i < 1000; i++)
    {
        for (int j = 1; j < i; j++)
        {
            if (i % j == 0)
            {
                sum += j;
            }
        }
        if (sum == i)
        {
            str += i.ToString() + ",";
        }
        sum = 0;
         
    }
    MessageBox.Show(str);
}
 
private void button10_Click(object sender, EventArgs e)
{
    int sum = 0;
    int length;
    for (int i = 1; i < 9; i++)
    {
 
        sum += Convert.ToInt32( 200 * Math.Pow(2, i));
    }
    sum += 200;
 
    length = 100 + Convert.ToInt32(sum / Math.Pow(2, 9));
    MessageBox.Show(length.ToString());
}
 
private void button11_Click(object sender, EventArgs e)
{
    int n = 225;
    int i = 2;
    string str = "";
    while (n != 1)
    {
        while (n % i == 0)
        {
            if (n / i !=1)
            {
                str += i.ToString()+"*";
            }
            else
            {
                str += i.ToString();
            }
             
            n = n / i;
        }
        i++;
    }
    MessageBox.Show(str);
}
 
private void button12_Click(object sender, EventArgs e)
{
    int max = 20;
    int min = 5;
    //最大公约数
    string strmax = "";
    //for (int i = min; i >0; i--)
    //{
    //    if (max % i == 0 && min % i == 0)
    //    {
    //        strmax = i.ToString();
    //        break;
    //    }
    //}
    //zuixiaogongbeishu
    for (int i = max; i <= max*min; i++)
    {
        if (i%max==0 && i%min==0)
        {
            strmax += i.ToString() + ",";
            break;
        }
    }
    MessageBox.Show(strmax);
}
 
private void button13_Click(object sender, EventArgs e)
{
    int[,] array = new int[20, 2];
    array[0, 0] = 2;
    array[0, 1] = 1;
 
    for (int i = 1; i < 20; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            if (j == 0)
            {
                array[i, j] = array[i - 1, j] + array[i - 1, j + 1];
            }
            else
            {
                array[i, j] = array[i - 1, j - 1];
            }
        }
    }
 
    double dmin = 1;
    for (int i = 0; i < 20; i++)
    {
        dmin *= array[i, 1];
    }
    double bsum = 0;
    for (int i = 0; i < 20; i++)
    {
        bsum += (dmin / array[i, 1]) * array[i, 0];
    }
 
    MessageBox.Show(bsum.ToString()+"/"+dmin.ToString());
    //double min=Math.Min(dmin,bsum);
    //double maxcount = 0;
    //for (double i = min; i > 0; i--)
    //{
    //    if (dmin % i == 0 && bsum % i == 0)
    //    {
    //        maxcount = i;
    //        break;
    //    }
    //}
    //string str = (bsum / maxcount).ToString() + "//" + (dmin / maxcount).ToString();
    //MessageBox.Show(str);
 
}
 
private void button14_Click(object sender, EventArgs e)
{
    Humen student1 = new Humen();
    student1.Age = 10;
    Humen student2 = new Humen();
    student2.Age = student1.Age + 2;
}

 

 

nine

C#基础测试题

一. 填空

1.      CLR中文意思是 公共语言运行库      

2.      MSIL中文意思是  微软中间语言           

3.      GC中文意思是    垃圾回收机制       

4.       Visual studio 2005 中INT 占   4     个字节.

5.      语句结构分为     顺序结构,选择结构,循环                                     结构.

6.      声明一个空间用      namespace    关键字,声明一个类用     class 关键字,定义抽象类用     abstract     关键字,定义密封类使用     sealed           关键字, 实例化一个对象使用                  new               是        关键字.类的默认修饰符是   internal     

7.      修饰符有  4   种,分别是  public protected internal private                                            

8.      LONG转换成INT 属于    显示   转换, INT 转换成STRING 用            方法.STRING 转换成INT 用       int.Parse()         方法,还可以使用                                     方法    Convert.ToInt32()          .

9.      弹出一个对话框使用     MessageBox.Show();            

10.  面向对象的三大特性是     继承        ,     封装        ,   多态         

11.  取出字符串中的子字符串用      Substring()          方法,替换用    Replace()         方法,分割用    Split()        方法,查找其中一项的位置用      IndexOf()          方法,查找其中是否包含用      Contains()          方法.

12.  快速排序用         Array.Sort()            ,倒序用           Array.Reverse()                 .

二. 选择题

1.c#程序的主方法是(B )

A.main()            B.Main()              C.class()        D.namespace()

 

2.Console类位于下列哪个命名空间( C)

A.System.Text   B.System.IO        C.System      D.System.Collections

 

3.下列属于值类型的有(B,C,D)(多选)

A.class        B.enum         C.struct         D.int             E.string

 

4. static const int i = 1   这句代码是否有误(A)

A.有  B.无

 

5.下列哪些方法能对字符串进行操作(ABCD)(多选)

A.Trim()             B.IndexOf()         C.Insert()             D.ToUpper()        E.Add()ArrayList HashTable

 

6.以下说法正确的是(AB)(多选)

A.构造函数名必须和类名相同

B.一个类可以声明多个构造函数

C.构造函数可以有返回值

D.编译器可以提供一个默认的带一个参数的构造函数

 

7.下列关于抽象类的说法正确的是(ACD)(多选)

A.抽象类能够拥有非抽象方法

B.抽象类可以被实例化

C.抽象类里可以声明属性

D.抽象类里可以声明字段

8. 可用作C#程序用户标识符的一组标识符是(B)

A. void    define    +WORD       B. a3_b3    _123     YN

C. for      -abc      Case         D. 2a      DO     sizeof

 

9. C#的数据类型有(B) 
A 值类型和调用类型 
B 值类型和引用类型 
C 引用类型和关系类型 
D 关系类型和调用类型

 

三. 判断题

(1).一个子类可以有多个父类。错

(2).子类可以直接继承父类中的构造函数。错

(3).抽象类里面可以有方法的实现。对

(4).int i,char x=‘a’,如果i=x,则i=97 对

(5).电脑是一个对象 错

(6).String.Format(“{0}*{1}={2}”,2,3,4),打印结果为2*3=4 对

(7).抽象方法在子类必须实现  错

(8).public int Add(Params int[]a)可以把一个数组当参数传递。对

(9).抽象类没有构造函数 错 有,不能被实例化

(10).string是密封类 对

(11).接口内有构造函数 错

四. 分析题

1.写出结果

 

 

2.写出结果

 

 

2

5

1

6

 

 

五. 比较题

1. Console.Write();与Console.WriteLine();

 Console.Write()是输出一行数据不换行

Cosole.WriteLine()是输出一行数据换行

2.  Console.Read();与Console.ReadLine();

Console.Read()是读出首字母的ASCII码,后者是读出字符串.

Console.ReadLine()是读取一行数据

3.  const与readonly

const是静态的编译常量,不能和static在一起用

readonly是执行常量

4.  值类型和引用类型

(1) 值类型:以栈进行存储.

(2) 引用类型:以堆进行存储.

5. String与string,StringBuilder

String是C#中独特数据类型 

string 是静态类型

StringBuilder的效率,比string的效率高,stringBuilder不声明新的区域只是扩大原有区域,而string是要声明新的区域,消耗内存

6.  Array与ArrayList,Hastable

Array 是数组

ArrayList处理字符串时和Array不一样,动态添删该ArrayList.Add();

Hashtable键值对

7.  接口与抽象类

接口里都是抽象的方法

接口没有构造函数,

抽象类可以包括能够哦实现的方法

抽象类有构造函数

8.  i++和++i

   ++i操作数加一   (i)先加,(i加的结果给变量)再用

   i++操作数不先加一  先用(值先给变量),在加(i再加)。

9.重写与重载

1.重载函数之间的参数类型或个数是不一样的.重载方法要求在同一个类中.

2.重写要求返回类型,函数名,参数都是一样的,重写的方法在不同类中.

10.&和&&

&位与

&&逻辑与

11.事件与委托

事件是委托的变量,委托不是事件

12.ref,out

ref开始必须赋值

out开始不须直接赋值 ,out必须要重新赋值。

 

六. 解答题

1.    解释static,void。

static 是静态类,寄存性变量,是全局变量

void无返回类型

2.    简述 private、 protected、 public、 internal 修饰符的访问权限

Public 是公共类,可以在所有项目中访问

Protected只能在子类和父类之间访问

Internal在当前项目访问

Private在当前类中访问

3.    什么是装箱和拆箱

装箱:值类型转换为引用类型

拆箱:引用类型转换为值类型

4.      输入一个英文小写字母,将它转换成大写字母(快速转换)。

 string a =Convert.ToString(Console.ReadLine());

 Console.WriteLine(a.ToUpper());

5.       * * * *

      * * *

          * *

             * 打印出来(算法)

 

 

 

6.      1!+3!+5!(使用递归实现)(算法)

      方法一:

 

               int A(int a)
 
               {
 
                        if(a==0 || a==1)
 
                          return 1;
 
                  else
 
                          returna*A(a-1);
 
               }
 
 
调用:
 
      int sum=0;
 
      for(inti=1;i<=5;i+=2)
 
      {
 
                 sum+=A(i);
 
      }

 

 

方法二:

 

int a = Convert.ToInt32(Console.ReadLine());
 
int m = 1; intsum = 0;
 
 for(int i = 1; i <= a; i++)
 
 {
 
     m *= 2 * i - 1;
 
     sum += m;
 
 }
 
 Console.WriteLine(sum);

 

 

7.      输入5个数,将其重小到大排列输出。(使用冒泡)(算法)

 

Console.WriteLine("请按1,2,3,4,5的格式输入五个数:");
 
string  str = Convert.ToString(Console.ReadLine());
 
string[] s  = str.Split(',');
 
int length = s.Length-1;
 
int[] num =newint[s.Length];
 
for( int i =0 ;i<s.Length; i++)
 
{
 
     num[i] = int.Parse(s[i]);
 
}
 
for(int i =0;i<length;i++)
 
{
 
  for(int j =0;j<length-i;j++)
 
  {
 
      int temp;
 
       if( num[j]>num[j+1])
 
     {
 
         temp =num[j+1];
 
         num[j+1]=num[j];
 
         num[j]=temp;
 
      }
 
  }
 
}
 
foreach(int i in num)
 
{
 
  Console.Write(i+"\0");
 
}

 

 

8.      定义一个事件,当我激发它时,返回一个结果给我。(算法)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Event1
{
       public delegatestring Eve();
 
       public event Eveeve;
 
       public stringTrigger()
       {
          if(eve!=null)
               return eve();
          else
              return "";
      }
      
     public stringA1()
     {
             return "A1";
     }
      
     public stringA2()
     {
             return "A2";
     }
}
 
调用:
Event1 event1=new Event1();
event1.eve+=event1.A1;
MessageBox.Show(event1.Trigger());

 

 

ten

笔试试卷

C#基础

1.  传入某个属性的set方法的隐含参数的名称是什么?

value

 

2.  如何在C#中实现继承?

继承类或接口

 

3.  C#支持多重继承么?

支持多层继承,不支持多继承

 

4.  被protected修饰的属性/方法在何处可以访问?

子类或当前类

 

5.  私有成员会被继承么?

不能

 

6.  请描述一下修饰符protected internal。

在子类和当前程序集可见

 

7.  C#提供一个默认的无参数构造函数,当我实现了另外一个有一个参数的构造函数时候,还想保留这个无参数的构造函数。这样我应该写几个构造函数?

2个

 

8.  C#中所有对象共同的基类是什么?

Object类(超级类)

 

9.  重载和覆写有什么区别?

1 重载是在当前类进行,而重写是在子类中;

2 重载的方法返回值可以不同,方法名相同,参数列表不同,而重写方法名和参数列表及返回值都相同

 

10.   在方法定义中,virtual有什么含意?

定义为虚方法,以便于子类重写或隐藏

 

11.   能够将非静态的方法覆写成静态方法么?

(不能)

但是可以写成静态方法关键字new

 

12.   可以覆写私有的虚方法么?

不能

 

13.   能够阻止某一个类被其他类继承么?

可以,将该类加上sealed申明可实现

 

14.   能够实现允许某个类被继承,但不允许其中的某个方法被覆写么?

可以,将不允许被重写的方法申明为私有方法或静态方法

 

15.   什么是抽象类(abstract class)?

是对现实世界中某个类型的一个抽象,例如可将一棵树定义为一个抽象类,各种树有不同的特性(高度,树形等),也有相同的部分(都由树枝树干组成等)。不能实例化,只能通过继承实现。抽象类中即可有抽象方法(以便个性的实现),也有实现方法(共性)。

(若干个类的共同特征。)

 

16.   何时必须声明一个类为抽象类?

当一个类的不同对象既有相同的共性的地方又有个性的地方时,可将共性的方法设计为实现方法,将其个性的地方设计为抽象方法

若干个类有共同的属性和方法,事件时候必须声明一个抽象类)

 

17.   接口(interface)是什么?

(接口是一种服务的描述,然后去实现它。)

定义各种抽象方法的一种机制,不同于类的是,接口可以多继承,接口不能实例化,通过继承实现接    口。实现接口必须要实现接口中的所有方法。

 

18.   为什么不能指定接口中方法的修饰符?

接口中的方法均为抽象方法,并且其默认为public,接口中的方法只有一个定义,没有实现,要使用其方法必须要继承该接口。而不能实例化接口,以获得该方法。

 

19.   可以继承多个接口么?

可以

 

20.   那么如果这些接口中有重复的方法名称呢?

可以在调用时加上其接口的前缀来唯一标示它

在接口方法名称前面带上一个接口名称)

 

21.   接口和抽象类的区别是什么?

1 接口中的方法都是抽象方法,而抽象类中可以有实现的方法

2 接口中无构造函数,而抽象类有

3 接口方法在子类重写不需要Override关键字修饰

4 继承接口的类必须实现接口的所有方法,而继承抽象类的类不需要实现父类的所有抽象方法

5.接口默认的是一个public方法 不能有访问修饰

接口和抽象类均不能实例化new)

 

22.   如何区别重载方法?

一个方法非重载版本返回值和参数列表可以不同

 

23.   const和readonly有什么区别?

Const为编译常量,编译后即不可更改,已经赋值;

Readonly为执行常量,在运行时赋值。

 

24.   System.String 和System.StringBuilder有什么区别?

System.String 的值不可修改,对System.String 的对象赋值是对其引用赋值;

对System.StringBuilder赋值,将直接对该对象的值进行修改,当需要进行大批量的字符串操作时其效率很高。

 

String.string返回新的值

String.StringBuilder不返回原始值

 

25.   如何理解委托?

委托是一种将方法封装的机制,它可以把方法当做参数进行传递和调用。可同时委托多个方法形成委托连,来关联多个事件或方法。(委托是一用来存储方法的,可以把方法当做变量传递。存储方法时候会检查方法的声明) 



Asp.Net

1.什么是服务器端控件?请举例。这些控件与一般的Html控件在使用上有何区别?

可以再服务器端运行的控件,比如Button,Text,DropDownList等,一般的Html控件只能激发客户端事件,而服务端控件激发后会发生回传,将控件的状态回传服务器端并激发相关的服务器控件的事件。

 

服务器控件被标识为ruant:server;而html不会被标识。

 

2.GridView数据源可以有哪些?

1 数据集(包括DateSet,DataTable等)

2 集合(键值对,哈希表,字典,队列等)

3.xml 文件

 

3.什么是用户控件?

用户自定义的控件集合,可通过将多个功能关联的控件组织在一起(如登录要用到的各种控件),形成用户控件以提高代码的重用性

 

(继承自UserControl)

 

4.什么是code-Behind技术?(后台)

即代码后置技术,完成前后台代码的分离,方便维护和代码管理

aspx,ascx,然后是程序代码逻辑文件,.cs,.vb,文件、将程序逻辑和页面分离开)  

 

5.向服务器发送请求有几种方式?

get和Post表单提交方式

 

6.在页面中传递变量有几种方式?

1 session(跨页面)

2 cookie(跨页面)

3 application (存在一定的风险,登录所有人使用一个application)

4 viewstate(当前页面)

 

7.Machine.Config和Web.Config是什么关系?

Machine.Config包括Web.Config,包含和被包含的关系

所页面继承Machine.Config,web.config某些项目和网站做些特殊的配置)

 

8.什么是强类型DataSet?如何使用?采用它对分层架构有什么作用?

强类型DataSet定义时即将确定其数据类型,用强类型DataSet定义数据库的实体,在业务逻辑层即可将它当做类来处理,使用起来就象操作类一样。它可尽量降低系统架构的耦合。

(DataSet1 dt = new DataSet1();)

强类型DataSet是指需要预先定义对应表的各个字段的属性和取值方式的数据集,对于所有这些属性都需要从DataSet,DataTable,DataRow继承,生成相应的用户自定义类。强类型的一个重要特征,就是开发者可以直接通过操作强类型数据集对象中的域属性来实现对关系数据对象的操作,而不是向非强类型数据集那样,使用结果集进行操作。

 

9.ViewState是什么?

它是保存网页控件状态的一个对象,在当前页中可设置及保存相关的控件的状态,其采用的是保存在请求体中,在服务器和客户端往返的机制,即其不需要保存在客户端或长期保持在服务器端,其采用Base64对控件状态编码。

 

ViewState是一个隐藏域,保存这个页面所有服务器的状态,在服务器和客户端进行往返,实现一个有状态的Http协议。

 

10.如何改变ViewState存储?

添加票据信息

 

11.如何在页面回发后清空服务器端控件值?

设置其IsPostBack属性值为TRUE,在调用控件的Clear()方法

跳转到当前页面。

(if(!IsPostBack){Response.Redirect(“this.aspx”);})

(if(!IsPostBack){Server.Transfer(“this.aspx”);})

//重写page的LoadViewState方法

//不加载viewState中的数据。

if(!IsPostBack){protected override voidLoadViewState(object savestatus)

{//base.LoadViewState(savestatus)}}

 

12.如何判断页面是否是回发?

通过IsPostBack属性

 

13.Global.asax是做什么用的?谈谈你的使用?

可用于站点全局变量的设置,session开始和结束事件的设置等。

在Global.Asax中设置一个全局变量Application,(让其自增)可用于统计登录站点人数的统计。

 

14.Asp.net身份验证有哪些方式?你是如何做的?

有4种:

1 none 无验证

2 Form验证(适合局域网及Internet)

3 Windows身份验证(使用于局域网)

4 PassPort验证微软提供,收费)

对身份验证的设置是在Web.Config中进行的

 

15.请描述Asp.net数据验证机制。根据验证逻辑生成对应脚本,验证时脚本

Asp.Net提供多种服务器端的验证控件,如是否为空的验证控件(验证是否为空值),比较验证控件(比较两个控件输入的值是否相等),范围验证控件(验证一个值是否在一个范围内),规则验证控件(如Email和电话号码的验证),综合验证控件(记录其他验证控件的验证结果信息,相当于一个验证汇总)

 

16.什么是Ajax技术?.Net2.0中提供了哪些接口支持?你是如何实现Ajax效果的?

异步Javascript 和 XML的英文简写,其包括异步无刷新,JavaScript,Dom操作及XML技术的运用,ASP.Net 2.0中集成了相关的Ajax控件(AjaxExtension),通过该空间中的属性及触发器的设置可实现一些Ajax效果。

IcallBackEventHander

 

JavaScript

 

1.获取以下html片段的第二个<span />元素。

<div id=”d1”><span>1</span><span>2</span></div>

 

2.说出以下表达式值:

       typeof(NaN)

      typeof(Infinity)

       typeof(null)

      typeof(undefined)

      Object.constructor

      Function.constructor

 

 

 

3.说出下面这个函数的作用

 

function(destination, source) {

for (property in source) {

   destination[property] = source[property];

  }

    return destination;

};

 

4.说出下面代码的运行结果

var a = "123abc";

alert(typeof(a++));

alert(a);

 

数据库

1.列举ADO.NET主要对象,并阐述其用途。

DataSet(离线数据集,相当于内存中的一张表),Connection(由于数据库连接操作),Command(由于数据库更新操作),DataAdaper(数据库离线状态下的查询及更新操作),DataReader(只读向前的数据查询对象)

 

2.DataReader和DataSet有什么区别?

DataReader:连线操作对象,只读向前 大量数据

DataSet:离线操作对象,相当于内存中的一张表,少量数据

 

3.什么是索引?有什么作用?

索引用于标示数据在内存的地址

索引是一个单独的、物理的数据库结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单。

 

4.Sql Server2000和Sql Server2005编写、调试存储过程有哪些不同?

 

5.如何设计树状数据结构?

通过设置标号和父标号,做成动态链表形式。

 

6.有以下3个表:

S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄

C (C#,CN )  C#,CN 分别代表课程编号、课程名称

SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩

 

a.使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名。

Select S#,SN from s

Where S# in

(select S# from SC where S.S#=SC.S# and C# in

(select C# from C where CN=’税收基础’

)

)

 

b.使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位。

Select SN ,SD from s where S# in(select S#from SC where C#=’C2’)

 

 

c.使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位。

Select SN ,SD from s where S# in(select S#from SC where C# is not C2)

 

d.使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位。

 

e.查询选修了课程的学员人数。

Select count(*) from S where S# in (select S#from SC )

 

f.查询选修课程超过5门的学员学号和所属单位。

Select SN,SD from S JOIN (SELECT SC.S# FROM SC GROUP BYS# having count(*)>5) C ON S.S# =C.S#

 
posted @ 2017-11-17 16:02  王春天  阅读(4296)  评论(0编辑  收藏  举报
云推荐