ImageVerifierCode 换一换
格式:DOC , 页数:39 ,大小:71.04KB ,
资源ID:8192891      下载积分:12 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/8192891.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(2024年c语言面试常问题.doc)为本站上传会员【天****】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

2024年c语言面试常问题.doc

1、已经n次倒在c语言面试的问题上,总结了一下,是因为基础知识不扎实。痛定思痛,决定好好努力! 1.引言   本文的写作目标并不在于提供C/C++程序员求职面试指引,而意在从技术上分析面试题的内涵。文中的大多数面试题来自各大论坛,部分试题解答也参考了网友的意见。   许多面试题看似简单,却需要深厚的基本功才能给出完美的解答。企业要求面试者写一个最简单的strcpy函数都可看出面试者在技术上到底达成了怎样的程度,我们能真正写好一个strcpy函数吗?我们都以为自己能,可是我们写出的strcpy很也许只能拿到10分中的2分。读者可从本文看到strcpy 函数从2分到10分解答的例子,

2、看看自己属于什么样的层次。另外,尚有某些面试题考查面试者灵敏的思维能力。   分析这些面试题,自身包括很强的趣味性;而作为一名研发人员,通过对这些面试题的深入剖析则可深入增强自身的内功。 2.找错题   试题1: void test1() {  char string[10];  char* str1 = "";  strcpy( string, str1 ); }   试题2: void test2() {  char string[10], str1[10];  int i;  for(i=0; i<10; i++)  {   st

3、r1[i] = 'a';  }  strcpy( string, str1 ); }   试题3: void test3(char* str1) {  char string[10];  if( strlen( str1 ) <= 10 )  {   strcpy( string, str1 );  } }   解答:   试题1字符串str1需要11个字节才能存储下(包括末尾的’\0’),而string只有10个字节的空间,strcpy会导致数组越界;   对试题2,假如面试者指出字符数组str1不能在数组内结束能够给3分;假如面试者指出st

4、rcpy(string, str1)调用使得从str1内存起复制到string内存起所复制的字节数具备不确定性能够给7分,在此基础上指出库函数strcpy工作方式的给10 分;   对试题3,if(strlen(str1) <= 10)应改为if(strlen(str1) < 10),因为strlen的成果未统计’\0’所占用的1个字节。   剖析:   考查对基本功的掌握:   (1)字符串以’\0’结尾;   (2)对数组越界把握的敏感度;   (3)库函数strcpy的工作方式,假如编写一个标准strcpy函数的总分值为10,下面给出几个不一样得分的答

5、案:   2分 void strcpy( char *strDest, char *strSrc ) {   while( (*strDest++ = * strSrc++) != ‘\0’ ); }   4分 void strcpy( char *strDest, const char *strSrc ) //将源字符串加const,表白其为输入参数,加2分 {   while( (*strDest++ = * strSrc++) != ‘\0’ ); }   7分 void strcpy(char *strDest, const char

6、strSrc) {  //对源地址和目标地址加非0断言,加3分  assert( (strDest != NULL) && (strSrc != NULL) );  while( (*strDest++ = * strSrc++) != ‘\0’ ); }   10分 //为了实现链式操作,将目标地址返回,加3分! char * strcpy( char *strDest, const char *strSrc ) {  assert( (strDest != NULL) && (strSrc != NULL) );  char *address = str

7、Dest;  while( (*strDest++ = * strSrc++) != ‘\0’ );   return address; }   从2分到10分的几个答案我们能够清楚的看到,小小的strcpy竟然暗藏着这么多玄机,真不是盖的!需要多么扎实的基本功才能写一个完美的strcpy啊!   (4)对strlen的掌握,它没有包括字符串末尾的'\0'。   读者看了不一样分值的strcpy版本,应当也能够写出一个10分的strlen函数了,完美的版本为: int strlen( const char *str ) //输入参数const {  as

8、sert( strt != NULL ); //断言字符串地址非0  int len;  while( (*str++) != '\0' )  {   len++;  }  return len; }   试题4: void GetMemory( char *p ) {  p = (char *) malloc( 100 ); } void Test( void ) {  char *str = NULL;  GetMemory( str );  strcpy( str, "hello world" );  printf( str ); }

9、   试题5: char *GetMemory( void ) {  char p[] = "hello world";  return p; } void Test( void ) {  char *str = NULL;  str = GetMemory();  printf( str ); }   试题6: void GetMemory( char **p, int num ) {  *p = (char *) malloc( num ); } void Test( void ) {  char *str = NULL;  G

10、etMemory( &str, 100 );  strcpy( str, "hello" );  printf( str ); }   试题7: void Test( void ) {  char *str = (char *) malloc( 100 );  strcpy( str, "hello" );  free( str );  ... //省略的其他语句 }   解答:   试题4传入中GetMemory( char *p )函数的形参为字符串指针,在函数内部修改形参并不能真正的变化传入形参的值(这个说法不准确,看高质量C一书),执行完

11、 char *str = NULL; GetMemory( str );   后的str仍然为NULL;   试题5中 char p[] = "hello world"; return p;   的p[]数组为函数内的局部自动变量,在函数返回后,内存已经被释放。这是许多程序员常犯的错误,其根源在于不了解变量的生存期。   试题6的GetMemory防止了试题4的问题,传入GetMemory的参数为字符串指针的指针,不过在GetMemory中执行申请内存及赋值语句 *p = (char *) malloc( num );   后未判断内存是否申请成功

12、应加上: if ( *p == NULL ) {  ...//进行申请内存失败处理 }   试题7存在与试题6同样的问题,在执行 char *str = (char *) malloc(100);   后未进行内存是否申请成功的判断;另外,在free(str)后未置str为空,导致也许变成一个“野”指针,应加上: str = NULL;   试题6的Test函数中也未对malloc的内存进行释放。   剖析:   试题4~7考查面试者对内存操作的了解程度,基本功扎实的面试者一般都能正确的回答其中50~60的错误。不过要完全解答正确,却也绝

13、非易事。   对内存操作的考查重要集中在:   (1)指针的了解;   (2)变量的生存期及作用范围;   (3)良好的动态内存申请和释放习惯。   再看看下面的一段程序有什么错误: swap( int* p1,int* p2 ) {  int *p;  *p = *p1;  *p1 = *p2;  *p2 = *p; }   在swap函数中,p是一个“野”指针,有也许指向系统区,导致程序运行的瓦解。在VC++中DEBUG运行时提示错误“Access Violation”。该程序应当改为: swap( int* p1,int* p2

14、 ) {  int p;  p = *p1;  *p1 = *p2;  *p2 = p; } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3.内功题   试题1:分别给出BOOL,int,float,指针变量 与“零值”比较的 if 语句(假设变量名为var)   解答:    BOOL型变量:if(!var)    int型变量: if(var==0)    float型变量:    const float EPSINON = 0.00001;   

15、 if ((x >= - EPSINON) && (x <= EPSINON)    指针变量:  if(var==NULL)   剖析:   考查对0值判断的“内功”,BOOL型变量的0判断完全能够写成if(var==0),而int型变量也能够写成if(!var),指针变量的判断也能够写成if(!var),上述写法虽然程序都能正确运行,不过未能清楚地体现程序的意思。   一般的,假如想让if判断一个变量的“真”、“假”,应直接使用if(var)、if(!var),表白其为“逻辑”判断;假如用if判断一个数值型变量(short、int、long等),应当用if(var=

16、0),表白是与0进行“数值”上的比较;而判断指针则适宜用if(var==NULL),这是一个很好的编程习惯。   浮点型变量并不精准,因此不可将float变量用“==”或“!=”与数字比较,应当设法转化成“>=”或“<=”形式。假如写成if (x == 0.0),则判为错,得0分。   试题2:如下为Windows NT下的32位C++程序,请计算sizeof的值 void Func ( char str[100] ) {  sizeof( str ) = ? } void *p = malloc( 100 ); sizeof ( p ) = ?   解

17、答: sizeof( str ) = 4 sizeof ( p ) = 4   剖析:   Func ( char str[100] )函数中数组名作为函数形参时,在函数体内,数组名失去了自身的内涵,仅仅只是一个指针;在失去其内涵的同时,它还失去了其常量特性,能够作自增、自减等操作,能够被修改。   数组名的本质如下:   (1)数组名指代一个数据结构,这种数据结构就是数组;   例如: char str[10]; cout << sizeof(str) << endl;   输出成果为10,str指代数据结构char[10]。   (

18、2)数组名能够转换为指向其指代实体的指针,并且是一个指针常量,不能作自增、自减等操作,不能被修改; char str[10]; str++; //编译犯错,提示str不是左值    (3)数组名作为函数形参时,沦为一般指针。   Windows NT 32位平台下,指针的长度(占用内存的大小)为4字节,故sizeof( str ) 、sizeof ( p ) 都为4。   试题3:写一个“标准”宏MIN,这个宏输入两个参数并返回较小的一个。另外,当你写下面的代码时会发生什么事 least = MIN(*p++, b);   解答: #define M

19、IN(A,B) ((A) <= (B) ? (A) : (B))   MIN(*p++, b)会产生宏的副作用   剖析:   这个面试题重要考查面试者对宏定义的使用,宏定义能够实现类似于函数的功效,不过它终究不是函数,而宏定义中括弧中的“参数”也不是真的参数,在宏展开的时候对“参数”进行的是一对一的替代。   程序员对宏定义的使用要非常小心,尤其要注意两个问题:   (1)谨慎地将宏定义中的“参数”和整个宏用用括弧括起来。因此,严格地讲,下述解答: #define MIN(A,B) (A) <= (B) ? (A) : (B) #define MIN(A

20、B) (A <= B ? A : B )   都应判0分;   (2)预防宏的副作用。   宏定义#define MIN(A,B) ((A) <= (B) ? (A) : (B))对MIN(*p++, b)的作用成果是: ((*p++) <= (b) ? (*p++) : (*p++))   这个体现式会产生副作用,指针p会作三次++自增操作。   除此之外,另一个应当判0分的解答是: #define MIN(A,B) ((A) <= (B) ? (A) : (B));   这个解答在宏定义的背面加“;”,显示编写者对宏的概念含糊不清,只能被无

21、情地判0分并被面试官裁减。   试题4:为何标准头文献都有类似如下的结构? #ifndef __INCvxWorksh #define __INCvxWorksh #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __cplusplus } #endif #endif /* __INCvxWorksh */   解答:   头文献中的编译宏 #ifndef __INCvxWorksh #define __INCvxWorksh #endif   的作用是预防被重复引用。

22、   作为一个面对对象的语言,C++支持函数重载,而过程式语言C则不支持。函数被C++编译后在symbol库中的名字与C语言的不一样。例如,假设某个函数的原型为: void foo(int x, int y);   该函数被C编译器编译后在symbol库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字。_foo_int_int这么的名字包括了函数名和函数参数数量及类型信息,C++就是考这种机制来实现函数重载的。   为了实现C和C++的混合编程,C++提供了C连接互换指定符号extern "C"来处理名字匹配问题,函数申明前加上extern

23、"C"后,则编译器就会按照C语言的方式将该函数编译为_foo,这么C语言中就能够调用C++的函数了。 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  试题5:编写一个函数,作用是把一个char组成的字符串循环右移n个。例如本来是“abcdefghi”假如n=2,移位后应当是“hiabcdefgh”   函数头是这么的: //pStr是指向以'\0'结尾的字符串的指针 //steps是要求移动的n void LoopMove ( char * pStr, int steps ) {

24、 //请填充... }   解答:   正确解答1: void LoopMove ( char *pStr, int steps ) {  int n = strlen( pStr ) - steps;  char tmp[MAX_LEN];  strcpy ( tmp, pStr + n );  strcpy ( tmp + steps, pStr);  *( tmp + strlen ( pStr ) ) = '\0';  strcpy( pStr, tmp ); }   正确解答2: void LoopMove ( char *pStr,

25、int steps ) {  int n = strlen( pStr ) - steps;  char tmp[MAX_LEN];  memcpy( tmp, pStr + n, steps );  memcpy(pStr + steps, pStr, n );  memcpy(pStr, tmp, steps ); }   剖析:   这个试题重要考查面试者对标准库函数的纯熟程度,在需要的时候引用库函数能够很大程度上简化程序编写的工作量。   最频繁被使用的库函数包括:   (1) strcpy   (2) memcpy   (3) mem

26、set Pre-interview Questions These are questions to ask in a phone interview. The idea is to qualify a person before bringing them in for a face-to-face session. What is a virtual method? A pure virtual method? When would you use/not use a virtual destructor? What is the difference

27、between a pointer and a reference? A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized.

28、 What is the difference between new/delete and malloc/free? Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. What does const mean? What methods should every c++ class define and

29、why? What does main return? Explain what the header for a simple class looks like. How do you handle failure in a constructor? According to Bjarne Stroustup, designer of the C++ language, you handle failures in a constructor by throwing an exception. See here for more details, including

30、 a link to his document on exception safety and the standard library: -------------------------------------------------------------------------------- Junior Questions What is the difference between C and C++? Would you prefer to use one over the other? C is based

31、 on structured programming whereas C++ supports the object-oriented programming paradigm. Due to the advantages inherent in object-oriented programs such as modularity and reuse, C++ is preferred. However almost anything that can be built using C++ can also be built using C. Explain operato

32、r precendence. Operator precedence is the order in which operators are evaluated in a compound expression. For example, what is the result of the following expression? 6 + 3 * 4 / 2 + 2 Here is a compound expression with an insidious error. while ( ch = nextChar() != '\0' ) The prog

33、rammer's intention is to assign ch to the next character then test that character to see whether it is null. Since the inequality operator has higher precendence than the assignment operator, the real result is that the next character is compared to null and ch is assigned the boolean result of the

34、test (i.e. 0 or 1). What are the access privileges in C++? What is the default access level? The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class

35、 and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone. What is data encapsulation? Data Encapsulation is also known as data hiding. The most important advantage of encapsulation is that

36、 it lets the programmer create an object and then provide an interface to the object that other objects can use to call the methods provided by the object. The programmer can change the internal workings of an object but this transparent to other interfacing programs as long as the interface remains

37、 unchanged. What is inheritance? Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass; the subclass has a ISA relationship with the superclass. For example Vehicle can be a superclass and Car can be a subclass derived from Vehicle. In

38、 this case a Car ISA Vehicle. The superclass 'is not a' subclass as the subclass is more specialized and may contain additional members as compared to the superclass. The greatest advantage of inheritance is that it promotes generic design and code reuse. What is multiple inheritance? What

39、are its advantages and disadvantages? Multiple Inheritance is the process whereby a sub-class can be derived from more than one super class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of compl

40、ex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion when two base classes implement a method with the same name. What is polymorphism? Polymorphism refers to the ability to have more than one method with the same signature in an inheritanc

41、e hierarchy. The correct method is invoked at run-time based on the context (object) on which the method is invoked. Polymorphism allows for a generic use of method names while providing specialized implementations for them. What do the keyword static and const signify? When a class memb

42、er is declared to be of a static type, it means that the member is not an instance variable but a class variable. Such a member is accessed using Classname.Membername (as opposed to Object.Membername). Const is a keyword used in C++ to specify that an object's value cannot be changed. What

43、is a static member of a class? Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. How do you access the static member of a class? ::. What feature of C+

44、 would you use if you wanted to design a member function that guarantees to leave this object unchanged? It is const as in: int MyFunc? (int test) const; What is the difference between const char *myPointer; and char *const myPointer;? const char *myPointer; is a non-constant point

45、er to constant data; while char *const myPointer; is a constant pointer to non-constant data. How is memory allocated/deallocated in C? How about C++? Memory is allocated in C using malloc() and freed using free(). In C++ the new() operator is used to allocate memory to an object and the

46、 delete() operator is used to free the memory taken up by an object. What is the difference between public, protected, and private members of a class? Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the c

47、lass and by members and friends of derived classes. Public members are accessible by everyone. How do you link a C++ program to C functions? By using the extern "C" linkage specification around the C function declarations. The candidate should know about mangled function names and ty

48、pe-safe linkages. They should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions. What does extern "C" int func(int *, Foo) accomplish? It will turn off "name mangling" for

49、 func so that one can link to code compiled by a C compiler. Why do C++ compilers need name mangling? Name mangling is the rule according to which C++ changes function names into function signatures before invoking the linker. Mangled names are used by the linker to differentiate betwe

50、en different functions with the same name. What is function's signature? function's signature is its name plus the number and types of the parameters it accepts. What are the differences between a C++ struct and C++ class? The default member and base class access specifiers

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服