/* attempt to deal with prototype, bootstrap, jquery conflicts */ /* for dropdown menus */

Jump to content

Image of the Day

Today's #ScreenshotSaturday features Face Your Fears from Turtle Roc ... #virtualrealityshop #mobilevr #vrshop https://t.co/3qhSNAVNOZ
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.


Sign up now

Tricky C++ questions

4: Adsense

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.


  • You cannot reply to this topic
37 replies to this topic

#1 daniel_i_l   Members   

295
Like
0Likes
Like

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.

#2 Omid Ghavami   Members   

1003
Like
0Likes
Like

Posted 21 February 2007 - 11:11 PM

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

#3 Emmanuel Deloget   Members   

1381
Like
0Likes
Like

Posted 21 February 2007 - 11:36 PM

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,

#4 joanusdmentia   Members   

1060
Like
0Likes
Like

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.

#5 RobTheBloke   Members   

2552
Like
0Likes
Like

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



#6 blaze02   Members   

100
Like
0Likes
Like

Posted 22 February 2007 - 02:58 PM

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

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


prints?

9



#7 mrbastard   Members   

1576
Like
0Likes
Like

Posted 22 February 2007 - 03:15 PM

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.

#8 Hodgman   Moderators   

50123
Like
0Likes
Like

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

#9 gunning   Members   

749
Like
0Likes
Like

Posted 22 February 2007 - 03:29 PM

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.

#10 King Mir   Members   

2478
Like
0Likes
Like

Posted 22 February 2007 - 03:37 PM

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.


#11 Hodgman   Moderators   

50123
Like
0Likes
Like

Posted 22 February 2007 - 03:37 PM

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


#12 Arelaith   Members   

122
Like
0Likes
Like

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

#13 MichaelT   Members   

214
Like
0Likes
Like

Posted 22 February 2007 - 03:56 PM

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?


#14 mrbastard   Members   

1576
Like
0Likes
Like

Posted 22 February 2007 - 03:59 PM

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



#15 King Mir   Members   

2478
Like
0Likes
Like

Posted 22 February 2007 - 04:01 PM

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.



#16 Hodgman   Moderators   

50123
Like
0Likes
Like

Posted 22 February 2007 - 04:02 PM

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

#17 MichaelT   Members   

214
Like
0Likes
Like

Posted 22 February 2007 - 04:05 PM

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

#18 MichaelT   Members   

214
Like
0Likes
Like

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


#19 Anonymous Poster_Anonymous Poster_*   Guests   

0Likes

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/

#20 Nathan Baum   Members   

1027
Like
0Likes
Like

Posted 22 February 2007 - 05:32 PM

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.




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.