求職經歷筆試題之美樂威

  我是在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了,不過重在筆試經驗的積累,這纔是我的最大收穫。今後我也會堅持把每天遇到的筆試題或者面試經驗記錄下來,每一次的積累是爲了最終的完美!加油!

發佈了233 篇原創文章 · 獲贊 195 · 訪問量 55萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章