• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Tricky C++ questions

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

37 replies to this topic

### #1daniel_i_l  Members

Posted 21 February 2007 - 10:58 PM

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.

### #2Omid Ghavami  Members

Posted 21 February 2007 - 11:11 PM

Check out Washu's Journal, it's full of great C++ "puzzles".
Best regards, Omid

### #3Emmanuel Deloget  Members

Posted 21 February 2007 - 11:36 PM

Quote:
 Original post by daniel_i_lThis 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,

### #4joanusdmentia  Members

Posted 22 February 2007 - 01:37 AM

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.

### #5RobTheBloke  Members

Posted 22 February 2007 - 02:52 PM

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'....

### #6blaze02  Members

Posted 22 February 2007 - 02:58 PM

Quote:
 Original post by RobTheBlokeThe trickiest i've ever seen is :int a =1;std::cout << (++a + ++a + ++a) << std::endl;prints?

9

### #7mrbastard  Members

Posted 22 February 2007 - 03:15 PM

Quote:
 Original post by blaze029

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.

### #8Hodgman  Moderators

Posted 22 February 2007 - 03:18 PM

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 RobTheBlokeinterview 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.

### #9gunning  Members

Posted 22 February 2007 - 03:29 PM

Quote:
 Original post by RobTheBlokeThe 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.

### #10King Mir  Members

Posted 22 February 2007 - 03:37 PM

Quote:
 Original post by RobTheBlokeThe 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.

### #11Hodgman  Moderators

Posted 22 February 2007 - 03:37 PM

Quote:
 Original post by skittleoSo 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 ;)

### #12Arelaith  Members

Posted 22 February 2007 - 03:42 PM

Wouldn't the answer be 12? Since the compiler processes all the ++ syntax (should known PREincrement) leaving 4*3 = 12 :P

### #13MichaelT  Members

Posted 22 February 2007 - 03:56 PM

--------------
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?

### #14mrbastard  Members

Posted 22 February 2007 - 03:59 PM

Quote:
 Original post by ArelaithWouldn't the answer be 12?
Yep that's what msvc 8 tells me...

### #15King Mir  Members

Posted 22 February 2007 - 04:01 PM

Quote:
 Original post by MichaelTForgot 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.

### #16Hodgman  Moderators

Posted 22 February 2007 - 04:02 PM

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

That depends on the initial value of d :P

### #17MichaelT  Members

Posted 22 February 2007 - 04:05 PM

Hodgman: ;) I was hoping nobody would catch that so quickly

### #18MichaelT  Members

Posted 22 February 2007 - 04:11 PM

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 ;) )

### #19Anonymous Poster_Anonymous Poster_*  Guests

Posted 22 February 2007 - 04:47 PM

The problem with

std::cout << (++a + ++a + ++a) << std::endl;

has to do with sequence points.

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15

got this info from

http://www.tutorials-blog.com/cpp/preincrementpostincrement-question/

### #20Nathan Baum  Members

Posted 22 February 2007 - 05:32 PM

Quote:
Original post by Hodgman
Quote:
 Original post by skittleoSo 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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.