AllieLee

导航

2013 微软校园实习生笔试题及详解(6-10题)

 

6. For the following Java or C# code(3 Points)

 

    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

 

 

 

 

 

 

 

问题解析:本题考查的是基本的语法知识。无论是Java语言还是C#语言,在计算机的基本语法中,数组始终是以0开始,对于一个具有n个元素的一位数组,其下标是从0到n-1。因此本例中定义的二维数组myArray3[][],第一个元素对应的下标表示为myArray3[0][0],问题中的myArray3[2][2]理解为第三行第三个元素,查找原始数据发现,第三行仅包含两个元素,超出了数组可访问的位置,发生溢出。

 

7. Please choose the right statement about const usage:(3 Points)

    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

 

 

 

 

 

 

 

问题解析:本题考查C++中用const限定符和指针来修饰对象时,对象的属性问题。

  1. const限定符可以将一个对象转换成一个常量。注意因为用const限定符定义的常量在定义后就不能被修改,所以定义时必须初始化。
  2. 指向const对象的指针,C++语言强制要求指向const对象的指针也必须具有const特性。如本题中const int *a;这里的a是一个指向int类型的const对象指针,const限定了a指针所指向的对象类型,而并非a本身,即a本身并不是const类型。同时把一个const对象的地址赋给一个普通的、非const对象的指针也会导致编译时错误。
    const int r = 2;
    const int *a = 1;
    //(1)
    a++;                             //correct
    *a++;                           //error:*a might be const
    //(2)
    const int *cptr = &r;    //correct: cptr is a pointer to const
    int *ptr = &r;                //error: ptr is a plain pointer
    //(3)
    const void *vp = &r;    //correct,vp is const
    void *pv = &r;             //error:r is const
  3. const指针:除指向const对象的指针外,C++语言还提供了const指针——本身的值不能修改。
    int errNumb = 0;
    int *const curErr = &errNumb;     //curErr is a constant pointer

    我们可以将curErr读作“指向int型对象的const指针”。即curErr不能指向其他对象,任何企图给const指针赋值的行为都会导致编译时的错误。另外,const指针必须在定义时初始化。

  4. 如何识别const指针和指向const对象的指针?const限定符既可以放在类型前也可以放在类型后,但对于非指针类型的const限定符,放在类型前和类型后效果相同。对于既有const限定符又有指针的情况下,我们可以比较*和const的位置来判断他们属于那种类型。const在*之前则为指向const对象的指针,const在*之后则为const指针。
8. Given the following code:(3 Points)

 

View Code
 1 #include <iostream>  
 2 
 3 class A{  
 4 public:  
 5     long a;  
 6 };  
 7   
 8 class B : public A  
 9 {  
10 public:  
11      long b;  
12 };  
13       
14 void seta(A* data, int idx)  
15 {  
16     data[idx].a = 2;  
17 }  
18       
19 int _tmain(int argc, _TCHAR *argv[])  
20 {  
21     B data[4];  
22       
23     for(int i=0; i<4; ++i)  
24     {  
25         data[i].a = 1;  
26         data[i].b = 1;  
27         seta(data, i);  
28     }  
29       
30     for(int i=0; i<4; ++i)  
31     {  
32         std::cout<<data[i].a<<data[i].b;  
33     }  
34       
35     return 0;  
36 }  
37         

What is the correct result?

    A. 11111111

    B. 12121212

    C. 11112222

    D. 21212121

 

 

 

 

 

 

问题解析:测试运行结果为——22221111

9. 1 of 1000 bottles 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?(5 Points)

    A. 9

    B. 10

    C. 32

    D. None of the above

 

 

 

 

 

 

 

问题解析:本题主要考察二进制与十进制的转换,但更多的需要灵活的思考问题。1000瓶水里只有一瓶有毒,但具体是哪一瓶未知,将这1000瓶水按二进制的方式编号1-1000,即0000000001、0000000010、0000000011、……、1111100111,然后小白鼠10只分别按顺序排列,即小白鼠喝与不喝某瓶水用0和1表示,所以10只小白鼠共能表示1024(210)种状态,1000瓶水按编号来喂小白鼠(如编号为1000110101时,给小白鼠1、3、5、6、10喂水),一周后观察结果,若1、3、5、6、10这几只小白鼠死亡,则代表编号为1000110101的瓶子中的水有毒,同理若1、2、3这三只小白鼠死亡,则代表编号为000000111的水有毒。

 

10. Which of the following statement(s) equal(s) value 1 in C programming language?(5 Points)

    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"=="miceosoft"

    E. None of the above

 

 

 

 

 

 

 

问题解析: 

  1. C语言中main函数返回类型为int时,若程序正常结束通常返回0;
  2. 7&1,即111&001,结果为1
  3. C语言中字符串的比较通常需要借助strcmp()或strncmp()函数,它没有直接比较字符串的机制,因此选项C和D错误。

posted on 2013-04-07 20:23  AllieLee  阅读(371)  评论(0编辑  收藏  举报