C++ Interview Questions
Q1. What is the full form of OOPS?
OOPS stands for Object Oriented Programming System.
Q2. What is a class?
A class is a data structure where entities, attributes and actions are clubbed under one name. Class is also considered as a carbon copy which reflects the entities, attributes and actions. In general, a class defines user defined data types. For example, when we say student class, it will have student name, class, age etc. as well as the methods needed for accessing or manipulating with its members.
#include
class Student{
public:
char Name[20];
char Class[15];
int Age;
void displayStdName();
int getAge(char stdName);
}
Q3. What is an object?
An instance of a class is called as object. A class defines the skeleton of the data that we need to represent. Object represents the class with its own values to class members. For example, Student is a class and it will be represented as above. We can create various instance of class, which can have different values at different instance of time.
Student std1, std2;
Here std1 and std2 are two objects of the class Student.
Q4. What is inheritance?
Inheritance is the process of getting the properties of existing class. That means, there will be one existing class, and another additional class will be created which will have properties of existing class as well as its own additional features. Suppose we have three classes – Person, Teacher and Student. Here Person would be a existing class – a base class, Teacher and Student will have the same property as Person, in addition they will have some more features that differentiate them from person or makes them more stronger. It is similar to parent child relationship.
Q5. What is the role of protected access specifier?
Protected data members can be accessed by the class in which it is declared / defined and its derived classes. But these members are not available for any other classes which are not derived class.
Q6. What is encapsulation?
It is the process of combining the data and its functions into one single name is called encapsulation. It is the way we combine all related data and their methods into class. In other words, in a class we can see data members as well as its functions under single name. So it will be easy to get all related data together.
Q7. What is abstraction?
It is the process of hiding all the implementation details from others and providing only the required information to the user. More clearly, when we have to calculate average, there would be two functions – one to calculate the sum and other one to divide the sum by the total number. When user requests for average, he need not know what the sum of the numbers provided is. User will be provided only the function details which actually call average. But he will not be aware of how it has been calculated.
Q8. List the types of inheritance supported in C++.
There are different inheritances supported by C++. Some of them are: Single, Multilevel, Multiple, Hierarchical and Hybrid inheritance.
Q9. Explain the purpose of the keyword volatile.
When we use
volatile
on the variable, it tells the compiler that it can be changed externally. Therefore compiler will not optimize the code while referencing the variables.Q10. What is an inline function?
When we prefix a keyword
inline
before any normal function, the function becomes inline function. These types of functions are considered as macros and they execute faster than the normal functions.Q11. What is a storage class?
Storage classes are used to indicate the scope of the variables and functions in a program.
Q12. Mention the storage class’s names in C++.
There are 5 types of storage classes:
auto
, static
, extern
, register
and mutable
Q13. What is the role of mutable storage class specifier?
Member variable of a constant class object can be altered by declaring it using mutable storage class specifier. This is applicable only for non-static and non-constant member variable of the class.
Q14. Distinguish between shallow copy and deep copy?
Shallow copy does memory allocation bit-by-bit from one object to another. Deep copy copies field by field from one object to another object. Deep copy is achieved by using copy constructor and or overloading ‘=’ (assignment operator).
Q15. What is pure virtual function in C++?
It is a virtual function with no function body and assigned with a value zero is known as pure virtual function.
Q16. What is an abstract class in C++?
Abstract class is the class with at least one pure virtual. One cannot instantiate abstract class. Objects are created out of its derived classes only.
Q17. What is a reference variable in C++?
This is another name for the variable. Both variable and reference variable will point to the same memory location. Hence changing the value of variable of reference variable will change the variable value.
Q18. What is role of static keyword on class member variable?
Static keyword is used for class member variables to share the common memory. That means, when objects are created using the class which has static member variable, then all the object’s static member variable will address same memory location. Hence this member variable will have same value in all the objects.
Q19. Explain the static member function.
A static member function can be called using the class name since static member exists before class objects comes into existence. It can access only static members of the class.
Q20. Which data type which can be used to store wide characters in C++?
Wide characters are stored using
wchar_t
data type.Q21. What operators are used to access the class members?
Normal class members are accessed using Dot (.) operator and pointer members are accessed using Arrow (->) operator.
Q22. Can we initialize a class/structure member variable as soon as the it is defined?
Defining a class/structure is a type definition which tells which types of variables and functions should be present in the class / structure and will not get any memory for them.
Q23. What is the data type to store the Boolean value?
Boolean values are represented using
bool
keyword. This is the primitive data type introduced in C++.Q24. What is function overloading?
Functions with same name and same list of parameters are called function overloading. It is also defined as function with same signature is called function overloading.
Q25. What is operator overloading?
Defining a new definition for the operator with respect to the class objects is known as operator overloading. For example, ‘+’ operator is used for adding two numbers. But if we use it to merge two numbers (5+8 = 58) then it is an operator overloading.
Q26. Do we have a String primitive data type in C++?
It is a class from Standard template library.
Q27. Name the default standard streams in C++.
There are four default standard streams in C++:
cin
, cout
, cerr
and clog
.Q28. Which access specifier/s can help to achieve data hiding in C++?
Private & Protected are the two access specifiers used to achieve data hiding.
Q29. When a class member is defined outside the class, which operator is used to associate the function definition to a particular class?
Scope resolution operator (::) can be used to serve this purpose.
Q30. What is a destructor? Can it be overloaded?
A destructor is the member function of the class with same name as the class name and it is prefixed with tilde (~) symbol. It gets executed automatically when the object is no more required in the program. It cannot be overloaded. Its only form is without the parameters.
Q31. What is a constructor?
Constructor is the function within the class with same name as class. It gets executed automatically when class objects are created.
Q32. What is a default constructor? Can we provide one for our class?
Every class contains a constructor provided by its compiler if the programmer doesn’t provide any constructor and known as default constructor. If the programmer writes a constructor with no parameter then that constructor is also called as default constructor if it does not have any parameters. Compiler doesn’t provides the constructor for them.
Q33. Which operator is used in C++ to allocate dynamic memory?
new
operator is used for dynamic memory allocation to the objects.Q34. What is the use of ‘delete’ operator?
delete
operator is used to release the memory of the object which is allocated using new
operator.Q35. Can we use malloc () function of C language to dynamically allocate memory in C++?
One can use malloc also to allocate dynamic memory, since C++ supports all the functionalities of C.
Q36. Can we use ‘delete’ operator to de-allocate the memory that is allocated using malloc() function of C language?
delete
operator cannot be used to release the memory allocated by malloc () command., One has to use free () to release the memory allocated by malloc ().Q37. What is a friend function?
As the name suggests, the function behaves as a friend to a class. Since it is a friend of a class, it can be used to access private and protected members of the class. A friend function is not a member of any class. But it must be programmed in the class definition.
Q38. What is a copy constructor in C++?
A copy constructor is the constructor which takes same class object reference as its parameter. It gets automatically called as soon as the object is initialized with another object of the same class during its creation.
Q39. Does C++ support exception handling? If so what are the keywords involved in exception handling?
C++ does support exception handling. It has try, catch & throw are keyword used for handling the exceptions.
Q40. Explain this pointer?
this
, is the pointer variable which always points to the current active object’s address.Q41. What is the difference between struct and class in C++?
The main difference between
struct
and class
is: in struct are members are public by default whereas in class, members are private by default.Q42. Can we implement all the concepts of OOPS using the keyword struct?
We can implement all the concepts of OOPS using structures.
Q43. What is the block scope variable?
A variable whose scope is valid or active only within a block is said to be block scope variable.
Q44. What is the role of the file opening mode ios::trunk?
If the file already exists, this command truncates the file before opening it.
Q45. What is the scope resolution operator?
The scope resolution operator is used to
- Determine the scope of global variables.
- Links the function definition to the class if the function is defined outside the class.
Q46. What is a namespace?
A namespace is the logical portion of the code which can be used to resolve the name conflict among the identifiers by placing them under different name space.
Q47. What are command line arguments?
The arguments or the parameters which are given to the main () function while executing from the command line/console are called as command line arguments. All the arguments sent are the strings only.
Q48. What is a class template?
A template class is a generic class. The keyword template is used to define a class template.
1. How can we catch all kind of exceptions in a single catch block?
Below is the syntax for using catch block to capture all kinds of exceptions.
catch(…)
{
//code to execute inside catch
}
Q49. What is keyword auto for?
All the local variables are automatic (
auto
) variable by default. We need not explicitly specify the variable as auto
while declaring it. In below example, both ‘I’ and ‘j’ are auto variables. But global variable cannot be auto
variable.void f()
{
int i;
auto int j;
}
Q50. What is a static variable?
These are the variable which will share the same memory. Hence its value will not be initialized to zero when any new object is created or function is called. It retains its previous value throughout the program. Its initial value would be zero. Latter it can be incremented or decremented. If a global variable is static then its visibility is limited to the same source code. In below program, the value of i will be automatically initialized to zero as soon as it is declared. It will be incremented subsequently as we call the function. i.e.; If function is called thrice then it will display 1, 2, 3.
#include
void f()
{
static int i;
++i;
printf("%d ", i);
}
Q51. What is the purpose of extern storage specifier?
It is used to resolve the scope of global symbo
#include
using namespace std;
int i = 20;
void main()
{
extern int i;
cout << i << endl;
}
Q52. What is the meaning of base address of the array?
Beginning memory address of the array is called base address of the array. It is the address of array with index zero, say &A[0].
Q53. When should we use the register storage specifier?
If a variable is used most frequently, then retrieving such variable from memory needs frequent access. Therefore it should be declared using register storage specifier, so that the compiler gives CPU register for its storage which speeds up the variable retrieval.
Q54. Can a program be compiled without main() function?
A program can be compiled without main program. But it should have main() function to execute the program.
Q55. Where an automatic variable is stored?
Every automatic variable are stored in stack memory. Even local variables are found in stack memory.
Q56. What is a container class?
A class with at least one member variable of another class type in it is called container class. In other words, a container class is the one which contains at least one object of another class as its member variable.
Q57. What is a token?
A token can be a keyword, an identifier, a constant, a string literal, or a symbol that are used in the program.
Q58. What is a preprocessor?
Preprocessor is a directive which directs the compiler to perform certain functions before compiling the actual code.
Q59. What are command line arguments?
In a general program, the user will be prompted to enter the data values when it executes. As the program goes on compiling and finds the request for any input, it prompts the user for their input. But in a command line program, while we enter the execute command itself, we need to input the values that are required in the program. Program will take the values entered as and when required. It will not prompt the user to enter the values while running the program.
This is achieved by passing the arguments to the main () function like below. It will have two parameters passed: first one is total number of arguments that are passed as the command line arguments. Second one is the array which will have the actual values that are used in the program for some operation. This can be any array with size equal to first argument. The second argument is always an array of character pointers.
main(int count, char *args[])
{
}
60. What are the different ways of passing parameters to the functions? Which to use when?
- Call by value: In this method, only the values are passed to the function as arguments. In this case, the values passed will be manipulated within the program, but its actual value in the variable will not be modified.
- Call by address: Here address of the actual parameters is passed instead of values. One can opt for this method if one needs to modify both actual parameters and formal parameters.
- Call by reference: The actual parameters are receives new reference variables as formal parameters. One can opt this method if they want both actual parameters and formal parameters to be modified.
61. What is reminder for 5.0 % 2?
It is an error as operand is a real number. Modulus function works only on integer values.
62. Which compilers switch to be used for compiling the programs using math library with g++ compiler?
Opiton –lm is used as compiler switch for compiling programs with math library.
g++ –lm <file.cpp>
63. Can we resize the allocated memory which was allocated using ‘new’ operator?
This operator does not allow to resize the allocated memory using ‘new’ operator. This operator is used for allocating new memory to the objects.
64. Who designed C++ programming language?
Bjarne Stroustrup designed C++ programming language.
65. Which operator can be used to determine the size of a data type/class or variable/object?
sizeof is used to find the size of the variable or object.
66. How can we refer to the global variable if the local and the global variable names are same?
Scope resolution operator (::) is used to refer the global variables in the program.
67. What are valid operations on pointers?
The valid operations on pointers are
- Comparison of two pointers
- Addition/Subtraction of two pointers (excluding void pointers)
68. What is recursion?
Function calling the same is called as recursion.
69. What is the first string in the argument vector w.r.t command line arguments?
First string in the command line argument is the name of the program.
70. What is the maximum length of an identifier?
In general it is 32 characters. But it varies depending on the machine.
71. What is the default function call method?
The functions are called by value by default.
72. What are available modes of inheritance to inherit one class from another?
Modes of inheritance are: Public, private & protected
73. What is the difference between delete and delete []?
delete will release the memory allocated to single object whereas delete [] will release the memory allocated to array of objects (all the memory assigned to the array of objects will be released).
74. Does an abstract class in C++ need to hold all pure virtual functions?
In order to a class to be abstract, at least one member function has to be pure virtual function.
75. Is it legal to assign a base class object to a derived class pointer?
It is not legal to assign base class object to a derived class pointer. The compiler will fail to do this, and throw an error.
76. What happens if an exception is thrown outside a try block?
The program will end abruptly.
77. Are the exceptions and error same?
They are not same. Exceptions are the errors or defects in the program that can be handled using try..catch blocks. Errors are also defects in the program that cannot be handled, but needs to be debugged.
78. What is function overriding?
If a function is declared in base class as virtual and is defined in the derived class with same name and signature, then it is called as function overriding.
79. Which function is used to move the stream pointer for the purpose of reading data from stream?
Seekg () is used for this purpose.
80. Which function is used to move the stream pointer for the purpose of writing data from stream?
Seekp () is used for this purpose.
81. Are class functions taken into consideration as part of the object size?
Only the class members (variables) are considered to calculate the object size.
82. Can we create and empty class? What would be the size of such object.
Yes, we can create an empty class. Size of the object of the empty class will be one.
83. What is ‘std’?
It is the default namespace defined by C++.
84. What is ‘cout’?
cout is the object of ostream class. The object ‘cout’ is connected to console output device by default.
85. What is ‘cin’?
cin is the object of istream class. The object ‘cin’ is connected to console input device by default.
86. What is the use of the keyword ‘using’ in C++?
The ‘using’ keyword is used before the namespace and is used for indicate the compiler that which namespace has to be used for the current code.
87. If a pointer declared for a class, which operator can be used to access its class members?
Arrow (->) operator can be used for accessing pointer member of the class.
88. What is difference between including the header file with-in angular and double quotes ?
If a header file is included with in angular bracket (<>) then the compiler searches for that header file only with in the built in include path. If a header file is included with in double quotes then the compiler searches for that header file first in the current working directory, if not found then it will search in the built in include path. This is because the header files within <> are built in header files where as the ones within the “” are user defined files.
89. S++ or S=S+1, which can be recommended to increment the value by 1? Why?
S++, is single machine instruction (INC) internally. Hence it will be faster than S=S+1.
90. What is the difference between actual parameters and formal parameters?
The parameters passed to the function at calling end are called as actual parameters and the parameters at the receiving of the function definition called as formal parameters.
91. What is the difference between variable declaration and variable definition?
When we declare a variable, the compiler will get know about its data type, whereas variable definition tells the value to the variable.
92. Which key word is used to perform unconditional branching?
goto keyword is used for unconditional branching in the program.
93. Is 068 a valid octal number?
No, since it has 8, it is an invalid octal number.
94. What is the purpose of #undef preprocessor?
It is used to undefine the macro definition that is already defined.
95. Can we nest multi line comments in a C++ code?
No, C++ does not support nesting a multiline comment.
96. What is a virtual destructor?
A virtual destructor guarantees that the objects are released in the reverse order of the object being constructed with respect to their inheritance.
97. What is the order of objects destroyed in the memory?
The objects are destroyed in the reverse order of their creation in the program.
98. What is a friend class?
A class is said to be friend class of another class, if the class members of one class can be accessed by another class. In other words, when two classes are not inherited or derived, and if we want to access the members of one class in another class, then we make one of the class friend to another. This is achieved by prefixing ‘friend’ keyword while declaring it.
Comments
Post a Comment