Tricky C++ questions

Started by
24 comments, last by ZQJ 17 years, 1 month ago
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
Advertisement
Check out Washu's Journal, it's full of great C++ "puzzles".
Best regards, Omid
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,
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 only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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'....

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

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


prints?

9

-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
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"]
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.
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
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.

This topic is closed to new replies.

Advertisement