Top 50 C++ Interview Questions And Answers

1.
Who is the founder of C++ language ?
  C++ language was created by Bjarne Stroustrup at AT & T Bell Laboratories, USA.
2.
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.
3.
What is class?
  A class is a group of objects of similar type that share common properties and relationships
4.
What is an object?
  An object is a real world entity. Each object holds data and code to operate on the data.
5.
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
6.
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++.
7.
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{}.
8.
What is cout in c++?
  cout is the standard output stream in c++ which is declared in stream.h header file.
9.
What is wchar_t?
  wchar_t is a wide character type, it has a size greater than the 8 bit character.
10.
What are Global variables?
  Global variables can be accessed anywhere in the code, within any function. It is declared outside of the main function.
11.
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
12.
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.
13.
What is cin?
  The input we provide through input device is stored in c++ standard input stream cin.
14.
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();
15.
What is function overloading?
  Function overloading is to define more than one function with the same name but with different behavior.
Example-
int multiply(int a, int b);
float multiplies(float a, float b, float c);
16.
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.
17.
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.
18.
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.
19.
What is the use of strlen?
  strlen is used to return length of string.
Example - size_t strlen(const char* string);
20.
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);
21.
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;
22.
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;

OR

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.
23.
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;
24.
What is typedef?
  typedef keyword is used to create an alias for a datatype.

Syntax of typedef operator -
typedef datatype aliasname;

Example-
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.
25.
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.
25.
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.



26.
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.
27.
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.
28.
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.
29.
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.
30.
What is an Identifiers?
  The name given to variables are called Identifiers.



30.
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.
31.
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).
32.
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.
33.
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.
34.
Mention the storage classes names in C++.
  The following are storage classes supported in C++ -
auto, static, extern, register and mutable



35.
Define logical operators with examples.
  Logical operators allow us to logically combine boolean variables, i.e. TRUE and FALSE.
Examples of logical operators are '&&', '||',
36.
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.
37.
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.
38.
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.



39.
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.
40.
What is constructor?
  A constructor is a member function that is executed automatically whenever an object is created.
41.
What is destructor?
  A destructor is a member function which is called automatically when an object is destroyed.
42.
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.



43.
Define copy constructor?
  A copy constructor is a member function which initializes an object using another object of the same class.
44.
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;
45.
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;
}
46.
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.



47.
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.
48.
What is Multiple Inheritance?
  Multiple Inheritance allows a child class to inherit from more than one parent class.
49.
What is the use of new operator?
  This versatile operator obtains memory from the operating system and returns a pointer to its starting point.
50.
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