数模论坛

 找回密码
 注-册-帐-号
搜索
热搜: 活动 交友 discuz
查看: 12463|回复: 5

苦练编程内功(c/c++程序员面试题一)

[复制链接]
发表于 2006-4-25 21:29:20 | 显示全部楼层 |阅读模式
<>前言:</P>
<>最近在找工作,也经历了一些大大小小的笔试面试。这些题目各有花样,无数陷阱。许多题目难度不小,从编译器优化到设计模式,从自定义库函数到重载多态,覆盖了c/c++的各个方面。通常设计得好的面试题能反映应试者的编程功底,思维能力,也能反映公司对招聘的人才的素质要求。</P>
<>所谓光读书不实验不行,光实验不思考不行,我觉得这些题目能很大程度上提高编程水平,于是决定和大家一块研究学习,每周推出一期c/c++程序员面试题,在第二周给出答案。这些题目中有一些经典c/c++书籍里的,有微软,百度之类大公司的笔试题,有一些是从网上收集汇总的,有一些是自己在找工作和实习过程中遇到过的,旨在激发大家学习和讨论的热情,共同提高。</P>
<P>或许有一些题看起来容易,但请不要忽视它,许多问题是你平时不太关注但实际上非常重要的。有一些难题我也无法给出标准答案,要靠大家一块去总结。如果有谁愿意共享自己觉得不错的题,请一块贴出来。</P>
<P>Now, here we go!</P>
<P>注:本贴起发在中国数学建模网(<a href="http://www.shumo.com" target="_blank" >www.shumo.com</A>)上,如要转载请注明出处。</P>
 楼主| 发表于 2006-4-25 21:35:03 | 显示全部楼层
< ><B>一、请填写<FONT face="Times New Roman">BOOL , float, </FONT></B><B>指针变量<FONT face="Times New Roman"> </FONT></B><B>与“零值”比较的<FONT face="Times New Roman"> if </FONT></B><B>语句。(<FONT face="Times New Roman">10</FONT></B><B>分)<o:p></o:p></B></P>
< >提示:这里“零值”可以是<FONT face="Times New Roman">0, 0.0 , FALSE</FONT>或者“空指针”。例如<FONT face="Times New Roman"> int </FONT>变量<FONT face="Times New Roman"> n </FONT>与“零值”比较的<FONT face="Times New Roman"> if </FONT>语句为:</P>
< ><FONT face="Times New Roman">    if ( n == 0 )</FONT></P>
<P ><FONT face="Times New Roman">    if ( n != 0 )</FONT></P>
<P >以此类推。</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR>
<TD  vAlign=top width=581>
<P >请写出<FONT face="Times New Roman"> BOOL  flag </FONT>与“零值”比较的<FONT face="Times New Roman"> if </FONT>语句:</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P></TD></TR>
<TR>
<TD  vAlign=top width=581>
<P >请写出<FONT face="Times New Roman"> float  x </FONT>与“零值”比较的<FONT face="Times New Roman"> if </FONT>语句:</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P></TD></TR>
<TR >
<TD  vAlign=top width=581>
<P >请写出<FONT face="Times New Roman"> char  *p </FONT>与“零值”比较的<FONT face="Times New Roman"> if </FONT>语句:</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P></TD></TR></TABLE>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><B>二、以下为<FONT face="Times New Roman">Windows NT</FONT></B><B>下的<FONT face="Times New Roman">32</FONT></B><B>位<FONT face="Times New Roman">C++</FONT></B><B>程序,请计算<FONT face="Times New Roman">sizeof</FONT></B><B>的值(<FONT face="Times New Roman">10</FONT></B><B>分)<o:p></o:p></B></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR >
<TD  vAlign=top width=284 rowSpan=2>
<P ><FONT face="Times New Roman">       char  str[] = “Hello” ;</FONT></P>
<P ><FONT face="Times New Roman">       char   *p = str ;</FONT></P>
<P ><FONT face="Times New Roman">int     n = 10;</FONT></P>
<P >请计算</P>
<P ><FONT face="Times New Roman">sizeof (str ) =        </FONT></P>
<P ><FONT face="Times New Roman">         </FONT></P>
<P ><FONT face="Times New Roman">sizeof ( p ) =        </FONT></P>
<P ><FONT face="Times New Roman">          </FONT></P>
<P ><FONT face="Times New Roman">sizeof ( n ) =</FONT></P></TD>
<TD  vAlign=top width=284>
<P ><FONT face="Times New Roman">void Func ( char str[100])</FONT></P>
<P ><FONT face="Times New Roman">{</FONT></P>
<P >请计算</P>
<P ><FONT face="Times New Roman"> sizeof( str ) =    </FONT></P>
<P ><FONT face="Times New Roman">}</FONT></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P></TD></TR>
<TR >
<TD  vAlign=top width=284>
<P ><FONT face="Times New Roman">void *p = malloc( 100 );</FONT></P>
<P >请计算</P>
<P ><FONT face="Times New Roman">sizeof ( p ) =</FONT></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P></TD></TR></TABLE>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><B>三、简答题(<FONT face="Times New Roman">25</FONT></B><B>分)<o:p></o:p></B></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">1</FONT>、头文件中的<FONT face="Times New Roman"> ifndef/define/endif </FONT>干什么用?</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">2</FONT>、<FONT face="Times New Roman">#include  &lt;filename.h&gt;   </FONT>和<FONT face="Times New Roman">  #include  “filename.h” </FONT>有什么区别?</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">3</FONT>、<FONT face="Times New Roman">const </FONT>有什么用途?(请至少说明两种)</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">4</FONT>、在<FONT face="Times New Roman">C++ </FONT>程序中调用被<FONT face="Times New Roman"> C</FONT>编译器编译后的函数,为什么要加<FONT face="Times New Roman"> extern “C”</FONT>声明?</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><FONT face="Times New Roman">5</FONT>、请简述以下两个<FONT face="Times New Roman">for</FONT>循环的优缺点</P>
<P ><o:p> </o:p></P>
<P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR >
<TD  vAlign=top width=268>
<P >// 第一个<o:p></o:p></P>
<P >for (i=0; i&lt;N; i++)<o:p></o:p></P>
<P >{<o:p></o:p></P>
<P >if (condition)<o:p></o:p></P>
<P >    DoSomething();<o:p></o:p></P>
<P >else<o:p></o:p></P>
<P >    DoOtherthing();<o:p></o:p></P>
<P >}<o:p></o:p></P></TD>
<TD  vAlign=top width=283>
<P >// 第二个<o:p></o:p></P>
<P >if (condition)<o:p></o:p></P>
<P >{<o:p></o:p></P>
<P >for (i=0; i&lt;N; i++)<o:p></o:p></P>
<P >    DoSomething();<o:p></o:p></P>
<P >}<o:p></o:p></P>
<P >else<o:p></o:p></P>
<P >{<o:p></o:p></P>
<P >    for (i=0; i&lt;N; i++)<o:p></o:p></P>
<P >    DoOtherthing();<o:p></o:p></P>
<P >}<o:p></o:p></P></TD></TR>
<TR >
<TD  vAlign=top width=268>
<P >优点:<o:p></o:p></P>
<P ><o:p> </o:p></P>
<P ><o:p> </o:p></P>
<P >缺点:<o:p></o:p></P>
<P ><o:p> </o:p></P>
<P ><o:p> </o:p></P>
<P ><o:p> </o:p></P></TD>
<TD  vAlign=top width=283>
<P >优点:<o:p></o:p></P>
<P ><o:p> </o:p></P>
<P ><o:p> </o:p></P>
<P >缺点:<o:p></o:p></P>
<P ><o:p> </o:p></P>
<P ><o:p> </o:p></P></TD></TR></TABLE>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><B>四、有关内存的思考题(<FONT face="Times New Roman">20</FONT></B><B>分)<o:p></o:p></B></P>
<P ><B><o:p><FONT face="Times New Roman"> </FONT></o:p></B></P>
<P>
<TABLE  cellSpacing=0 cellPadding=0 border=1>

<TR>
<TD  vAlign=top width=291>
<P >void GetMemory(char *p)<o:p></o:p></P>
<P >{<o:p></o:p></P>
<P >p = (char *)malloc(100);<o:p></o:p></P>
<P >}<o:p></o:p></P>
<P ><FONT face="Times New Roman">void Test(void) </FONT></P>
<P ><FONT face="Times New Roman">{</FONT></P>
<P >char *str = NULL;<o:p></o:p></P>
<P >GetMemory(str);   <o:p></o:p></P>
<P >strcpy(str, "hello world");<o:p></o:p></P>
<P >printf(str);<o:p></o:p></P>
<P >}<o:p></o:p></P>
<P ><o:p> </o:p></P>
<P >请问运行Test函数会有什么样的结果?<o:p></o:p></P>
<P >答:</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P></TD>
<TD  vAlign=top width=291>
<P >char *GetMemory(void)<o:p></o:p></P>
<P >{   <o:p></o:p></P>
<P >char p[] = "hello world";<o:p></o:p></P>
<P >return p;<o:p></o:p></P>
<P >}<o:p></o:p></P>
<P >void Test(void)<o:p></o:p></P>
<P >{<o:p></o:p></P>
<P >char *str = NULL;<o:p></o:p></P>
<P >str = GetMemory();    <o:p></o:p></P>
<P >printf(str);<o:p></o:p></P>
<P >}<o:p></o:p></P>
<P ><o:p> </o:p></P>
<P >请问运行Test函数会有什么样的结果?<o:p></o:p></P>
<P >答:</P></TD></TR>
<TR >
<TD  vAlign=top width=291>
<P >Void GetMemory2(char **p, int num)<o:p></o:p></P>
<P >{<o:p></o:p></P>
<P >*p = (char *)malloc(num);<o:p></o:p></P>
<P >}</P>
<P >void Test(void)<o:p></o:p></P>
<P >{<o:p></o:p></P>
<P >char *str = NULL;<o:p></o:p></P>
<P >GetMemory(&amp;str, 100);<o:p></o:p></P>
<P >strcpy(str, "hello");   <o:p></o:p></P>
<P >printf(str);    <o:p></o:p></P>
<P >}</P>
<P >请问运行Test函数会有什么样的结果?<o:p></o:p></P>
<P >答:</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P></TD>
<TD  vAlign=top width=291>
<P >void Test(void)<o:p></o:p></P>
<P >{<o:p></o:p></P>
<P >char *str = (char *) malloc(100);<o:p></o:p></P>
<P >    strcpy(str, <FONT face="Times New Roman">“</FONT>hello<FONT face="Times New Roman">”</FONT>);<o:p></o:p></P>
<P >    free(str);      <o:p></o:p></P>
<P >    if(str != NULL)<o:p></o:p></P>
<P >    {<o:p></o:p></P>
<P >      strcpy(str, <FONT face="Times New Roman">“</FONT>world<FONT face="Times New Roman">”</FONT>); <o:p></o:p></P>
<P >printf(str);<o:p></o:p></P>
<P >}<o:p></o:p></P>
<P ><FONT face="Times New Roman">}</FONT></P>
<P >请问运行Test函数会有什么样的结果?<o:p></o:p></P>
<P >答:</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P></TD></TR></TABLE>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><B>五、编写<FONT face="Times New Roman">strcpy</FONT></B><B>函数(<FONT face="Times New Roman">10</FONT></B><B>分)<o:p></o:p></B></P>
<P >已知<FONT face="Times New Roman">strcpy</FONT>函数的原型是</P>
<P ><FONT face="Times New Roman">       char *strcpy(char *strDest, const char *strSrc);</FONT></P>
<P ><FONT face="Times New Roman">       </FONT>其中<FONT face="Times New Roman">strDest</FONT>是目的字符串,<FONT face="Times New Roman">strSrc</FONT>是源字符串。</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >(<FONT face="Times New Roman">1</FONT>)不调用<FONT face="Times New Roman">C++/C</FONT>的字符串库函数,请编写函数<FONT face="Times New Roman"> strcpy</FONT></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P >(<FONT face="Times New Roman">2</FONT>)<FONT face="Times New Roman">strcpy</FONT>能把<FONT face="Times New Roman">strSrc</FONT>的内容复制到<FONT face="Times New Roman">strDest</FONT>,为什么还要<FONT face="Times New Roman">char * </FONT>类型的返回值?</P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P ><B>六、编写类<FONT face="Times New Roman">String</FONT></B><B>的构造函数、析构函数和赋值函数(<FONT face="Times New Roman">25</FONT></B><B>分)<o:p></o:p></B></P>
<P >已知类<FONT face="Times New Roman">String</FONT>的原型为:</P>
<P >    class String<o:p></o:p></P>
<P >    {<o:p></o:p></P>
<P >      public:<o:p></o:p></P>
<P >        String(const char *str = NULL); // 普通构造函数<o:p></o:p></P>
<P >        String(const String &amp;other);        // 拷贝构造函数<o:p></o:p></P>
<P >        ~ String(void);                     // 析构函数<o:p></o:p></P>
<P >        String &amp; operate =(const String &amp;other);    // 赋值函数<o:p></o:p></P>
<P >      private:<o:p></o:p></P>
<P >        char    *m_data;                // 用于保存字符串<o:p></o:p></P>
<P >    };<o:p></o:p></P>
<P ><FONT face="Times New Roman">       </FONT>请编写String的上述4个函数。</P>
<p>
 楼主| 发表于 2006-4-25 21:39:06 | 显示全部楼层
以上题目摘自林锐博士的《高质量C++编程指南》。大家可以去看这本书作为参考。[em17]
发表于 2006-9-27 22:53:40 | 显示全部楼层
我说怎么看的这么眼熟了
发表于 2006-9-27 22:54:16 | 显示全部楼层
<p>还有程序员的程序标准怎么没有贴上来</p><p></p>
发表于 2007-4-23 09:55:26 | 显示全部楼层
<p>void Shiyan1()<br/>{<br/>爄nt i=1;<br/>燽ool flag=false;<br/>爁loat f=0.0;</p><p>爄f(i==0) cout&lt;&lt;"i=0"&lt;&lt;endl;<br/>爄f(i!=0) cout&lt;&lt;"i!=0"&lt;&lt;endl;</p><p>爄f(flag==0) cout&lt;&lt;"flag=False"&lt;&lt;endl;<br/>爄f(flag!=0) cout&lt;&lt;"flag=True"&lt;&lt;endl;</p><p>爄f(f==0) cout&lt;&lt;"f=0.0"&lt;&lt;endl;<br/>爄f(f!=0) cout&lt;&lt;"f!=0.0"&lt;&lt;endl;</p><p>}</p>
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

小黑屋|手机版|Archiver|数学建模网 ( 湘ICP备11011602号 )

GMT+8, 2024-4-16 23:23 , Processed in 0.052432 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表