新闻  |   论坛  |   博客  |   在线研讨会
看看你的c语言
tongxin | 2009-04-12 15:48:44    阅读:734   发布文章

考查自己的编程质量究竟如何。然后参照答案严格打分。
%A (1)如果你只得了几十分,请不要声张,也不要太难过。编程质量差往往是由于不良习惯造成的,与人的智力、能力没有多大关系,还是有药可救的。成绩越差,可以进步的空间就越大,中国不就是在落后中赶超发达资本主义国家吗?只要你能下决心改掉不良的编程习惯,第二次考试就能及格了。
%A (2)如果你考及格了,表明你的技术基础不错,希望你能虚心学习、不断进步。如果你还没有找到合适的工作单位,不妨到上海贝尔试一试。
%A (3)如果你考出85分以上的好成绩,你有义务和资格为你所在的团队作"C++/C编程"培训。希望你能和我们多多交流、相互促进。半年前我曾经发现一颗好苗子,就把他挖到我们小组来。
%A (4)如果你在没有任何提示的情况下考了满分,希望你能收我做你的徒弟。
%A
%A
%A   本试题仅用于考查C++/C程序员的基本编程技能。内容限于C++/C常用语法,不涉及数据结构、算法以及深奥的语法。考试成绩能反映出考生的编程质量以及对C++/C的理解程度,但不能反映考生的智力和软件开发能力。
%A
%A   笔试时间90分钟。请考生认真答题,切勿轻视。
%A
%A 一、请填写BOOL , float, 指针变量 与"零值"比较的 if 语句。(10分)
%A 提示:这里"零值"可以是0, 0.0 , FALSE或者"空指针"。例如 int 变量 n 与"零值"比较的 if 语句为:
%A if ( n == 0 )
%A if ( n != 0 )
%A 以此类推。
%A
%A 请写出 BOOL flag 与"零值"比较的 if 语句:
%A
%A
%A 请写出 float x 与"零值"比较的 if 语句:
%A
%A
%A
%A 请写出 char *p 与"零值"比较的 if 语句:
%A
%A
%A
%A 二、以下为Windows NT下的32位C++程序,请计算sizeof的值(10分)
%A
%A   char str[] = "Hello" ;
%A   char *p = str ;
%A int n = 10;
%A 请计算
%A sizeof (str ) =
%A
%A sizeof ( p ) =
%A
%A sizeof ( n ) =   void Func ( char str[100])
%A {
%A 请计算
%A sizeof( str ) =
%A }
%A
%A   void *p = malloc( 100 );
%A 请计算
%A sizeof ( p ) =
%A
%A
%A 三、简答题(25分)
%A
%A 1、头文件中的 ifndef/define/endif 干什么用?
%A
%A
%A
%A 2、#include <filename.h> 和 #include "filename.h" 有什么区别?
%A
%A
%A
%A 3、const 有什么用途?(请至少说明两种)
%A
%A
%A
%A 4、在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern "C"声明?
%A
%A
%A
%A
%A 5、请简述以下两个for循环的优缺点
%A
%A // 第一个
%A for (i=0; i<N; i++)
%A {
%A if (condition)
%A DoSomething();
%A else
%A DoOtherthing();
%A }   // 第二个
%A if (condition)
%A {
%A for (i=0; i<N; i++)
%A DoSomething();
%A }
%A else
%A {
%A for (i=0; i<N; i++)
%A DoOtherthing();
%A }
%A 优点:
%A
%A
%A 缺点:
%A
%A
%A   优点:
%A
%A
%A 缺点:
%A
%A
%A
%A 四、有关内存的思考题(20分)
%A
%A void GetMemory(char *p)
%A {
%A p = (char *)malloc(100);
%A }
%A void Test(void)
%A {
%A char *str = NULL;
%A GetMemory(str);  
%A strcpy(str, "hello world");
%A printf(str);
%A }
%A
%A 请问运行Test函数会有什么样的结果?
%A 答:
%A
%A
%A
%A   char *GetMemory(void)
%A {  
%A char p[] = "hello world";
%A return p;
%A }
%A void Test(void)
%A {
%A char *str = NULL;
%A str = GetMemory();  
%A printf(str);
%A }
%A
%A 请问运行Test函数会有什么样的结果?
%A 答:
%A Void GetMemory2(char **p, int num)
%A {
%A *p = (char *)malloc(num);
%A }
%A void Test(void)
%A {
%A char *str = NULL;
%A GetMemory(&str, 100);
%A strcpy(str, "hello");  
%A printf(str);  
%A }
%A 请问运行Test函数会有什么样的结果?
%A 答:
%A
%A
%A
%A
%A   void Test(void)
%A {
%A char *str = (char *) malloc(100);
%A   strcpy(str, "hello");
%A   free(str);  
%A   if(str != NULL)
%A   {
%A   strcpy(str, "world");  
%A printf(str);
%A }
%A }
%A 请问运行Test函数会有什么样的结果?
%A 答:
%A
%A
%A
%A
%A
%A
%A 五、编写strcpy函数(10分)
%A 已知strcpy函数的原型是
%A   char *strcpy(char *strDest, const char *strSrc);
%A   其中strDest是目的字符串,strSrc是源字符串。
%A
%A (1)不调用C++/C的字符串库函数,请编写函数 strcpy
%A
%A
%A
%A
%A
%A
%A
%A (2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
%A
%A
%A
%A
%A 六、编写类String的构造函数、析构函数和赋值函数(25分)
%A 已知类String的原型为:
%A   class String
%A   {
%A   public:
%A     String(const char *str = NULL);   // 普通构造函数
%A     String(const String &other);   // 拷贝构造函数
%A     ~ String(void);             // 析构函数
%A     String & operate =(const String &other);   // 赋值函数
%A   private:
%A     char   *m_data;           // 用于保存字符串
%A   };
%A   请编写String的上述4个函数。
%A
%A
%A __________________
%A 来自互联网,就是我。代码男孩,记住我codeboy~  
%A 返回顶端      
%A   
%A cboy
%A 中级会员
%A
%A
%A
%A 注册时间: 07-12-2005
%A 在线状态: 离线
%A 发帖数: 197  发表: 17-04-2006 21:02 | IP已记录    
%A
%A --------------------------------------------------------------------------------
%A
%A
%A C++/C试题的答案与评分标准
%A
%A
%A
%A
%A
%A
%A 一、请填写BOOL , float, 指针变量 与"零值"比较的 if 语句。(10分)
%A
%A 请写出 BOOL flag 与"零值"比较的 if 语句。(3分)
%A 标准答案:
%A if ( flag )
%A if ( !flag )  
%A 如下写法均属不良风格,不得分。
%A   if (flag == TRUE)  
%A   if (flag == 1 )    
%A   if (flag == FALSE)
%A   if (flag == 0)    
%A 请写出 float x 与"零值"比较的 if 语句。(4分)
%A 标准答案示例:
%A const float EPSINON = 0.00001;
%A if ((x >= - EPSINON) && (x <= EPSINON)
%A 不可将浮点变量用"=="或"!="与数字比较,应该设法转化成">="或"<="此类形式。
%A   
%A 如下是错误的写法,不得分。
%A   if (x == 0.0)  
%A   if (x != 0.0)    
%A   
%A 请写出 char *p 与"零值"比较的 if 语句。(3分)
%A 标准答案:
%A if (p == NULL)
%A if (p != NULL)  
%A 如下写法均属不良风格,不得分。
%A   if (p == 0)  
%A   if (p != 0)    
%A   if (p)
%A   if (!)    
%A
%A 二、以下为Windows NT下的32位C++程序,请计算sizeof的值(10分)
%A
%A   char str[] = "Hello" ;
%A   char *p = str ;
%A int n = 10;
%A
%A
%A 请计算
%A sizeof (str ) = 6 (2分)
%A
%A sizeof ( p ) = 4 (2分)
%A
%A sizeof ( n ) = 4 (2分)  
%A
%A
%A
%A void Func ( char str[100])
%A {
%A 请计算
%A sizeof( str ) = 4 (2分)
%A }
%A
%A
%A
%A void *p = malloc( 100 );
%A 请计算
%A sizeof ( p ) = 4 (2分)
%A
%A
%A 三、简答题(25分)
%A
%A 1、头文件中的 ifndef/define/endif 干什么用?(5分)
%A 答:防止该头文件被重复引用。
%A
%A 2、#include <filename.h> 和 #include "filename.h" 有什么区别?(5分)
%A 答:对于#include <filename.h> ,编译器从标准库路径开始搜索 filename.h
%A 对于#include "filename.h" ,编译器从用户的工作路径开始搜索 filename.h
%A
%A 3、const 有什么用途?(请至少说明两种)(5分)
%A 答:(1)可以定义 const 常量
%A (2)const可以修饰函数的参数、返回值,甚至函数的定义体。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。
%A
%A 4、在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern "C"? (5分)
%A 答:C++语言支持函数重载,C语言不支持函数重载。函数被C++编译后在库中的名字与C语言的不同。假设某个函数的原型为: void foo(int x, int y);
%A 该函数被C编译器编译后在库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字。
%A C++提供了C连接交换指定符号extern"C"来解决名字匹配问题。
%A
%A 5、请简述以下两个for循环的优缺点(5分)
%A
%A for (i=0; i<N; i++)
%A {
%A if (condition)
%A DoSomething();
%A else
%A DoOtherthing();
%A }   if (condition)
%A {
%A for (i=0; i<N; i++)
%A DoSomething();
%A }
%A else
%A {
%A for (i=0; i<N; i++)
%A DoOtherthing();
%A }
%A 优点:程序简洁
%A
%A 缺点:多执行了N-1次逻辑判断,并且打断了循环"流水线"作业,使得编译器不能对循环进行优化处理,降低了效率。   优点:循环的效率高
%A
%A 缺点:程序不简洁
%A
%A
%A
%A 四、有关内存的思考题(每小题5分,共20分)
%A
%A void GetMemory(char *p)
%A {
%A p = (char *)malloc(100);
%A }
%A void Test(void)
%A {
%A char *str = NULL;
%A GetMemory(str);  
%A strcpy(str, "hello world");
%A printf(str);
%A }
%A
%A 请问运行Test函数会有什么样的结果?
%A 答:程序崩溃。
%A 因为GetMemory并不能传递动态内存,
%A Test函数中的 str一直都是 NULL。
%A strcpy(str, "hello world");将使程序崩溃。
%A   char *GetMemory(void)
%A {  
%A char p[] = "hello world";
%A return p;
%A }
%A void Test(void)
%A {
%A char *str = NULL;
%A str = GetMemory();  
%A printf(str);
%A }
%A
%A 请问运行Test函数会有什么样的结果?
%A 答:可能是乱码。
%A 因为GetMemory返回的是指向"栈内存"的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。
%A void GetMemory2(char **p, int num)
%A {
%A *p = (char *)malloc(num);
%A }
%A void Test(void)
%A {
%A char *str = NULL;
%A GetMemory(&str, 100);
%A strcpy(str, "hello");  
%A printf(str);  
%A }
%A 请问运行Test函数会有什么样的结果?
%A 答:
%A (1)能够输出hello
%A (2)内存泄漏
%A
%A   void Test(void)
%A {
%A char *str = (char *) malloc(100);
%A   strcpy(str, "hello");
%A   free(str);  
%A   if(str != NULL)
%A   {
%A   strcpy(str, "world");  
%A printf(str);
%A }
%A }
%A 请问运行Test函数会有什么样的结果?
%A 答:篡改动态内存区的内容,后果难以预料,非常危险。
%A 因为free(str);之后,str成为野指针,
%A if(str != NULL)语句不起作用。
%A
%A
%A
%A 五、编写strcpy函数(10分)
%A 已知strcpy函数的原型是
%A   char *strcpy(char *strDest, const char *strSrc);
%A   其中strDest是目的字符串,strSrc是源字符串。
%A (1)不调用C++/C的字符串库函数,请编写函数 strcpy
%A char *strcpy(char *strDest, const char *strSrc);
%A {
%A assert((strDest!=NULL) && (strSrc !=NULL));   // 2分
%A char *address = strDest;                 // 2分
%A while( (*strDest++ = * strSrc++) != ‘\0‘ )     // 2分
%A NULL ;
%A return address ;                     // 2分
%A }
%A
%A (2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
%A 答:为了实现链式表达式。                   // 2分
%A 例如   int length = strlen( strcpy( strDest, "hello world") );
%A
%A 六、编写类String的构造函数、析构函数和赋值函数(25分)
%A 已知类String的原型为:
%A   class String
%A   {
%A   public:
%A     String(const char *str = NULL);   // 普通构造函数
%A     String(const String &other);   // 拷贝构造函数
%A     ~ String(void);             // 析构函数
%A     String & operate =(const String &other);   // 赋值函数
%A   private:
%A     char   *m_data;           // 用于保存字符串
%A   };
%A   请编写String的上述4个函数。
%A 标准答案:
%A
%A // String的析构函数
%A   String::~String(void) // 3分
%A {
%A   delete [] m_data;  
%A // 由于m_data是内部数据类型,也可以写成 delete m_data;
%A   }
%A
%A   // String的普通构造函数
%A   String: tring(const char *str) // 6分
%A {
%A   if(str==NULL)
%A   {
%A     m_data = new char[1]; // 若能加 NULL 判断则更好
%A     *m_data = ‘\0‘;
%A   }  
%A   else
%A   {
%A     int length = strlen(str);
%A     m_data = new char[length+1]; // 若能加 NULL 判断则更好
%A     strcpy(m_data, str);
%A   }
%A }  
%A // 拷贝构造函数
%A   String: tring(const String &other) // 3分
%A   {  
%A   int length = strlen(other.m_data);  
%A   m_data = new char[length+1]; // 若能加 NULL 判断则更好
%A   strcpy(m_data, other.m_data);
%A }
%A // 赋值函数
%A   String & String: per ate =(const String &other) // 13分
%A   {  
%A     // (1) 检查自赋值 // 4分
%A     if(this == &other)
%A         return *this;
%A   
%A // (2) 释放原有的内存资源 // 3分
%A     delete [] m_data;
%A     
%A     // (3)分配新的内存资源,并复制内容 // 3分
%A   int length = strlen( other.m_data);  
%A   m_data = new char[length+1]; // 若能加 NULL 判断则更好
%A     strcpy(m_data, other.m_data);
%A     
%A     // (4)返回本对象的引用 // 3分
%A     return *this;
%A }
%A
%A
%A%A
%A

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
最近文章
寂寞如雪
2009-05-19 19:01:18
夜色花
2009-05-19 18:56:22
没有爱可以重来
2009-05-19 18:54:59
推荐文章
最近访客