微軟2013校園招聘筆試試題及詳細解答

版權所有,轉載請註明出處,謝謝!
http://blog.csdn.net/walkinginthewind/article/details/8770201

(不定項選擇題)

1. Which of the following calling conversion(s) support(s) variable-lengt parameter(e.g. printf)?
   A. cdecl
   B. stdcall
   C. pascal
   D. fastcall
解答:printf在VS2010的stdio.h中的聲明爲
_Check_return_opt_ _CRTIMP int __cdecl printf(_In_z_ _Printf_format_string_ const char * _Format, ...);
即printf爲cdecl調用。下面對各種調用簡要說明:
各種調用方式的區別主要有參數的壓棧順序,是從左到右還是從右到左;函數執行結束由誰來負責清除調用前壓入棧中的參數,調用方還是被調用方;是否使用寄存器傳遞參數。
(1)cdecl:c/c++默認的調用方式。參數從右向左傳遞,放在棧中;棧平衡由調用函數來執行;不定參數函數可以使用。C語言中的printf就是典型的cdecl調用方式。因爲printf函數不知道具體傳入的參數的個數,所以printf內部不能完成平衡棧的操作,只能由被調用方負責平衡棧的操作。printf是通過例如%d、%c、%s等進行參數解析,但它不能確定傳入參數個數,如printf("%d\n", 0, "Hello world!")也是合法的,但printf意識不到"Hello world"(實際是首地址)。
(2)stdcall:參數從右向左傳遞,放在棧中;棧平衡操作由被調用函數執行;不定參數的函數無法使用。Win32 API函數絕大部分都是採用__stdcall調用約定的。WINAPI其實也只是__stacall的一個別名而已。由於被調用函數完成棧平衡平衡操作,因此函數參數個數必須確定,不能使用不定參數。
(3)pascal:參數由左到右的順序入棧;由被調用函數負責棧平衡操作;也不能使用不定參數。
(4)fastcall: 最左邊的兩個不大於4字節的參數分別放在ecx和edx寄存器,其餘參數仍然從右到左壓入棧,但是,對於浮點值、遠指針和__int64類型總是通過棧來傳遞;被調用方平衡棧;不定參數無法使用。

2. What's the output of the following code?
        class A
        {
        public:
            virtual void f()
            {
                cout << "A::f()" << endl;

            }
            void f() const
            {
                cout << "A::f() const" << endl;
            }
        };

        class B : public A
        {
        public:
            void f()
            {
                cout << "B::f()" << endl;
            }
            void f() const
            {
                cout << "B::f() const" << endl;
            }
        };
        void g(const A * a)
        {
            a->f();
        }

        int main()
        {
            A * a = new B();
            a->f();
            g(a);
            delete a;
        }
   A. B::f() B::f() const
   B. B::f() A::f() const
   C. A::f() B::f() const
   D. A::f() A::f() const
解答:首先,兩個成員函數如果只是常量性不同,可以被重載。
先看main函數中的a->f(),由於a不是常量的指針,所以編譯器首先匹配到的是void f(),由於此函數是虛函數,因此實行動態綁定,因爲a所指的實際類型是class B,所以執行過程中實際調用的是void B::f()。
函數g中的調用,因爲參數a是const A*,所以a->f()匹配的是void f() const,由於該函數不是虛函數,因此執行靜態綁定,a所指的靜態類型是class A,所以執行的是void A::f() const。

3. What is the difference bewteen a linked list and an array?
   A. Search complexity when both are sorted
   B. Dynamically add/remove
   C. Random access efficiency
   D. Data storage type
解答:鏈表與數組的區別。ABCD都是。
數組:元素在內存中連續;可通過下標隨機訪問;插入刪除元素需要移動大量元素。
鏈表:元素在內存中不一定連續,通過指針聯繫在一起;不能隨機訪問某元素。增加刪除簡單,只操作指針即可。

4. About the Thread and Process in Windows, which description(s) is(are) correct:
   A. One application in OS must have one Process, but not a necessary to have one Tread
   B. The Process could have its own Stack but the thread only could share the Stack of its parent Process
   C. Thread must belongs to a Process
   D. Thread could change its belonging Process
解答:此題考察進程與線程的概念。
進程是系統進行資源分配的基本單位,有獨立的內存地址空間;線程是CPU調度的基本單位,沒有單獨地址空間,有獨立的棧、局部變量、寄存器、程序計數器等。
創建進程的開銷大,包括創建虛擬地址空間等需要大量系統資源;創建線程開銷小,基本上只有一個內核對象和一個堆棧。
進程切換開銷大,線程切換開銷小。進程間通信開銷大,線程間通信開銷小。
線程屬於進程,不能獨立執行。每個進程至少有一個線程,成爲主線程。

5. What is the output of the following code?

        {
            int x = 10;
            int  y = 10;
            x = x++;
            y = ++y;
            printf("%d, %d\n", x, y);
        }

   A. 10, 10
   B. 10, 11
   C. 11, 10
   D. 11, 11
解答:前置++與後置++。這個問題不同編譯器表現的結果常常有差異,特別是多餘連續多個出現在同一個語句中時。這裏以VS2010的處理方式爲例,不保證其他編譯器下能得到相同結果。
++i表示,i自增1後再參與其它運算;而i++ 則是i參與運算後,i的值再自增1。自減運算符--與之類似。
語句x = x++; VS2010將其處理成x = x; x = x+1; 對應的彙編代碼爲:
mov         eax,dword ptr [x]  
mov         dword ptr [x],eax  
mov         ecx,dword ptr [x]  
add         ecx,1  
mov         dword ptr [x],ecx
語句y = ++y; VS2010將其處理成y = y+1; y = y; 對應的彙編代碼爲:
mov         eax,dword ptr [y]  
add         eax,1  
mov         dword ptr [y],eax  
mov         ecx,dword ptr [y]  
mov         dword ptr [y],ecx
所以結果是11, 11。
下面看一下以下兩個語句:
int a = (x++)+(x++);
int b = (++y)+(++y);
按照上面的方式,可以解釋成:
int a = x + x;
x = x + 1;
x = x + 1;
y = y + 1;
y = y + 1;
int b = y + y;
最後a = 20,y = 24(VS2010結果)。

6. For the following Java or C# code

        int[][] myArray3 = 
            new int[3][]{
                    new int[3]{5, 6, 2},
                    new int[5]{6, 9, 7, 8, 3},
                    new int[2]{3, 2}};

   what will
            myArray3[2][2]
    returns?

   A. 9
   B. 2
   C. 6
   D. overflow
解答:數組下標問題。越界,所以overflow。

7. Please choose the right statement about const usage:
   A. const int a; // const integer
   B. int const a; // const integer

   C. int const *a; // a pointer which point to const integer
   D. const int *a; // a const pointer which point to integer
   E. int const *a; // a const pointer which point to integer
解答:const寫在類型前和類型後是一樣的。因此const int a和int const a一樣,int const *a和const int *a一樣。這裏主要是看一下指針的情況。
如果const出現在星號左邊,表示被指物是常量,即不能通過指針修改所指植物,但是指針可以指向其他常量;如果出現在星號右邊,表示指針自身是常量,指針的值不能被修改,但是可以通過指針修改指針所指之物的值;如果出現在星號兩邊,表示被指物和指針兩者都是常量。
強化練習: const char * const * pp,解釋該指針的類型。
首先,該指針是指針的指針,指向的是一個const char * const,即一個指向const char的const指針。因此(*pp)++是非法表達式,(**pp) = 'c'也是非法表達式,pp++是合法的。

8. Given the following code:
        #include<iostream>
        class A
        {
        public:
            long a;
        };
        class B : public A
        {
        public:
            long b;
        };
        void seta(A* data, int idx)
        {
            data[idx].a = 2;
        }
        int _tmain(int argc, _TCHAR * argc[])
        {
            B data[4];
            for(int i = 0; i < 4; i++)
            {
                data[i].a = 1;
                data[i].b = 1;
                seta(data, i);
            }
            for(int i = 0; i < 4; ++i)
            {
                std::cout << data[i].a << data[i].b;

            }
            return 0;
        }
   What is the correct result?
   A. 11111111
   B. 12121212
   C. 11112222
   D. 21212121
解答:此題答案應該是22221111,選項中沒有。
首先爲了區分,我們重命名如下:
B data[4];
A *pA = (A*)data;
這樣上面的函數調用seta就轉換爲
pA[i] = 2;
通過data[i]和pA[i]操作數據並沒有什麼關係,不要受B繼承自A影響。data和pA只是指向相同的起始地址,但它們對內存的解釋方式不同,data[i]  = data + i*sizeof(B),pA[i] = pA+i*sizeof(A)。下面看一下內存說明圖:


這樣執行結果就明晰了。

9. 1 of 1000bottles of water is poisoned which will kill a rat in 1 week if the rat drunk any amout of the water. Given the bottles of water have no visual difference, how many rats are needed at least to find the poisoned one in 1 week?
   A. 9
   B. 10
   C. 32
   D. 999
   E. None of the above
解答:此題巧妙的使用二進制的思想。把所有瓶子編號00 0000 0001 ~ 11 1110 1000(1~1000),老鼠編號0~9,從第一個瓶子開始,若該編號的二進制表示中第k位爲1則就給第k個老鼠喝。一個星期後,若第0只老鼠沒死,則代表有毒瓶子的二進制表示的第0位不是1,若死了就代表是1,逐一下去,最後就確定了有毒的瓶子的編號。
此問題還有一個變種,如果你有兩個星期的時間(換句話說你可以做兩輪實驗),爲了從 1000 個瓶子中找出毒藥,你最少需要幾隻老鼠?注意,在第一輪實驗中死掉的老鼠,就無法繼續參與第二次實驗了。

10. Which of following statement(s) equal(s) value 1 in C programming language?
   A. the return value of main function if program ends normally
   B. return (7&1);
   C. char *str = "microsoft"; return str == "microsoft";
   D. return "microsoft" == "microsoft";
   E. None of the above
解答:c語言中main函數若執行成功返回0,故A不對。7&1 = (00000111b & 00000001b ) = 1,B正確。C和D相同,先說D,常量字符串“microsoft”位於常量區,編譯器一般都只保留一份,不會有重複,故D正確。C也是,char *str = "microsoft",也是將常量區中字符串的起始地址賦值給str,但我們不能通過str修改那個字符串,否則程序會崩潰,因爲它在常量區。
VS2010彙編代碼參考:
char *str = "microsoft"; 
 mov         dword ptr [str],offset string "microsoft" (12F7830h)  
int flag1 = (str == "microsoft");
 xor         eax,eax  
 cmp         dword ptr [str],offset string "microsoft" (12F7830h)  
 sete        al  
 mov         dword ptr [flag1],eax  
int flag2 = ("microsoft" == "microsoft");
 mov         eax,offset string "microsoft" (12F7830h)  
 xor         ecx,ecx  
 cmp         eax,offset string "microsoft" (12F7830h)  
 sete        cl  
 mov         dword ptr [flag2],ecx  

11. If you computed 32 bit signed integers F and G from 32 bit signed integer X using F = X / 2 and G = (X >> 1), and you found F != G, this implies that
   A. There is a complier error
   B. X is odd
   C. X is negative
   D. F - G = 1
   E. G - F = 1
解答:X爲正整數或負偶數時,沒有問題,F等於G。X爲負奇數時F-1等於G,F = abs(X)/2,G = (X-1)/2。所以BCD正確。

12. How many rectangles you can find from 3*4 grid?
   A. 18
   B. 20
   C. 40
   D. 60
   E. None of above is correct
解答:3*4個格子,4*6條線。每個長方形都是由兩條橫線和兩條豎線圍成,求長方形的個數也就是求選出2條橫線並和2條豎線共有多少種選法,在橫線中選2條的選法有C(4,2),在豎線中選2條的選法有C(5,2),相乘得60。

13. One line can split a surface to 2 part, 2 line can split a surface to 4 part. Give 100 lines, no two parallel lines, no three lines join at same point, how many parts can 100 line split?
A. 5051
B. 5053
C. 5510
D. 5511
解答:若當前有k條線,已經分成了f(k)個區域,則增加一條線會增加多少區域呢?分割一個區域要麼是一條線段將一個區域分割成兩個區域,要麼是一條線段將一個區域分成兩個區域,增加的區域就是增加的線段(或射線)的個數。由於沒有三條線交於一點,因此新增加的線與之前k條線都有一個交點,因此就增加了k+1條線段(或射線),也就增加了k+1個區域。由此可得f(n) = f(n-1) + n。f(100) = f(99) + 100 = f(98) + 99 + 100 = f(1) + 2 + 3 + ... + 100 = 5051。

14. Which of the following sorting algorithm(s) is(are) stable sorting?
   A. bubble sort
   B. quicksort
   C. heap sort
   D. merge sort
   E. selection sort
解答:快速排序不穩定。如 5 6 3 16 2,以5作爲partition的劃分元素,一趟partition之後,綠色的6和2交換,然後5和1交換,變爲1 2 3 5 66,所以不穩定。
堆排序不穩定。如最大堆3 2 1,堆排序的結果是1 2 3。
      3
    /    \
   2    1
  /
1

交換堆頂和尾部元素
      1
    /    \
   2    1
  /
3
調整堆爲最大堆
      2
    /    \
   1    1
  /
3
交換堆頂和尾部元素
      1
    /    \
   1    2
  /
3
已是最大堆,交換堆頂和尾部元素
      1
    /    \
   1    2
  /
3
排序完成,黃色1到綠色1前面了。所以堆排序不穩定。
選擇排序不穩定。如6 5 2 6 3 1,選擇排序第一趟執行完後,綠色6和1交換,變爲1 5 2 6 3 6,所以不穩定。

15. Model-View-Controller(MVC) is an architecture pattern that frequently used in web applications. Which of the following statement(s) is(are) correct:
   
A. Models often represent data and the business logics needed to manipulate the data in the application
   B. A view is a (visual) representation of its model. It renders the model into a form suitable for interaction, typically a user interface element
   C. A controler is the link between a user and the system. It accepts input from the user and instructs the model and a view to perform actions based on that input
   D. The common practice of MVC in web applications is, the model receives GET or POST input from user and decides what to do with it, handing over to controller and which hand control to views(HTML-generating components)
   E. None of the above
解答:MVC模型。
模型(Model):模型持有所有的數據、狀態和程序邏輯。模型沒有注意到視圖和控制器,雖然它提供了操縱和檢索狀態的接口,併發送狀態改變通知給觀察者。
視圖(View):用來呈現模型。視圖通常直接從模型中取得它需要顯示的數據。視圖對象通常是空間,用來顯示和編輯。
控制器(Controller):取得用戶的輸入並解讀其對模型的意思。

16. We can revocer the binary tree if given the output of

   A. Preorder traversal and inorder traversal
   B. Preorder traversal and postorder traversal
   C. Inorder traversal and postorder traversal
   D. Postorder traversal 
解答:前序遍歷和後續遍歷不能恢復出原來的二叉樹結構,因爲存在不同的二叉樹樹,前序遍歷和後續遍歷都相同。如:
第一棵二叉樹
        1
      /
    2
前序遍歷是1 2, 後序遍歷是2 1。
另一棵二叉樹
       1
          \
           2
前序遍歷是1 2, 後序遍歷是2 1。
所以B不正確,D就更不正確了。

17. Given a string with n characters, suppose all the characters are different from each other, how many different substrings do we have?
   A. n+1
   B. n^2
   C. n(n+1)/2
   D. 2^n-1
   E. n!
解答:長度爲1的子串有n個,長度爲2的子串有n-1個,長度爲3的子串有n-2個,... ,長度爲n的有1個,總共n(n+1)/2。

18. Given the following database table, how many rows will the following SQL statement update?
            update Books set NumberOfCopies = NumberOfCopies+1 where AuthorID
            in
            Select AuthorID from Books
            group by AuthorID
            having sum(NumberOfCopies) <= 8

   A. 1
   B. 2
   C. 3
   D. 4
   E. 5
解答:先看最內層,是選出以AuthorID分組的每組內NumberOfCopies之和小於等於8的AuthorID,結果只有2。然後外層,對所有AuthorID爲2的元組的NumberOfCopies加1。共更新了兩行。

19. What is the shortest path between node S and node T, given the graph below? Note: the numbers represent the lengths of the connected nodes.

   A. 17
   B. 18
   C. 19
   D. 20
   E. 21
解答:最短路徑問題。不多說了,看就能看出來,看不出來就按Dijkstra一步步來。

20. Given a set of N balls and one of which is defective(weighs less than others), you are allowed to weigh with a balance 3 times to find the defective. Which of the following are possible N?
   A. 12
   B. 16
   C. 20
   D. 24
   E. 28
解答:12個,分成三組4,4,4,先拿出其中兩組稱量,若左邊輕,則壞球在左邊,若右邊輕則壞球在右邊,若平衡,則壞球在剩下的四個當中。這樣一次稱量就將球減少到4個。將4個分成兩組2,2,若左邊輕則壞球在左邊,反之在右邊。第二次稱量將球減少到2個。剩下兩個再稱量一次顯然就確定了。也有其他可行的分組方法。A正確。
16個,分成三組6,6,4,先稱量兩組6個球的,則壞球要麼在4個當中,要麼在兩組6個當中的一組,4個的用兩次可以確定壞球。6個的再分成2,2,2三組,同樣可以用一次減少到兩個球,最後確定壞球。也有其他可行的分組方法。B正確。
20個,分成6,6,8三組,若在6個當中,兩次可以找出,若在8個當中,將其分成3,3,2三組,兩次也可以確定。也有其他可行的分組方法
24個,分成8,8,8。第一次稱量減少到8個,剩下8個兩次可以確定,也有其他可行的分組方法
28個無法三次確定。
總之,3個球以內一次,可一次稱量確定。4~9個球可兩次稱量確定。10~27個球可三次稱量確定。對於三次稱量,只要第一次分成的三組,每組不多於9個即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章