C and C++ programming Interview Questions and Answers

C and C++ programming Interview Questions and Answers. If you are preparing for an interview then here is the collection of most important questions and answers in C and C++.

1. What is a pointer on pointer?

Ans: It’s a pointer variable which can hold the address of another pointer variable. Eg- int a=10 , *p=&a, **q=&p;  read more about Pointer in c.

2. Distinguish between malloc() & calloc() memory allocation?

Ans Both calloc() and malloc() allocates memory from heap area/dynamic memory But by default calloc fills the allocated memory with 0’s. Memory allocation in C.

3. Difference between c and c++?

Ans C is a procedural programming language and does not support classes and objects, whereas C++ is a combination of both procedural and object-oriented programming languages.

4. i++ or i=i+1 which can be recommended to increment the value of i by 1 and why?

Ans i++ is recommended because it is single machine instruction (INC) internally.

5. What is recursion?

Ans- Recursion is the process of repeating items in a self-similar way. In programming languages.

6. What is an abstract class in C++?

Ans An abstract class in C++ is a class that has at least one pure virtual function (i.e., a function that has no definition). The classes inheriting the abstract class must provide a definition for the pure virtual function, otherwise, the subclass would become an abstract class itself.

7. Can we initialize a class/structure member variable as soon as the same is defined?

Ans No, We can’t because defining a class/structure is just a type definition and will not be allocated memory for the same.

8. What is a destructor? Can it be overloaded?

Ans A destructor is the member function of the class which is having the same name as the class name and prefixed with tilde (~) symbol. It cannot be overloaded and the only form is without the parameters.

9. What will be the output of the following program? 

#include<iostream>
using namespace std;
// global variable
int global = 19;
int main()
{
int global = 22; //local variable
cout << global << endl;
}

Ans : (detail Explanation)

The variable “global” declared at the top is global and stores the value 19 whereas that declared within the main function is local and stores a value 22. So, the question is when the value stored in the variable named “global” is printed from the main function then what will be the output? 19 or 22?

Usually when two variables with the same name are defined then the compiler produces a compile-time error. But if the variables are defined in different scopes then the compiler allows it.

Whenever there is a local variable defined with the same name as that of a global variable then the compiler will give precedence to the local variable Here in the above program also, the local variable named “global” is given precedence. So the output is 22.

10. Which is the most preferred loop for traversing an array and why?

Ans: (Detailed Explanation)

The most preferable loop for traversing an array is the “for loop” since an array uses the index of the element for traversal. Using the for loop ensures easy access to the element.
Basically, an iterative block is required along with a looping variable ‘i’ serving an index counter, incrementing from:

0 to ((length of the array) – 1)
For instance,
Array [ 5 ] = {10, 20, 30, 40, 50}
for(i=0;i<5;i++)
{
cout<<Array[i];
}