- Where vtable is initialized?
This type of question is supposed to get you out of the saddle. In constructor, of course.
- Question regarding constructor and destructor : What would be the output of the program1? Could you explain the role of virtual destructor ?
This was a simple question to check your basic understanding:
- A Constructor
- B Constructor
- C Constructor
- C Destructor
- B Destructor
- A Destructor
A virtual destructor ensures that, when delete is applied to a base class pointer or reference, it calls the destructor implemented in the derived class, if an implementation exists.
- What would be the output of the program2?
- Step 1: A::f() (f() is called within class A)
- Step 2: B::DO() ('a' downcasted to 'b')
- Step 3: A::test() (because class C is a friend class of A and, hence, has an access to private members of the class A)
- What's wrong with the following code ?
class A { private: int i; int j;
public: A(); }
A::A() :j(0), i(j) {}
|
In C++ the initialization of members is performed in order of their declaration. 'i' was declared before 'j'. Therefore, 'i' will be initialized first and the value of 'j' is undefined at that time.
- How does C++ copy objects if your program doesn't provide a copy constructor?
If you do not supply a copy constructor, the compiler attempts to generate one. A compiler-generated copy constructor sets up a new object and performs a memberwise (not bitwise!!!) copy of the contents of the object to be copied. If base class or member constructors exist, they are called; otherwise, bitwise copying is performed.
- What's inappropriate in the following code:
...
CMyObject *pobj = new CObject[128];
char *pArray = new char[128];
...
delete pArray;
delete pobj;
You should understand the difference between scalar and vector deleting of the objects. If you allocate memory for a vector of objects and then delete the pointer to the vector, only the destructor of the first element of the vector will be deallocated. To ensure all elements of the vector are being destroyed, you should use the following syntax:
delete [] pobj; // must
delete [] pArray; // should
Strictly saying, for simple types such as char, int, etc. you don't have to use delete[], but it is highly recommended to use the same form delete[] for all vectors.
- What happens when the object is being destructed (or, in other words, what's the order of object destruction)?
When an object goes out of scope or is deleted, the sequence of events in its complete destruction is as follows:
- The class's destructor is called, and the body of the destructor function is executed.
- Destructors for nonstatic member objects are called in the reverse order in which they appear in the class declaration. The optional member initialization list used in construction of these members does not affect the order of construction or destruction.
- Destructors for nonvirtual base classes are called in the reverse order of declaration.
- Destructors for virtual base classes are called in the reverse order of declaration.
- (Very important question - many people fail here despite this question is as stupid(as all others!!!) Mark which lines are wrong (you may be asked how references or/and pointers are initialized)?
int a = 10;
int *b = a;
int *c = &a;
int &d = a;
int &e = &a;
int &f = *c;
One thing that you must remember as the word c++ itself is
references are always initialized with value, while pointers are always initialized with address. Once you remember this simple rule, you'll be able to easily answer this type of question.
int a = 10;
// error cannot convert from int to int*: int *b = a;
int *c = &a;
int &d = a;
// error cannot convert from int* to int&: int &e = &a;
int &f = *c;
- Write all constructors for the following classes so that DoorWithLock class could be called with the constructor DoorWithLock(1,300,400,300), where 1,300,400,300 are the default parameters:
class Door{ private: int m_Height, m_Width; }
class Lock{ private: int m_Type; }
class DoorWithLock{ private: Lock lock; int m_Owner; }
This should go like this:
class Lock
{
public:
Lock(int Type = 300)
{
m_Type = Type;
}
private:
int m_Type;
};
class DoorWithLock: public Door, public Lock
{
public:
DoorWithLock(int Owners = 1, int Type = 300, int Height = 400, int Width = 150)
:Door(Height, Width), lock(Type), m_Owners(Owners)
{
}
private:
Lock lock;
int m_Owners;
};
- What's wrong with the following code? Could you suggest alternative way of array's initialization?
char *pArray = new char[256];
for( int i = 0; i <>
Nothing wrong really with this code. You may mention that 0 is not char and compiler may complain, that "cannot convert from int to char". However, most compiler would eat this initialization without problem whatsoever. You may mention that other ways of array initialization are using memset(pArray, 0, 256), ZeroMemory() (if we are talking about Windows) or stl's for_each() algorithm. You might want to mention also that in this particular case you would use stl::string rather than char* (but this really depends on context of the question)
- Why is it important to use reference during "catch" statement?
"important" is inappropriate word, because it depends on the situation. One thing that might come to mind is that C++'s exception mechanism is able to catch exceptions of any type, for example a custom class. To avoid a copy of the exception object during exception twice, you may use a references. In this case the exception object is initialized to point to the actual thrown exception object. Exception is always thrown by value. Therefore, if you use by-value parameter in catch statement, you force the compiler to invoke the copy constructor twice.
- What's wrong with the following code ?
Type& ClassA::FunctionX();
{
Type1 obj;
obj.SetName("MyName");
return obj;
}
You cannot return the address of a local variable, because it will be destroyed when the function returns.
- What is wrong with the following version of the swap() function?
void swap(int& x, int& y)
{
x = y;
y = x;
}
It's not really a swap function, because the value of x will be hopelessly lost after the first assign.
- What is wrong with the following version of the swap() function?
void swap(int x, int y)
{
temp = x;
x = y;
y = x;
}
The values of x and y will not be changed (they will be changed inside of swap function, but not for the outside world.
- What is the difference between returning by value and returning by reference?
When you return the object by value, copy constructor is called, which is not the case when you return by reference (see item 22 of Scott Meyers.) But see item 23 for discussion when you don't have to pass by reference.
- How can C++ code call a function compiled by a C compiler?
You should use extern "C" for functions, compiled by C compiler and called within a C++ class. You should do that to force the linker to resolve the function name (precisely, the mangling of the name) correctly.
- How can you create a C++ function that is callable from C-compiled code?
What a perverted logic! Why would you need this?! Anyway, if you need this, you should know that only static or friend functions of the class may be called from C program. If you don't have static function, you should create a static wrapper around the member function. Then, declare the function with extern "C".
- What are the limitation when calling C++ functions from C-compiled code?
See previous answer. Only static and friend functions might be called from C program.
- What is the difference between C's string and C++'s string?
The major difference is that string in C is a char*, i.e. stream, while C++'s string is a class that works with streams.
- What is the difference between C and C++?
Shortly speaking, C++ is an object-oriented language (well, kind of), which supports such OO terms as encapsulation, inheritance and polymorphism. C is a functional programming language, which has no meaning of object (here you may shine with you erudition, mentioning object-oriented C, which does know about objects)
- Could you use the following function in C:
void foo(int i = 0; int j);
No, you cannot use this signature in C, since default parameters (int i = 0) are defined only in C++.
- What is the difference between list and vector?
There are few differences:
- list and vector implement fundamentally different data structures.
- The major difference is the way of element access. Vector elements may accessed randomly while list elements must be accessed sequentially.
- Iterators are different as a result of the first item: list support bi-directional iterators while vector uses random access iterator.
- Some methods are difference (for example, only list supports merge and splice, adds push_front and pop_front etc.
- What is an iterator?
Vague question, vague answer. You should ask the interviewer which iterator it means: as a general term, an STL object or a design pattern? Regardless of the answer, you should answer that iterator is an object that provides a sequential access to the elements of the other objects without knowing the internal structure of the controlled object.
- What is the difference between vector and array?
Again, context is important. Vector is a one-dimensional array. It's also an STL class while array is a data type.
- What is the difference between erase() and remove() of std::list?
This is from the collection of dumb questions. MSDN is there precisely for that! However, for the documentation purposes, remove() removes elements by value, while erase() uses iterator for the same purpose.
- What are the advantages and disadvantages of using a hash table?
First, I would strongly suggest to open any data structures book and to refresh your knowledge about hash tables. The advantage of hash table is quick access to stored elements. Disadvantage is that there no universal function to resolve the hash conflict and each and every way (chaining, rehashing etc.) has advantages and disadvntages for various cases.
- What is stl::string?
typedef basic_string string;
The type describes a specialization of template class basic_string
for elements of type char
- What are parameters of stl::basic_string's constructor? What is char_traits class?
class itself, char_traits, allocator. char_traits class is templatized class of one char's characterstics.
- Which of the following containers do not belong to STL: queue, list, vector?
All three belong to STL.
- Implement function equal from header file of STL
As a matter of fact (I though originally it was a joke) you might be required to write _any_ function from algorithm collection (I guess way to go for a standard committee)
- What are advantages/disadvantages of applying binary search algorithm to array (vector)?
Comments