(C#习题) 杂题1

1.  Deference between "private","protected","public","internal" access modifiers.

2.  一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。

Fibonacci number 的递归模型是:
Xn = Xn-1 + Xn-2   , n >=  2
X1=1, X0 =0 ,  0<= n < 2;

 

3. What is delgate in C# , is the "event" a kind of delegate ?

4. Override and overload.

5. Bubble sort .

Bubble sort  is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted

namespace ConsoleApplication2
{
    class Program
    {      
        static int[] BubbleSort(int[] numbers)
        {
            int tmp = 0;
            for (int i = 0; i < numbers.Length; i++)
            {
                for (int j = i + 1; j < numbers.Length; j++)
                {
                    if (numbers[i] > numbers[j])
                    {
                        tmp = numbers[i];
                        numbers[i] = numbers[j];
                        numbers[j] = tmp;
                    }
                }
            }
            return numbers;         
        }

        static void Main(string[] args)
        {
            int[] numberArray = new int[] { 6, 8, 7, 9, 5, 4, 1, 2, 3 };
            int[] bubleSortedNumberArray = BubbleSort(numberArray); 
            Console.Write("Bubble sort result: ");
            for (int i = 0; i < bubleSortedNumberArray.Length; i++)
            {
                Console.Write("{0}, ", bubleSortedNumberArray[i]);
            }
            Console.ReadLine();
        }
    }
}

 6. 求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m

namespace ConsoleApplication2
{
    class Program
    {
        // Sum = 1-2+3-4+……+m 
        // Non-recurse
        static int Sum1(int length)
        {   
            // Validation length (Skip)
            int sum = 0; 
            for (int i = 0; i < length + 1 ; i++)
            {
                sum = (i % 2) == 0 ? sum - i : sum + i;
            }
            return sum; 
        }

        // Recurse.
        static int Sum2(int length)
        {
            // Validation length (Skip)
            int sum = 0;
            if (length == 1)
            {
                sum = 1;
            }
            else
            {
                if (length % 2 == 0)
                {
                    sum = Sum2(length - 1) - length;
                }
                else
                {
                    sum = Sum2(length - 1) + length;
                }
            }
            return sum;
        }      

        static void Main(string[] args)
        {
            int m = 21;
            Console.WriteLine("Non-recurse result: {0}", Sum1(m));
            Console.WriteLine("Recurse result: {0}", Sum2(m));
            Console.ReadLine();
        }
    }
}

7. 什么是装箱和拆箱?

8.请详述在dotnet中类(class)与结构(struct)的异同?
答:Class可以被实例化,属于引用类型,是分配在内存的堆上的,Struct属于值类型,是分配在内存的栈上的.

 9.分析以下代码,完成填空 
string strTmp = "abcdefg某某某"; 
int i= System.Text.Encoding.Default.GetBytes(strTmp).Length; 
int j= strTmp.Length; 
以上代码执行完后,i= j= 
答:i=13,j=10

10.根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。
public void test(int i) 
{ 
lock(this) 
{ 
if (i>10) 
{ 
i--; 
test(i); 
} 
} 
}
答:不会发生死锁,(但有一点int是按值传递的,所以每次改变的都只是一个副本,因此不会出现死锁。但如果把int换做一个object,那么死锁会发生)

11. String s = new String("xyz");创建了几个String Object?

答:两个对象,一个是“xyx”,一个是指向“xyx”的引用对象s。

12. .abstract class和interface有什么区别?
答:
声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。不能创建abstract 类的实例。然而可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实例。不能有抽象构造函数或抽象静态方法。Abstract 类的子类为它们父类中的所有抽象方法提供实现,否则它们也是抽象类为。取而代之,在子类中实现该方法。知道其行为的其它类可以在类中实现这些方法。
接口(interface)是抽象类的变体。在接口中,所有方法都是抽象的。多继承性可通过实现这样的接口而获得。接口中的所有方法都是抽象的,没有一个有程序体。接口只可以定义static final成员变量。接口的实现与子类相似,除了该实现类不能从接口定义中继承行为。当类实现特殊接口时,它定义(即将程序体给予)所有这种接口的方法。然后,它可以在实现了该接口的类的任何对象上调用接口的方法。由于有抽象类,它允许使用接口名作为引用变量的类型。通常的动态联编将生效。引用可以转换到接口类型或从接口类型转换,instanceof 运算符可以用来决定某对象的类是否实现了接口。

 13. 构造器Constructor是否可被override?
答:构造器Constructor不能被继承,因此不能重写Overriding,但可以被重载Overloading。

14. sleep() 和 wait() 有什么区别?
答:sleep()方法是使线程停止一段时间的方法。在sleep 时间间隔期满后,线程不一定立即恢复执行。这是因为在那个时刻,其它线程可能正在运行而且没有被调度为放弃执行,除非(a)“醒来”的线程具有更高的优先级
(b)正在运行的线程因为其它原因而阻塞。
wait()是线程交互时,如果线程对一个同步对象x 发出一个wait()调用,该线程会暂停执行,被调对象进入等待状态,直到被唤醒或等待时间到

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

namespace ConsoleApplication2
{
    class Program
    {
        static List<int> GenerateRandomList(int length)
        {   
            // Validation length (Skip)
            // Loop and add into array. 
            int tmp = 0; 
            List<int> randomsList = new List<int>(length);
            Random random = new Random();
            while (randomsList.Count < length)
            {
                tmp = random.Next(1, length+1); // range : 1 ~ (length+1)-1
                if (!randomsList.Contains(tmp))
                {
                    randomsList.Add(tmp);
                }
            }
            return randomsList; 
        }

  
        static void Main(string[] args)
        {
            int lengh = 100;
            List<int> randoms = GenerateRandomList(lengh);
            foreach (int random in randoms)
            {
                Console.Write("{0},", random);
            }
            Console.ReadLine();
        }
    }
}

16.C# 数组[]、List、Array、ArrayList 区别及应用
[] 是针对特定类型、固定长度的。
// System.Int32 是结构
   int[] arr = new int[] { 1, 2, 3 };

List 是针对特定类型、任意长度的。
// List 的命名空间是 System.Collections.Generic
        List<int> list = new List<int>();

Array 是针对任意类型、固定长度的。
// Array 的命名空间是 System
       Array array = Array.CreateInstance(System.Type.GetType("System.Int32"), 3); // Array 是抽象类,不能使用 new Array 创建。

ArrayList 是针对任意类型、任意长度的。
/ ArrayList 的命名空间是 System.Collections
        ArrayList arrayList = new ArrayList(3);

Array 和 ArrayList 是通过存储 object 实现任意类型的,所以使用时要转换。

 17.需要实现对一个字符串的处理,首先将该字符串首尾的空格去掉,如果字符串中间还有连续空格的话,仅保留一个空格,即允许字符串中间有多个空格,但连续的空格数不可超过一个.

string inputStr=" xx xx ";
inputStr=Regex.Replace(inputStr.Trim()," *"," ");

18.abstract class和interface有什么区别?
答:
声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。
不能创建abstract 类的实例。然而可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实例。不能有抽象构造函数或抽象静态方法。
Abstract 类的子类为它们父类中的所有抽象方法提供实现,否则它们也是抽象类为。取而代之,在子类中实现该方法。知道其行为的其它类可以在类中实现这些方法。

接口(interface)是抽象类的变体。在接口中,所有方法都是抽象的。多继承性可通过实现这样的接口而获得。
接口中的所有方法都是抽象的,没有一个有程序体。接口只可以定义static final成员变量。
接口的实现与子类相似,除了该实现类不能从接口定义中继承行为。当类实现特殊接口时,它定义(即将程序体给予)所有这种接口的方法。
然后,它可以在实现了该接口的类的任何对象上调用接口的方法。由于有抽象类,它允许使用接口名作为引用变量的类型。通常的动态联编将生效。
引用可以转换到接口类型或从接口类型转换,instanceof 运算符可以用来决定某对象的类是否实现了接口。

19. --------------------- ENGLISH---------------------ENGLISH--------------------------------ENGLISH------------------------------

    1. What is the implicit name of the parameter passed into a property’s ’set’ method?
      ‘value’. And it’s data type depends on whatever variable we’re changing.
    2. How do you inherit from a class in C#? Place a colon and then the name of the base class.
    3. Does C# support multiple inheritances? No, use interfaces instead.
    4. When you inherit a protected class-level variable, who is it available to? Only classes that ultimately inherit from the class with a protected member can see that member.
    5. Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.
    6. Describe the accessibility modifier protected internal. It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).
    7. C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.
    8. What’s the top .NET class that everything is derived from? System.Object.
    9. How’s method overriding different from overloading? When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.
    10. What does the keyword virtual mean in the method definition? The method can be over-ridden.
    11. Can you declare the override method static while the original method is non-static? No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.
    12. Can you override private virtual methods? No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.
    13. Can you prevent your class from being inherited and becoming a base class for some other classes?Yes, that’s what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot inherit from Sealed class WhateverBaseClassName. It’s the same concept as final class in Java.
    14. Can you allow class to be inherited, but prevent the method from being over-ridden? Yes, just leave the class public and make the method sealed.
    15. What’s an abstract class? A class that cannot be instantiated. A concept in C++ known as pure virtual method. A class that must be inherited and have the methods over-ridden. Although an abstract class does not require implementations (its methods can be abstract) it can also offer implementations of methods (either virtual or not) which can be called in implementing classes.
    16. When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)? When at least one of the methods in the class is abstract. When the class itself is inherited from an abstract class, but not all base abstract methods have been over-ridden.
    17. What’s an interface class? It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.
    18. Why can’t you specify the accessibility modifier for methods inside the interface? They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it’s public by default.
    19. Can you inherit multiple interfaces? Yes, why not.
    20. And if they have conflicting method names? It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay.
    21. What’s the difference between an interface and abstract class? In the interface all methods must be abstract. In the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.
    22. How can you overload a method? Different parameter data types, different number of parameters, different order of parameters.
    23. What is the difference between const and readonly?
      The const keyword is used for compile time constants while the readonly keyword is used for runtime constants.
    24. What’s the difference between System.String and System.StringBuilder classes? System.String is immutable while System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.
原文地址:https://www.cnblogs.com/fdyang/p/2951760.html