10 Advanced C++ Interview Questions and Answers
Tags: beginners, programming, tutorial, opensource
Published: 2023-10-13 05:09:15

## 1. What is a mutable storage class specifier? How can it be used?

Answer: A mutable storage class specifier is used only on the class’s non-static and non-constant member variables. It’s used for altering the constant class object’s member by declaring it. This can be done by using a storage class specifier.

You can’t use the mutable specifier with names declared as static or const or reference members.


## 2. What are the differences between a shallow and a deep copy?


### Shallow Copy: 

1. A shallow copy of an object creates a new object but copies references to the elements within the original object. Changes to the inner elements are reflected in both the original and the shallow copy.

2. It copies only the top-level structure of an object, not its nested objects.

3. Has a copy depth of 1, meaning it only clones the top-level object and references to the inner objects.

4. Useful when you want to create a new object with the same structure but sharing some inner elements, like lists or dictionaries.

5. Typically created using methods like slicing, the copy() method, or using the copy module in Python.

6. Usually faster and more memory-efficient because it doesn't duplicate nested objects.

7. Shares references to nested objects, so changes made within nested objects are reflected in both the original and the shallow copy.

### Deep Copy: 

1. A deep copy of an object creates a completely independent duplicate, including all nested objects. Changes to inner elements in the copy do not affect the original.
Level of Copying:

2. It duplicates the entire object hierarchy, including nested objects.

3. Has a copy depth that extends to all levels, creating duplicates of every nested object.

4. Suitable when you need a completely independent copy of an object, ensuring that changes in the copied object do not affect the original.

5. Achieved using libraries like copy in Python, which provides functions to perform deep copies.

6. Slower and may consume more memory due to duplicating the entire object hierarchy.

7. Creates independent copies of all nested objects, ensuring changes do not affect the original object.

## 3. What is an abstract class in C++?

Answer: An abstract class in C++ is referred to as the base class, which has at least one pure virtual function and is designed to be specifically used as a base class.
You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.


## 4. Can we have a string primitive data type in C++?

Answer: A primitive type is a data type where the values it can represent have a straightforward nature (a number, a character, or a truth value). The primitive types are the most basic building blocks for any programming language and are the base for more complex data types.

We cannot have a String Primitive data type in C++. Instead, we can have a class from the Standard Template Library (STL).

## 5. What is the function of scope resolution operator in C++?
Answer: The scope resolution operator refers to the out-of-scope global variable or member function. It’s denoted by the double colon (::) symbol.

Functions of the scope resolution operator include the following:

They help to resolve the scope of various global variables.
When defined outside the class, a function allows associating the function with the class.


## 6. What are the C++ tokens?
Answer: Generally, a token is an object that represents something else, such as another object (physical or virtual), or an abstract concept, such as a gift, sometimes referred to as a symbol of the giver’s respect for the recipient.

A token is a name given to various functions in C++ programs. Examples of tokens include a keyword, symbol, string literal, identifier, constant, etc.

## 7. What is the diamond problem?
Answer: The diamond problem in C++ occurs when two superclasses of a class have a common base class, representing the inability of the programming language to support hybrid inheritance using multiple and hierarchical inheritances.

## 8. What is a block scope variable?
Answer: Block scope is the definition of a variable within a block of code, such as a for loop or if statement, and a variable specified as a block using C++ that can be declared anywhere within the block.


## 9. How is the data hiding concept achieved in C++?
Answer: Access specifiers define how members of a class (attributes and methods) can be accessed.

C++ supports data hiding and, accordingly, data abstraction and encapsulation by creating user-defined types known as classes. Members of this generated class are scoped by keywords known as access specifiers.

## 10. What is a “translation unit” in C++?
Answer: According to standard C++, a translation unit is the basic compilation unit in C++. It consists of the contents of a single source file and any header file directly or indirectly included in it; lines are ignored by conditional preprocessing statements.

A translation unit consists of:

Contents of a source file
Plus contents of files included directly or indirectly
Minus source code lines ignored by any conditional preprocessing directives ( the lines ignored by #ifdef,#ifndef, etc.)