Top 50 C++ Interview Questions And Answers

Who is the founder of C++ language ?
  C++ language was created by Bjarne Stroustrup at AT & T Bell Laboratories, USA.
What is Object Oriented Programming?
  Object oriented programming is a method of programming where a system is considered as a collection of objects that interact together to accomplish certain tasks. The main purpose of OOP is to simplify the design, programming and debugging a program.
What is class?
  A class is a group of objects of similar type that share common properties and relationships
What is an object?
  An object is a real world entity. Each object holds data and code to operate on the data.
What are advantages of Object Oriented Programming?
  Object oriented Programming offers major advantages-
1. easy to reuse code
2. low maintenance requirement
3. reduced susceptibility to errors
What was #include iostream.h?
  h is deprecated and not a standard header. It was used in older programs before C++ was standardized,
The header <iostream.h> is replaced by <iostream> in standard C++.
What is the role of main() function in c++?
  main() function is a necessary function, Program execution begin from main() function. All statements belong to the main are written within braces{}.
What is cout in c++?
  cout is the standard output stream in c++ which is declared in stream.h header file.
What is wchar_t?
  wchar_t is a wide character type, it has a size greater than the 8 bit character.
What are Global variables?
  Global variables can be accessed anywhere in the code, within any function. It is declared outside of the main function.
How to declare constants with specific type and without specific type?
  We can declare constants by using a const prefix with a specific type.
Syntax -
const datatype name = value;
Example -
const int width = 200;

and using #define preprocessor directive, we can declare constants without specific data type.
Syntax -
#defined identifier value
Example -
#define WIDTH 200
What is difference between pre increment or post increment?
  In pre increment, the value is increased before the expression is evaluated and in post increment, the value is increased after being evaluated.
Like in pre increment
i = 5;
j = ++i;
// i is 6 and j is 6

And in post increment
i = 5;
j = i++;
// i is 6 and j is 5.
What is cin?
  The input we provide through input device is stored in c++ standard input stream cin.
What is the use of exit function?
  exit function is used to terminate the running program. It is defined in stdlib.h library.
Example -
void exit();
What is function overloading?
  Function overloading is to define more than one function with the same name but with different behavior.
int multiply(int a, int b);
float multiplies(float a, float b, float c);
What is an inline function?
  If a function is inline, the compiler places the inline function call to the copy of code of that function at compile time. To make a function inline place inline keyword before the function.
What is the use of strcpy?
  It is used to copy the content of one string into another.

Like - char* strcpy (char* string2, char* string1);
It copies the content of string1 in string2 and returns string2.
What is the use of strcat?
  strcat is used to append one variable string into another.

Example - char* strcat(char* string1, char* string2);
It appends the content of string2 into string1 and return string1.
What is the use of strlen?
  strlen is used to return length of string.
Example - size_t strlen(const char* string);
Define strcmp?
  strcmp is used to compare two strings. It returns zero if both strings are equal.
Example - int strmp(const char* string1, const char* string2);
How can you know the address of a variable.
  By preceding the variable identifier with & (address operator), we can know the address of the stored variable.
Example - &number1;
What is the role of new operator?
  The new operator is used to assign dynamic memory to one single element of type and a block of element of type.
int *var1 = new int 3;


int *var2;
var2 = new int[3];

In a first example, operating system assigns space for var1 and in second example, operating system assign space for 3 elements of type int in a heap. It returns a pointer to the beginning block of memory that have empty space for 3 elements.
Is it possible to free the dynamic allocated memory?
  Yes, by using the delete operator, we can free the dynamic allocated memory of a single element or multiple elements.

Example- delete var1;
delete [] var2;
What is typedef?
  typedef keyword is used to create an alias for a datatype.

Syntax of typedef operator -
typedef datatype aliasname;

typedef short SmallNumer;
typedef unsigned int Positive;

In typedef short SmallNumber, SmallNumber can now be used as a data type to declare a variable for a small integer. In the second example, typedef unsigned int Positive, The Positive word can then be used to declare a variable of an unsigned int type.
What is Inheritance?
  Inheritance is a mechanism in which a class derived from existing base class. The derived class is called child class which can acquire the properties and methods of other classes.
What is Inheritance?
  Inheritance is a mechanism in which a class derived from existing base class. The derived class is called child class which can acquire the properties and methods of other classes.

What is a Member Function?
  A function contained within a class or that have their declaration inside the class is called Member Function. It operates on any object of the class of which it is a member.
What is Data Hiding?
  Data Hiding is a process of protecting data from access by unauthorized functions. In Object Oriented Programming, Data Hiding is used to hide internal object details.
What is Polymorphism?
  Polymorphism is an Object Oriented Programming concept in which a function or operator has ability to act in different ways on different datatypes.
What is whitespace? Is Compiler read the whitespaces?
  Whitespace is defined as spaces, carriage returns, linefeeds, tabs, vertical tabs, and formfeeds. No, Compiler ignores the whitespaces. All whitespace characters are invisible to the compiler.
What is an Identifiers?
  The name given to variables are called Identifiers.

What is const qualifier?
  The const qualifier specifies that the value of a variable will not change throughout the program. If we attempt to alter the value of a const, then it will elicit an error message from the compiler.
Define Relational Operators with examples?
  A relational operator compares two values. The values can be any built-in data type, such as int, char, float.
The examples of relational operator are '>' (greater than), '<' (less than), '==' (equal to), '!=' (Not equal to), '>=' (greater than or equal to), '<=' (less than or equal to).
Define getche() function?
  getche() is a non-standard function present in conio.h. It reads a single character from the keyboard and displays immediately on output screen without waiting for enter key.
What is the difference between overloading and overriding?
  Overloading is used to have same function name which behave differently depending upon parameters passed to them. Overriding function is used in inheritance when derived class function has to do some added or different tasks than the base class.
Mention the storage classes names in C++.
  The following are storage classes supported in C++ -
auto, static, extern, register and mutable

Define logical operators with examples.
  Logical operators allow us to logically combine boolean variables, i.e. TRUE and FALSE.
Examples of logical operators are '&&', '||',
Differenciate between Break and Continue?
  The break statement is placed inside the loop to terminate the iteration and start to execute the code immediately after the loop and the continue statement is also used inside a loop. When continue statement is executed, it stops the current iteration of the loop and continue with the next iteration of the loop.
What is a function?.
  A function groups a number of program statements into a unit and gives it a name. This unit can then be invoked from other parts of the program.
Define recursion function.
  A function is called recursive if a statement within the body of the function calls the same function. Recursion is the process defining something in terms of itself.

What is the scope of a variable?.
  The scope of a variable determines which parts of the program can access it.
These are two types of variable scope - local and global.
Variables with local scope are visible only within a block wheras variables with global scope are visible throughout a file.
What is constructor?
  A constructor is a member function that is executed automatically whenever an object is created.
What is destructor?
  A destructor is a member function which is called automatically when an object is destroyed.
What is a reference variable in C++?
  A reference variable is an alias name for the existing variable. Which mean both the variable name and reference variable point to the same memory location. Therefore updation on the original variable can be achieved using reference variable too.

Define copy constructor?
  A copy constructor is a member function which initializes an object using another object of the same class.
What is pointer?
  A pointer is a variable whose value is the address of another variable. Asterisk (*) is used to declare a pointer.
Example - int *ac;
What is virtual function?
  A virtual function is a member function that is declared within a base class and redefined by a derived class. Virtual keyword is used to make a function virtual.
Example -
virtual void display()
	cout <<ds;
What is the difference between actual and formal parameters?
  The arguments that are passed in a function call are called actual arguments wheras the formal arguments are the parameters/arguments in a function declaration.

What is a friend function?
  A function which is declared as friend function of any class can access private and protected members of that class.
What is Multiple Inheritance?
  Multiple Inheritance allows a child class to inherit from more than one parent class.
What is the use of new operator?
  This versatile operator obtains memory from the operating system and returns a pointer to its starting point.
What is type casting?
  Converting an expression of a given type into another type is known as type-casting.

C++ Related Articles

Queue using linked list c++
Queue implementation in c++
Program to find prime number
Program to find LCM of two numbers
Program to find armstrong number
Program to find HCF of two numbers
Program to reverse a number
Fibonacci series program
Palindrome number program
Program to find square root of a number