夏普程序員筆試題和面試題答案目

大風(fēng)車考試網(wǎng)

  試題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個字節(jié)才能存放下(包括末尾的’\0’),而string只有10個字節(jié)的空間,strcpy會導(dǎo)致數(shù)組越界;

  對試題2,如果面試者指出字符數(shù)組str1不能在數(shù)組內(nèi)結(jié)束可以給3分;如果面試者指出strcpy(string, str1)調(diào)用使得從str1內(nèi)存起復(fù)制到string內(nèi)存起所復(fù)制的字節(jié)數(shù)具有不確定性可以給7分,在此基礎(chǔ)上指出庫函數(shù)strcpy工作方式的給10分;

  對試題3,if(strlen(str1) <= 10)應(yīng)改為if(strlen(str1) < 10),因為strlen的結(jié)果未統(tǒng)計’\0’所占用的1個字節(jié)。

  考查對基本功的掌握:

  (1)字符串以’\0’結(jié)尾;

  (2)對數(shù)組越界把握的敏感度;

  (3)庫函數(shù)strcpy的工作方式,

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

  試題4:

  void GetMemory( char *p )

  {

  p = (char *) malloc( 100 );

  }

  void Test( void )

  {

  char *str = NULL;

  GetMemory( str );

  strcpy( str, "hello world" );

  printf( str );

  }

  解答:

  試題4傳入中GetMemory( char *p )函數(shù)的形參為字符串指針,在函數(shù)內(nèi)部修改形參并不能真正的改變傳入形參的值,執(zhí)行完

  char *str = NULL;

  GetMemory( str );

  后的str仍然為NULL;

  試題5:

  char *GetMemory( void )

  {

  char p[] = "hello world";

  return p;

  }

  void Test( void )

  {

  char *str = NULL;

  str = GetMemory();

  printf( str );

  }

  試題5中

  char p[] = "hello world";

  return p;

  的p[]數(shù)組為函數(shù)內(nèi)的局部自動變量,在函數(shù)返回后,內(nèi)存已經(jīng)被釋放。這是許多程序員常犯的錯誤,其根源在于不理解變量的生存期。

  試題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 );

  }

  試題6的GetMemory避免了試題4的問題,傳入GetMemory的參數(shù)為字符串指針的指針,但是在GetMemory中執(zhí)行申請內(nèi)存及賦值語句

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

  后未判斷內(nèi)存是否申請成功,應(yīng)加上:

  if ( *p == NULL )

  {

  ...//進(jìn)行申請內(nèi)存失敗處理

  }

  試題7:

  void Test( void )

  {

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

  strcpy( str, "hello" );

  free( str );

  ... //省略的其它語句

  }

  試題7存在與試題6同樣的問題,在執(zhí)行

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

  后未進(jìn)行內(nèi)存是否申請成功的判斷;另外,在free(str)后未置str為空,導(dǎo)致可能變成一個“野”指針,應(yīng)加上:

  str = NULL;

  試題6的Test函數(shù)中也未對malloc的內(nèi)存進(jìn)行釋放。

  剖析:

  試題4~7考查面試者對內(nèi)存操作的理解程度,基本功扎實的面試者一般都能正確的回答其中50~60的錯誤。但是要完全解答正確,卻也絕非易事。

  對內(nèi)存操作的考查主要集中在:

  (1)指針的理解;

  (2)變量的生存期及作用范圍;

  (3)良好的動態(tài)內(nèi)存申請和釋放習(xí)慣。

  再看看下面的一段程序有什么錯誤:

  swap( int* p1,int* p2 )

  {

  int *p;

  *p = *p1;

  *p1 = *p2;

  *p2 = *p;

  }

  在swap函數(shù)中,p是一個“野”指針,有可能指向系統(tǒng)區(qū),導(dǎo)致程序運(yùn)行的崩潰。在VC++中DEBUG運(yùn)行時提示錯誤“Access Violation”。該程序應(yīng)該改為:

  swap( int* p1,int* p2 )

  {

  int p;

  p = *p1;

  *p1 = *p2;

  *p2 = p;

  }

  預(yù)處理器(Preprocessor)

  1. 用預(yù)處理指令#define 聲明一個常數(shù),用以表明1年中有多少秒(忽略閏年問題)

  #define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL

  2. 寫一個“標(biāo)準(zhǔn)”宏MIN,這個宏輸入兩個參數(shù)并返回較小的一個。

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

  • 相關(guān)文章