C/C++及数据结构笔试题集锦(校园招聘)之一


                                      

嗯,这是我从学校bbs精华区整理的c/c++及数据结构的笔试以及部分面试题集锦,觉得只要搞懂了这些题目,大部分的笔试都不惧了。希望能给需要的人带来帮助。顺便攒rp ^_^

不过大部分题目没有答案,欢迎补充哦~~~~

一:

已知类String的原型为:

class String

{

 public

     String(const char *str = NULL);            //普通构造函数

     String(const String ©);                //拷贝构造函数

     ~String(void);                             //析构函数

     String & operator = (const String ©);  //赋值构造函数

 private:

     char * m_data;                             //用于保存字符串

};

请编写String的上述4个函数。

答案:

版本1

// String 的析构函数

String::~String(void) // 3 

{

 delete [] m_data;

// 由于m_data 是内部数据类型,也可以写成delete m_data;

}

String::String(const char *str)

{

 if(str==NULL)

{

 m_data = new char[1]; // 若能加NULL 判断则更好

 *m_data = ‘{post.content}’;

 }

else

 {

 int length = strlen(str);

 m_data = new char[length+1]; // 若能加NULL 判断则更好

 strcpy(m_data, str);

 }

}

// 拷贝构造函数

String::String(const String &other)

{

 int length = strlen(other.m_data);

 m_data = new char[length+1]; // 若能加NULL 判断则更好

 strcpy(m_data, other.m_data);

}

// 赋值函数

String & String:operate =(const String &other)

{

// (1) 检查自赋值

if(this == &other)

return *this;

// (2) 释放原有的内存资源

delete [] m_data;

// 3)分配新的内存资源,并复制内容

 int length = strlen(other.m_data);

m_data = new char[length+1]; // 若能加NULL 判断则更好

 strcpy(m_data, other.m_data);

// 4)返回本对象的引用

 return *this;

}

版本2

String::String (const char *str)

{

     if(str){

      memset(m_data,0,strlen(m_data));

      strcpy(m_data,str);

     }

     else *m_data=0;

}

  String::String (const String ©)

 {

     strcpy(m_data,copy.m_data);

  }

  String& String:operator =(const String ©)

 {

    if(this==©) retrun *this;

     strcpy(m_data,copy.m_data);

      return *this;

}

版本3

String::String (const char *str)

{

     if ( m_data )

         delete[] m_data;

     if(str){

      m_data = new char[strlen(str)];

      memset(m_data,0,strlen(m_data));

      strcpy(m_data,str);

     }

     else *m_data=0;

}

 

  String::String (const String ©)

 {

     if ( m_data )

     delete[] m_data;

     m_data = new char[strlen(copy.m_data+1)]

     strcpy(m_data,copy.m_data);

  }

 

  String& String:operator =(const String ©)

 {

    if(this==©) retrun *this;

     if ( m_data )

         delete[] m_data;

     m_data = new char[strlen(copy.m_data+1)]

     strcpy(m_data,copy.m_data);

      return *this;

  }

~String::String(void)

{

  if ( m_data )

      delete[] m_data;

}

二:改错题,只能在原来的基础上增加代码,不能删除代码

#include

#include

void foo(int age,char *b)

{

   b = (char *)malloc(64);

   sprintf(b,"Your Age is %d",age);

}

int main()

{

  char *f;

  foo(23,f);

  printf("%s\n",f);

}

答案

版本1

#include

#include

void foo(int age,char **b)

{

   *b = (char *)malloc(64);

   sprintf(*b,"Your Age is %d",age);

}

int main()

{

  char **f;

  foo(23,f);

  printf("%s\n",**f);

  return 0;

}

版本2

#include

#include

void foo(int age,char *&b)

{

   b = (char *)malloc(64);

   sprintf(b,"Your Age is %d",age);

}

int main()

{

  char *f;

  foo(23,f);

  printf("%s\n",f);

  free(f);//不要忘了free;

}

三:有程序片断如下

int main()

{

   int I = 20;

   pid_t pid = 5;

   if((pid = fork()) > 0)

   {

      I = 50;

      printf("%d\n",I);   (1)

   }

   else if(pid == 0)

   {

      printf("%d\n",I);   (2)

   }

}

请问该程序用的是进程方式还是线程方式,并说明进程与线程的区别:

请问该程序输出什么结果?

无参考答案L

四、constant pointer points for String

   pointer points for constant string

五、下面等价的是:

A int i=0

    if(i)

   {

    printf("hello,world");

   }

   B  int i=1;

      int j=2;

      if(i==1 || j==2)

      {

       printf("hello,world");

       }

   C  Boolean b1=true;

      Boolean b2=true;

      if(b1==b2)

      {

       printf("hello,world");

     }

    D  int i=1;

      int j=2;

      if(i==1 &| j==2)

      {

       printf("hello,world");

     }

六、排序二叉树插入一个节点或双向链表的实现

四~六为IBM面试题。

七、指针++的含义和用法

八、stack heap的分配,rtos的特点、同步的方式

九、怎样避免内存泄漏的问题

十、编程实现十进制数转化为十六进制输出,不准用任何已经定义的库函数,比方说String                                                                         

Mathint toHex(int )

 

十一、编程实现大于100的两个数值相乘的结果输出,同样不准使用任何已定义函数,Math,st                                                                                  

ring,convert等。比方说12345*32534677                                                                                                                           

输入为两个string      int toPlus('12345','32434677')                                                                                                            

输出为一个长型的

十二、int delete(node * head)                                                                                         

{                                                                                                                                                                                                                                   

  free(head);                                                                                                                                                   

 head=head->link;                                                                                                                                               

return(0);                                                                                                                                                     

}                                                                                                                                                              

指出程序的错误,并且写出正确的程序

十三、写一个程序可以算出字节在计算机中的存储是由大到小还是有小到大。

十四、一段程序,写出输出结果     

大概是                                                                                                                                                          

class A                                                                                                                                                         

{                                                                                                                                                              

 static void virtual print(){cout<<"A::print()"<                                                                                                        

}                                                                                                                                                               

class B                                                                                                                                                        

{                                                                                                                                                               

 static void virtual print(){cout<<"B::print()"<                                                                                                        

}                                                                                                                                                               

class C   

{                                                                                                                                                         

 static void  print(){cout<<"C::print()"<                                                                                                               

}                                                                                                                                                              

print (A a)                                                                                                                                                    

{                                                                                                                                                              

 a.print();                                                                                                                                                     

}                                                                                                                                                              

main()                                                                                                                                                          

{                                                                                                                                                              

A a,*aa,*ab,*ac;                                                                                                                                                

B b;                                                                                                                                                            

C c; 

aa=&a;                                                                                                                                                         

ab=&b;                                                                                                                                                          

ac=&c;                                                                                                                                                          

a.print();                                                                                                                                                     

b.print();                                                                                                                                                      

c.print();                                                                                                                                                     

aa.print();                                                                                                                                                    

ab.print();                                                                                                                                                     

ac.print();                                                                                                                                                    

print(a);                                                                                                                                                       

print(b);                                                                                                                                                     

print(c);                                                                                                                                                      

}     

十五、给两个变量,如何找出一个带环单链表中是什么地方出现环的。(答案参考expert C programming)。

十~十五为MS笔试题。

十六、写一个带参数宏get_struct_addr_from_member_addr(p, stru, m)

能够根据任意结构实体的某一个成员的地址,算出该结构实体的地址,其中参数p是指向该

成员的指针,stru是该结构体,m是该成员。(SUN试题)

十七、给一个函数

int main(){

    int i, n=20;

    for(i=0;i

        printf("-");

    return 0;

}

要求替换、增加或者减少一个字符,使该程序可以打出20个“-”号,并要求写出三种解法

。(sun试题)

参考:1i-- 换成 n--

2i换成 -i

十八、解释 typedef char (*FUNC)(int, char*)的含义

十九、问#include #include "abc.h"的区别,#define MAX_NUM 10  const int

MAX_NUM=10区别

二十、问用什么方法可以避免一些潜在错误,比如if( myvar = 3)这一类

编程规范的问题,用 if( 3 = myvar)就可以在编译时报错

十六~二十为SUN笔试题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值