筆試面試題整理,慢慢蒐集....持續更新

現在的公司招聘,都要筆試面試.如果你不是那種編程功底非常深厚的人,又不好好準備一番,在筆試面試中往往會處於被動局面.雖然有些筆試題是故意爲難我們,有點鑽牛角尖.但是很多筆試題面試題確實能夠很好地看出我們的基礎.

       在這裏,我就略去那些鑽牛角尖的題.從csdn論壇我近半年的收集中選出10道有代表性的題目,難度基本上是逐漸加大.對數組,指針,數據結構,算法,字 符串,文件操作等問題都有覆蓋.主要以c語言的實現爲主,也有c++的題.大家可以先做做這10道題,測試一下自己的水平.

1. 下面這段代碼的輸出是多少(在32位機上).

    char *p;

    char *q[20];

    char *m[20][20];

    int (*n)[10];

    struct MyStruct

{

char dda;

double dda1;

int type ;

};
MyStruct k;

 printf("%d %d %d %d",sizeof(p),sizeof(q),sizeof(m),sizeof(n),sizeof(k));

2.

(1)

char a[2][2][3]={{{1,6,3},{5,4,15}},{{3,5,33},{23,12,7}} };
for(int i=0;i<12;i++)
printf("%d ",_______);
在空格處填上合適的語句,順序打印出a中的數字

 (2)

char **p, a[16][8]; 

問:p=a是否會導致程序在以後出現問題?爲什麼?

3.用遞歸方式,非遞歸方式寫函數將一個字符串反轉.

   函數原型如下:char *reverse(char *str);

4.strcpy函數和memcpy函數有什麼區別?它們各自使用時應該注意什麼問題?

5.寫一個函數將一個鏈表逆序.

一個單鏈表,不知道長度,寫一個函數快速找到中間節點的位置.

寫一個函數找出一個單向鏈表的倒數第n個節點的指針.(把能想到的最好算法寫出).

6.用遞歸算法判斷數組a[N]是否爲一個遞增數組。

7.

有一個文件(名爲a.txt)如下,每行有4項,第一項是他們的名次,寫一個c程序,將五個人的名字打印出來.並按名次排序後將5行數據仍然保存到a.txt中.使文件按名次排列每行.


2,07010188,0711,李鎮豪,
1,07010154,0421,陳亦良,
3,07010194,0312,凌瑞鬆,
4,07010209,0351,羅安祥,
5,07010237,0961,黃世傳,

8.寫一個函數,判斷一個unsigned char 字符有幾位是1.

  寫一個函數判斷計算機的字節存儲順序是升序(little-endian)還是降序(big-endian).

 9.微軟的筆試題.

Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).

Please do not use MFC, STL and other libraries in your implementation.

10.有個數組a[100]存放了100個數,這100個數取自1-99,且只有兩個相同的數,剩下的98個數不同,寫一個搜索算法找出相同的那個數的值.(注意空間效率時間效率儘可能要低).

這十道題還是能夠看出自己的水平如何的.如果你能不假思索地做出這10道題,估計去國外大公司是沒有問題了,呵呵.

答案我在整理中,以後陸續發佈.................

下面有些題也不錯,可以參考.

1.下面的代碼輸出是什麼,爲什麼?
       void foo(void)
       {
            unsigned int a = 6;
            int b = -20;
            (a+b>6)?puts(">6"):puts("<=6");//puts爲打印函數
       }
輸出 >6.

就是考察隱式轉換.int型變量轉化成unsigned int, b成了正數.

2. b)運行下面的函數會有什麼結果?爲什麼?
       void foo(void)
          {
               char string[10],str1[10];
               int i;
               for(i=0;i<10;i++)
               {
                    str1[i] = 'a';
               }
               strcpy(string, str1);
           printf("%s",string);
          }

首先搞清strcpy函數的實現方法,

char * strcpy(char * strDest,const char * strSrc)
{
  if ((strDest == NULL) || (strSrc == NULL)) 
    throw "Invalid argument(s)";
  char * strDestCopy = strDest; 
  while ((*strDest++ = *strSrc++) != '/0'); 
  return strDestCopy;
}

由於str1末尾沒有'/0’結束標誌,所以strcpy不知道拷貝到何時結束.
printf函數,對於輸出char* 類型,順序打印字符串中的字符直到遇到空字符('\0')或已打印了由精度指定的字符數爲止.

下面是微軟的兩道筆試題....

3. Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).

Please do not use MFC, STL and other libraries in your implementation.

我的實現方案如下,這道題真地對c++的主要特性都進行了較好地考察.

String.h:

#ifndef STRING_H
#define STRING_H

#include <iostream>
using namespace std;

class String{
   public:
    String();
       String(int n,char c);
    String(const char* source);
    String(const String& s);
    //String& operator=(char* s);
    String& operator=(const String& s);
    ~String();

    char& operator[](int i){return a[i];}
    const char& operator[](int i) const {return a[i];}//對常量的索引.
    String& operator+=(const String& s);
    int length();

   friend istream& operator>>(istream& is, String& s);//搞清爲什麼將>>設置爲友元函數的原因.
   //friend bool operator< (const String& left,const String& right);
   friend bool operator> (const String& left, const String& right);//下面三個運算符都沒必要設成友元函數,這裏是爲了簡單.
   friend bool operator== (const String& left, const String& right);
   friend bool operator!= (const String& left, const String& right);
   private:
    char* a;
    int size;
};

#endif


String.cpp:


#include "String.h"
#include <cstring>
#include <cstdlib>

String::String(){
    a = new char[1];
    a[0] = '/0';
    size = 0;
}

String::String(int n,char c){
 a = new char[n + 1];
 memset(a,c,n);
 a[n] = '/0';
 size = n;
}

String::String(const char* source){
 if(source == NULL){
  a = new char[1];
  a[0] = '/0';
  size = 0;
 }
 else
 {   size = strlen(source);
  a = new char[size + 1];
  strcpy(a,source);
 }
}

String::String(const String& s){
 size = strlen(s.a);//可以訪問私有變量.
 a = new char[size + 1];
 //if(a == NULL)
 strcpy(a,s.a);
}

 

String& String::operator=(const String& s){
 if(this == &s)
  return *this;
 else
 {
  delete[] a;
        size = strlen(s.a);
  a = new char[size + 1];
  strcpy(a,s.a);
  return *this;
 }
}
String::~String(){
 delete[] a;//    
}

String& String::operator+=(const String& s){
  int j = strlen(a);
  int size = j + strlen(s.a);
  char* tmp = new char[size+1];
  strcpy(tmp,a);
  strcpy(tmp+j,s.a);
 delete[] a;
 a = tmp;

 return *this;
 }

int String::length(){
 return strlen(a);
}

main.cpp:

#include <iostream>
#include "String.h"

using namespace std;

bool operator==(const String& left, const String& right)
{
 int a = strcmp(left.a,right.a);
    if(a == 0)
  return true;
 else
  return false;
}
bool operator!=(const String& left, const String& right)
{
 return  !(left == right);
}

ostream& operator<<(ostream& os,String& s){
 int length = s.length();
 for(int i = 0;i < length;i++)
  //os << s.a[i];這麼不行,私有變量.
  os << s[i];
 return os;
}


String operator+(const String& a,const String& b){
 String temp;
 temp = a;
 temp += b;
 return temp;

}

bool operator<(const String& left,const String& right){
 
 int j = 0;
 while((left[j] != '/0') && (right[j] != '/0')){
  if(left[j] < right[j])
   return true;
  else
  {
   if(left[j] == right[j]){
    j++;
    continue;
   }
   else
    return false;
  }
 }
 if((left[j] == '/0') && (right[j] != '/0'))
  return true;
 else
  return false;
}

bool operator>(const String& left, const String& right)
{   int a = strcmp(left.a,right.a);
    if(a > 0)
  return true;
 else
  return false;
 
}

istream& operator>>(istream& is, String& s){
 delete[] s.a;
 s.a = new char[20];
 int m = 20;
    char c;
 int i = 0;
 while (is.get(c) && isspace(c));
    if (is) {
  do {s.a[i] = c;
       i++;
    /*if(i >= 20){
      cout << "Input too much characters!" << endl;
      exit(-1);
    }*/
    if(i == m - 1 ){
     s.a[i] = '/0';
     char* b = new char[m];
     strcpy(b,s.a);
                 m = m * 2;
        s.a = new char[m];
     strcpy(s.a,b);
     delete[] b;
    }
  }
  while (is.get(c) && !isspace(c));
        //如果讀到空白,將其放回.
  if (is)
   is.unget();
 }
 s.size = i;
 s.a[i] = '/0';
 return is;
}


int main(){
 String a = "abcd";
 String b = "www";
 //String c(6,b);這麼寫不對.
    String c(6,'l');
 String d;
 String e = a;//abcd
 String f;
 cin >> f;//需要輸入...
 String g;
 g = a + b;//abcdwww

 if(a < b)
  cout << "a < b" << endl;
 else
  cout << "a >= b" << endl;
 if(e == a)
  cout << "e == a" << endl;
 else
  cout << "e != a" << endl;
 
 b += a;
 
 cout << a << endl;
 cout << b << endl;
    cout << c << endl;
 cout << d << endl;
 cout << e << endl;
 cout << f << endl;
 cout << g << endl;
 cout << g[0] << endl;
 return 0;
}

 

 

4. Implement a single-direction linked list sorting algorithm. Please first define the data structure of linked list and then implement the sorting algorithm.

 

5.編寫一個函數,返回兩個字符串的最大公串!例如,“adbccadebbca”和“edabccadece”,返回“ccade”

 

聯想筆試題
  1.設計函數 int atoi(char *s)。

 int atoi(const char *nptr);
 函數說明
 atoi()會掃描參數nptr字符串,跳過前面空格字符,直到遇上數字或正負符號纔開始做轉換,而再 遇到非數字或字符串結束時('/0')才結束轉換,並將結果返回。
返回值 返回轉換後整型數。

#include <stdio.h>
#include <ctype.h>

int myAtoi(const char* s){
 int result = 0;
 int flag = 1;
 int i = 0;
 while(isspace(s[i]))
  i++;
 if(s[i] == '-'){
  flag = -1;
  i++;
 }
 if(s[i] == '+')
  i++;
 while(s[i] != '/0'){
  if((s[i] > '9') || (s[i] < '0'))
   break;
  int j = s[i] - '0';
  result = 10 * result + j;
  i++;
 }
 result = result * flag;
 return result;
}

int main(){
 char* a = "   -1234def";
 char* b = "+1234";
 int i = myAtoi(a);
 int j = myAtoi(b);
 printf("%d /n",i);
 printf("%d",j);
 return 0;
}

 
 



 
  2.int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); 輸出是多少?
  3.解釋局部變量、全局變量和靜態變量的含義。
  4.解釋堆和棧的區別。
  5.論述含參數的宏與函數的優缺點。
  普天C++筆試題
  1.實現雙向鏈表刪除一個節點P,在節點P後插入一個節點,寫出這兩個函數。
  2.寫一個函數,將其中的/t都轉換成4個空格。
  3.Windows程序的入口是哪裏?寫出Windows消息機制的流程。
  4.如何定義和實現一個類的成員函數爲回調函數?
  5.C++裏面是不是所有的動作都是main()引起的?如果不是,請舉例。
  6.C++裏面如何聲明const void f(void)函數爲C程序中的庫函數?
  7.下列哪兩個是等同的
  int b;
  A const int* a = &b;
  B const* int a = &b;
  C const int* const a = &b;
  D int const* const a = &b;
  8.內聯函數在編譯時是否做參數類型檢查?
  void g(base & b){
   b.play;
  }
  void main(){
   son s;
   g(s);
   return;
  }
華爲筆試題
  1.請你分別畫出OSI的七層網絡結構圖和TCP/IP的五層結構圖。
  2.請你詳細地解釋一下IP協議的定義,在哪個層上面?主要有什麼作用?TCP與UDP呢?
  3.請問交換機和路由器各自的實現原理是什麼?分別在哪個層次上面實現的?
  4.請問C++的類和C裏面的struct有什麼區別?
  5.請講一講析構函數和虛函數的用法和作用。
  6.全局變量和局部變量有什麼區別?是怎麼實現的?操作系統和編譯器是怎麼知道的?
  7.8086是多少位的系統?在數據總線上是怎麼實現的?
Sony筆試題
  1.完成下列程序
  *
  *.*.
  *..*..*..
  *...*...*...*...
  *....*....*....*....*....
  *.....*.....*.....*.....*.....*.....
  *......*......*......*......*......*......*......
  *.......*.......*.......*.......*.......*.......*.......*.......
  #include <stdio.h>
  #define N 8
  int main()
  {
   int i;
   int j;
   int k;
   ---------------------------------------------------------
   | |
   | |
   | |
   ---------------------------------------------------------
   return 0;
  }
  2.完成程序,實現對數組的降序排序
  #include <stdio.h>
  void sort( );
  int main()
  {
   int array[]={45,56,76,234,1,34,23,2,3}; //數字任//意給出
   sort( );
   return 0;
  }
  void sort( )
  {
   ____________________________________
   | |
   | |
   |-----------------------------------------------------|
  }
  3.費波那其數列,1,1,2,3,5……編寫程序求第十項。可以用遞歸,也可以用其他方法,但要說明你選擇的理由。
  #include <stdio.h>
  int Pheponatch(int);
  int main()
  {
   printf("The 10th is %d",Pheponatch(10));
   return 0;
  }
  int Pheponatch(int N)
  {
  --------------------------------
  | |
  | |
  --------------------------------
  }
  4.下列程序運行時會崩潰,請找出錯誤並改正,並且說明原因。
  #include <stdio.h>
  #include <malloc.h>
  typedef struct{
   TNode* left;
   TNode* right;
   int value;
  } TNode;
  TNode* root=NULL;
  void append(int N);
  int main()
  {
   append(63);
   append(45);
   append(32);
   append(77);
   append(96);
   append(21);
   append(17); // Again, 數字任意給出
  }
  void append(int N)
  {
   TNode* NewNode=(TNode *)malloc(sizeof(TNode));
   NewNode->value=N;
  
   if(root==NULL)
   {
   root=NewNode;
   return;
   }
   else
   {
   TNode* temp;
   temp=root;
   while((N>=temp.value && temp.left!=NULL) || (N<temp. value && temp. right!=NULL
  ))
   {
   while(N>=temp.value && temp.left!=NULL)
   temp=temp.left;
   while(N<temp.value && temp.right!=NULL)
   temp=temp.right;
   }
   if(N>=temp.value)
   temp.left=NewNode;
   else
   temp.right=NewNode;
   return;
   }
  }

MSRA Interview Written Exam(December 2003,Time:2.5 Hours)


1寫出下列算法的時間複雜度。
(1)冒泡排序;
(2)選擇排序;
(3)插入排序;
(4)快速排序;
(5)堆排序;
(6)歸併排序;

2寫出下列程序在X86上的運行結果。

struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
}test

void main(void) 
{
int i;
test.a=2;
test.b=3;
test.c=0;

i=*((short *)&test);
printf("%d/n",i);
}

3寫出下列程序的運行結果。

unsigned int i=3;
cout<<i * -1;

4寫出下列程序所有可能的運行結果。

int a;
int b;
int c;

void F1()
{
b=a*2;
a=b;
}

void F2()
{
c=a+1;
a=c;
}

main()
{
a=5;
//Start F1,F2 in parallel
F1(); F2();
printf("a=%d/n",a);
}

5考察了一個CharPrev()函數的作用。

6對 16 Bits colors的處理,要求:
(1)Byte轉換爲RGB時,保留高5、6bits;
(2)RGB轉換爲Byte時,第2、3位置零。

7一個鏈表的操作,注意代碼的健壯和安全性。要求:
(1)增加一個元素;
(2)獲得頭元素;
(3)彈出頭元素(獲得值並刪除)。

8一個給定的數值由左邊開始升位到右邊第N位,如
0010<<1 == 0100
或者
0001 0011<<4 == 0011 0000
請用C或者C++或者其他X86上能運行的程序實現。

附加題(只有在完成以上題目後,才獲准回答)
In C++, what does "explicit" mean? what does "protected" mean?

1。在C++中有沒有純虛構造函數?
2。在c++的一個類中聲明一個static成員變量有沒有用?
3。在C++的一個類中聲明一個靜態成員函數有沒有用?
4。如何實現一個非阻塞的socket?
5。setsockopt, ioctl都可以對socket的屬性進行設置,他們有什麼不同?
6。解釋一下進程和線程的區別?
7。解釋一下多播(組播)和廣播的含義?
8。多播採用的協議是什麼?
9。在c++中純虛析構函數的作用是什麼?請舉例說明。
10。編程,請實現一個c語言中類似atoi的函數功能(輸入可能包含非數字和空格)

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