1. 程式人生 > >北航軟院2012級C#期末考試部分考題解答

北航軟院2012級C#期末考試部分考題解答

linq 沒有 present prop between you 參數 變量 只讀

博主註:本渣渣水平有限,文中若有不對的地方敬請指出,謝謝。

一、選擇題(2*15=30)

1.In C# what is base class of all reference types?

System.Type B. System.Base C.System.Object D.System.Root

解答:所有引用類型的基類為System.Object

引用老師PPT:

技術分享

技術分享枚舉是值類型,放在這裏只是作為擴展)

2.Wrong statement?

A.double a = 123E; B. long b = 0xFeel; C. string c = “C#”; D.int d = 2014;

解答:英文字體換成Times New Roman就分得清1和l還有I了(可能是題目問題不糾結,這裏的A錯誤太明顯了吧)

3.Choose the output of the code below:

double a = 10, b = 10;

++a;

Console.WriteLine(a>b?(2>>1):(1<<2));

A.Conpile error B. 1 C. 2 D. 4

解答:a=11>b=10,2>>1=1

7.Which of the following keyword is used when a virtual is refined by a derived class?

A: base B:virtual C:overload D:override

解答:子類中的方法與父類中的方法完全一致,叫做重寫(override);同一個類中方法名相同參數個數不同or類型不同or次序不同,叫做重載(overload)。

8.Which of the following are correct ways to pass a parameter to a attribute?

1) By value 2)by reference 3)by position 4)by name

A 1 ,2 B 3,4 C 1,2,3,4 D1,2,3

解答:老師的PPT上寫的,有點看不懂。

技術分享

9 if a method is marked as protected who can access it?

A classes in the declaring assembly

B only methods that are in the same class

C classed derived from the declaring class

D anywhere the application has a reference to an object of that base class

解答:仔細看PPT:)。

技術分享

10.If you want to select all the numbers which are greeter than 1000 in an int array, which of the following can you use?

A.LINQ To Objects

B.LINQ To XML

C.LINQ To SQL

D.None of above

解答:LINQ查詢:LINQ to Object, LINQ to XML, LINQ to ADO.NET 包括兩種獨立的技術: LINQ to DataSet 和 LINQ to SQL

對於LINQ to Object可以查詢數組等裏面的數據;

對於LINQ to SQL可以查詢SQL能夠查詢的表中的數據;

對於LINQ to XML可以查詢XML文檔的標簽等;

對於LINQ to DataSet可以查詢DataSet中的數據;

技術分享

11.The statement f = delegate(int x){return x + 1;}; is equivalent to which of the following statements?

A.f(x) =>x + 1;

B.f(x) = x + 1;

C.f => x + 1;

D.f = x => x + 1;

解答:考的是委托的形式(老師PPT的搬運工)

技術分享

這裏是一段測試代碼:

技術分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lambda
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            Func<int, int> f1 = x => 2 * x + 1;
            Func<int, int, bool> f2 = (x, y) => x > y;
            Func<string, int, string> f3 = (x, y) => x.Substring(y);
            Func<int, int> f4 = (x) => sum += x;
            Action a1 = () => { System.Console.WriteLine("HelloWorld"); };
            Action<int> a2 = (x) => { System.Console.WriteLine(x); };
            Action<bool> a3 = (x) => { System.Console.WriteLine(x); };
            Action<string> a4 = (x) => { System.Console.WriteLine(x); };

            for (int i = 1; i <= 10; i++)
            {
                f4(i);
            }
            a2(sum);
            a1();
            a2(f1(1));
            a3(f2(3, 5));
            a4(f3("AlvinZH", 5));
            Console.ReadKey();
        }
    }
}
View Code

運行結果:

技術分享

12.In C#, we can use the following statement to convert a string s to an integer num

1)int num = Convert.ToInt32(s);

2)int nym = Int32.Parse(s);

3)int num = s.ToInt();

4)int num = int.Parse(s);

A.1,2,3,4 B.1,2 C.1,3,4 D.2,3,4

解答:本題沒有答案好像。

技術分享

二、判斷題(2*10=20)

1.In a switch statement, every statement sequence in a case clause must be terminated with break(or return, goto, throw).

解答:(T)(搬運工又來了)

技術分享

2.Property 可聲明在 class, struct, interface裏。

解答:(T)

簡單測試一下,反正就是可以有:

技術分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fraction
{
    public struct StructTest
    {
        private int x;
        public int X
        {
            get { return x; }
            set { x = value; }
        }
        public StructTest(int _x)
        {
            x = _x;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            StructTest str = new StructTest();
            str.X = 1;
            Console.WriteLine(str.X);
            string s = "123";
            int num1 = Convert.ToInt32(s);
            int num2 = Int32.Parse(s);
            //int num3 = s.ToInt();
            int num4 = int.Parse(s);
            Console.WriteLine("num1="+num1);
            Console.WriteLine("num2="+num2);
            //Console.WriteLine(num3);
            Console.WriteLine("num4="+num4);
            Console.ReadKey();
        }
    }
}
View Code

運行結果:

技術分享

PS:使用屬性的好處:允許只讀或者只寫字段;可以在訪問時驗證字段;接口和實現的數據可以不同;替換接口中的數據。

技術分享

5.if a constructor was declared, no default constructor is generated

解答:這個問題要看對象是誰了,對於類:只有在你沒有構造器的時候編譯器才會給你一個默認的構造器,你可以自定義一個無參構造器,當你自己寫了一個構造器(無參或有參),編譯器就不管你了;對於結構體:它總是會有一個默認的構造器,你也不能聲明一個無參的構造器。

技術分享

技術分享

技術分享

6.nested types cannot be interfaces and delegates

解答:(F)內嵌類型可以是類、結構、結構體、枚舉、委托

技術分享

7.Only the class that declares the event can fire it.

解答:(T)PPT上好詳細啊,大家認真看嘍!

技術分享

8.In C#, we can only throw an exception by an invalid operation.

解答:(F)可以是非法操作,也可以是人為拋出異常。

技術分享

三、填空題(2*7 + 3*6 = 32)

1.A constructor may call another constructor with the keyword__this___.(這個不用講了吧)

2.In C#, if you want to change a thread’s state from suspended into running, you should call method__Resume()____.(詳見PPT207頁)

3.A(n)____static__ variable represents classwide information that is shared by all the objects of the class.(靜態變量)

5.In C#,__indexer__is used to retrieve specific element in an enumerable object directly.(索引器檢索)

6.In C#,__assembly is a set of types that are compiled together and it is the smallest unit for development and dynamic loading.(PPT181頁)

技術分享

7.The following class is often called___Generic____ Class.(通用類,PPT254頁)

Class Buffer<Element>{

private Element[]data;

public Buffer(int size){…}

public void Put(Element x){…}

public Element Get(){…}

}

技術分享

8.Suppose you have a double array called numbers ,please sum all the elements of the array using foreach statement

double sum = 0;
foreach( double x in numbers)
{
   sum = sum + x;
}

11.What is the output of the following program?

技術分享
using System;

class Plant
{
    public virtual void ShowName()
    {
        Console.WriteLine("Food");
    }
}

class Fruit : Plant
{
    public override void ShowName()
    {
        Console.WriteLine("Fruit");
    }
}

class Apple : Fruit
{
    public new virtual void ShowName()
    {
        Console.WriteLine("Apple");
    }
}

class LittleApple : Apple
{
    public override void ShowName()
    {
        Console.WriteLine("LittleApple");
    }
}

class Test
{
    public static void Main()
    {
        Plant plant = new LittleApple();
        plant.ShowName();
        Apple apple = new LittleApple();
        apple.ShowName();

        Console.ReadKey();
    }
}
View Code

解答:代碼裏面的Plant.ShowName();應該是plant.ShowName();,Apple.ShowName();應該是apple.ShowName();

代碼考的是virtual與new的用法。我的理解是new會把你的一路繼承生生打斷,重新開始,就醬:)

運行結果:

技術分享

12.What is the output of the following program?

技術分享
using System;
class Fraction
{
    int x, y;
    public Fraction(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public override string ToString()
    {
        return x + "/" +y;
    }
    public override int GetHashCode()
    {
        return x ^ y;
    }
    public override bool Equals(Object o)
    {
        Fraction f = (Fraction)o;
        return f.x == x && f.y == y;
    }
    public Fraction ShallowCopy()
    {
        return (Fraction)MemberwiseClone();
    }
    class Client
    {
        static void Main()
        {
            Fraction a = new Fraction(1, 2);
            Fraction b = new Fraction(1, 2);
            Fraction c = new Fraction(3, 4);
            Console.WriteLine(a.Equals(b));
            Console.WriteLine(a == b);
            a = c.ShallowCopy();
            Console.WriteLine(a);

            Console.ReadKey();
        }
    }
}
View Code

解答:這道題考的是equals函數和==的區別,這裏給大家貼一個講得很好的帖子:http://www.cnblogs.com/chen0720/p/3209398.html

運行結果:

技術分享

13.Please fill in the blanks.

namespace test
{
    public delegate void OnDBOperate();
    public class UserControlBase : System.Windows.Forms.UserControl
    {
        public event OnDBOperate OnNew;
        private void toolbar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
        {
            if (e.Button.Equals(BtnNew))
            {
                //Please write code to call OnNew events
                if (OnNew != null)
                    OnNew();
            }
        }
    }
}

解答:這道題什麽個鬼意思呢?不太懂,大概是考一下event的用法吧,不仔細看會發現你讀不懂這段代碼:)

event介紹在PPT164~166頁,大家仔細看哦~

四、簡單題

1.What is the difference between Overloading and Overriding?

解答:(截圖妙不可言qwq)

技術分享

3. Give brief description about the differences and connections between classes and interfaces。

Class

Interface

解答:繼續老師PPT放送!!!咦,老師PPT裏面沒有這個對比。。。(Interface介紹在PPT152~157頁)

技術分享

大家加油呀,祝明天考試順利~

作者: AlvinZH

出處: http://www.cnblogs.com/AlvinZH/

本人Github:https://github.com/Pacsiy/JobDu

本文版權歸作者AlvinZH和博客園所有,歡迎轉載和商用,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。

北航軟院2012級C#期末考試部分考題解答