求职经历笔试题之美乐威

来源:互联网 发布:php chmod 777 编辑:程序博客网 时间:2024/05/01 13:47

  我是在9月7日做的美乐威的笔试题,然后因为一些原因被淘汰了,但是美乐威公司的笔试题还是不错的,现将之整理归纳(声明:答案是本人自己做的,仅供参考,不保证正确):

1、请指出下列程序中的错误:

class BaseClass{public:BaseClass():m_a1(1), m_a2(2) { }~BaseClass();public:int GetValue1() { return m_a1; }int GetValue2() { return m_a2; }static int GetValue3() { return (m_b1 + m_a1); }public:int m_a1;static int m_b1;protected:int m_a2;private:int m_a3;};class DerivedClass : public BaseClass{public:DerivedClass() { }~DerivedClass() { }public:void SetValue1(int i) { m_a1 = i; }void SetValue2(int i) { m_a2 = i; }void SetValue3(int i) { m_a3 = i; }};
错误1:BaseClass类析构函数声明了,但是没有实现,没有函数主体

错误2:GetValue3()为static静态函数,在静态函数中只能调用静态数据成员,这里的m_a1不是静态成员,错误

错误3:DerivedClass类公有继承于BaseClass类,所以DerivedClass类不可访问BaseClass类中的私有数据成员

错误4:m_b1为static变量,需在类外进行初始化


2、请问运行下面两个程序会有什么结果?

程序一:

class A{public:A(int n){m_a = n;funcA();}virtual void funcA() { m_a--; }public:int m_a;};class B : public A{public:B(int n) : A(5) {}void funcA() { m_a = 6; }};int main(){int nValue = 10;A a(nValue++);B b(++nValue);A* c = new B(nValue);c->funcA();printf("A.m_a = %d, B.m_a = %d, C.m_a = %d\n", a.m_a, b.m_a, c.m_a);delete c;return 0;}
程序一中:

  第24行执行的是A a(10);在这句话执行完之后,nValue值为11,a中m_a值为9;

  第25行执行的是B b(12);在这句话执行完之后,nValue值为12,b中m_a值为4;

  第26和27行执行的是A* c = new B(12);c->funcA();c中m_a值为6;

最终输出结果:A.m_a = 9, B.m_a = 4, C.m_a = 6


程序二:

void GetMemory(char* p){p = (char*)malloc(100);}int main(){char* str = NULL;GetMemory(str);strcpy(str, "hello world");printf(str);return 0;}
程序二中:

  GetMemory函数实际上是值传递,而不是地址传递,传过去一个指针变量,然后在函数中将指针赋于其值,但是函数结束时,指针并没有发生任何变化,分配内存应该传递一个二级指针。这里内存分配失败,操作空指针,段错误。


3、不调用C++/C的字符串库函数,请编写代码实现stricmp函数:

原   型:int stricmp(const char* s1, const char* s2);

功   能:比较字符串s1和s2,但不区分字母的大小写;

返回值:当s1<s2时,返回值=-1;当s1=s2时,返回值=0;当s1>s2时,返回值=1;

int stricmp(const char* s1, const char* s2){   char ch1, ch2;   do   {      if ( ((ch1 = *(s1++)) >= 'A') &&(ch1 <= 'Z') )        ch1 += 0x20;      if ( ((ch2 = *(s2++)) >= 'A') &&(ch2 <= 'Z') )        ch2 += 0x20;   } while ( ch1 && (ch1 == ch2) );   if((ch1 - ch2) > 0 )   {   return 1;   }   else if(ch1 - ch2 < 0)   {   return -1;   }   else    {   return 0;   }}

4、某学生管理系统中,使用双向链表存储学生信息,学生信息如下边结构表示;指针STUDENT* pHead,*pTail分别指向链表头和链表尾,请编写函数,在链表中找到指定ID的学生,将其删除;并返回处理结果(注:学生ID唯一)

struct STUDENT{    int nID;    char szName[64];    STUDENT *pNext;    STUDENT *pPre;};

bool DeleteStudent(int nID){if(pHead == NULL || pTail == NULL){return false;}STUDENT *temp = pHead;if(temp->pNext->pNext == NULL && temp->pNext->nID == nID){free(temp->pNext);temp->pNext = NULL;return true;}while(temp->pNext != NULL){if(temp->nID == nID){temp->pNext->pPre = temp->pPre;temp->pPre->pNext = temp->pNext;free(temp);temp = NULL;return true;}temp = temp->pNext;}}

5、编写类String的构造函数和赋值函数:

    class string      {      public:          string(const char* str = NULL);     //普通构造函数          string(const string &other);        //复制构造函数          ~string(void);                      //析构函数          string & operater = (const string &other);  //赋值函数      private:          char *m_string;                     //私有成员,保护字符串      };

String::~String(void)  {      cout<<"Destructing"<<endl;      if(m_string != NULL)                //如果m_string不为NULL,释放内存      {          delete[] m_string;          m_string = NULL;                //释放后置为NULL      }  }    String::String(const char* str)  {                 cout<<"Constructing"<<endl;               if(str == NULL)                     //如果str为NULL,存字符串""      {          m_string = new char[1];         //分配一个字节          *m_string = '\0';               //赋值为字符串结束符      }      else      {          m_string = new char[strlen(str)+1];     //分配空间容纳str          strcpy(m_string, str);                  //复制str到私有成员      }  }    String::String(const String& other)  {      cout<<"Constructing Copy"<<endl;      m_string = new char[strlen(other.m_string)+1];      //分配空间容纳str内容      strcpy(m_string, other.m_string);                   //复制str到私有成员  }    String & String::operator = (const String& other)  {      cout<<"operator = Function"<<endl;      if(this == &other)                                  //如果对象与other是同一个对象      {          return *this;      }      delete[] m_string;      m_string = new char[strlen(other.m_string)+1];      strcpy(m_string, other.m_string);      return *this;  }  



  这就是7号做的美乐威的笔试题,总体来说题目都是基础题,有几道题还是之前做过的。在笔试时,1、2、5题对我来说都是非常熟悉的题目,第3和4题之前没怎么碰到过,在笔试的时候考虑了一会,最终想了一个大体的思路,具体的代码实现不确定是否正确。而且目前为止已经过了一个星期,我可能已经被pass了,不过重在笔试经验的积累,这才是我的最大收获。今后我也会坚持把每天遇到的笔试题或者面试经验记录下来,每一次的积累是为了最终的完美!加油!

原创粉丝点击