c语言面试常问题.docx

上传人:b****4 文档编号:4538151 上传时间:2023-05-07 格式:DOCX 页数:29 大小:30.11KB
下载 相关 举报
c语言面试常问题.docx_第1页
第1页 / 共29页
c语言面试常问题.docx_第2页
第2页 / 共29页
c语言面试常问题.docx_第3页
第3页 / 共29页
c语言面试常问题.docx_第4页
第4页 / 共29页
c语言面试常问题.docx_第5页
第5页 / 共29页
c语言面试常问题.docx_第6页
第6页 / 共29页
c语言面试常问题.docx_第7页
第7页 / 共29页
c语言面试常问题.docx_第8页
第8页 / 共29页
c语言面试常问题.docx_第9页
第9页 / 共29页
c语言面试常问题.docx_第10页
第10页 / 共29页
c语言面试常问题.docx_第11页
第11页 / 共29页
c语言面试常问题.docx_第12页
第12页 / 共29页
c语言面试常问题.docx_第13页
第13页 / 共29页
c语言面试常问题.docx_第14页
第14页 / 共29页
c语言面试常问题.docx_第15页
第15页 / 共29页
c语言面试常问题.docx_第16页
第16页 / 共29页
c语言面试常问题.docx_第17页
第17页 / 共29页
c语言面试常问题.docx_第18页
第18页 / 共29页
c语言面试常问题.docx_第19页
第19页 / 共29页
c语言面试常问题.docx_第20页
第20页 / 共29页
亲,该文档总共29页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c语言面试常问题.docx

《c语言面试常问题.docx》由会员分享,可在线阅读,更多相关《c语言面试常问题.docx(29页珍藏版)》请在冰点文库上搜索。

c语言面试常问题.docx

c语言面试常问题

已经 n 次倒在 c 语言面试的问题上,总结了一下,是由于基础知识不扎实。

痛定思痛,决

定好好努力!

1.引言

  本文的写作目的并不在于提供 C/C++程序员求职面试指导,而旨在从技术上分析面试

题的内涵。

文中的大多数面试题来自各大论坛,部分试题解答也参考了网友的意见。

  许多面试题看似简单,却需要深厚的基本功才能给出完美的解答。

企业要求面试者写

一个最简单的 strcpy 函数都可看出面试者在技术上究竟达到了怎样的程度,我们能真正写

好一个 strcpy 函数吗?

我们都觉得自己能,可是我们写出的 strcpy 很可能只能拿到 10 分中

的 2 分。

读者可从本文看到 strcpy函数从 2 分到 10 分解答的例子,看看自己属于什么样

的层次。

此外,还有一些面试题考查面试者敏捷的思维能力。

  分析这些面试题,本身包含很强的趣味性;而作为一名研发人员,通过对这些面试题

的深入剖析则可进一步增强自身的内功。

2.找错题

  试题 1:

void test1()

{

 char string[10];

 char* str1 = "0123456789";

 strcpy( string, str1 );

}

  试题 2:

void test2()

{

 char string[10], str1[10];

 int i;

 for(i=0; i<10; i++)

 {

  str1[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 分;如果面试者

指出 strcpy(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,下面给出

几个不同得分的答案:

  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 *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 = strDest;

 while( (*strDest++ = * strSrc++) !

= ‘\0’ );

  return address;

}

  从 2 分到 10 分的几个答案我们可以清楚的看到,小小的 strcpy 竟然暗藏着这么多玄机,

真不是盖的!

需要多么扎实的基本功才能写一个完美的 strcpy 啊!

  (4)对 strlen 的掌握,它没有包括字符串末尾的'\0'。

  读者看了不同分值的 strcpy 版本,应该也可以写出一个 10 分的 strlen 函数了,完美的

版本为:

int strlen( const char *str ) //输入参数 const

{

 assert( 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 );

}

  试题 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;

 GetMemory( &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 一书),执行完

char *str = NULL;

GetMemory( str );

  后的 str 仍然为 NULL;

  试题 5 中

char p[] = "hello world";

return p;

  的 p[]数组为函数内的局部自动变量,在函数返回后,内存已经被释放。

这是许多程序

员常犯的错误,其根源在于不理解变量的生存期。

  试题 6 的 GetMemory 避免了试题 4 的问题,传入 GetMemory 的参数为字符串指针的

指针,但是在 GetMemory 中执行申请内存及赋值语句

*p = (char *) malloc( num );

  后未判断内存是否申请成功,应加上:

if ( *p == NULL )

{

 ...//进行申请内存失败处理

}

  试题 7 存在与试题 6 同样的问题,在执行

char *str = (char *) malloc(100);

  后未进行内存是否申请成功的判断;另外,在 free(str)后未置 str 为空,导致可能变成

一个“野”指针,应加上:

str = NULL;

  试题 6 的 Test 函数中也未对 malloc 的内存进行释放。

  剖析:

  试题 4~7 考查面试者对内存操作的理解程度,基本功扎实的面试者一般都能正确的回

答其中 50~60 的错误。

但是要完全解答正确,却也绝非易事。

  对内存操作的考查主要集中在:

  

(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 )

{

 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;

   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==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 ) = ?

  解答:

sizeof( str ) = 4

sizeof ( p ) = 4

  剖析:

  Func ( char str[100] )函数中数组名作为函数形参时,在函数体内,数组名失去了本身

的内涵,仅仅只是一个指针;在失去其内涵的同时,它还失去了其常量特性,可以作自增、

自减等操作,可以被修改。

  数组名的本质如下:

  

(1)数组名指代一种数据结构,这种数据结构就是数组;

  例如:

char str[10];

cout << sizeof(str) << endl;

  输出结果为 10,str 指代数据结构 char[10]。

  

(2)数组名可以转换为指向其指代实体的指针,而且是一个指针常量,不能作自增、

自减等操作,不能被修改;

char str[10];

str++; //编译出错,提示 str 不是左值 

  (3)数组名作为函数形参时,沦为普通指针。

  Windows NT 32 位平台下,指针的长度(占用内存的大小)为 4 字节,故 sizeof( str )

、sizeof ( p ) 都为 4。

  试题 3:

写一个“标准”宏 MIN,这个宏输入两个参数并返回较小的一个。

另外,当你

写下面的代码时会发生什么事

least = MIN(*p++, b);

  解答:

#define MIN(A,B) ((A) <= (B) ?

 (A) :

 (B))

  MIN(*p++, b)会产生宏的副作用

  剖析:

  这个面试题主要考查面试者对宏定义的使用,宏定义可以实现类似于函数的功能,但

是它终归不是函数,而宏定义中括弧中的“参数”也不是真的参数,在宏展开的时候对“参数”

进行的是一对一的替换。

  程序员对宏定义的使用要非常小心,特别要注意两个问题:

  

(1)谨慎地将宏定义中的“参数”和整个宏用用括弧括起来。

所以,严格地讲,下述解

答:

#define MIN(A,B) (A) <= (B) ?

 (A) :

 (B)

#define MIN(A,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));

  这个解答在宏定义的后面加“;”,显示编写者对宏的概念模糊不清,只能被无情地判 0

分并被面试官淘汰。

  试题 4:

为什么标准头文件都有类似以下的结构?

#ifndef __INCvxWorksh

#define __INCvxWorksh

#ifdef __cplusplus

extern "C" {

#endif

/*...*/

#ifdef __cplusplus

}

#endif

#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++就是考这种机制来实现函数重载的。

  为了实现 C 和 C++的混合编程,C++提供了 C 连接交换指定符号 extern"C"来解决名

字匹配问题,函数声明前加上 extern"C"后,则编译器就会按照 C 语言的方式将该函数编

译为_foo,这样 C 语言中就可以调用 C++的函数了。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 试题 5:

编写一个函数,作用是把一个 char 组成的字符串循环右移 n 个。

比如原来是

“abcdefghi”如果 n=2,移位后应该是“hiabcdefgh”

  函数头是这样的:

//pStr 是指向以'\0'结尾的字符串的指针

//steps 是要求移动的 n

void LoopMove ( char * pStr, int steps )

{

 //请填充...

}

  解答:

  正确解答 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, 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) memset

 

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 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.

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 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 a link to his document on exception

safetyandthestandardlibrary:

http:

//www.arkestra.demon.co.uk/errors_cpp.html#acquire_resources_in_constructors

 

--------------------------------------------------------------------------------

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 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 operator 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 programmer'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 test (i.e. 0 or 1).

 

What are the 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2