1、c语言面试常问题已经n次倒在c语言面试的问题上,总结了一下,是由于基础知识不扎实。痛定思痛,决定好好努力!1.引言本文的写作目的并不在于提供C/C+程序员求职面试指导,而旨在从技术上分析面试题的内涵。文中的大多数面试题来自各大论坛,部分试题解答也参考了网友的意见。许多面试题看似简单,却需要深厚的基本功才能给出完美的解答。企业要求面试者写一个最简单的strcpy函数都可看出面试者在技术上究竟达到了怎样的程度,我们能真正写好一个strcpy函数吗?我们都觉得自己能,可是我们写出的strcpy很可能只能拿到10分中的2分。读者可从本文看到strcpy 函数从2分到10分解答的例子,看看自己属于什么样
2、的层次。此外,还有一些面试题考查面试者敏捷的思维能力。分析这些面试题,本身包含很强的趣味性;而作为一名研发人员,通过对这些面试题的深入剖析则可进一步增强自身的内功。2.找错题试题1:void test1()char string10;char* str1 = 09;strcpy( string, str1 );试题2:void test2()char string10, str110;int i;for(i=0; i10; i+)str1i = a;strcpy( string, str1 );试题3:void test3(char* str1)char string10;if( strlen
3、( str1 ) = 10 )strcpy( string, str1 );解答:试题1字符串str1需要11个字节才能存放下(包括末尾的0),而string只有10个字节的空间,strcpy会导致数组越界;对试题2,如果面试者指出字符数组str1不能在数组内结束可以给3分;如果面试者指出strcpy(string, str1)调用使得从str1内存起复制到string内存起所复制的字节数具有不确定性可以给7分,在此基础上指出库函数strcpy工作方式的给10 分;对试题3,if(strlen(str1) = 10)应改为if(strlen(str1) 10),因为strlen的结果未统计0所
4、占用的1个字节。剖析:考查对基本功的掌握:(1)字符串以0结尾;(2)对数组越界把握的敏感度;(3)库函数strcpy的工作方式,如果编写一个标准strcpy函数的总分值为10,下面给出几个不同得分的答案:2分void strcpy( char *strDest, char *strSrc ) while( (*strDest+ = * strSrc+) != 0 );4分void strcpy( char *strDest, const char *strSrc ). .功题试题1:分别给出BOOL,int,float,指针变量 与“零值”比较的 if 语句(假设变量名为var)解答:BOO
5、L型变量:if(!var)int型变量: if(var=0)float型变量:const float EPSINON = ;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判断一个数值型变量
6、(short、int、long等),应该用if(var=0),表明是与0进行“数值”上的比较;而判断指针则适宜用if(var=NULL),这是一种很好的编程习惯。浮点型变量并不精确,所以不可将float变量用“=”或“!=”与数字比较,应该设法转化成“=”或“=”形式。如果写成if (x = ,则判为错,得0分。试题2:以下为Windows NT下的32位C+程序,请计算sizeof的值void Func ( char str100 )sizeof( str ) = ?void *p = malloc( 100 );sizeof ( p ) = ?解答:sizeof( str ) = 4siz
7、eof ( p ) = 4剖析:Func ( char str100 )函数中数组名作为函数形参时,在函数体内,数组名失去了本身的内涵,仅仅只是一个指针;在失去其内涵的同时,它还失去了其常量特性,可以作自增、自减等操作,可以被修改。数组名的本质如下:(1)数组名指代一种数据结构,这种数据结构就是数组;例如:char str10;cout sizeof(str) endl;输出结果为10,str指代数据结构char10。(2)数组名可以转换为指向其指代实体的指针,而且是一个指针常量,不能作自增、自减等操作,不能被修改;char str10;str+; .*/#ifdef _cplusplus#e
8、ndif#endif /* _INCvxWorksh */解答:头文件中的编译宏#ifndef_INCvxWorksh#define_INCvxWorksh#endif的作用是防止被重复引用。作为一种面向对象的语言,C+支持函数重载,而过程式语言C则不支持。函数被C+编译后在symbol库中的名字与C语言的不同。例如,假设某个函数的原型为:void foo(int x, int y);该函数被C编译器编译后在symbol库中的名字为_foo,而C+编译器则会产生像_foo_int_int之类的名字。_foo_int_int这样的名字包含了函数名和函数参数数量及类型信息,C+就是考这种机制来实现
9、函数重载的。为了实现C和C+的混合编程,C+提供了C连接交换指定符号extern C来解决名字匹配问题,函数声明前加上extern C后,则编译器就会按照C语言的方式将该函数编译为_foo,这样C语言中就可以调用C+的函数了。+试题5:编写一个函数,作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh”函数头是这样的:.解答:正确解答1:void LoopMove ( char *pStr, int steps )int n = strlen( pStr ) - steps;char tmpMAX_LEN;strcpy (
10、 tmp, pStr + n );strcpy ( tmp + steps, pStr);*( tmp + strlen ( pStr ) ) = 0;strcpy( pStr, tmp );正确解答2:void LoopMove ( char *pStr, int steps )int n = strlen( pStr ) - steps;char tmpMAX_LEN;memcpy( tmp, pStr + n, steps );memcpy(pStr + steps, pStr, n );memcpy(pStr, tmp, steps );剖析:这个试题主要考查面试者对标准库函数的熟练程
11、度,在需要的时候引用库函数可以很大程度上简化程序编写的工作量。最频繁被使用的库函数包括:(1) strcpy(2) memcpy(3) memsetPre-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/n
12、ot use a virtual destructor?What is the difference 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 ref
13、ers to an object with which it was initialized. 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 sho
14、uld every c+ class define and 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 detai
15、ls, including 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 on structured programming whereas C+ supports the object-oriented programming paradigm. Due to the advan
16、tages 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 operator precendence.Operator precedence is the order in which operators are evaluated in a compound expression. For example
17、, 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 programmers intention is to assign ch to the next character then test that character to see whether it is null. Since the inequality operator
18、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 test . 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
19、protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and its sub-classes. Public members of a class can be accessed by anyone. W
20、hat is data encapsulation?Data Encapsulation is also known as data hiding. The most important advantage of encapsulation is that 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 c
21、an change the internal workings of an object but this transparent to other interfacing programs as long as the interface remains 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 relations
22、hip with the superclass. For example Vehicle can be a superclass and Car can be a subclass derived from Vehicle. In 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 advanta
23、ge of inheritance is that it promotes generic design and code reuse. What is multiple inheritance? What 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
24、a class to inherit the functionality of more than one base class thus allowing for modeling of complex 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 refe
25、rs to the ability to have more than one method with the same signature in an inheritance 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 implementation
26、s for them. What do the keyword static and const signify?When a class member 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 (as opposed to . Const is a keyword used in C+ to specify that an objects value
27、cannot be changed. What 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+ would you use if you wanted to
28、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 pointer to constant data; while char *const myPointer; is
29、 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 delete() operator is used to free the memory taken up by an object
30、. 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 class 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. Th
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1