C_C++语言程序设计笔试面试题7

上传人:飞****9 文档编号:130668366 上传时间:2020-04-30 格式:DOC 页数:11 大小:69.50KB
返回 下载 相关 举报
C_C++语言程序设计笔试面试题7_第1页
第1页 / 共11页
C_C++语言程序设计笔试面试题7_第2页
第2页 / 共11页
C_C++语言程序设计笔试面试题7_第3页
第3页 / 共11页
C_C++语言程序设计笔试面试题7_第4页
第4页 / 共11页
C_C++语言程序设计笔试面试题7_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《C_C++语言程序设计笔试面试题7》由会员分享,可在线阅读,更多相关《C_C++语言程序设计笔试面试题7(11页珍藏版)》请在金锄头文库上搜索。

1、4.技巧题 试题2:写一个函数返回1+2+3+n的值(假定结果不会超过长整型变量的范围) 解答: int Sum( int n ) return ( (long)1 + n) * n / 2;/或return (1l + n) * n / 2; 剖析: 对于这个题,只能说,也许最简单的答案就是最好的答案。下面的解答,或者基于下面的解答思路去优化,不管怎么“折腾”,其效率也不可能与直接return ( 1 l + n ) * n / 2相比! int Sum( int n ) long sum = 0; for( int i=1; i= - EPSINON) & (x =”或“=”此类形式。如下

2、是错误的写法,不得分。if (x = 0.0) if (x != 0.0) 请写出char *p 与“零值”比较的 if 语句。(3分)标准答案:if (p = NULL) if (p != NULL)如下写法均属不良风格,不得分。if (p = 0) if (p != 0) if (p) if (!) 二、以下为Windows NT下的32位C+程序,请计算sizeof的值(10分) char *p = str ;int n = 10;char str = “Hello” ; 请计算sizeof (str ) = 6 sizeof ( p ) = 4 (2分) (2分) sizeof ( n

3、 ) = 4 (2分)void Func ( char str100)请计算sizeof( str ) = 4 (2分)void *p = malloc( 100 );请计算sizeof ( p ) = 4 (2分) 三、简答题(25分) 1、头文件中的 ifndef/define/endif 干什么用?(5分)答:防止该头文件被重复引用。#include ,编译器从标准库路径开始搜索 filename.h对于#include “filename.h” ,编译器从用户的工作路径开始搜索 filename.h3.const 有什么用途?(请至少说明两种)(5分)答:4.在C+ 程序中调用被 C编

4、译器编译后的函数,为什么要加 extern “C”? (5分)答:5.请简述以下两个for循环的优缺点(5分)for (i=0; iif (condition)DoSomething();elseDoOtherthing();if (condition)for (i=0; iDoSomething();elsefor (i=0; iDoOtherthing();优点:程序简洁缺点:多执行了N-1次逻辑判断,并且打断了循环“流水线”作业,使得编译器不能对循环进行优化处理,降低了效率。优点:循环的效率高缺点:程序不简洁5分,共20分) void GetMemory(char *p)p = (cha

5、r *)malloc(100);void Test(void) char *str = NULL;GetMemory(str); strcpy(str, hello world);printf(str);请问运行Test函数会有什么样的结果?答:程序崩溃。因为Test函数中的 str一直都是 NULL。strcpy(str, hello world);将使程序崩溃。GetMemory并不能传递动态内存,char *GetMemory(void) char p = hello world;return p;void Test(void)char *str = NULL;str = GetMemo

6、ry(); printf(str);请问运行Test函数会有什么样的结果?答:可能是乱码。因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。void GetMemory2(char *p, int num)*p = (char *)malloc(num);void Test(void)char *str = NULL;GetMemory(&str, 100);strcpy(str, hello); printf(str); 请问运行Test函数会有什么样的结果?答:(1)能够输出hello2)内存泄漏void Test(vo

7、id)char *str = (char *) malloc(100);strcpy(str, free(str); if(str != NULL)strcpy(str, printf(str);请问运行Test函数会有什么样的结果?答:篡改动态内存区的内容,后果难以预料,非常危险。因为free(str);之后,str成为野指针,if(str != NULL)语句不起作用。“hello”);“world”); strcpy函数(10分)strcpy函数的原型是char *strcpy(char *strDest, const char *strSrc);其中strDest是目的字符串,strS

8、rc是源字符串。1)不调用C+/C的字符串库函数,请编写函数 strcpystrDest!=NULL) & (strSrc !=NULL); / 2分strDest; / 2分strDest+ = * strSrc+) != 0 ) / 2分2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?int length = strlen( strcpy( strDest, “hello world”) ); String的构造函数、析构函数和赋值函数(25分)String的原型为:String:String(void) / 3分String:String(const char *str) / 6分0; -来源为林锐博士的 已知六、编写类已知类class Stringpublic:String(const char *str = NULL); / 普通构造函数String(const String &other); / 拷贝构造函数 String(void); / 析构函数String & operate =(const String &other); /

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 管理论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号