Sign in to follow this  
daniel_i_l

Tricky C++ questions

Recommended Posts

daniel_i_l    295
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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
RobTheBloke    2553
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'....

Share this post


Link to post
Share on other sites
mrbastard    1577
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.

Share this post


Link to post
Share on other sites
Hodgman    51223
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.

Share this post


Link to post
Share on other sites
gunning    749
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.

Share this post


Link to post
Share on other sites
King Mir    2490
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.

Share this post


Link to post
Share on other sites
Hodgman    51223
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 ;)

Share this post


Link to post
Share on other sites
MichaelT    214
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?

Share this post


Link to post
Share on other sites
King Mir    2490
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.

Share this post


Link to post
Share on other sites
MichaelT    214
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 ;) )

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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/

Share this post


Link to post
Share on other sites
Nathan Baum    1027
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.

Share this post


Link to post
Share on other sites
Excors    715
As an extra data point, ICC gives 9. With that kind of question, you can't just work out an answer - the trick is to make sure you step back and think about it at a higher level, to determine that the code in the question is invalid, rather than immediately pushing yourself into the details and trying to work out what the execution would be.

(But you have to hope the interviewer is sufficiently competent and won't listen to your explanation of undefined behaviour and sequence points and then just say "no, the answer is 10, I tried it last night" [smile])

Share this post


Link to post
Share on other sites
Quote:
Original post by 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.


It prints whatever the compiler wants to print. This statement has undefined behavior (google for sequence points for more information).

Edit: Or read Nathan's post :)

Share this post


Link to post
Share on other sites
Glak2    122
the part that I found tricky was that I forgot that preincrement returns a reference rather than a value. If it returned a value then it would be undefined behavior and yet any reasonable compiler would return a 9 (2+3+4 or 2+4+3 or some other permutation)

Share this post


Link to post
Share on other sites
Riekistyx    100
1. How do you add with out using addition, subtraction, modulus, multiply, divide operators.

2. How do you multiply without multiply operators

3. Whats so bad about iterating through an array of abstract objects?

4. Whats slower casting from float -> int or int -> float or are they the same?

5. Whats negative zero?

6. Whats malloc? Whats new operator? Is there a difference? Why?


lolz enjoy.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this