Skip to main content
GameDev.net gamedev.net
🔒 Locked

Tricky C++ questions

Started by daniel_i_l Feb 22, 2007 at 4:58 AM 24 replies 17.2k views
Original Post
daniel_i_l
daniel_i_l
This sunday i'm supposed to get interviewed in order to get into a programming project. People have told me that the interviewer likes to ask tricky C++ questions (as that is the language that the project will be written in). Can you post "tricky" questions (with the answers) so that i can see which areas i need to go over - what my weak spots are? Thanks in advance.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Emmanuel Deloget
Emmanuel Deloget
Quote:
Original post by daniel_i_l
This sunday i'm supposed to get interviewed in order to get into a programming project. People have told me that the interviewer likes to ask tricky C++ questions (as that is the language that the project will be written in).
Can you post "tricky" questions (with the answers) so that i can see which areas i need to go over - what my weak spots are?
Thanks in advance.

What do you know about C++? Without that information, it's quite difficult for us to spot holes in your knowledge :)

You can have many many "difficult" question - but some of the members here will find them easy, and not tricky at all. For example:

* is this main() prototype standard compliant? Why?
int main(std::size_t security_checksum, int ac, char *av[]);

You'll have to find the answer by yourself :)

It would also be good to have one or two examples of what the interviewer thinks a tricky question is.

Regards,
joanusdmentia
joanusdmentia
It'll probably be worth checking out the Guru of the Week archives, or better yet Sutter's Exceptional C++ and More Exceptional C++ books.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
RobTheBloke
RobTheBloke
The trickiest i've ever seen is :

int a =1;std::cout << (++a + ++a + ++a) << std::endl;


prints?

But honestly, interview questions are used to determine if you have the knowledge, experiance and correct approach to work through problems. Don't expect some golden nugget of information to get you through - interview questions are there to see if *you* can work through a problem and tackle it. It's unlikely to be 'do you know meta-template-programming-technique X' and more likely to be 'how would you tackle the following problem' or 'find the errors in the following'....

blaze02
blaze02
Quote:
Original post by RobTheBloke
The trickiest i've ever seen is :

int a =1;std::cout << (++a + ++a + ++a) << std::endl;


prints?

9

mrbastard
mrbastard
Quote:
Original post by blaze02
9


I was going to laugh, but I got it wrong as well... I said 6. Will have to read the spec on preincrement and brackets I think.
[size="1"]
Hodgman
Hodgman
Some questions I've got in C++ interviews before:

what does the explicit keyword do?

what does the mutable keyword do?

what is "const-correctness"?

When is it appropriate to use multiple inheritance?
(One guy answered this question with "NEVER!" and was hired, but there are better answers...)

When is it appropriate to use templates?

When is it appropriate to use the virtual keyword?

When should you use virtual destructors? ?

How do you call the constructor of a parent class?


Sorry, dont have time to post all the answers, but there's lots and lots of good advice at the C++ FAQ.

I also recommended Scott Meyers "Effective C++" books - those books get you jobs!

Quote:
Original post by RobTheBloke
interview questions are used to determine if you have the knowledge, experiance and correct approach to work through problems. interview questions are there to see if *you* can work through a problem and tackle it. It's unlikely to be 'do you know meta-template-programming-technique X' and more likely to be 'how would you tackle the following problem' or 'find the errors in the following'....

Yeah, most interviews you get given some hypothetical problems to see if you approach them logically. But I've been to some interviews (at games studios with lots and lots of applicants) where they've asked obscure C++ questions about "technique X" to separate the gurus from the experienced from the newbies.
gunning
gunning
Quote:
Original post by RobTheBloke
The trickiest i've ever seen is :

int a =1;std::cout << (++a + ++a + ++a) << std::endl;


So the increment operation even gets precidence over its return? That's very strange... I wonder if this is specified in the C++ standard or one of those undefined parts left up to compilers.
....[size="1"]Brent Gunning
King Mir
King Mir
Quote:
Original post by RobTheBloke
The trickiest i've ever seen is :

int a =1;std::cout << (++a + ++a + ++a) << std::endl;


I guess that's tricky because you have to know that it's undefined.
Hodgman
Hodgman
Quote:
Original post by skittleo
So the increment operation even gets precedence over its return? That's very strange... I wonder if this is specified in the C++ standard or one of those undefined parts left up to compilers.

Its well defined in the spec (i think). It would be a much easier question if it was a++ instead of ++a ;)
Arelaith
Arelaith
Wouldn't the answer be 12? Since the compiler processes all the ++ syntax (should known PREincrement) leaving 4*3 = 12 :P
MichaelT
MichaelT
Forgot to login (Moderator: can you please remove the AP post?)
--------------
its 10, here is another one for you

cout << "d:" << ( (d1 = ++d + ++d) + (d2 = ++d) + (d3 = ++d) + (d4 = ++d)) << endl;


what is the result and the content of each dn while and after?
No no no no! :)
mrbastard
mrbastard
Quote:
Original post by Arelaith
Wouldn't the answer be 12?
Yep that's what msvc 8 tells me...

[size="1"]
King Mir
King Mir
Quote:
Original post by MichaelT
Forgot to login (Moderator: can you please remove the AP post?)
--------------
its 10, here is another one for you

*** Source Snippet Removed ***

what is the result and the content of each dn while and after?

No, It really is undefined. Your compiler may give an answer of 10, but that doesn't mean every compiler will. That's why everybody is giving you different answers.

Same with your new example.

Hodgman
Hodgman
Quote:
Original post by MichaelT
what is the result and the content of each dn while and after?

That depends on the initial value of d :P
MichaelT
MichaelT
Hodgman: ;) I was hoping nobody would catch that so quickly
No no no no! :)
MichaelT
MichaelT
Mentally I expected 12 but as you said my compiler gave 10. I understood why it gave that but I also understand why the behaviour is undefined. Unfortunately I also see that programmers might ignore that last part. At least I did (because I didn't take the time to actually think ;) )
No no no no! :)
Nathan Baum
Nathan Baum
Quote:
Original post by Hodgman
Quote:
Original post by skittleo
So the increment operation even gets precedence over its return? That's very strange... I wonder if this is specified in the C++ standard or one of those undefined parts left up to compilers.

Its well defined in the spec (i think). It would be a much easier question if it was a++ instead of ++a ;)

Nope, it wouldn't.

In general, the order of evaluation of expressions is undefined. This means side-effects might occur at any time. The standard defines sequence points where any pending side-effects from before the sequence point are guaranteed to complete before any expression after the sequence point is executed. Assigning a value to a given object more than once between sequence points leads to undefined behaviour.

The standard C++ sequence points are:


  1. The end of a full expression.

  2. Between evaluating a function's arguments and actually calling it.

  3. After returning from a function.

  4. After the first operand of "&&", "||", "," and "?:".

  5. After initializing a base type or member in a constructor.



Note that sequence point (2) applies to functions invoked via operator overloading.
RobTheBloke
RobTheBloke
Quote:
Original post by King Mir
I guess that's tricky because you have to know that it's undefined.


correct ;) VC will give you 12. borland & gcc 10.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.