C++ Interview Questions

What is C++?

As an extension of the C language, C++ was developed by Bjarne Stroustrup as a general purpose cross-platform language which gives programmers a high level of control over system resources and memory.

What is namespace in C++?

“If there are two or more functions with the same name defined in different libraries then how will the compiler know which one to refer to? Thus namespace came to picture. A namespace defines a scope and differentiates functions, classes, variables etc. with the same name available in different libraries. The namespace starts with the keyword “namespace”. The syntax for the same is as follows:

namespace namespace_name {
// code declarations
}”

How to input string in C++?

“There are three ways to input a string, using cin, get, and getline. All three methods are mentioned in the sample program below.

include

using namespace std;

int main()
{
char s[10];

cout << ""Enter a string: "";
cin >> str;

cout << ""\nEnter another string: "";
cin.get(s, 10);

getline(cin, str);

return 0;

}”

C++ Interview Questions

What is operator overloading in C++?

An overloaded declaration is a declaration in the same scope of function or operator declared with the same name more than once.

How to learn C++?

C++ is a programming language which is an extension of C. Thus, one should prefer to learn C first (it’s not necessary). After learning C, then understand the basic difference between C and C++. Implement all the basic programs you learnt in C in C++ also. Then dive into the OOPs concept of C++. Do as many hands-on as possible to understand basic OOPs, and then dive into advanced level OOPs. When all the basics are clear, build a small game to understand the structure and remain concepts if any. By following all these steps one can learn C++.

What is the difference between C and C++?

The difference between c and C++ is that C++ is a object oriented language, which means that it has all the features of C as well as its own thing that is the concept of OOP. C++ has many functionalities of OOP that are missing from C such as encapsulation, abstraction, classes, objects, etc.

Advance C++ Interview Questions

How to reverse a string in C++?

“To reverse a string, a sample code is mentioned below.

include

include

using namespace std;
int main ()
{
char n[50], t;
int i, j;
cout << “”Enter a string : “”;
gets(n);
i = strlen(n) – 1;
for (j = 0; j < i; j++,i–)
{
t = s[j];
s[j] = s[i];
s[i] = t;
}
cout << “”\nReverse string : “” << s;
return 0;
}”

What is template in C++?

“A template in C++ is used to pass data types as parameters . These make it easier and more simpler to use classes and functions.

template

            int fun (T a,T b)
            {
                    return (a+b);
            }

            int main(){
                    cout<<fun<int>(11,22);
            }"

What is using namespace std in C++?

Using namespace std in C++ tells the compiler that you will be making use of the name space called ‘std’. The ‘std’ namespace contains all the features of the standard library. You need to put this statement at the start of all your C++ codes if you don’t want to keep on writing std:: in front of every variable/string or whatever standard library feature you are making use of, as it becomes tedious to do so.

C++ Interview Questions

How to download turbo C++ for windows 10?

“To download turbo C++ follow the steps mentioned below:
Step-1: Download turbo C++ from http://www.turboc8.com/p/download.html
Step-2: Extract Turbo.C.3.2.zip file.
Step-3: Run setup.exe file.
Step-4: Follow the instructions mentioned.”

How to paste in turbo C++?

“Paste in turbo C++ can be done by two techniques:

  1. Shift+ Insert
  2. Open the file in notepad with .CPP extension. Make the changes and save it. After saving the file, you can open it from the Turbo C++ application file menu from where you stored the cpp file.”

What is pointer in C++?

“Pointers in C++ are a data type that store the memory address of another variable.
For eg.

char *str = “”Hi, How are you?””;
Here the pointer variable *str points to the string “”Hi, How are you?””

            or

            int age;
            int *int_value;

            *int_value = &age;

            cout<<""Enter your age please:"";
            cin>>age;

            cout<<""\n Your age is:""<<*int_value;"

Advance C++ Interview Questions

What is function in C++?

“A function in C++ is a block of code that can be referenced from anywhere in the system and that serves a specific purpose.

int fun(){
int a = 11;
return 11;
}

    int main(){

            int b = fun();
    }"

What is destructor in C++?

Destructors in C++ are special function/methods that are used to remove memory allocation for objects. They are called usually when the scope of an object ends. eg. when a function ends you can call a destructor. They are of the same name as the class – syntax – ~();

Who invented C++?

Bjarne Stroustrup invented C++ in 1985.

C++ Interview Questions

How to convert integer to string in C++?

“There are 2 approaches to convert integer variables to string. Both the approaches with a sample code are mentioned below.

Approach-1

include

include

using namespace std;
void main()
{
int n= 1;
string s= to_string(n);
cout << s;
}

Approach-2

include

include

include

using namespace std;
int main()
{
int n = 17;

// declaring output string stream 
ostringstream s1; 

// Sending a number as a stream into output str
s<< n; 
// the str() converts number into string 
string fin = s.str(); 
// Displaying the string
cout << fin; 
return 0; 

} “

What is function overloading in C++?

Function Overloading happens in C++ when two or more functions share the same name. They can be differentiated on the basis of the type of data they are passing as parameters or even the number of parameters they are passing. eg. int fun(char a); & int fun(int b); & void fun(int a, int b)

How to run C++ program in cmd?

verify gcc installtion using the command:
$ gcc -v

then go to your working directory or folder where your code is:
$ cd

then build the file containing your c code as such:
$ gcc main.cpp

            or

    $ g++ -o main main.cpp 

then run the executable generated in your system:
$ main.exe”

Advance C++ Interview Questions

What is type casting in C++?

Type casting in C is used to change the data type. They are of two types: Implicit Type Conversion: It is automatic. Explicit Type Conversion: It is user-defined.

How to use string in C++?

“A string is a sequence of characters. In C++, string is a data type as well as a header file. This header file consists of powerful functions of string manipulation. A variable of string is declared as follows:

string str= “”Hello””;

And to use string one needs to include the header file.

// Include the string library

include

// Create a string variable
string str= “”Hello””;”

How to input string in C++ with spaces?

“The code to input a string in C++ with spaces is as follows:

include

include

using namespace std;

int main()
{
string s;

cout << ""Enter the sentence""; 
getline(cin, s); 
cout << str;
return 0; 

} “

C++ Interview Questions

What is stream in C++?

Stream refers to a stream of characters to be transferred between program thread and i/o.

What is the difference between structure and class in C++?

“The difference between structure and class is as follows:
– By default, the data members of class are private whereas data members of structure are public.
– While implementing inheritance, the access specifier for struct is public whereas for class its private.
– Structures do not have data hiding features whereas class does.
– Structures contain only data members whereas class contains data members as well as member functions.
– In structure, data members are not initialized with a value whereas in class, data members can be initialised.
– Structures are stored as stack in memory whereas class is stored as heap in memory.”

How to clear screen in C++?

One can clear screen using – clrscr() or system(“clear”).

Advance C++ Interview Questions

Who developed C++?

Bjarne Stroustrup in 1998 at Bell Labs developed the language C++.

How to compile and run C program in notepad++ ?

“To compile and run c program in notepad++ follow the steps mentioned below:
Step-1: Download and install notepad++
Step-2: Download and install MinGw gcc along with gcc.
Step-3: Configure notepad++ for gcc. This step can be further divided into two sub-steps. A: Create C compiler tool in Notepad++
B: Creating C execution tool.
Step-4: Execute C program in Notepad++”

How many keywords in C++ ?

There are 95 reserved keywords in C++ which are not available for re-definition or overloading.

C++ Interview Questions

What is iostream in C++?

It is a header file that includes basic objects such as cin, cout, cerr, clog.

How to give space in C++?

“In C++ programming, the space can be given using the following code.
cout << ” ” ;”

How to dynamically allocate a 2d array in C++ ?

“There are several methods by which one can allocate memory to 2D array dynamically one of which is as follows.

include

int main()
{
int row = 2, col = 2;
int* a = new int[row * col];

int i, j, count = 0; 
for (i = 0; i <  row; i++) 
  for (j = 0; j < col; j++) 
     *(a+ i*col + j) = count++; 

for (i = 0; i <  row; i++) 
  for (j = 0; j < col; j++) 
     printf(""%d "", *(a + i*col + j)); 

delete[ ] a;
return 0; 

} “

Advance C++ Interview Questions

How to use goto statement in C++ ?

“Goto statement provided unconditional jump in the code. The syntax is: goto label;

label: statement;

include

using namespace std;

void main () {
float d, avg, add = 0.0;
int j, n;
cin >> n;

for(j = 1; j <= n; ++j)
{
    cout << ""Enter number"" << i;
    cin >> d;

    if(d < 0.0)
    {
           goto jump;
    } 
    add+= d;
}

jump:
avg = add/ (j- 1);
cout << avg;
}”

What is function overriding in C++?

“When a function with same name is present in both parent and child class then it is called function overriding.

include

using namespace std;
class parent {
public:
void display(){
cout<<“”Parent Class””;
}
};
class child: public parent{
public:
void display() {
cout<<“”Child Class””;
}
};
int main() {
child o = parent();
o.display();
return 0;
}”

Which operator cannot be overloaded in C++ ?

“Some of the operators that cannot be overloaded are as follows:

– Dot operator- “.”
– Scope resolution operator- “::”
– “size of” operator
– Pointer to member operator- “.*””

C++ Interview Questions

How to copy and paste in turbo C++ ?

“Press Ctrl + Insert to copy.
Press Shift + Insert to paste.”

Why C++?

“The use of C++ is varied such as:
– It is used in developing graphic user interface based applications like adobe photoshop.
– It is used in developing games as it overrides the complexity of 3D games.
– There are many animated softwares developed in C++
– Most of the compilers are written in C++.
– Google Chrome, Mozilla Firefox etc. web browser are developed using C++

There are many more such uses that make C++ a desired language.”

What is bool in C++?

“Bool is a data type in C++ which takes two values- True and False. Syntax is as follows:
bool b1 = true;

A sample code is as follows:

include

using namespace std;
int main()
{
int a= 60, b= 70;
bool c, d;
c= a== b; // false

c= a< b; // true 

cout <<b1; 
cout << b2 ; 

return 0; 

}”

Advance C++ Interview Questions

What is exception in C++ ?

“Runtime abnormal conditions that occur in the program are called exceptions. These are of 2 types:
– Synchronous
– Asynchronous

C++ has 3 specific keywords for handling these exceptions:
– try
– catch
– throw”

How to set decimal places in C++ ?

“For limiting the decimal places in C++ there are five functions : floor(), ceil(), trunc(), round() and set precision(). Out of these five, only set precision() function is used for setting the decimal places to put as output. All the functions are mentioned in the following sample code.

include

using namespace std;

int main()
{
float a =2.33333;
cout << floor(a) << endl;
cout << ceil(a) << endl;
cout << trunc(a) << endl;
cout << round(a) << endl;
cout << setprecision(2) << a;
return 0;
} “

How to get absolute value in C++?

“In C++, there are three functions in the cstdlib header file to return the absolute value of the integer. Those are:
– abs()
– labs()
– llabs()

The syntax for all the functions is same – function_ name(integer value)

The difference lies in the range for integer value being passed as an argument. For abs() its type int in C++. For labs(), its type long int in C++ and for llabs() its long int in C++.

Sample code for the illustrating the three functions is as follows:

include

include

using namespace std;

int main()
{
int a, b, c;

a = abs(22); 
b= labs(1234355L); 
c= llabs(1234863551LL);
cout << a; 
cout << b; 
cout<< c;
return 0; 

} “

C++ Interview Questions

What is the difference between C++ and Java?

“The difference between C++ and java are as follows:

– C++ supports go to statements whereas Java does not.
– C++ is majorly used in system programming whereas Java is majorly used in application programming.
– C++ supports multiple inheritance whereas Java does not support multiple inheritance
– C++ supports operator overloading whereas Java does not support operator overloading.
– C++ has pointers which can be used in the program whereas Java has pointers but internally.
– C++ uses a compiler only whereas Java uses both compiler and interpreter.
– C++ has both call by value and call by reference whereas Java supports only call by value.
– C++ supports structures and joins whereas Java does not support structure and joins
– Java supports unsigned right shift operator (>>>) whereas C++ does not.
– C++ is interactive with hardware whereas Java is not that interactive with hardware.”

How to concatenate string in C++ ?

“The strings in C++ can be concatenated in two ways- one considering them string objects and second concatenating them C style strings.

include

using namespace std;

int main()
{
string s_1, s_2, fin;
cout << “”Enter string””;
getline (cin, s_1);
cout << “”Enter string “”;
getline (cin, s_2);
fin= s_1 + s_2;
cout << fin;

char str1[50], str2[50], fin[100];

cout << ""Enter string"";
cin.getline(str1, 50);

cout << ""Enter string"";
cin.getline(str2, 50);

strcat(str1, str2); 

cout << ""str1 = "" << str1 << endl;
cout << ""str2 = "" << str2;

return 0;

}”

How to convert char to int in C++ ?

“There are three methods for converting char variable to int type variable. These are as follows: – atoi()
– scanf()
– typecasting

A sample code depicting all three functions are as follows:

include

include

int main() {
char *s = “”6790″”;
char d = ‘s’;
int a,b,c;

sscanf(s, “”%d””, &a); // Using sscanf
printf(“”a : %d””, a);

b = atoi(s); // Using atoi()
printf(“b : %d””, b);

c = (int)(d); // Using typecasting
printf(“”c : %d””, c);

return 0;
}”

Advance C++ Interview Questions

How to generate random numbers in C++ with a range?

“Using the rand() function we can generate random numbers in C++ within a range.

include

include

int main()
{
int max=100, min=54,i;
int range = max – min + 1;
for (i=min; i<max;i++)
{
int num = rand() % range + min;
cout<<num;
}
return 0;
}”

What is stack in C++?

“A linear data structure which implements all the operations (push, pop) in LIFO (Last In First Out) order. Stack can be implemented using either arrays or linked list. The operations in Stack are
– Push: adding element to stack
– Pop: removing element from stack
– is Empty: returns true if stack is empty
– Top: returns the top most element in stack”

What is conio.h in C++?

Conio.h is a header file used for console input and output operations and is used for creating text based user interfaces.

C++ Interview Questions

How to find absolute value in C++?

“To find the absolute value in c++, we can use abs() function. The abs() function in C++ returns the absolute value of an integer number.

include

include

using namespace std;

int main()
{
int a=3.456;
int x = abs(a);
cout << x;
return 0;
}”

How to exit from turbo C++?

To exit Turbo C++, use the Quit option under the File Menu, or press Alt + X.

What is iterator in C++?

Any object which has an ability to iterate through elements of the range it has been pointing to is called iterator.

Advance C++ Interview Questions

What is :: in C++?

:: is called a scope resolution operator which is used to access global variables with the same name as of local variables, for defining functions outside the class, for accessing static variables, and for referring to a class inside of another class.

C++ Part 2C++ Part 3
Back to top