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

加入VIP,免费下载
 

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

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

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(程序员面试试题深刻剖析.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

程序员面试试题深刻剖析.docx

1、程序员面试试题深刻剖析程序员面试试题面试例题1:如果鸟是可以飞的,那么鸵鸟是鸟么?鸵鸟如何继承鸟类?美国某著名分析软件公司2005年面试题解析:如果所有鸟都能飞,那鸵鸟就不是鸟!回答这种问题时,不要相信自己的直觉!将直觉和合适的继承联系起来还需要一段时间。根据题干可以得知:鸟是可以飞的。也就是说,当鸟飞行时,它的高度是大于0的。鸵鸟是鸟类(生物学上)的一种。但它的飞行高度为0(鸵鸟不能飞)。不要把可替代性和子集相混淆。即使鸵鸟集是鸟集的一个子集(每个驼鸟集都在鸟集内),但并不意味着鸵鸟的行为能够代替鸟的行为。可替代性与行为有关,与子集没有关系。当评价一个潜在的继承关系时,重要的因素是可替代的行

2、为,而不是子集。答案:如果一定要让鸵鸟来继承鸟类,可以采取组合的办法,把鸟类中的可以被鸵鸟继承的函数挑选出来,这样鸵鸟就不是“a kind of”鸟了,而是“has some kind of”鸟的属性而已。代码如下:#include#includeusing namespace std;class birdpublic:void eat();void sleep();void fly();class ostrichpublic:bird eat()coutostrich eat;bird sleep()coutostrich sleep;int main()ostrich xiaoq;xiao

3、q.eat();xiaoq.sleep();return 0; 面试例题2:Find the defects in each of the following programs, and explain why it is incorrect.(找出下面程序的错误,并解释它为什么是错的。)中国台湾某著名杀毒软件公司2005年面试题#include using namespace std;class Base public:int val;Base() val=1;class Derive: Base public:int val;Derive(int i) val=Base:val+i; ;i

4、nt main(int, char*, char*) Derive d(10);coutd.Base:valendld.valendl;return 0; 答案:把class Derive: Base改成class Derive:public Base。解析:这是个类继承问题。如果不指定public,C+默认的是私有继承。私有继承是无法继承并使用父类函数中的公有变量的。扩展知识(组合)若在逻辑上A是B的“一部分”(a part of),则不允许B从A派生,而是要用A和其他东西组合出B。例如眼(Eye)、鼻(Nose)、口(Mouth)、耳(Ear)是头(Head)的一部分,所以类Head应该由

5、类Eye、Nose、Mouth、Ear组合而成,而不是派生而成。程序如下:class Eyepublic:void Look(void);class Nosepublic:void Smell(void);class Mouthpublic:void Eat(void);class Earpublic:void Listen(void);class Headpublic:void Look(void) m_eye.Look(); void Smell(void) m_nose.Smell(); void Eat(void) m_mouth.Eat(); void Listen(void) m_

6、ear.Listen(); private:Eye m_eye;Nose m_nose;Mouth m_mouth;Ear m_ear; Head由Eye、Nose、Mouth、Ear组合而成。如果允许Head从Eye、Nose、Mouth、Ear派生而成,那么Head将自动具有Look、Smell、Eat、Listen这些功能。程序十分简短并且运行正确,但是下面这种设计方法却是不对的。class Head : public Eye, public Nose, public Mouth, public Ear;面试例题3:Find the defects in each of the foll

7、owing programs, and explain why it is incorrect.(找出下面程序的错误,并解释它为什么是错的。)德国某著名软件咨询企业2005年面试题class baseprivate: int i;public: base(int x)i=x;class derived: public baseprivate: int i;public: derived(int x, int y) i=x;void printTotal() int total = i+base:i; 解析:要在子类中设定初始成员变量,把derived(int x, int y)改成derive

8、d(int x, int y) : base(x)。答案:代码如下:class baseprotected: /这里的访问属性需要改变int i;public:base(int x)i=x;class derived: public baseprivate:int i;public:derived(int x, int y) : base(x) /以前没有初始化基类的成员变量i=y;void printTotal()int total = i+base:i;32.请说出const与#define 相比,有何优点?答案:1) const 常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行

9、类型安全检查。而对后者只进行字符替换,没有类型安全检查,并且在字符替换可能会产生意料不到的错误。2) 有些集成化的调试工具可以对const 常量进行调试,但是不能对宏常量进行调试。33.简述数组与指针的区别?数组要么在静态存储区被创建(如全局数组),要么在栈上被创建。指针可以随时指向任意类型的内存块。(1)修改内容上的差别char a = “hello”;a0 = X;char *p = “world”; / 注意p 指向常量字符串p0 = X; / 编译器不能发现该错误,运行时错误(2) 用运算符sizeof 可以计算出数组的容量(字节数)。sizeof(p),p 为指针得到的是一个指针变量

10、的字节数,而不是p 所指的内存容量。C+/C 语言没有办法知道指针所指的内存容量,除非在申请内存时记住它。注意当数组作为函数的参数进行传递时,该数组自动退化为同类型的指针。char a = hello world;char *p = a;cout sizeof(a) endl; / 12 字节cout sizeof(p) endl; / 4 字节计算数组和指针的内存容量void Func(char a100)cout sizeof(a) endl; / 4 字节而不是100 字节34.类成员函数的重载、覆盖和隐藏区别?答案:a.成员函数被重载的特征:(1)相同的范围(在同一个类中);(2)函数

11、名字相同;(3)参数不同;(4)virtual 关键字可有可无。b.覆盖是指派生类函数覆盖基类函数,特征是:(1)不同的范围(分别位于派生类与基类);(2)函数名字相同;(3)参数相同;(4)基类函数必须有virtual 关键字。c.“隐藏”是指派生类的函数屏蔽了与其同名的基类函数,规则如下:(1)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆)。(2)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual 关键字。此时,基类的函数被隐藏(注意别与覆盖混淆)35. There are two i

12、nt variables: a and b, dont use “if”, “? :”, “switch”or other judgement statements, find out the biggest one of the two numbers.答案:( ( a + b ) + abs( a - b ) ) / 236. 如何打印出当前源文件的文件名以及源文件的当前行号?答案:cout _FILE_ ;cout_LINE_ ;_FILE_和_LINE_是系统预定义宏,这种宏并不是在某个文件中定义的,而是由编译器定义的。37. main 主函数执行完毕后,是否可能会再执行一段代码,给出

13、说明?答案:可以,可以用_onexit 注册一个函数,它会在main 之后执行int fn1(void), fn2(void), fn3(void), fn4 (void);void main( void )String str(zhanglin);_onexit( fn1 );_onexit( fn2 );_onexit( fn3 );_onexit( fn4 );printf( This is executed first.n );int fn1()printf( next.n );return 0;int fn2()printf( executed );return 0;int fn3(

14、)printf( is );return 0;int fn4()printf( This );return 0;The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions pas

15、sed to _onexit cannot take parameters.38. 如何判断一段程序是由C 编译程序还是由C+编译程序编译的?答案:#ifdef _cpluspluscoutc+;#elsecoutc;#endif39.文件中有一组整数,要求排序后输出到另一个文件中答案:i ncludei ncludeusing namespace std;void Order(vector& data) /bubble sortint count = data.size() ;int tag = false ; / 设置是否需要继续冒泡的标志位for ( int i = 0 ; i coun

16、t ; i+)for ( int j = 0 ; j dataj+1)tag = true ;int temp = dataj ;dataj = dataj+1 ;dataj+1 = temp ;if ( !tag )break ;void main( void )vectordata;ifstream in(c:data.txt);if ( !in)couttemp;data.push_back(temp);in.close(); /关闭输入文件流Order(data);ofstream out(c:result.txt);if ( !out)coutfile error!;exit(1);

17、for ( i = 0 ; i data.size() ; i+)outnext = NULL )return head;Node *p1 = head ;Node *p2 = p1-next ;Node *p3 = p2-next ;p1-next = NULL ;while ( p3 != NULL )p2-next = p1 ;p1 = p2 ;p2 = p3 ;p3 = p3-next ;p2-next = p1 ;head = p2 ;return head ;(2)已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)Node

18、 * Merge(Node *head1 , Node *head2)if ( head1 = NULL)return head2 ;if ( head2 = NULL)return head1 ;Node *head = NULL ;Node *p1 = NULL;Node *p2 = NULL;if ( head1-data data )head = head1 ;p1 = head1-next;p2 = head2 ;elsehead = head2 ;p2 = head2-next ;p1 = head1 ;Node *pcurrent = head ;while ( p1 != NU

19、LL & p2 != NULL)if ( p1-data data )pcurrent-next = p1 ;pcurrent = p1 ;p1 = p1-next ;elsepcurrent-next = p2 ;pcurrent = p2 ;p2 = p2-next ;if ( p1 != NULL )pcurrent-next = p1 ;if ( p2 != NULL )pcurrent-next = p2 ;return head ;(3)已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 (Autodesk)答案:Node *

20、MergeRecursive(Node *head1 , Node *head2)if ( head1 = NULL )return head2 ;if ( head2 = NULL)return head1 ;Node *head = NULL ;if ( head1-data data )head = head1 ;head-next = MergeRecursive(head1-next,head2);elsehead = head2 ;head-next = MergeRecursive(head1,head2-next);return head ;C/C+ C/C+ 笔试、面试题目大

21、汇总41-45 bioeconomy 发表于 2006-3-22 20:28:00 41. 分析一下这段程序的输出 (Autodesk)class Bpublic:B()coutdefault constructorB()coutdestructed instance of B)coutconstructed by parameter data private:int data;B Play( B b) return b ;(1) results:int main(int argc, char* argv) constructed by parameter 5 destructed B(5)形

22、参析构B t1 = Play(5); B t2 = Play(t1); destructed t1形参析构return 0; destructed t2注意顺序! destructed t1(2) results:int main(int argc, char* argv) constructed by parameter 5 destructed B(5)形参析构B t1 = Play(5); B t2 = Play(10); constructed by parameter 10return 0; destructed B(10)形参析构 destructed t2注意顺序! destru

23、cted t142. 写一个函数找出一个整数数组中,第二大的数 (microsoft)答案:const int MINNUMBER = -32767 ;int find_sec_max( int data , int count)int maxnumber = data0 ;int sec_max = MINNUMBER ;for ( int i = 1 ; i maxnumber )sec_max = maxnumber ;maxnumber = datai ;elseif ( datai sec_max )sec_max = datai ;return sec_max ;43. 写一个在一

24、个字符串(n)中寻找一个子串(m)第一个位置的函数。KMP算法效率最好,时间复杂度是(n+m),详见:44. 多重继承的内存分配问题:比如有class A : public class B, public class C 那么A的内存结构大致是怎么样的?这个是compiler-dependent的, 不同的实现其细节可能不同。如果不考虑有虚函数、虚继承的话就相当简单;否则的话,相当复杂。可以参考深入探索C+对象模型,或者:45. 如何判断一个单链表是有环的?(注意不能用标志位,最多只能用两个额外指针)struct node char val; node* next;bool check(const node* head) /return false : 无环;true: 有环一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然):bool check(const node* head)if(head=NULL) return false;node *low=head, *fast=head-next;while(fast!=NULL & fast-next!=NULL)low=low-next;fast=fast-next-next;if(low=fast) return true;return false;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1