I was going through interview questions [1] 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.
What is the difference between
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:
new[]
and delete[]
plus the difference between a reference and a pointer - Let_Me_Be
realloc(ptr,0);
is actually free(ptr);
and realloc(0,size);
is actually malloc(size);
- Let_Me_Be
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.
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.
sizeof(void*)==64
and sizeof(long long)==8
(or smaller if CHAR_BIT>8
). Of course this would be pretty inefficient.. - R..
What can happen if your destructor isn't virtual?
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
Explain when you would use a std::vector; a std::map; a std::list and so on.
Explain the different types of casting and how they are applied in the real world.
reinterpret_cast
and static_cast
and dynamic_cast
instead of (int)
:( - meagar
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 :)
const
. - sbi
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
They are different languages, C and C++. "C/C++" is a wrong term.
My favorite question when I interview people: "What is a difference between a copy constructor and an assignment operator?"
type id = value
actually means i.e. it calls a constructor, not assignment. - David Rodríguez - dribeas
Why would you choose C over more modern languages?
How would you go about implementing a template function for swapping two variables?
std::swap
". - Let_Me_Be
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. - Cheers and hth. - Alf
How will you access tenth element of an array without using square brackets?
Interesting question I just saw:
What’s the auto keyword good for?
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
extern
, static
, auto
, or register
, and if it's not specified it will implicitly be auto
- Nick T
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.
&
can bring a whole huge software system down to its knees and takes seemingly for ever to be found. (BTDTGTLS) You have to be very quite confident about a lot of the dark corners of the language (and it's a huge one) in order to write real good code (TM). - sbi
malloc()
fails? How do you know new
fails?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
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?
class A
{
...
};
A a[5];
How many times A's constructor would be called?
What is the difference between static in C and static in C++
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.
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....
which containers are provided by STL and in which scenario you would use a particular container.
I think this really goes for all programming languages, but that being said I think if you program C, C++ you should be able:
:)
- sbi
friend
token - Tom
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.
Q: Differentiate C, C++, and C#.
A: Googles....
My favorite: how do you declare a pointer to a function returning void and taking two pointers to void as parameters?
Which debugging tool do you use?
Explain how (break points) or (memory leak detection) work in debugging tools.
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…
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.
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?
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.
How can you effectively manage the memory? Can you show me through some examples...
And obviously looking for smart pointers usage..
effectively manage the memory
? Examples as an answer? - Anisha Kaul
There are a lot of questions which could be formulated by simply changing the tasks of C++ FAQ lite into questions: C++ FAQ lite [1]
[1] http://www.parashift.com/c++-faq-lite/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);
%x
for printing a pointer value. Never do that. In particular on a 64 bit machine with 32 bit int
... - Jens Gustedt
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
malloc()
fails? How do you knownew
fails? - How do you read binary files and text files? - Donotalo