Interview Questions and Answers of C++ Advanced For Experienced

How to create a pure virtual function?
A function is made as pure virtual function by the using a specific signature, ” = 0″ appended to the function declaration as given below,class SymmetricShape{public:// draw() is a pure virtual function.virtual void draw() = 0;};Why pure virtual functions are used if they don’t have implementation / When does a pure virtual function become useful?Pure virtual functions are used when it doesn’t make sense to provide definition of a virtual function in the base class or a proper definition does not exists in the context of base class. Consider the above example, class Symmetric Shape is used as base class for shapes with symmetric structure(Circle, square, equilateral triangle etc). In this case, there exists no proper definition for function draw() in the base class Symmetric Shape instead the child classes of Symmetric Shape (Cirlce, Square etc) can implement this method and draw proper shape..What is virtual destructors? Why they are used?[This c++ interview question is in a way related to polymorphism.]Virtual destructors are used for the same purpose as virtual functions. When you remove an object of subclass, which is referenced by a parent class pointer, only destructor of base class will get executed. But if the destructor is defined using virtual keyword, both the destructors [ of parent and sub class ] will get invoked.What you mean by early binding and late binding? How it is related to dynamic binding?[This c++ interview question is related to question about virtual functions ]Binding is the process of linking actual address of functions or identifiers to their reference. This happens mainly two times.During compilation : This is called early binding
For all the direct function references compiler will replace the reference with actual address of the method.At runtime : This is called late binding.
In case of virtual function calls using a Base reference, as in shown in the example of question no: 2, compiler does not know which method will get called at run time. In this case compiler will replace the reference with code to get the address of function at runtime.
Dynamic binding is another name for late binding.What is meant by reference variable in C++?In C++, reference variable allows you create an alias (second name) for an already existing variable. A reference variable can be used to access (read/write) the original data. That means, both the variable and reference variable are attached to same memory location. In effect, if you change the value of a variable using reference variable, both will get changed (because both are attached to same memory location).26.How to create a reference variable in C++
Appending an ampersand (&) to the end of datatype makes a variable eligible to use as reference a = 20;
int& b = a;
The first statement initializes a an integer variable a. Second statement creates an integer reference initialized to variable a
Take a look at the below example to see how reference variables main ()
int a;
int& b = a;
a = 10;
cout << “Value of a : ” << a << endl;
cout << “Value of a reference (b) : ” << b << endl;
b = 20;
cout << “Value of a : ” << a << endl;
cout << “Value of a reference (b) : ” << b << endl;
return 0;
Above code creates following output.Value of a : 10
Value of a reference (b) : 10
Value of a : 20
Value of a reference (b) : 20 What are the difference between reference variables and pointers in C++?[This question is usually asked in a twisted way during c++ interviews. Sometimes the interviewer might use examples and ask you to find the error.]Pointers Reference Variables
Pointers can be assigned to NULL References cannot be assigned NULL. It should always be associated with actual memory, not NULL.
Pointers can be (re)pointed to any object, at any time, any number of times during the execution. Reference variables should be initialized with an object when they are created and they cannot be reinitialized to refer to another object
Pointer has own memory address and location on stack Reference variables has location on stack, but shares the same memory location with the object it refer to.
What will the line of code below print out and why?cout << 25u – 50;The answer is not -25. Rather, the answer (which will surprise many) is 4294967271, assuming 32 bit integers. Why?In C++, if the types of two operands differ from one another, then the operand with the “lower type” will be promoted to the type of the “higher type” operand, using the following type hierarchy (listed here from highest type to lowest type): long double, double, float, unsigned long int, long int, unsigned int, int (lowest).So when the two operands are, as in our example, 25u (unsigned int) and 50 (int), the 50 is promoted to also being an unsigned integer (i.e., 50u).Moreover, the result of the operation will be of the type of the operands. Therefore, the result of 25u – 50u will itself be an unsigned integer as well. So the result of -25 converts to 4294967271 when promoted to being an unsigned integer.C++ supports multiple inheritance. What is the “diamond problem” that can occur with multiple inheritance? Give an example.Let’s consider a simple example. A university has people who are affiliated with it. Some are students, some are faculty members, some are administrators, and so on. So a simple inheritance scheme might have different types of people in different roles, all of whom inherit from one common “Person” class. The Person class could define an abstract getRole() method which would then be overridden by its subclasses to return the correct role type.But now what happens if we want to model a the role of a Teaching Assistant (TA)? Typically, a TA is both a grad student and a faculty member. This yields the classic diamond problem of multiple inheritance and the resulting ambiguity regarding the TA’s getRole() method:Which getRole() implementation should the TA inherit? That of the Faculty Member or that of the Grad Student? The simple answer might be to have the TA class override the getRole() method and return newly-defined role called “TA”. But that answer is also imperfect as it would hide the fact that a TA is, in fact, both a faculty member and a grad student. What is the error in the code below and how should it be corrected?my_struct_t *bar;/* … do stuff, including setting bar to point to a defined my_struct_t object … */memset(bar, 0, sizeof(bar));The last argument to memset should be sizeof(*bar), not sizeof(bar). sizeof(bar) calculates the size of bar (i.e., the pointer itself) rather than the size of the structure pointed to by bar.The code can therefore be corrected by using sizeof(*bar) as the last argument in the call to memset.A sharp candidate might point out that using *bar will cause a dereferencing error if bar has not been assigned. Therefore an even safer solution would be to use sizeof(my_struct_t). However, an even sharper candidate must know that in this case using *bar is absolutely safe within the call to sizeof, even if bar has not been initialized yet, since sizeof is a compile time construct. What will i and j equal after the code below is executed? Explain your i = 5;int j = i++;After the above code executes, i will equal 6, but j will equal 5.Understanding the reason for this is fundamental to understanding how the unary increment (++) and decrement (–) operators work in C++.When these operators precede a variable, the value of the variable is modified first and then the modified value is used. For example, if we modified the above code snippet to instead say int j = ++i;, i would be incremented to 6 and then j would be set to that modified value, so both would end up being equal to 6.However, when these operators follow a variable, the unmodified value of the variable is used and then it is incremented or decremented. That’s why, in the statement int j = i++; in the above code snippet, j is first set to the unmodified value of i (i.e., 5) and then i is incremented to 6.

Leave a Reply