views:

2943

answers:

35

I was going through interview questions tag and found those question are quite useful, so in the same spirit, I am asking this question for C and C++ developers

What questions do you think good C and C++ programmers should be able to answer?

Looking forward for some amazing responses.

Please answer questions too, so that people could learn something new regarding the language, too.

+2  A: 

How would you implement a working binary search? This goes for any serious programmer, I think.

PS: This question isn't really C++ specific, but I found this to be a shockingly good interview question. The concept of binary search is so simple that everybody should know it and everybody should be able to derive a working implementation from this concept.

Sebastian
That's not a question :-D
Let_Me_Be
@Let_Me_Be Postfix "How would you..." :p
meagar
Not sure it really tests their C/C++ expertise, though. Worthwhile question to assess their approach to coding generally, though.
Paul
@meager "How would you find a specific number in a sorted array?"
Let_Me_Be
I think the last time I have implemented a binary search was as a student. I'm using them almost daily, though. Well, at least I suppose that the map/set find functions I'm calling are binary.
sbi
For me, the point is not whether they can code binary search. It's just a not entirely trivial problem to see how they approch the problem (for isntance, what do they ask to clarify the requirements), how they code it, and whether they know how to test it (choice of test cases). Also it can then go into a discussion of performance, choice of algorithm etc.
Paul
Jay
@sbi: The "I suppose" is a pretty big problem. With things like that which could kill performance, it's probably a mistake to use opaque classes which don't have a guarantee about their big-O (in both time and space) in either the language specification or the implementation's documentation.
R..
@R..: I know that the set/map find functions are O(log n) at worst. But I have never looked at any of their _implementations_.
sbi
sbi
Binary search is really, really easy to get wrong. In Knuth's Art of Computer Programming (probably volume 3), he comments that the first appearance of binary search in print is several years earlier than the first correct appearance. This may make it a really good or a really bad programming question.
David Thornley
not relevant now days. you can find the code in google in less then a minute.
none
@none How will you know if the code is correct?
Sebastian
@Sebastian the same way i know a piece of code that i write is correct.
none
+13  A: 

Q: How does a pointer point to an object?

A: The pointer stores the address of that object.

Usual answer is "emmmm, it... well... points..." which is pointless.

sharptooth
love this. there's this pointer tutorial that starts with that kind of text. i'd really recommend reading it! http://pastebin.com/vQbu9Lek
ianmac45
@ianmac45: That's great, especially the "C is insane too" part.
sharptooth
Good question, but i think it's very easy :)
Miro
@Miro: Yes, it is easy. It's not about knowledge, it's about understanding. You see, when a developer can craft an insane class hierarchy and use multiple virtual inheritance but can't explain that simplest thing in plain words what are the chances he will be able to answer tougher questions?
sharptooth
The correct answer would be "it's implementation-specific". While present-day implementations store numeric addresses as pointers, there's no reason it couldn't be something much more elaborate (like a symbol name, or a local variable name combined with an index into the block scope/call frame, or that combined with a serial number that expires and becomes a trap after a call returns, etc.).
R..
@ianmac that link is sick... I like it... although if I didn't already know what a pointer was, I'd probably be more confused than I was when i started.
WernerCD
@R.: Sure, but it does have to fit into some integral type. Of course, the implementation could have a really big integral type.
David Thornley
its phrased badly. it test the understanding if you not been able to ask a good question, then current you without telling you and then answering the question. working for you sounds not so fun. a better phrased question would be : "What a compiler do with a pointer to get to the Object?"
none
@R.. : I asked a separate question about other actually used implementations: http://stackoverflow.com/questions/3939876/how-can-a-pointer-be-implemented-except-storing-an-address
sharptooth
It doesn't have to fit into an integral type. It's totally possible that `sizeof(void*)==64` and `sizeof(long long)==8` (or smaller if `CHAR_BIT>8`). Of course this would be pretty inefficient..
R..
+32  A: 

What is the difference between

  • the stack and the heap,
  • malloc, new, new[],
  • free, delete, delete[]
  • pointers, references and pointers-to-pointers. When do you use which?

Obviously, these are fundamental aspects of C++ programming, but that is why every good C++ programmer has to know the answers to these.

Very good answers are given in:

Sebastian
You beat me to it.....
The Elite Gentleman
+1 and `new[]` and `delete[]` plus the difference between a reference and a pointer
Let_Me_Be
Difference between realloc() and free()?
The Elite Gentleman
@Elite That's a nasty question :-) Most people probably don't even know that `realloc(ptr,0);` is actually `free(ptr);` and `realloc(0,size);` is actually `malloc(size);`
Let_Me_Be
Thanks @Let_Me_Be, even I didn't know it....granted I did C++ over 5 years ago, so it brings back good old memories.
The Elite Gentleman
+1 for putting your answer in verse.
JeremyP
@Sebastian Please answer your question in the same post.
piemesons
+6  A: 

Explain the different types of casting and how they are applied in the real world.

Matt G.
+1 because I don't know... I still WTF when I see `reinterpret_cast` and `static_cast` and `dynamic_cast` instead of `(int)` :(
meagar
@ Meagar, you can read Thinking in C++, or refer to this question http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used
anijhaw
@anijhaw I'm badly out of touch with C++; It's been about 7 years since I considered myself primarily a C++ developer and I don't foresee getting back into it anytime soon. I'm a Ruby man now, and C++ looks like *pain incarnate*. Thanks though.
meagar
@meagar: To get detailed explanation of why whatever_cast is better than just (int) look for *why C-style casts are bad*.
sharptooth
Extra points if they can explain when it's better to use C style casts in C++ code.
Ferruccio
This can also be a good interview question by providing different scenarios and different casts and asking what each one does in that particular case, together with implicit conversions... as in what is the result of unsigned short i = 61440; cout << (int)i;
David Rodríguez - dribeas
you forgot const_cast! My favorite bad-practice-enabler :-)
ldx
A: 

I think this really goes for all programming languages, but that being said I think if you program C, C++ you should be able:

  1. Implement a recursive algorithm (or multiple different types, tail recursion etc.)
  2. Explain their affect on program execution and memory (Stack allocation etc.).
Kevin
@Kevin - Recursive, I can bet on that.I have seen quite a senior developer at a bank, who told me I was wrong because I had written a recursive function to find the height of a tree.
DumbCoder
@DumbCoder: I suppose he was wrong because your function found the _depth_ of the tree? `:)`
sbi
I've had more than one discussion (read argument) where someone will angrily say "how you would implement that? It's just not possible in the code. You'll have to use the database to access all this information over and over." I finally, got tired of saying, "All you need is a recursive function to ..." and have people look at me like I was speaking another language to them.
Kevin
A: 

Q: Differentiate C, C++, and C#.

A: Googles....

Ruel
This is probably the only one of these I wouldn't be offended to be asked, and the only one I would likely ask.
Kate Gregory
+3  A: 

How would you go about implementing a template function for swapping two variables?

Shynthriir
Extra points if answer is "I wouldn't I would use `std::swap`".
Let_Me_Be
well, I'd open up my favorite text editor, and then...
Tom
@Tom: LOL! liked it ;)
usta
Uhm, due to certain bugs in Visual C++ regarding friendship and access, it's actually a quite deep question, touching both on deep knowledge of the language, and on the difference between Theory and Practice. I currently prefer to call the member `swapWith`, and place a non-member `swap` in the global namespace. When/if Visual C++ gets its act together it would be back to straightforward code.
Alf P. Steinbach
+5  A: 

Explain when you would use a std::vector; a std::map; a std::list and so on.

Chris Card
Yes, but if asked, give some hints. Making up examples is hard.
Let_Me_Be
It's easy and map is very different of other two, but an harder question is: "Whats difference of storing(accessing) values in vector and list?"
Miro
+1  A: 

which containers are provided by STL and in which scenario you would use a particular container.

Naveen
+17  A: 

A bit meta, but I think still worthwhile: "What are the questions every good C/C++ Developer should be able to answer?" :)

I'm serious - a good developer should be able to suggest these.

Paul
so recursive of you
Gary
Sooo... what is your answer to your answer??
kotlinski
"Error: stack overflow"
Paul
Who did up-vote this comment?
sbi
C and C++ are different languages. In C, you can have `int class = 5;".
Thomas Matthews
A: 
  • What is function overloading and operator overloading?
  • What is a friend class?
The Elite Gentleman
A friend class is your own worst enemey.
Shynthriir
As they say, keep your friends close but your enemies closer....lol
The Elite Gentleman
@Shyntriir An enemy I miss every time my java compiler complains that it cant resolve the `friend` token
Tom
Explain when you need to use friend function instead of method when implementing an operator and why.
Let_Me_Be
I'm not saying friends aren't useful in C++, I'm just saying people tend to shoot themself in the foot more often with them.
Shynthriir
Who needs friends if you have C++?
Matt Joiner
+6  A: 

They are different languages, C and C++. "C/C++" is a wrong term.

Abyx
Doesn't answer the question. Belongs in comments.
Ferruccio
@Ferrucio - this is a correct answer to the original question - not a comment at all
KevinDTimm
This can indeed be interpreted as a genuine answer. I'm not sure it was meant that way, though.
sbi
@Ferricio - I agree with Kevin. A valid answer to a question is a correction on the question (if there is something in it to be corrected.) In other words, a poorly constructed question deserves a correcting answer. Whether people want to make it a comment, that's pretty subjective and open to interpretation.
luis.espinal
I agree with the sentiment that "The C/C++ Language" does not exist but the question never actually refers to the C/C++ Language but to C/C++ Developers which I chose to interpret as "developers who have used both C and C++ and know the differences" and not as "developers who use the C/C++ language". Although the title refers to C/C++ Developers, the question itself refers to "C and C++ Developers". I guess the question is open to interpretation.
Ferruccio
A: 

I liked this one: http://stackoverflow.com/questions/3931156/algorithm-interview-question

It's not enough just to know algorithms. Programming is about making decisions.

ruslik
A: 

How to declare and initialize an int pointer to a specific memory address like 0x604768, and make it store the value 0xdead?

int* address = (int*)(void*)(uintptr_t)0x604768;
printf("Memory address is: 0x%p \n", address);

*address = 0xdead;
printf("Content of that address is: 0x%p \n", *address);
karlphillip
I feel a sharp pain in my eye while reading this.
Let_Me_Be
to add to @Let_Me_Be - while this is a correct question for certain positions, it is not a question that lives in the 'good c developer' space (and it REALLY doesn't belong in the c++ world)
KevinDTimm
excellent!!!!!!
Donotalo
You failed your own interview. Your answer uses `%x` for printing a pointer value. Never do that. In particular on a 64 bit machine with 32 bit `int`...
Jens Gustedt
@Jens Would %p be better?
karlphillip
@KevinDTimm and I was thinking pointers were a strong feature of C++ and everyone should knew how to use them, heheh, my mistake.
karlphillip
@karlphilip: i like this question. C++ is considered a superset of C. and pointer is indeed a part of C. this is a valid C question.
Donotalo
@Donotalo: C++ is not a superset of C, even C90. In the first place, there are valid C programs that are invalid C++ programs, or do something else. In the second place, a well-written C program is not a well-written C++ program. BTW, this question relies on undefined or implementation-defined behavior.
David Thornley
@karlphillip: sure it would be better, but you'd also have to cast the pointer to `void*`, since in C pointers of different base type may be realized differently. But I see that you now switched to your C++ mode for that, what a pity ;-) BTW also casting an `int` to a pointer in C is implementation defined. Better would be a sequence `(int*)(void*)(uintptr_t)0x604768`.
Jens Gustedt
@Jens Thank you.
karlphillip
+9  A: 

C++ :

What are Virtual Constructors in C++? :P

P.S : Notice the smiley


C (also a valid C++ question)

What's wrong with the following code?

char *p = "Sam";
p[0] = 'R';
Prasoon Saurav
For some reason my students seem to think that they would make a lot of sense :-D
Let_Me_Be
You mean INvalid c++ too?
Moshe
@Moshe : I mean the question is valid for C++ too. :)
Prasoon Saurav
Wait...I found the solution....Segmentation Fault? lol
The Elite Gentleman
@The Elite Gentleman : You get 7/10 ;)
Prasoon Saurav
@Prasoon, at least I passed... :)
The Elite Gentleman
Aren't strings in C guaranteed to be writeable?
Let_Me_Be
@Let_Me_Be : No string literals are stored in read only section. Modifying the content of any string literal invokes Undefined Behaviour.
Prasoon Saurav
@Prasoon, you actually closed this?
The Elite Gentleman
@The Elite Gentleman : I think questions like this have been asked at SO before that's why closed it. Ohh damn!! I didn't notice that it has been made CW. Voted to reopen.
Prasoon Saurav
Ok, I understand...
The Elite Gentleman
assign "string" in declarations of char* is considered as declaring const. changing a const . since you use a pointer the compiler cant realy know for every possible code written if the pointer at run time still points to the const string "Sam" so the compiler let it pass and should SEGMENTATION FAULT in run time, when it finds you are trying to change a const on run time. it would be nicer if the C language would have a better error presentation such as in Java.
none
@Praason: a "virtual constructor" in C++ refers to the generalized idea of cloning, as explained by FAQ item <a href="http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8">20.8 'What is a "virtual constructor"?'</a>. I'm mentioning this because I have the impression that folks here think that there's no such thing.
Alf P. Steinbach
@Alf : Yes I have read that Marshall Cline's article. However we cannot directly make a constructor virtual. I wanted to ask people why such thing is not directly possible in C++? Thats why added the smiley. You can use [text](link) to add links to comments. And its "Prasoon" sir! `:)`
Prasoon Saurav
+3  A: 

Why

void strcpy(char* dest, char* src)
{
    while(*dest++ = *src++);
}

copies the entire src string to destination?

The question was about good C/C++ programmers :)

Aamir
Eh... I'm usually not a fan of throwing obscure or weird looking constructs at people as a way to gauge their real-world practical competence, but I guess a good C programmer should understand it pretty quickly.
meagar
Ferruccio
char src[] = "abc"; char *dest = src + 2; Your version of strcpy would never terminate (until memory bounds occur). Of course, the "docs" could say don't do that, but it's best to be sure.
Caleb Huitt - cjhuitt
This function's signature misses a `const`.
sbi
@meagar - that's not a obscure or weird looking construct. It's a classical C and C++ construct (and a very elegant one in terms of C coding standards and programming tradition). It also elucidates syntactic and semantic rules in C and C++ that every C and C++ programmer should know. Furthermore, it is a valid interview question: I would be very suspicious of a C or C++ programmer who find this "obscure" and/or cannot answer it satisfactorily.
luis.espinal
@Caleb - you bring a good point. But one could argue that the function will terminate provided the precondition that src points to a legitimate, null-terminated string. That is, the onus is on the programmer. However, this would bring a good interview question. How does it work *and why it would not work*?
luis.espinal
@luis.espinal: Good points about the purpose of the question, and in general I would expect interviewees to answer with the normal bit of how it works. If someone pointed out the problems with it, they would get bonus points. However, you missed my point about never terminating: because the dest pointer points into the middle of the src pointer, the function would never terminate, even if the src pointer was originally properly null-terminated. As it continues to copy, it will continue to overwrite memory with non-null characters until it hits a bounds violation.
Caleb Huitt - cjhuitt
@sbi: Having a const is off course better but not having it is good in its own right. This can be the next question i.e., what should be the improvement to this code.
Aamir
@Caleb Huitt: There is a good discussion here on why there are no sanity checks in such an implementation. Here: http://stackoverflow.com/questions/3616295/why-no-sanity-checks-in-legacy-strcpy
Aamir
The original question was what should be a good interview question and my question was that why does this copy the string. The discussion should be about whether it is a good question or not. Not about whether the code is correct (in every aspect) or not. BTW, gcc has a very similar implementation of strcpy as well :) which most of us use knowingly or unknowingly.
Aamir
@Aamir: I don't see anything a C++ programmer should now here. I would rather say that any C++ code containing this tidbit is worth shooting down.
Matthieu M.
@Aamir: Last time I looked C had borrowed `const` from C++. I'm not sure about common C coding standards, but in C++ it's considered very bad style not to constify what can be constified. This is the exactly kind of interview question that always makes me cringe, because it makes the one asking the question (and probably also the ones who had answered it before me) look somewhat stupid for missing the obvious, while trying to see what I would miss.
sbi
@Matthieu M: Then you should probably shoot down a lot of strcpy's implementations which a lot of programmers use on a daily basis. I do think this is very good implementation and any good C/C++ programmer should know what is going on between the lines.
Aamir
Why should a typical C++ programmer be mucking around with raw character arrays on a regular basis? I've had to do it on occasion, but the vast majority of my life is spent with the far safer std::string. For a C programmer, this question is quite good. For C++, it's just not that relevant.
nsanders
@Aamir: nsanders just answered for me. It's not a matter of being good or bad, it's just completely irrelevant for C++ :)
Matthieu M.
+2  A: 

How will you access tenth element of an array without using square brackets?

Manoj R
char* arr = "Hello World";char c = *(arr + 10);
@user394242: That would be the eleventh element. :-)
Thanatos
+2  A: 

Interesting question I just saw:

What’s the auto keyword good for?

The Elite Gentleman
Especially if they can differentiate between C++03 and C++0x usage.
Ferruccio
Not that anyone uses it in C++03
Tamás Szelei
@Tamás Szelei: But `auto` could've been used in C++03. What does `Item emptyItem( std::string() );` do? Correct, it declares a function. Now what does `auto Item emptyItem( std::string() );` do? This time, it declares a variable.
usta
That is a good question for C programmers, too.
Jens Gustedt
Isn't it totally pointless in C (I don't know C++ at all)? The storage class will be one of `extern`, `static`, `auto`, or `register`, and if it's not specified it will implicitly be `auto`
Nick T
+8  A: 

What can happen if your destructor isn't virtual?

ldx
Cthulu will hunt you down.
Tom
No, Cthulu still waits dreaming. Which part of Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn don't you understand??!!?!
ldx
For an answer, first read the Necronomicon by Abdul Al-Hazred. Then if you're still sane, read 'The C++ Programming Language' by Bjarne Stroustrup ... or should that be the other way round?
MikeJ-UK
+2  A: 

My favorite question when I interview people: "What is a difference between a copy constructor and an assignment operator?"

Nemanja Trifunovic
Hmm, and what do you expect as an answer?
Let_Me_Be
@Let_Me_Be: A cctor _initializes_ data members that previously was raw memory. An assignment op changes previously valid data members to have different values. Related: http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
sbi
@sbi I was expecting an answer from the author, but if what you wrote is what he wants to hear as an answer, then I would definitely skip this one.
Let_Me_Be
@Let_Me_Be: What do you mean by "skip this one"?
sbi
@sbi I mean that if this is the requested wording, then I don't consider this question to be "A question that a good C and C++ programmer should be able to answer."
Let_Me_Be
@Let_Me_Be: I disagree with you then. There's an important difference between initializing a piece of raw memory so that it becomes an actual value to be used, and assigning a new value to an existing, valid value. See, for example, http://stackoverflow.com/questions/1734628/1734687#1734687, http://stackoverflow.com/questions/3761325/3763563#3763563, or the link I already gave: http://stackoverflow.com/questions/3279543/.
sbi
I agree with @sbi. Also it is important to know what `type id = value` actually means i.e. it calls a constructor, not assignment.
David Rodríguez - dribeas
A: 

A questions about Virtual Destructor, why you need them etc, or a questions about why a bit of code does not work, when the need for a Virtual Destructor is part of the answer.

Also a question on when it is better to use Java and/or C# than C++, e.g does the person know what an garbage collector is.

Ian Ringrose
A: 

What is the difference between static in C and static in C++

anijhaw
There are many uses of the static keyword in C++ though, and the C version is one of them.
Adhemar
So thats what the question is supposed to ask.
anijhaw
A: 

How can you speed up compile time?

Any experienced C/C++ programmer will be able to talk about how to arrange header files etc so as to reduce the number of header files that need to be read. This then leads into some questions about good design etc…

Ian Ringrose
Compile time usually doesn't matters. The more important is run speed :)
Miro
@Miro... it all depends... I once was in a project for which a single file --heavily templated-- would require almost 20 minutes in a P4 with 1.5 Gb of memory (the compiler would require enough memory to trigger swapping into disk and that raised hell...) With some tweaking that was reduced to about 30 seconds, note that a big compilation delay can distract you, and that will reduce your productivity...
David Rodríguez - dribeas
@Miro, just by adding #ifndef FileName_Included to header files I have taken compile times down form over an hour on a project to about 5 minutes, so it is inportant for programmers to understand the issues.
Ian Ringrose
Answer: By switching from Windows to Linux and cross-compiling under Linux. BTW: That way everything gets faster: USB speed, network transmission, default browser, folder browser, games - server - the .NET/mono framework - compiler error detection - IDE, as said, everything.
Quandary
@Quandary: Enjoy your stay.
Matt Joiner
@lan Ringrose, this is basics of creating headers :)
Miro
@Quandary Yes linux is much better than windows, but it's not very popular for now
Miro
@Miro: As long as you just use it to cross-compile it doesn't need to be popular. Neither does it for you to run a completely free server on it.
Quandary
A: 

You can ask the candidate how would they implement an algorithm in C and later how would they do the same in C++ making use of the STL.

You can ask why did he/she choose C or C++ for the projects they've done and also ask how would they approach if the C projects had to be done in C++ and vice versa.

codaddict
A: 
  • When to use friend-classes?
  • When do Destructors have to be virtual and what is the difference to nonvirtual Dtors?
  • What do Smart-Pointers do and which types of Smartpointers exist (aiming towards shared/scoped)

There are a lot of questions which could be formulated by simply changing the tasks of C++ FAQ lite into questions: C++ FAQ lite

MOnsDaR
A: 
class A
{
...
};

A a[5];

How many times A's constructor would be called?

Yulia Rogovaya
A: 

Well for a start I wouldn't ask questions whose answers can be found easily in a C or C++ reference manual or with a simple Google search unless the job for which they were being interviewed forbade using those resources (in which case the successful candidate will come to wish he wasn't).

Go for more high level questions with less clear cut or even controversial answers.

e.g. I might ask

what are some of the disadvantages of C or C++ as compared to (say) Java?

what strategies might you use to mitigate the disadvantages?

under what circumstances do the disadvantages become advantages?

JeremyP
This assumes that the interviewer and the interviewee are both thoroughly familiar with either C or C++ and a common other language. While you would want a senior person to be familiar with another language, there's no guarantee it's Java or C# or any other specific one.
David Thornley
That was just an example question. In any case, I would expect any competent programmer to know something about the pros and cons of reasonably common languages, particularly where they involve gotcha issues like indexing off the end of arrays and memory leaks.
JeremyP
A: 
  • How do you know malloc() fails? How do you know new fails?
  • What are binary files and text files?
Donotalo
Why would I want to know that? If it fails, the program can't run. Does it matter why it crashes, or whether it crashes or aborts with an error message ?
Quandary
@Quandary: if you know that then you can handle error situations. if you don't know that, then it can be assumed by interviewer that you don't write safe code.
Donotalo
@Donotalo: I don't. I write elegant code, that is to say code without all that 'unnecessary' stuff around ;-))
Quandary
It depends on your platform. In Linux (with glibc at least) malloc will never return an error- if not enough memory is available the kernel will wait or just kill a process to free up memory.
CodeninjaTim
There are situations even in Linux, with glibc, in which `malloc` can return "out of memory": If virtual memory has run out or is too limited, or if you request more memory than exists (typically a bug), or if the kernel doesn't allow optimistic allocation. I have seen code where a simple `if` would have revealed a failing `malloc` call, and the bug that caused it.
Thanatos
A: 

How can you effectively manage the memory? Can you show me through some examples...

And obviously looking for smart pointers usage..

liaK
By using the C++ garbage collector. It works.
Quandary
@ Quandary , Oh yeah... Actually am not sure if you are the one who downvoted.. If so, pls don't attend a C++ interview...
liaK
A: 

Most asked question in embedded companies... 1. what is the major difference b/w structure and union? 2. what is Dangling pointer? 3. Memory leakage???

More questions will be like this only....

ulaga
+1  A: 

Why would you choose C over more modern languages?

Blankman
+1. I actually see good reasons to use C, but never had the chance of meeting someone using C for any of those (or any other good reasons).
back2dos
You wouldn't. Unless you would care about your customer's execution speed instead of the time you need to write the program in C, and that you wouldn't do.
Quandary
A: 

My favorite: how do you declare a pointer to a function returning void and taking two pointers to void as parameters?

luis.espinal
My answer would be: "I hope this is only an interview question and there are no such things in your code base".
Nemanja Trifunovic
In that case, I would not hire you because a lot of system-level programming (right or wrong) might involves creating such constructs. It is also how you pass a comparator to the standard qsort() function (C/C++ 101). It would tell me you can work only working in ideal conditions given that most places have ugly code to one degree and another. The later must be maintained improved/refactored. It would not be pretty but it is work to be done (and this is the reality in any software shop.) Engineering is not just about creating clean pretty things, but to solve problems as well.
luis.espinal
Don't get me wrong, I would not want to see constructs like that all over the place without justification. But if I were an employer, I would like to know that a person applying for a C or C++ actually knows more than a superficial exposure to pointers. I want to know that you can (within reason of course), make sense of ugly code, get through it, analyze it, and get things done if it ever comes to that. Pondering about ideal conditions does nothing without having the skills to handle ugly things if they come our way. There is more to interviews than pondering technical ideals.
luis.espinal
A: 

Which debugging tool do you use?

Explain how (break points) or (memory leak detection) work in debugging tools.

smailq
+2  A: 

For C++ developers:

Given class A

class A 
{
public:
    A();
    ~A();

    A operator=(A an_a);
private:
    int  i;
}

A::A()
{
}

A::~A()
{
}

A A::operator=(A an_a)
{
    i = an_a.i;
}

1) How many A's will be created by the following

   {
      A  a1, a2;

      a1 = a2;
   }

2) Why won't the code compile?

3) What's the value of i in a1 from above?

4) What's wrong with the operator= method?

5) (refinement of question from above) What is special about a static method?

jwernerny
A: 

Just what is a protected abstract virtual base pure virtual private destructor? -- Tom Cargill

Most people might not be able to answer it, but hopefully most will give it a good thought.

Alan