One of the most asked Programming Questions for Interview is from C++. Because it is still as relevant today as it was during its advent in the mid-80s. The imperative, object-oriented programming language is widely used as a general-purpose programming language. As such, several jobs require candidates to have a profound understanding of C++. If you wish to sharpen your C++
So these are the best Programming Questions for Interview in C++
1st & Most common Programming Questions for Interview is “Define C++”?
This Question is first and foremost Programming Questions for Interview in C++, whose probability to asked is around 90-100%. So below is the answer
Answer: C++ is a computer programming language that is a superset of C wherein additional features are made in the C language.
What is the precedence when there are a Global variable and a Local variable in the program with the same name?
Answer: Whenever there is a local variable with the same name as that of a global variable, the compiler gives precedence to the local variable.
int globalVar = 2;
int globalVar = 5;
The output of the above code is 5. This is because, although both the variables have the same name, the compiler has given preference to the local scope
When there are a Global variable and Local variable with the same name, how will you access the global variable?
Answer: When there are two variables with the same name but different scope, i.e. one is a local variable and the other is a global variable, the compiler will give preference to a local variable.
In order to access the global variable, we make use of a “scope resolution operator (::)”. Using this operator, we can access the value of the global variable.
int x= 10;
int x= 2;
cout<<”Global Variable x = “<<::x;
cout<<”\nlocal Variable x= “<<x;
What are the Comments in C++?
Answer: Comments in C++ are simply a piece of source code ignored by the compiler. They are only helpful for a programmer to add a description or additional information about their source code.
In C++ there are two ways to add comments:
• //single-line comment
• /* block comment */
The first type will discard everything after the compiler encounters “//”. In the second type, the compiler discards everything between “/*” and “*/”.
What are the various Compound Assignment Operators in C++?
Answer: Following are the Compound assignation operators in C++:
+=, -=, *=, /=, %=, >>=, <<=, &=, ^=,|=
Compound assignation operator is one of the most important features of C++ language which allow us to change the value of a variable with one of the basic operators:
value += increase; is equivalent to value = value + increase;
if base_salary is a variable of type int.
int base_salary = 1000;
base_salary += 1000; #base_salary = base_salary + 1000
base_salary *= 5; #base_salary = base_salary * 5;
What are the Extraction and Insertion operators in C++? Explain with examples.
Answer: In the iostream.h library of C++, cin, and cout are the two data streams that are used for input and output respectively. Cout is normally directed to the screen and cin is assigned to the keyboard.
“cin” (extraction operator): By using overloaded operator >> with cin stream, C++ handles the standard
As shown in the above example, an integer variable ‘age’ is declared and then it waits for cin (keyboard) to enter the data. “cin” processes the input only when the RETURN key is pressed.
“cout” (insertion operator): This is used in conjunction with the overloaded << operator. It directs the data that followed it into the cout stream.
What are Default Parameters? How are they evaluated in the C++ function?
Answer: Default Parameter is a value that is assigned to each parameter while declaring a function. This value is used if that parameter is left blank while calling to the function. To specify a default value for a particular parameter, we simply assign a value to the parameter in the function declaration. If the value is not passed for this parameter during the function call, then the compiler uses the default value provided. If a value is specified, then this default value is stepped on and the passed value is used.
int multiply(int a, int b=2)
r = a * b;
As shown in the above code, there are two calls to multiply function. In the first call, only one parameter is passed with a value. In this case, the second parameter is the default value provided. But in the second call, as both the parameter values are passed, the default value is overridden and the passed value is used.
Why are arrays usually processed with for loop?
Answer: Array uses the index to traverse each of its elements. If A is an array, then each of its elements is accessed as A[i]. Programmatically, all that is required for this to work is an iterative block with a loop variable i that serves as an index (counter) incrementing from 0 to A. length-1. This is exactly what a loop does and this is the reason why we process arrays using for loops.
Explain Mutable Storage class specifier.
Answer: The variable of a constant class object’s member cannot be changed. However, by declaring the variables as “mutable”, we can change the values of these variables.
What is the keyword auto for?
Answer: By default, every local variable of the function is automatic i.e., auto. In the below function both the variables ‘i’ and ‘j’ are automatic variables.
auto int j;
NOTE: A global variable is not an automatic variable
What is a Static Variable?
Answer: A static variable is a local variable that retains its value across the function calls. Static variables are declared using the keyword “static”. Numeric variables which are static have the default value as zero. The following function will print 1 2 3 if called thrice.
static int i;
If a global variable is static, then its visibility is limited to the same source code.
Difference between Class and Structure.
Answer: Structure: In C language, the structure is used to bundle different types of data types together. The variables inside a structure are called the members of the structure. These members are by default public and can be accessed by using the structure name followed by a dot operator and then the member’s name.
Class: Class is a successor of the Structure. C++ extends the structure definition to include the functions that operate on its members. By default, all the members inside the class are private
What is the use of ‘using’ declaration?
Answer: Using Declaration is used to refer a name from the namespace without the scope resolution operator.
What is Name Mangling?
Answer: C++ compiler encodes the parameter types with function/method into a unique name. This process is called name mangling. The inverse process is called as demangling.
A:: b (int, long) const is mangled as ‘b__C3Ail’.
For a constructor, the method name is left out.
That is A:: A (int, long) const is mangled as ‘C3Ail’.
Explain Function Overloading and Operator Overloading.
Answer: C++ supports OOPs concept Polymorphism which means “many forms”. In C++ we have two types of polymorphism, i.e., Compile-time polymorphism, and Run-time polymorphism. Compile-time polymorphism is achieved by using an Overloading technique. Overloading simply means giving additional meaning to an entity by keeping its base meaning intact. C++ supports two types of overloading:
Function Overloading: Function overloading is a technique that allows the programmer to have more than one function with the same name but different parameter list. In other words, we overload the function with different arguments i.e., be it the type of arguments, number of arguments or the order of arguments. Function overloading is never achieved on its return type.
Operator Overloading: This is yet another type of compile-time polymorphism that is supported by C++. In operator overloading, an operator is overloaded, so that it can operate on the user-defined types as well with the operands of the standard data type. But while doing this, the standard definition of that operator is kept intact. For Example, an Addition operator (+) that operates on numerical data types can be overloaded to operate on two objects just like an object of complex number class.
What is the difference between Method Overloading and Method Overriding in C++?
Answer: Method overloading is having functions with the same name but different argument lists. This is a form of compile-time polymorphism.
Method overriding comes into picture when we rewrite the method that is derived from a base class. Method overriding is used while dealing with run-time polymorphism or virtual functions.
Does C++ support Multilevel and Multiple Inheritances?
What is an Iterator class?
Answer: In C++ a container class is a collection of different objects. If we need to traverse through this collection of objects, we cannot do it using simple index variables. Hence, we have a special class in STL called an Iterator class which can be used to step through the contents of the container class. The various categories of iterators include input iterators, output iterators, forward iterators, bidirectional iterators, random access, etc.
What is the difference between an External Iterator and an Internal Iterator? Describe an advantage of the External Iterator.
Answer: An internal iterator is implemented with member functions of the class that has items to step through. An external iterator is implemented as a separate class that can be bound to the object that has items to step through. The basic advantage of an External iterator is that it’s easy to implement as it is implemented as a separate class. Secondly, as it’s a different class, many iterator objects can be active simultaneously.
Comment on C++ standard exceptions?
Answer: C++ supports some standard exceptions that can be caught if we put the code inside the try block. These exceptions are a part of the base class “std:: exception”. This class is defined in the C++ header file <exception>.
Few Examples of Exceptions supported by this class include:
bad_alloc – thrown by ‘new’
runtime_error – thrown for runtime errors
bad_typeid – thrown by type id
So these were the Programming Questions for Interview in C++.
You can try these codes on online compiler