frequently asked C and C++ interview questions along with their answers

D

Dave Townsend

Most of the interview questions I've been given at interviews are totally
irrelevant to serious C++ programming, they are more of a vehicle to
let the interviewer show off with some obscure programming tidbit he found
on the internet the other night. For example, swapping two values with a
temporary,
find me somebody who has actually used this trick in real life. If your
interviewer
starts asking questions like this, this is a certain indication of a low
grade organization
and you need to plan accordingly.
 
G

Gary Labowitz

Dave Townsend said:
Most of the interview questions I've been given at interviews are totally
irrelevant to serious C++ programming, they are more of a vehicle to
let the interviewer show off with some obscure programming tidbit he found
on the internet the other night.

Agreed.

For example, swapping two values with a
temporary,
find me somebody who has actually used this trick in real life.

First, I'll assume you meant "without" a temporary, which is the usual. And
here I am, having used it in "real life."
Of course, it was many years ago when memory was precious. I've worked on
machines with 2K characters and we would reach a point where, when
instructed to add some new functionality, we would ask, "Okay, what do you
want taken out?"
 
I

Ioannis Vranos

DaKoadMunky said:
Could someone with a copy of the standard verify this and provide the relevant
text?

I had never heard this before and flipping through TCPPPL I can't find anything
regarding this.




C++98 includes C90 as a subset unless in cases it mentions
differences/specialisations. The extern behaviour I said is an inherited
from C90 thing. Here is what the latest draft of C90 that I have says:


"3.7.2 External object definitions

Semantics

If the declaration of an identifier for an object has file scope
and an initializer, the declaration is an external definition for the
identifier.

A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or with
the storage-class specifier static , constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains no
external definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration of that
identifier, with the composite type as of the end of the translation
unit, with an initializer equal to 0.

If the declaration of an identifier for an object is a tentative
definition and has internal linkage, the declared type shall not be an
incomplete type.

Examples

int i1 = 1; /* definition, external linkage */
static int i2 = 2; /* definition, internal linkage */
extern int i3 = 3; /* definition, external linkage */
int i4; /* tentative definition, external linkage */
static int i5; /* tentative definition, internal linkage */

int i1; /* valid tentative definition, refers to previous */
int i2; /* $3.1.2.2 renders undefined, linkage disagreement */
int i3; /* valid tentative definition, refers to previous */
int i4; /* valid tentative definition, refers to previous */
int i5; /* $3.1.2.2 renders undefined, linkage disagreement */



extern int i1; /* refers to previous, whose linkage is external */
extern int i2; /* refers to previous, whose linkage is internal */
extern int i3; /* refers to previous, whose linkage is external */
extern int i4; /* refers to previous, whose linkage is external */
extern int i5; /* refers to previous, whose linkage is
internal */"






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
I

Ioannis Vranos

Ioannis said:
C++98 includes C90 as a subset unless in cases it mentions
differences/specialisations. The extern behaviour I said is an inherited
from C90 thing. Here is what the latest draft of C90 that I have says:


"3.7.2 External object definitions

Semantics

If the declaration of an identifier for an object has file scope
and an initializer, the declaration is an external definition for the
identifier.

A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or with
the storage-class specifier static , constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains no
external definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration of that
identifier, with the composite type as of the end of the translation
unit, with an initializer equal to 0.

If the declaration of an identifier for an object is a tentative
definition and has internal linkage, the declared type shall not be an
incomplete type.

Examples

int i1 = 1; /* definition, external linkage */
static int i2 = 2; /* definition, internal linkage */
extern int i3 = 3; /* definition, external linkage */
int i4; /* tentative definition, external linkage */
static int i5; /* tentative definition, internal linkage */

int i1; /* valid tentative definition, refers to previous */
int i2; /* $3.1.2.2 renders undefined, linkage disagreement */
int i3; /* valid tentative definition, refers to previous */
int i4; /* valid tentative definition, refers to previous */
int i5; /* $3.1.2.2 renders undefined, linkage disagreement */



extern int i1; /* refers to previous, whose linkage is external */
extern int i2; /* refers to previous, whose linkage is internal */
extern int i3; /* refers to previous, whose linkage is external */
extern int i4; /* refers to previous, whose linkage is external */
extern int i5; /* refers to previous, whose linkage is internal
*/"



.... which means that only when we have an initialisation an extern
object declaration becomes a definition. So what I had said does not
apply and the behaviour that JKop was experiencing cannot be considered
portable.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
J

JKop

Ioannis Vranos posted:
... which means that only when we have an initialisation an extern
object declaration becomes a definition. So what I had said does not
apply and the behaviour that JKop was experiencing cannot be considered
portable.


Not me.


-JKop
 
Joined
May 20, 2011
Messages
3
Reaction score
0
try this

Julie writes:

> > int main()
> > {
> > int * x;
> >
> > *x = 10;
> > return 0;
> > }
> >
> > // What's wrong here?

>
> Absolutely nothing is wrong. All of the above is perfectly legal C++.


legal c++ but
the thing is you have a pointer of type int (int *x;); but it is instantiated means you have to allocate a memory where it is going to point.

then only the statment (*x = 10;) will be executed.

so try the simplest way:
int main()
{
int num;
int *x = # // you can allocate dynamic memory too
*x =10;
return 0;
}
 
Joined
May 20, 2011
Messages
3
Reaction score
0
Julie writes:

> > int main()
> > {
> > int * x;
> >
> > *x = 10;
> > return 0;
> > }
> >
> > // What's wrong here?

>
> Absolutely nothing is wrong. All of the above is perfectly legal C++.


Only a fool would give that answer if he wanted the job. If you don't want
to work there, why bother with the interview?

They are hiring programmers, not language lawyers.
legal c++ but
the thing is you have a pointer of type int (int *x; but it is instantiated means you have to allocate a memory where it is going to point.

then only the statment (*x = 10 will be executed.

so try the simplest way:
int main()
{
int num;
int *x = # // you can allocate dynamic memory too
*x =10;
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top