完全回答的面试问题 [英] Fully Answered Interview questions

查看:58
本文介绍了完全回答的面试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完整回答c和c ++中的面试问题来自
http://www.faiqs。 com

Fully answered interview questions in c and c++ from
http://www.faiqs.com

推荐答案

Syam写道:
Syam wrote:

完全回答了c和c ++中的面试问题,来自
http://www.faiqs.com



答案(至少对于前几个问题)是正确的。

他们不是我要问的问题无论如何,在采访中。


对于实习问题#1

printf的输出是什么(?%d?)




1.%d有助于读取给定变量的整数数据类型

2.当我们写(?%d?,X)编译器时打印x

的值假设在主要的
3.但是没有(?%d?)之后没有任何内容所以输出将是垃圾

4. printf是一个过载函数doe snt检查

arg列表的一致性?分段错误

#1 Printf读取NOTHING。

#2x假设在主要是错的,首先,C ++是依赖于案例的。第二,

X打印的内容取决于调用范围中定义的内容。即使

如果从main()调用printf,也可以在其他地方声明X.


#3可能但不是正确的答案。 />
#4开始听起来是正确的,但printf不是超载

functino。


正确的答案是:


1. Printf没有(不能)检查参数的一致性。

你必须传递正确的数字和类型参数来匹配

格式字符串。


2.如果不这样做会产生未定义的行为(打印垃圾和

分段错误只是无界的一部分可能性

这里)。

The answers (at least for the first few questions) are in correct.
They aren''t the questions I would ask during an interview anyhow.

For instannce question #1
What is the output of printf(?%d?)
?

1. %d helps to read integer data type of a given variable
2. when we write (?%d?, X) compiler will print the value of x
assumed in the main
3. but nothing after (?%d?) so the output will be garbage
4. printf is an overload function doesnt check consistency of the
arg list ? segmentation fault
#1 Printf reads NOTHING.
#2 "x assumed in main" is wrong, first, C++ is case dependent. Second,
what X gets printed depends what is defined in the calling scope. Even
if the printf is called form main(), the X may be declared elsewhere.

#3 is likely but not the correct answer.
#4 is beginning to sound correct, but printf is NOT "an overload
functino".

The correct answer is:

1. Printf does not (can not) check the consistency of the arguments.
You must pass the correct number and type arguments to match the
format string.

2. Failure to do so yields undefined behavior (printing garbage and
segmentation faults are just part of the unbounded possibilities
here).


(好吧,让我们再试一次。我没看到这是comp .lang.c ++。审核。如果

这最终会发布两次,这是因为几分钟之后这个

响应仍然没有出现)

Syam写道:
(ok, let''s try again. I didn''t see this was comp.lang.c++.moderated. If
this ends up posting twice, it''s because several minutes later this
response has still not appeared)
Syam wrote:

完全回答用c / c ++中的红色访谈问题来自
http://www.faiqs.com



许多答案都不准确,或者他们没有回答

问题。


1.什么是printf的输出("%d" );


Ron Natalie给出了正确的答案:因为函数将看起来是另一个不存在的参数的
,它将导致未定义

行为。它可能输出一个随机数或者可能会崩溃(seg

fault)。不太可能做除了这两个以外的任何事情,虽然技术上它可能是b $ b。


2.如果我说删除这个会发生什么? ?


给出的答案是一堆废话。有些陈述是明白的,有些陈述是假的,其他人只是转移而不回答这个问题。


无论如何,它应该导致对象被破坏,就像你有来自外面的指针上有一个名为delete的
。为了使它正常工作,

必须是这样的:


- 必须使用new创建对象。也就是说,常规新的。

new(nothrow)也可以工作,但没有新的位置,没有新的[]。


- 如果这是一个基类并且真实的对象来自于此,

析构函数必须是虚拟的。


3. C结构和C ++结构之间的区别。


我会假设它们是指C struct和C ++ struct,因为struct是

关键字而不是结构。问题并没有问到

a C ++类和C ++结构之间的区别。


真正的答案是C ++结构实际上是一个带有<的C ++类br />
默认的公共访问和继承。与C结构不同,您可以拥有

成员函数,如果需要,可以使用私有成员,并从基类派生

(以及从中派生类)。 br />

使用一个符号也有区别,即你可以简单地用它的类型引用它而没有struct前缀或类型定义。


这个答案是错误的:

C中结构的成员默认是公共的,在C ++私有中。这不是因为C没有私有概念因为

从技术上讲它是真的,即使它是

无法改变它。但是在C ++中它们默认是公共的,

因此声明是假的。


这个答案是偏离主题的:

工会结构,除了他们的记忆...... (继续给工会提供

的详细信息)。;


4.赋值运算符和复制构造函数之间的区别。


没有给出的答案是完整和准确的。这很接近。

复制构造函数创建一个新对象,赋值运算符必须与对象中的现有数据交易




更好的答案是复制构造函数创建一个新对象,克隆现有的一个对象,分配修改现有对象为
另一个。您可以添加尽管可能会使副本重载

构造函数和赋值,这样两个对象实际上并不是
匹配,这样做会不常见并且会反对正常的性质

这些应该做什么。


这个答案不太正确:


复制构造函数总是创建一个新对象,赋值永远不会。

因为赋值可以,它通常会创建一个临时对象

异常安全应该这个动作失败。


5.重载和覆盖之间的区别。

让我说这是一个可怕的问题,因为它基于一个

知道事物的技术名称。


无论如何,重载使用相同的名称,但使用不同的参数

list。覆盖使用相同的参数列表,但原始

函数已在基类中声明为虚拟。在编译时解决了重载

,通常在运行时覆盖。在一组代码可以根据不同情况调用任一版本

时,超载可以特别有用,例如模板

参数。


6.虚拟

7.动态绑定。


这些都不是问题。或者它是否意味着它们是什么。在

这种情况​​下,他们也可以一起被问到,或者询问什么是

多态性。也许可以与上面的

问题结合起来。


无论如何,原则是允许代码按照它的方式调用方法

而不是它是如何做到的,实际的函数被称为

在运行时被解析。保存指针或

引用基类的相同代码可以在不同的b / b $ b $时调用不同的重载,具体取决于它具有的实际对象及其类型< br $> b $ b对象。


8.解释需要虚拟析构函数:


第2和第3和第5个答案几乎回答这个问题。


9.规则3.

第一个答案最好的答案,尽管术语用户定义 ;是

从未实际使用过,但可能是隐含的。一般来说,如果它需要
中的任何一个,它还需要其他两个。


10.当有人说删除时,为什么需要虚拟析构函数?使用

a Base ptr指向@派生对象?


因为标准说你做了。

Many of those answers are inaccurate or they do not answer the
question.

1. What is the output of printf( "%d" );

The correct answer is given by Ron Natalie: as the function will look
for another argument that doesn''t exist, it will cause undefined
behaviour. It might output a random number or it might crash (seg
fault). Unlikely to do anything other than these two though technically
it could.

2. What will happen if I say "delete this" ?

The answer given is a load of nonsense. Some of the statements are
plain false, others simply divert and do not answer the question.

Anyway, it should cause the object to be destructed as though you had
called delete on the pointer from outside. For it to work correctly the
following must be the case:

- The object must have been created with new. That is, regular new.
new(nothrow) would also work but no placement new, no new[].

- If this is a base class and the real object is derived from this, the
destructor must be virtual.

3. Difference between C structure and C++ structure.

Well I will assume they mean C struct and C++ struct as struct is the
keyword not structure. The question does not ask the difference between
a C++ class and a C++ struct.

The real answer is that a C++ struct is effectively a C++ class with
default public access and inheritance. Unlike a C struct you can have
member functions, have private members if you want and have it derive
from base classes (as well as derive classes from it).

There is also a difference in the notation for using one, i.e. you can
simply refer to it by its type without a "struct" prefix or a typedef.

This answer is false:
"members of a struct in C are by default public, in C++ private". It is
false not because C doesn''t have a concept of private because
technically it''s true, they are by default public even if it is
impossible to change it. But in C++ they are also by default public,
therefore the statement is false.

This answer is off-topic:
"unions like structs except they shaer memory ..." (goes on to give
details on unions).;

4. Difference between assignment operator and copy constructor.

None of the given answers are complete and accurate. This is close.
"copy constructor creates a new object, assignment operator has to deal
with existing data in the object" .

Better answer is "copy constructor creates a new object that clones the
existing one, assignment modifies an existing object to be the clone of
another one". You might add that "although one may overload the copy
constructor and assignment such that the two objects do not actually
match, doing so would be unusual and would be against the normal nature
of what these are supposed to do".

This answer is not quite correct:

"copy constructor always creates a new object, assignment never does".
Because assignment can, it will often create a temporary object to be
exception safe should this action fail.

5. Difference between overloading and overriding.
Let me say this is a horrible question because it is based on one
knowing the technical names for things.

Anyway, overloading is using the same name but a different parameter
list. Overriding uses the same parameter list but where the original
function has been declared virtual in a base class. Overloads are
resolved at compile time, overrides usually at run-time. Overloads can
be particularly useful where one set of code could call either version
according to different circumstances, for example, a template
parameter.

6. Virtual
7. Dynamic binding.

Neither of these are questions. Or does it mean "what are they" in
which case they may as well be asked together, or ask "what is
polymorphism" and perhaps could have been combined with the above
question.

Anyway, the principle is to allow code to call a method by what it does
rather than how it does it, with the actual function that gets called
to be resolved at run-time. The same code that holds a pointer or
reference to a base class could invoke different overloads at different
times, depending on what actual object it has and the type of that
object.

8. Explain the need for a virtual destructor:

The 2nd and 3rd and 5th answers pretty much answer this.

9. Rule of 3.

The first answer answers it best, although the term "user-defined" was
never actually used, but was probably implied. Generally though if it
needs any of the 3 it needs the other two as well.

10. Why do you need a virtual destructor when someone says delete using
a Base ptr thats pointing @ a derived object?

Because the standard says you do.




Syam写道:

Syam wrote:

完全回答c和c ++中的面试问题来自
http://www.faiqs.com



其中许多人答案是不准确的,或者他们没有回答

的问题。


1. printf的输出是什么("%d");


Ron Natalie给出了正确的答案:因为函数将看起来是另一个不存在的参数
,它将导致未定义的

行为。它可能输出一个随机数或者可能会崩溃(seg

fault)。不太可能做除了这两个以外的任何事情,虽然技术上它可能是b $ b。


2.如果我说删除这个会发生什么? ?


给出的答案是一堆废话。有些陈述是明白的,有些陈述是假的,其他人只是转移而不回答这个问题。


无论如何,它应该导致对象被破坏,就像你有来自外面的指针上有一个名为delete的
。为了使它正常工作,

必须是这样的:


- 必须使用new创建对象。也就是说,常规新的。

new(nothrow)也可以工作,但没有新的位置,没有新的[]。


- 如果这是一个基类并且真实的对象来自于此,

析构函数必须是虚拟的。


3. C结构和C ++结构之间的区别。


我会假设它们是指C struct和C ++ struct,因为struct是

关键字而不是结构。问题并没有问到

a C ++类和C ++结构之间的区别。


真正的答案是C ++结构实际上是一个带有<的C ++类br />
默认的公共访问和继承。与C结构不同,您可以拥有

成员函数,如果需要,可以使用私有成员,并从基类派生

(以及从中派生类)。 br />

使用一个符号也有区别,即你可以简单地用它的类型引用它而没有struct前缀或类型定义。


这个答案是错误的:

C中结构的成员默认是公共的,在C ++私有中。这不是因为C没有私有概念因为

从技术上讲它是真的,即使它是

无法改变它。但是在C ++中它们默认是公共的,

因此声明是假的。


这个答案是偏离主题的:

工会结构,除了他们的记忆...... (继续给工会提供

的详细信息)。;


4.赋值运算符和复制构造函数之间的区别。


没有给出的答案是完整和准确的。这很接近。

复制构造函数创建一个新对象,赋值运算符必须与对象中的现有数据交易




更好的答案是复制构造函数创建一个新对象,克隆现有的一个对象,分配修改现有对象为
另一个。您可以添加尽管可能会使副本重载

构造函数和赋值,这样两个对象实际上并不是
匹配,这样做会不常见并且会反对正常的性质

这些应该做什么。


这个答案不太正确:


复制构造函数总是创建一个新对象,赋值永远不会。

因为赋值可以,它通常会创建一个临时对象

异常安全应该这个动作失败。


5.重载和覆盖之间的区别。

让我说这是一个可怕的问题,因为它基于一个

知道事物的技术名称。


无论如何,重载使用相同的名称,但使用不同的参数

list。覆盖使用相同的参数列表,但原始

函数已在基类中声明为虚拟。在编译时解决了重载

,通常在运行时覆盖。在一组代码可以根据不同情况调用任一版本

时,超载可以特别有用,例如模板

参数。


6.虚拟

7.动态绑定。


这些都不是问题。或者它是否意味着它们是什么。在

这种情况​​下,他们也可以一起被问到,或者询问什么是

多态性。也许可以与上面的

问题结合起来。


无论如何,原则是允许代码按照它的方式调用方法

而不是它是如何做到的,实际的函数被称为

在运行时被解析。保存指针或

引用基类的相同代码可以在不同的b / b $ b $时调用不同的重载,具体取决于它具有的实际对象及其类型< br $> b $ b对象。


8.解释需要虚拟析构函数:


第2和第3和第5个答案几乎回答这个问题。


9.规则3.

第一个答案最好的答案,尽管术语用户定义 ;是

从未实际使用过,但可能是隐含的。一般来说,如果它需要
中的任何一个,它还需要其他两个。


10.当有人说删除时,为什么需要虚拟析构函数?使用

a Base ptr指向@派生对象?


因为标准说你做了。

Many of those answers are inaccurate or they do not answer the
question.

1. What is the output of printf( "%d" );

The correct answer is given by Ron Natalie: as the function will look
for another argument that doesn''t exist, it will cause undefined
behaviour. It might output a random number or it might crash (seg
fault). Unlikely to do anything other than these two though technically
it could.

2. What will happen if I say "delete this" ?

The answer given is a load of nonsense. Some of the statements are
plain false, others simply divert and do not answer the question.

Anyway, it should cause the object to be destructed as though you had
called delete on the pointer from outside. For it to work correctly the
following must be the case:

- The object must have been created with new. That is, regular new.
new(nothrow) would also work but no placement new, no new[].

- If this is a base class and the real object is derived from this, the
destructor must be virtual.

3. Difference between C structure and C++ structure.

Well I will assume they mean C struct and C++ struct as struct is the
keyword not structure. The question does not ask the difference between
a C++ class and a C++ struct.

The real answer is that a C++ struct is effectively a C++ class with
default public access and inheritance. Unlike a C struct you can have
member functions, have private members if you want and have it derive
from base classes (as well as derive classes from it).

There is also a difference in the notation for using one, i.e. you can
simply refer to it by its type without a "struct" prefix or a typedef.

This answer is false:
"members of a struct in C are by default public, in C++ private". It is
false not because C doesn''t have a concept of private because
technically it''s true, they are by default public even if it is
impossible to change it. But in C++ they are also by default public,
therefore the statement is false.

This answer is off-topic:
"unions like structs except they shaer memory ..." (goes on to give
details on unions).;

4. Difference between assignment operator and copy constructor.

None of the given answers are complete and accurate. This is close.
"copy constructor creates a new object, assignment operator has to deal
with existing data in the object" .

Better answer is "copy constructor creates a new object that clones the
existing one, assignment modifies an existing object to be the clone of
another one". You might add that "although one may overload the copy
constructor and assignment such that the two objects do not actually
match, doing so would be unusual and would be against the normal nature
of what these are supposed to do".

This answer is not quite correct:

"copy constructor always creates a new object, assignment never does".
Because assignment can, it will often create a temporary object to be
exception safe should this action fail.

5. Difference between overloading and overriding.
Let me say this is a horrible question because it is based on one
knowing the technical names for things.

Anyway, overloading is using the same name but a different parameter
list. Overriding uses the same parameter list but where the original
function has been declared virtual in a base class. Overloads are
resolved at compile time, overrides usually at run-time. Overloads can
be particularly useful where one set of code could call either version
according to different circumstances, for example, a template
parameter.

6. Virtual
7. Dynamic binding.

Neither of these are questions. Or does it mean "what are they" in
which case they may as well be asked together, or ask "what is
polymorphism" and perhaps could have been combined with the above
question.

Anyway, the principle is to allow code to call a method by what it does
rather than how it does it, with the actual function that gets called
to be resolved at run-time. The same code that holds a pointer or
reference to a base class could invoke different overloads at different
times, depending on what actual object it has and the type of that
object.

8. Explain the need for a virtual destructor:

The 2nd and 3rd and 5th answers pretty much answer this.

9. Rule of 3.

The first answer answers it best, although the term "user-defined" was
never actually used, but was probably implied. Generally though if it
needs any of the 3 it needs the other two as well.

10. Why do you need a virtual destructor when someone says delete using
a Base ptr thats pointing @ a derived object?

Because the standard says you do.


这篇关于完全回答的面试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆