C++ Interview Questions

Question-1. What is an object ?

Answer- An instance of the class is called as object.

Question-2. Types of Inheritance in C++ ?

Answer- Single, Multilevel, Multiple, Hierarchical and Hybrid.

Question-3. What is the role of protected access specifier ?

Answer- If a class member is protected then it is accessible in the inherited class. However, outside the both the private and protected members are not accessible.

Question-4. Explain the purpose of the keyword volatile ?

Answer- Declaring a variable volatile directs the compiler that the variable can be changed externally. Hence avoiding compiler optimization on the variable reference.

Question-5. What is an Inline function ?

Answer- A function prefixed with the keyword inline before the function definition is called as inline function. The inline functions are faster in execution when compared to normal functions as the compiler treats inline functions as macros.

Question-6. What is a storage class ?

Answer- Storage class specifies the life or scope of symbols such as variable or functions.

Question-7. Mention the storage classes names in C++ ?

Answer- The following are storage classes supported in C++ auto, static, extern, register and mutable

Question-8. What is the role of mutable storage class specifier ?

Answer- A constant class object’s member variable can be altered by declaring it using mutable storage class specifier. Applicable only for non-static and non-constant member variable of the class.

Question-9. Distinguish between shallow copy and deep copy ?

Answer- Shallow copy does memory dumping bit-by-bit from one object to another. Deep copy is copy field by field from object to another. Deep copy is achieved using copy constructor and or overloading assignment operator.

Question-10. What is a pure virtual function ?

Answer- A virtual function with no function body and assigned with a value zero is called as pure virtual function.

Question-11. What is abstract class in C++ ?

Answer- A class with at least one pure virtual function is called as abstract class. We cannot instantiate an abstract class.

Question-12. What is reference variable in C++ ?

Answer- 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.

Question-13. What is the role of static keyword on a class member variable ?

Answer- A static variable does exist though the objects for the respective class are not created. Static member variable share a common memory across all the objects created for the respective class. A static member variable can be referred using the class name itself.

Question-14. Explain the static member function ?

Answer- A static member function can be invoked using the class name as it exists before class objects comes into existence. It can access only static members of the class.

Question-15. What is function overloading ?

Answer- Defining several functions with the same name with unique list of parameters is called as function overloading.

Question-16. What is operator overloading ?

Answer- Defining a new job for the existing operator w.r.t the class objects is called as operator overloading.

Question-17. What is default standard streams in C++ ?

Answer- cin, cout, cerr and clog.

Question-18. Which access specifier/s can help to achieve data hiding in C++ ?

Answer- Private and Protected

Question-19. When a class member is defined outside the class, which operator can be used to associate the function definition to a particular class ?

Answer- Scope resolution operator (::)

Question-20. What is destructor? Can it be overloaded ?

Answer- 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 gets executed automatically w.r.t the object as soon as the object loses its scope. It cannot be overloaded and the only form is without the parameters.

Question-21. What is constructor ?

Answer- A constructor is the member function of the class which is having the same as the class name and gets executed automatically as soon as the object for the respective class is created.

Question-22. What is default constructor ? Can we provide one for our class ?

Answer- Every class does have a constructor provided by the compiler if the programmer doesn’t provides one and known as default constructor. A programmer provided constructor with no parameters is called as default constructor. In such case compiler doesn’t provides the constructor.

Question-23. What is the purpose of ‘delete’ operator ?

Answer- ‘delete’ operator is used to release the dynamic memory which was created using ‘new’ operator.

Question-24. Can I use malloc() function of C language to allocate dynamic memory in C++ ?

Answer- Yes, as C is the subset of C++, we can all the functions of C in C++ too.

Question-25. Can I use ‘delete’ operator to release the memory which was allocated using malloc() function of C language ?

Answer- No, we need to use free() of C language for the same.

Question-26. What is a friend function ?

Answer- A function which is not a member of the class but still can access all the member of the class is called so. To make it happen we need to declare within the required class following the keyword ‘friend’.

Question-27. What is a copy constructor ?

Answer- A copy constructor is the constructor which take same class object reference as the parameter. It gets automatically invoked as soon as the object is initialized with another object of the same class at the time of its creation

Question-28. Does C++ supports exception handling? If so what are the keywords involved in achieving the same.

Answer- C++ does supports exception handling. try, catch & throw are keyword used for the same.

Question-29.Explain the pointer- this ?

Answer- This, is the pointer variable of the compiler which always holds the current active object’s address.

Question-30. What is the difference between the keywords struct and class in C++ ?

Answer- By default the members of struct are public and by default the members of the class are private.

Question-31. Can we implement all the concepts of OOPS using the keyword struct ?

Answer- Yes.

Question-32. What is the block scope variable in C++ ?

Answer- A variable whose scope is applicable only within a block is said so. Also a variable in C++ can be declared anywhere within the block.

Question-33. What is the role of the file opening mode ios::trunk ?

Answer- If the file already exists, its content will be truncated before opening the file.

Question-34. What is the scope resolution operator ?

Answer- The scope resolution operator is used to
• Resolve the scope of global variables.
• To associate function definition to a class if the function is defined outside the class.
                            

Question-35. What is a namespace ?

Answer- A namespace is the logical division of the code which can be used to resolve the name conflict of the identifiers by placing them under different name space.

Question-36. What are command line arguments ?

Answer- The arguments/parameters which are sent to the main() function while executing from the command line/console are called so. All the arguments sent are the strings only.

Question-37. What is a class template ?

Answer- A template class is a generic class. The keyword template can be used to define a class template.

Question-38. What is the meaning of base address of the array ?

Answer- The starting address of the array is called as the base address of the array.

Question-39. When should we use the register storage specifier ?

Answer- If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable.

Question-40. Can a program be compiled without main() function ?

Answer- Yes, it can be but cannot be executed, as the execution requires main() function definition.

Question-41. Where an automatic variable is stored ?

Answer- Every local variable by default being an auto variable is stored in stack memory.

Question-42. What is a container class ?

Answer- A class containing at least one member variable of another class type in it is called so.

Question-43. What is a token ?

Answer- A C++ program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.

Question-44. What is a preprocessors ?

Answer- Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

Question-45. What are the different ways of passing parameters to the functions? Which to use when?

Answer-
• Call by value: We send only values to the function as parameters. We choose this if we do not want the
 actual parameters to be modified with formal parameters but just used.
• Call by address: We send address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.
• Call by reference: The actual parameters are received with the C++ new reference variables as formal parameters. We choose this if we do want the actual parameters to be modified with formal parameters.

Question-46. What is reminder for 5.0 % 2 ?

Answer- Error, It is invalid that either of the operands for the modulus operator (%) is a real number.

Question-47. Which compiler switch to be used for compiling the programs using math library with g++ compiler ?

Answer- Opiton –lm to be used as > g++ –lm

Question-48. What is recursion ?

Answer- Function calling itself is called as recursion.

Question-49. What is the maximum length of an identifier ?

Answer- Ideally it is 32 characters and also implementation dependent.

Question-50. What is the default function call method ?

Answer-By default the functions are called by value.

Question-51. What are available mode of inheritance to inherit one class from another ?

Answer-Public, Private and Protected.

Question-52. What is the difference between delete and delete[] ?

Answer- Delete[] is used to release the array allocated memory which was allocated using new[] and delete is used to release one chunk of memory which was allocated using new.

Question-53. Does an abstract class in C++ need to hold all pure virtual functions ?

Answer- Not necessarily, a class having at least one pure virtual function is abstract class too.

Question-54. Is it legal to assign a base class object to a derived class pointer ?

Answer- No, it will be error as the compiler fails to do conversion.

Question-55. What is function overriding ?

Answer- Defining the functions within the base and derived class with the same signature and name where the base class’s function is virtual.

Question-56. Are the exceptions and error same? ?

Answer- No, exceptions can be handled whereas program cannot resolve errors.

Question-57. Which function is used to move the stream pointer for the purpose of reading data from stream ?

Answer- seekg()

Question-58. Which function is used to move the stream pointer for the purpose of writing data from stream ?

Answer- seekp()

Question-59. Are class functions taken into consideration as part of the object size ?

Answer- No, only the class member variables determines the size of the respective class object.

Question-60. Can we create and empty class? If so what would be the size of such object ?

Answer- We can create an empty class and the object size will be 1.

Question-61. What is ‘std’ ?

Answer- Default namespace defined by C++.

Question-62. What is the full form of STL ?

Answer- Standard template library.

Question-63. What is ‘cout’ ?

Answer- cout is the object of ostream class. The stream ‘cout’ is by default connected to console output device.

Question-64. What is ‘cin’ ?

Answer- cin is the object of istream class. The stream ‘cin’ is by default connected to console input device.

Quedtion-65. What is the use of the keyword ‘using’ ?

Answer- It is used to specify the namespace being used in.

Question-66. If a pointer declared for a class, which operator can be used to access its class members ?

Answer- Arrow (->) operator can be used for the same.

Question-67. What is difference between including the header file with-in angular braces < > and double quotes “ “ ?

Answer- If a header file is included with in < > then the compiler searches for the particular header file only with in the built in include path. If a header file is included with in “ “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built in include path.

Question-68. S++ or S=S+1, which can be recommended to increment the value by 1 and why ?

Answer- S++, as it is single machine instruction (INC) internally.

Question-69. What is the difference between actual and formal parameters ?

Answer- The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters.

Question-70. What is the difference between variable declaration and variable definition ?

Answer- Declaration associates type to the variable whereas definition gives the value to the variable.

Question-71. Which key word is used to perform unconditional branching ?

Answer- goto.

Question-72. What is the purpose of #undef preprocessor ?

Answer- It will be used to undefine an existing macro definition.

Question-73. What is virtual destructor ?

Answer- A virtual destructor ensures that the objects resources are released in the reverse order of the object being constructed w.r.t inherited object.

Question-74. What is the order of objects destroyed in the memory ?

Answer- The objects are destroyed in the reverse order of their creation.

Question-75. What is a friend class ?

Answer- A class members can gain accessibility over other class member by placing the class declaration prefixed with the keyword ‘friend’ in the destination class.

Question-76. What is C++ ?

Answer- C++ is an object oriented programming language created by Bjarne Stroustrup. It is released in 1985.

Question-77. What are the advantages of C++ ?

Answer- C++ doesn't only maintains all aspects from C language, it also simplify memory management and add several features like:
Includes a new datatype known as a class.
Allows object oriented programming.
                            

Question-78. What is the difference between C and C++ ?

Answer-
C C++
1) C follows the procedural style programming. 1) C++ is multi-paradigm. It supports both procedural and object oriented.
2) Data is less secured in C. 2) In C++, you can use modifiers for class members to make it inaccessible for outside users.
3) C follows the top-down approach. 3) C++ follows the bottom-up approach.
4) C does not support function overloading. 4) C++ supports function overloading.
5) In C, you can't use functions in structure. 5) In C++, you can use functions in structure.
6) C does not support reference variables.C++ supports reference variables.
7) In C, scanf() and printf() are mainly used for input/output.7) C++ mainly uses stream cin and cout to perform input and output operations.

Question-79. What is the difference between reference and pointer ?

Answer-
Reference Pointer
1) References are less powerful than pointers. Once a reference is created, it can't refer to other object later. 1) Pointers provide the powerful facilities than references.
2) References are safer and easier to use than pointers. 2) Pointers are comparatively difficult to use.

Question-80. What is the first string in the argument vector w.r.t command line arguments ?

Answer- Program name.

Question-81. What are the C++ access specifiers ?

Answer- The access specifiers are used to define how to functions and variables can be accessed outside the class. There are three types of access specifiers:
Private: Functions and variables declared as private can be accessed only within the same class 
and they cannot be accessed outside the class they are declared.
Public: Functions and variables declared under public can be accessed from anywhere.
Protected: Functions and variables declared as protected cannot be accessed outside the class except a child class. This specifier is generally used in inheritance.

Question-82. What is Object Oriented Programming (OOP) ?

Answer- OOP is a methodology or paradigm that provides many concepts. The basic concepts of Object Oriented Programming are given below:
Classes and Objects: Classes are used to specify the structure of the data. They define datatype. 
You can create any number of objects from a class. Objects are the instances of classes.
Encapsulation: Encapsulation is a mechanism which binds the data and associated operations together and thus hide the data from outside world. Encapsulation is also known as data hiding. In C++, It is achieved using the access specifiers i.e. public, private and protected .
Abstraction: Abstraction is used to hide the internal implementations and show only the necessary details to the outer world. Data abstraction is implemented using interfaces and abstract classes in C++. Some people confused about Encapsulation and abstraction. But they both are different. Inheritance: Inheritance is used to inherit the property of one class into another class. It facilitates you to define one class in term of another class.

Question-82. What is the difference between array and a list ?

Answer- Array is a collection of homogeneous elements while list is a collection of heterogeneous elements. Array memory allocation is static and continuous while List memory allocation is dynamic and random. In Array, users don't need to keep in track of next memory allocation while In list user has to keep in track of next location where memory is allocated.

Question-83. What is the difference between new() and malloc() ?

Answer- new() is a preprocessor while malloc() is a function. There is no need to allocate the memory while using "new" but in malloc() you have to use sizeof(). "new" initializes the new memory to 0 while malloc() gives random value in the newly allotted memory location.

Question-84. What are the methods of exporting a function from a DLL ?

Answer- There are two ways: By using the DLL's type library. Taking a reference to the function from the DLL instance.

Question-85. What is virtual function ?

Answer- A virtual function is used to replace the implementation provided by the base class. The replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.

Question-86. When should we use multiple inheritance ?

Answer- You can answer this question in three manners: Never Rarely If you find that the problem domain cannot be accurately modeled any other way.

Question-87. What is an overflow error? ?

Answer- It is a type of arithmetical error. It is happen when the result of an arithmetical operation been greater than the actual space provided by the system.

Question-88. What is virtual inheritance ?

Answer- Virtual inheritance facilitates you to create only one copy of each object even if the object appears more than one in the hierarchy.

Question-89. What does Scope Resolution operator ?

Answer- A scope resolution operator(::) is used to define the member function outside the class.
Tutors
  • Manjit Singh

  • Umesh Kumar

  • Sandeep Kaur

  • Ramandeep Singh

  • Sunil Sharma

  • Vijay Mahajan

  • Gurpreet Kaur

Our Skills
  • Java 100%
  • Android 95%
  • IOS 85%
  • PHP 95%
  • Design 90%
  • HTML CSS 95%
  • WordPress 95%
  • Magento 95%
  • UI 95%
FOLLOW US ON
facebook link  linkedin link  google plus link  twitter link  website rss link
Placements Done Over 5000
WE ACCEPT ONLINE PAYMENTS
online payment
PAY ONLINE
online payment