If you are going to face an interview in the upcoming weeks or months, all you need is last-minute preparation for it. When it comes to a programming language, all you need is practice, practice, and practice!
We are Presenting here some of the top interview questions in c++ for experienced professionals. This ultimate list of the top interview questions on C++ will help you clarify many topics like Move constructor, Slack unwinding, Name mangling, etc., and land your dream job as C++ Developer, Senior C++ Developer, C++ Development Manager, etc. So just take out 10 mins every day and go through each of them to master the concepts of C++.
1. What do you mean by implicit conversion/coercion in c++?
Answer: Implicit conversion, also known as automatic type conversion. The compiler carries out this conversion on its own. It is done when one expression has multiple data types. To overcome this, the datatypes are upgraded to the datatype of larger variables. The main reason behind the conversion process is to avoid the loss of data.
Some loss of information may occur, and signs can be lost (if signed is implicitly converted to unsigned), and overflow can occur (if long is implicitly converted to float).
2. State the difference between realloc() and free().
Answer: void free()
the void free() function is used to deallocate a block of memory that was allocated using malloc(), calloc(), or realloc(). This function does not do anything if the ptr is null. It simply means that it empties the array.
void realloc()
It is used to free the memory in the program. This function helps resize and allocate the memory by having a prototype void * realloc (void * ptr, unsigned int num). This function modifies the size of the previously allocated memory pointed by *ptr. If the ‘ptr’ is an invalid pointer, its behavior may occur depending on the implementation technique. In case ptr is a null pointer, then realloc will behave like malloc(). If the ptr has already been deallocated by free(), or dealloc() or ptr do not match a pointer returned by malloc(), calloc() or realloc(), it shows an Undefined behavior.
3. What are pure virtual functions?
Answer: a pure virtual function is a function that doesn’t require any implementation. We are only required to declare it. It is also known as an abstract function. It is declared by assigning 0 in the declaration. An abstract class is a class that has at least one pure virtual function in it.
4. What are virtual destructors? State their use.
Answer: it works the same as virtual functions. It is used to free up the derived class object’s memory space while deleting instances of the derived class using a base class pointer object. If you remove an object of the subclass, which is referenced by a parent class pointer, only the base class’s destructor will get executed. But if the destructor is defined using a virtual keyword, both the destructors [ of parent and subclass ] will get invoked.
5. What is meant by the translation unit?
Answer: it is the basic unit of compilation in c++. It is the ultimate input to a C++ compiler from which an object file is generated. The app files are organized into different sources such as (.cpp, .cxx, etc.). When a source file is considered preprocessing, some extra content gets added to the source code(for example, the contents of header files included), and some content may be removed. This effective content is known as a translation unit.
The other important thing the translation unit consists of is the source file’s content along with the content of the file included directly or indirectly.
6. What is the scope resolution operator in C++?
Answer: scope resolution operator is a multipurpose function represented by a double colon(::). These are the various places they are used:
- It is used when there is a local variable with the same name as of global variable.
- When a function has to be defined outside a class, a scope resolution operator can handle this.
- When class’s static variables need to be accessed scope resolution operator deals with it.
- scope resolution operator is used when a class inside another class has to be referred
- In the case of multiple Inheritance arises, we handle this by using this operator.
7. What do you mean by visual C++?
Answer: C++ is a standard language, and Visual C++ is a product that implements the standard of C++. It is developed by Microsoft to create Windows applications in C or C++ language. One can write portable C++ programs using Visual C++, and can also use Microsoft-only extensions, which on one hand can destroy the portability but on the other enhances your productivity.
8. Is it possible to get the source code back from the binary file?
Answer: Technically, it is possible to generate the source code from binary. It is called reverse engineering. There are many reverse engineering tools available. But, in real-world problems, most of them will not regenerate the exact source code because much information will be lost due to compiler optimization and other interpretations.
9. List the differences between pointers and reference variables.
Answer: pointers
- Pointers can be repointed to any object, at any time, any number of times during the execution.
- Pointer has its memory address and location on the stack.
- Pointers can be assigned to NULL.
Reference variables:
- Reference variables must be initialized with an object when created, and they cannot be reinitialized to refer to another object.
- Reference variables have a particular location on the stack, but at the same time, they share the same memory location with the object they refer to.
- References cannot be assigned NULL. It should always be associated with actual memory, not NULL.
10. State the use of the “Volatile” Keyword.
Answer: Volatile Keyword is mainly used during the declaration of code. It reminds the compiler that the value may change at any time. It instructs the compiler that the variable declared using this Keyword may be used outside the current scope so that it would not possibly apply any optimization to the code. This situation matters only in the case of multi-threaded applications.