C++ Interview Questions
For my first interview, I was asked to write a linked list implementation in C. Over the phone. When I got something right, I could hear furious typing on the other end as the interviewer entered something in a form. When I got a question wrong, silence.
In order to prepare for future interviews, here are some possible C++ questions you might encounter.
Q: What's wrong with the following code snippet?
int i = 0;
if (i = 0)
printf("Hello world!\n");
This is a classic. What do you expect to happen here? If you expect to see "Hello world!" printed, you're wrong. The conditional here isn't a conditional, it's an assignment.
Q: Can you have a variable of type void?No. Void is a special type that can be used to indicate a location in memory that has no specific type. You can't have an untyped variable in C++, so a variable of type void is not valid.# Q: What's the difference between a deep copy and a shallow copy?# Q: Name all the default methods for every C++ class. * Constructor - * Destructor - * Copy constructor - shallow copy.
Q: What's the difference between "const operator *" and "operator * const?"When the const is in front of the type operator, the object pointed to must not change. When the const is after the asterisk, the pointer itself must not change. That means if you have const char *c, the string c can't change. Of course, you can change c to point to another string altogether.If you have char *const c, on the other hand, the string may change but the pointer cannot.
If you're really paranoid, you can use "const char *const c" to specify a pointer that can't change to a string that can't change.
Q: When is it okay to initialize a reference to something other than a lvalue?When the reference is constant.
Q: What operations can you perform on a variable of type void*?- Assign a pointer of any type to a variable of type void*
- Assign to another void*
- Compare for equality and inequality
- Explicitly convert (cast) to another type