C语言笔试题

来源:互联网 发布:spice语音优化 编辑:程序博客网 时间:2024/04/24 12:30

  

 
 
 
现在的公司招聘,都要笔试面试.如果你不是那种编程功底非常深厚的人,又不好好准备一番,
在笔试面试中往往会处于被动局面.虽然有些笔试题是故意为难我们,有点钻牛角尖.
但是很多笔试题面试题确实能够很好地看出我们的基础.

 在这里,我就略去那些钻牛角尖的题.从csdn论坛我近半年的收集中选出10道有代表性的题目,
难度基本上是逐渐加大.对数组,指针,数据结构,算法,字符串,文件操作等问题都有覆盖.
主要以c语言的实现为主,也有c++的题.大家可以先做做这10道题,测试一下自己的水平.

1. 下面这段代码的输出是多少(在32位机上).

    char *p;   // 4

    char *q[20];   // 80

    char *m[20][20];  // 1600

    int (*n)[10];   // 4

    struct MyStruct

{

char dda;

double dda1;

int type ;

};
MyStruct k;   // 24

 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+i)____);


在空格处填上合适的语句,顺序打印出a中的数字

(2)

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

问:p=a是否会导致程序在以后出现问题?为什么?

编译就通不过,p是一个指针的指针,而a是一个2维数组的首地址。
但是*p = a也是错误的。

3.用递归方式,非递归方式写函数将一个字符串反转.

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

#include <stdio.h>

/*=======================================================
函 数 名: reverse()
参    数: str
功能描述: 将一个字符串翻转
返 回 值: const char*
抛出异常: 无
作    者: 刘基伟 2007/4/21
=======================================================*/

const char *reverse(char *str);

int main()
 {
  const char *pch;       // 用于取得函数的返回值,来输出翻转后的结果
     char chArray[] = " Hello World ! ";   // 存储一个将要翻转的字符串
  pch = reverse(chArray);     // 将字符串chArray翻转
  printf("%s/n",pch);         // 打印字符串chArray
  return 0;
 }

const char *reverse(char *str)
 {
  if(str == NULL)
   return NULL;
  int  nCount = 0;           // 用来统计字符串的大小
  int  nCount_div;           // 将字符串的大小折半
  const char *pRemark_begin;       // 标记字符串的首地址
  char chTemp;               // 用于交换字符串的临时变量
  char *pString_begin;       // 存储交换的头指针
  char *pString_end;         // 存储交换的尾指针

  pString_begin = str;
  pRemark_begin = str;

  while(*str != '/0')        // 寻找字符串的结尾
  {
   str++;
   nCount++;
  }
  pString_end = --str;       // 退回一个才是字符串的末尾
  nCount_div = nCount/2;

  while(nCount_div>0)        // 将字符串翻转
  {
   chTemp = *pString_begin;
   *pString_begin = *pString_end;
   *pString_end = chTemp;
   pString_begin++;
   pString_end--;
   nCount_div--;
  }
  return pRemark_begin;
 }

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.

a+b这里做了隐式的转换,把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);
          }

str1没有结束标志,因此陷入死循环

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); 输出是多少?
       输出32
  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的函数功能(输入可能包含非数字和空格) 
 
创建于: 2007-02-08 09:55:18,修改于: 2007-02-08 09:55:18,已浏览422次,有评论2条 
 
  

原创粉丝点击