.NET学习 数组测试题级及参考答案

来源:互联网 发布:3g软件开发工程师 编辑:程序博客网 时间:2024/05/16 14:28

1. 数组有没有length()这个方法? String有没有length()这个方法?

    数组没有Length()方法,只有属性。String 有这个length()这个方法

2.请编程实现int[] max={6,5,2,9,7,4,0}冒泡排序算法?

  代码节选

 int[] max ={ 6, 5, 2, 9, 7, 4, 0 };

            foreach (int a in max)

            {

               Console.Write(a + " ");

            }

            Console.WriteLine();

 

            //冒泡排列

         for (int i=1;i<max.Length;i++)

            {

                for (int j=0;j<max.Length-i;j++)

                {

                    if (max[j]>max[j+1])

                    {

                      int temp=max[j];

                        max[j]=max[j+1];

                        max[j+1]=temp;

                    }

                }

            }

            //排序后的数组

             foreach (int a in max)

            {

                Console.Write(a + " ");

            }

            Console.WriteLine();

3.请写出程序的输出结果:

  string[] aa = new string[5];

  aa[0] = "33";

  aa[5] = "66";

  string s = "";

  foreach (string m in aa)

  {

      s += m;

 }

Console.WriteLine(s);

错误, aa[5]超出数组索引的最大范围。

4.产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。

int[] intArr=new int[100];


ArrayList myList=new ArrayList();
Random rnd=new Random();
while(myList.Count<100)
{
int num=rnd.Next(1,101);
if(!myList.Contains(num))
myList.Add(num);
}
for(int i=0;i<100;i++)
intArr[i]=(int)myList[i];

 

using System;

using System.Collections.Generic;

using System.Text;

//产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[100]; //定义一个长度为100数组

            for (int i = 0; i < array.Length; i++)

            {

            label://标签

                Random Rnd = new Random();

                int a = (int)Rnd.Next(1, 101);

                array[i] = a;   //产生随机数赋值给数组

                for (int j = 0; j < i; j++)

                {

                    //判断随机数是否有重复

                    if (array[j] == array[i])

                    {

                        goto label; //跳转执行到label,继续产生随机数

                    }

                }

                Console.Write(array[i] + " /t");//输出数组中的元素

            }

        }

    }

}

5. int[24]数组a1中的第6个起的10元素复制到int[24]数组a2

            int[] al = new int[24] {1,2,3,4,5,6,7,8,9,10,11,0,0,0,0,0,0,0,0,0,0,0,0,0 };

            int[] a2 = new int[24];

Console.WriteLine("数组a1为:");

            foreach (int a in al)

            {

                Console.Write(a + " ");

            }

            Console.WriteLine();

 

            for (int i = 5; i < 10;i++ )

            {

                a2[i - 5] = al[i];

            }

            Console.WriteLine("数组a2为:");

            foreach (int a in a2)

            {

                Console.Write(a+" ");

            }

            Console.WriteLine();

 

6. C#数组简单问题(每段代码的输出结果)

 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication167
{
class Program
{
static void Main(string[] args)
{
int[] a1 = new int[] { 1, 2 };
int[] a2 = a1;
a2[1] = 5;
Console.WriteLine("{0} {1}", a1[0],a1[1]); 1
5

Console.WriteLine("{0} {1}", a2[0], a2[1]); 1
5
Console.ReadLine();
}
}
}
-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication168
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = a;
a = 5;
Console.WriteLine(a);
Console.WriteLine(b);
}
}
}

 

7. (单选)

  int[][] myArray3=new int[3][]{new int[3]{5,6,2},new int[5]{6,9,7,8,3},new int[2]{3,2}}; myArray3[2][2]的值是()。

  1. 9

  2. 2

  3. 6

  4. 越界

 

8. 已知数组int[] max={6,5,2,9,7,4,0};选择排序算法按降序对其进行排列,并返回数组   

           int[] max ={ 6, 5, 2, 9, 7, 4, 0 };

            Console.WriteLine("排序前数组max为:");

            foreach (int a in max)

            {

                Console.Write(a + " ");

            }

            Console.WriteLine();

 

            for (int i = 0; i < max.Length-1; i++)

            {

                int M = i; //数组最大值的下标M

                for (int j = i+1 ; j < max.Length; j++)

                {

 

                    if (max[j] > max[M])

                    {

                        M = j;

                    }

                }

                int temp; //进行交换

                temp = max[M];

                max[M] = max[i];

                max[i] = temp;

            }

 

            Console.WriteLine("按降序排序后数组max");

            foreach (int a in max)

            {

                Console.Write(a + " ");

            }

      Console.WriteLine();