Oct 9, 2019
C++ interview questions
C++ is a general-purpose programming language with a bias toward systems programming. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early 1980's. Stroustrup, an admirer of Simula 67 and a strong supporter of C, wanted to combine the best of both languages and create a more powerful language that could support object-oriented programming features and still retain the power and elegance of C. The result is C++.
C++ is a versatile object-oriented programming language for handling very large programs. It is suitable for virtually any programming task including the development of editors, compilers, database, communication system and any complex real-life application systems. C++ is a superset of C. Most of known part of the C can be applied to C++. Therefore, almost all C programs are also C++ programs. In this article, we will highlight some C++ Interview Questions that have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of C++.
So, let's start it -
Q. What is C++?
Ans. C++ is an object-oriented programming language created by Bjarne Stroustrup. It is released in 1985.
Q. Why do use C++?
Ans. C++ doesn't only maintains all aspects from C language, it also simplifies memory management and add several features like - it includes a new data type known as a class and allows object-oriented programming.
Q. What are the differences between C and C++?
Ans. C++ is a kind of superset of C, most C programs except few exceptions (See this and this) work in C++ as well.
C is a procedural programming language, but C++ supports both procedural and Object Oriented programming.
Since C++ supports object-oriented programming, it supports features like function overloading, templates, inheritance, virtual functions, friend functions. These features are absent in C.
C++ supports exception handling at the language level, in C exception handling is done in traditional if-else style.
C++ supports references, C doesn’t.
In C, scanf() and printf() are mainly used input/output. C++ mainly uses streams to perform input and output operations. cin is the standard input stream and cout is standard output stream.
Q. What is C++ access specifiers?
Ans. Access specifiers are used to defining how the members (functions and variables) can be accessed outside the class.
Private - Members declared as private are accessible only within the same class and they cannot be accessed outside the class they are declared. Child classes are also not allowed to access private members of the parent.
Public - Members declared as the public are accessible from anywhere.
Protected - Only the class and its child classes can access protected members.
Q.What is Object Oriented Programming (OOP)?
Ans. 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 the 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 hides the data from the 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++.
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.
Q. What is the difference between new() and malloc()?
Ans. 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.
Q. What is a class?
Ans. A class is a blueprint which reflects the entities attributes and actions. Technically defining a class is designing a user-defined data type.
Q. What is a virtual function?
Ans. 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.
Q. What is the destructor?
Ans. A destructor is used to delete any extra resources allocated by the object.
Q. How should runtime errors be handled in C++?
Ans. The runtime errors in C++ can be handled using exceptions. This exception handling mechanism in C++ is developed to handle the errors in software made up of independently developed components operating in one process and under synchronous control. According to C++, any routine that does not fulfill its promise throws an exception. The caller who knows the way to handle these exceptions can catch it.
Q. Where are setjmp and longjmp used in C++?
Ans. Setjmp and longjmp should not be used in C++. Longjmp jumps out of the function without unwinding the stack. This means that the local objects generated are not destructed properly. The better option is to use try/catch/throw instead. They properly destruct the local objects.
Q. Explain storage qualifiers in C++?
Ans. Const - This variable means that if the memory is initialised once, it should not be altered by a program.
Volatile - This variable means that the value in the memory location can be altered even though nothing in the program code modifies the contents.
Mutable - This variable means that a particular member of a structure or class can be altered even if a particular structure variable, class or class member function is constant.
Q. How does code-bloating occur in C++?
Ans. Improper use of Inline functions and templates may lead to code bloat. Multiple Inheritance may also lead to code bloat.
Q. What is a stack? How can it be implemented?
Ans. A Stack is a linear data structure which is a collection of homogeneous elements but where insertion and deletion operations take place at one end only, called TOP of the stack. The stack is also known as LIFO (Last In First Out) structure as the element which is inserted in the last will be the first element to come out from the stack.
It can be implemented by using a linear array say S  containing st size locations along with an integer variable TOP storing the index of the topmost element in the stack.
Q. What is Member Functions in Classes?
Ans. The member function regulates the behavior of the class. It provides a definition for supporting various operations on data held in the form of an object.
Q. What is Polymorphism in C++?
Ans. Polymorphism in C++ is the ability to call different functions by using only one type of the function call. Polymorphism is referred to codes, operations or objects that behave differently in a different context.
Q. What is data encapsulation in C++?
Ans. Encapsulation is an object-oriented programming concept (oops) which binds together the data and functions. It is also referred as data hiding mechanism.
Q. What is multithreading in C++?
Ans. To run two or more programs simultaneously multithreading is useful. There are two types -
Process-based - It handles the concurrent execution of the program.
Thread-based - It deals with the concurrent execution of pieces of the same program.
Q. What is upcasting in C++?
Ans. Upcasting is the act of converting a subclass reference or pointer into its superclass reference or pointer is called upcasting.
Q. Explain what is preprocessor in C++?
Ans. Pre-processors are the directives, which give instruction to the compiler to pre-process the information before actual compilation starts.
Q. What is a reference variable in C++?
Ans. 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.
Q. Do we have a String primitive data type in C++?
Ans. No, it’s a class from STL (Standard template library).
Q. Which operator can be used in C++ to allocate dynamic memory?
Ans. ‘new’ is the operator can be used for the same.
Lastly say, these are all not only questions that you will be asked in the interviews. There is a huge list of questions available related to C++ that interviewer may ask, actually, it only depends on them. Hope this article successfully guided you and helped you to understand the level of interview questions. Best of luck for the future! Thank you!