C++ Interview Questions

This website contains C++ interview questions with answers. We have given C++ interview questions faced by freshers and experienced in real interviews in IT industries.Dear readers, these C++ Programming Interview questions have been specially designed so that you can get acquainted with the nature of the questions you may be ask during your interview.

1. What are the differences between C and C++?

Answer:-The differences between C and C++ are :

1) When compared to C++, C is a subset of C++. C++ is a superset of C. C++ can run most of C code while C cannot run C++ code.
2) C is Procedural Language, C++ is non Procedural i.e Object oriented Language.
3)  Since C++ supports object oriented programming, it supports features like function overloading, templates, inheritance, virtual functions, friend functions. These features are absent in C.
4)  Top down approach is used in C Program Design. Bottom up approach adopted in C++ Program Design.
5)  C++ supports references, C doesn’t.
6)  In C, scanf() and printf() are mainly used input/output. C++ mainly uses streams to perform input and output operations. cin is standard input stream and cout is standard output stream.
7)  C was developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs. C++ was developed by Bjarne Stroustrup in 1979 with C++'s predecessor "C with Classes".
8)  Multiple Declaration of global variables are allowed in C. Multiple Declaration of global varioables are not allowed in C++.


2. Major Differences between JAVA and C++?

Answer:- Some of the major differences are:

1)  C++ is mainly used for system programming. Java is mainly used for application programming. It is widely used in window, web-based, enterprise and mobile applications.
2)  C++ supports multiple inheritance. Java doesn't support multiple inheritance through class. It can be achieved by interfaces in java.
3)  C++ supports pointers. You can write pointer program in C++. Java supports pointer internally. But you can't write the pointer program in java. It means java has restricted pointer support in java.
4)  C++ is platform-dependent. Java is platform-independent.
5)  C++ uses compiler only. C++ is compiled and run using the compiler which converts source code into machine code so, C++ is platform dependent. Java uses compiler and interpreter both. Java source code is converted into bytecode at compilation time. The interpreter executes this bytecode at runtime and produces output. Java is interpreted that is why it is platform independent.
6)  C++ supports structures and unions. Java doesn't support structures and unions.
7)  C++ doesn't support documentation comment. Java supports documentation comment (/** ... */) to create documentation for java source code.


3. What is OOPS?

Answer:- Object-Oriented Programming is abbreviated as OOPs. It is a technique of programming that focuses on the object.

It's main principles include —
1)  Abstraction:- hiding the complexity of the program — simplifies the program
2)  Encapsulation:- binding the code and data — protecting it from being tampered
3)  Inheritance:- reusability of the code — new object can inherit the property of parent object
4)  Polymorphism:-many forms — method overloading


4. What are the advantages of C++?

Answer:- C++ doesn't only maintains all aspects from C language, it also simplifies memory management and adds several features like:

1)  C++ Programming language is an extension of the C language, and it has a rich function library.
2)  C++ is a readable, Re-usable and Modular Language.
3)  C++ Programming Language allows function overloading and exception handling which are not possible in C.
4)  C++ is a highly portable language. This language is used for multi-device, multi-platform app development.
5)  C++ programming Languages convenience has a great benefit that no hardware affects the encoding of C++ language.
6)  The classes in the C++ programming language are a significant benefit from encoding point of view. It is flexible and well-organized.


5. What is the difference between reference and pointer?

Answer:- Following are the differences between reference and pointer:

1)  A pointer can be re-assigned any number of times while a reference cannot be re-assigned after binding.
2)  Pointers can point nowhere (NULL), whereas a reference always refers to an object.
3)  You can't take the address of a reference like you can with pointers.
4)  There's no "reference arithmetic" (but you can take the address of an object pointed by a reference and do pointer arithmetic on it as in &obj + 5).


6. What are virtual functions?

Answer:- A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.
Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.
For example:- a base class Animal could have a virtual function eat. Subclass Fish would implement eat() differently than subclass Wolf, but one can invoke eat() on any class instance referred to as Animal, and get the eat() behavior of the specific subclass.


7. What are C++ access specifiers?

Answer:- There are three access specifiers in C++.

1.  Public - when members are declared as Public ,they can be accessible from outside the Class through an object of the class.
2.  Protected - when members are declared as Protected then they can be accessible from outside the class but only in a class derived from it.
3.  Private - when members are declared as Private then they can only accessible from within the class.


8. What is a class?

Answer:- A class is a blueprint that defines the variables and the methods common to all objects of a certain kind.
Example :- Your bicycle is just one of many bicycles in the world. Using object-oriented terminology, we say that your bicycle object is an instance (in the glossary) of the class of objects known as bicycles. Bicycles have some state (current gear, current cadence, two wheels) and behavior (change gears, brake) in common. However, each bicycle's state is independent of and can be different from that of other bicycles. When building bicycles, manufacturers take advantage of the fact that bicycles share characteristics, building many bicycles from the same blueprint. It would be very inefficient to produce a new blueprint for every individual bicycle manufactured.


9. What are the different types of polymorphism in C++?

Answer:- The word polymorphism came from two Greek words ‘poly‘ and ‘morphs‘. Here poly means many and morphs means forms.Polymorphism represents the ability of an object to assume different forms. And it enables the programmers to create/write programs that are easier to understand and reuse.
1)  Compile-time Polymorphism:Polymorphism occurs in method overloading because method overloading allows access to different methods through the same interface.In method overloading, two or more methods in a class can use the same name as long as their parameter declarations are different.
2)  Run-time Polymorphism:The run-time polymorphism is also called dynamic polymorphism or late binding.C++ supports run-time polymorphism by dynamically dispatching methods at run time through method overriding.


10. Define namespace in C++?

Answer:- Namespace is used to define a scope where identifiers like variables, functions, classes, etc are declared. The main purpose of using a namespace is to prevent ambiguity that may occur when two identifiers have same name.
For example:- Run is an English word which can have different meanings that vary according to the context, like His running won him a gold medal at the Olympics and He's running a movie theater. Here, meaning of the some additional information/context is provided to prevent the ambiguity..


11. Can a program be compiled without a main() function?

Answer:- Yes, we can compile a C program without main function but it will not be exceuted as its execution starts from the main function only.


12. Define token in C++?

Answer:-Following are the C++ tokens : (most of c++ tokens are basically similar to the C tokens).
A token in C++ can be a keyword, identifier, literal, constant and symbol.


13. Which operations are permitted on pointers?

Answer:-Following are the operations that can be performed on pointers:
1)  Incrementing a Pointer:We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer.
2)  Decrementing a Pointer: The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data type.
3)  Pointer Comparisons: Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared.


14. How delete [] is different from delete?

Answer:- Delete is used to release a unit of memory, delete[] is used to release an array.
The delete operator deallocates memory and calls the destructor for a single object created with new.
The delete [] operator deallocates memory and calls destructors for an array of objects created with new [].


15. What is the difference between an array and a list?

Answer:-

Array:- An array is an ordered collection of items, where each item inside the array has an index.Taking the one dimensional array one step further, we can have arrays with two (or even more) dimensions.
List:-It’s a collection of items (called nodes) ordered in a linear sequence. There is a general theoretical programming concept of a list, and there is the specific implementation of a list data structure, which may have taken this basic list idea and added a whole bunch of functionality. Lists are implemented either as linked lists (singly, doubly, circular, …) or as dynamic (re-sizable) array.


16. What are the methods of exporting a function from a DLL?

Answer:- There are two ways:

1)  By using the DLL's type library.
2)  Taking a reference to the function from the DLL instance.


17. Define friend function?

Answer:- Friend functions are those functions which can access all the functions and variables of a class though it is not a member function of that class. Actually to share a function among two or more classes friend functions are used. If it is declared so, then it will able to access all variables and functions of those classes.

Use of Friend Function:
1)  When certain operator overloading is required friend functions can be useful.
2) Friend function makes the creation of some types of I/O functions easier.


18. When should we use multiple inheritance?

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


19. What does Scope Resolution operator do?

Answer:- Scope resolution operator will enable us to declare a function outside the class and it also helps us in knowing about the function and to which class it belongs to.
Syntax: return type classname:: function_name().


20. What is an overflow error?

Answer:- An overflow error is an error that happens when a program receives a number, value or variable outside the scope of its ability to handle. This type of error is somewhat common in programming, especially when dealing with integers or other numerical types of variables.


21. What is the difference between new() and malloc()?

Answer:- The difference between new() and malloc() is :-

New():- 1) The operator new is a specific feature of C++, Java, and C#.
2)"new" is an operator.
3)new doesn't need the sizeof operator as it allot enough memory for specific type.
4) Operator new can call the constructor of an object.
5) The operator new could initialize an object while allocating memory to it. 6) Operator new can be overloaded.
MALLOC( ):- 1) The function malloc( ) is a feature of C.
2)malloc( ) is a function.
3)malloc requires the sizeof operator to know what memory size it has to allot.
4) malloc( ) can not at all make a call to a constructor.
5) Memory initialization could not be done in malloc.
6) The malloc( ) can never be overloaded.


22. Define 'std'?

Answer:-Disch (13769) "std" a namespace.Std is the default namespace standard used in C++.


23. What is function overriding?

Answer:- When the base class and derived class have member functions with exactly the same name, same return-type, and same arguments list, then it is said to be function overriding.
Condition of function overriding:-
Functions of both parent and child class must have the same name.Functions must have the same argument list and return type.A function declared static cannot be overridden.If a function cannot be inherited, it cannot be overridden.


24. What is a pure virtual function?

Answer:- A virtual function whose declaration ends with =0 is called a pure virtual function.
Example :-virtual void features() = 0


25. What is the difference between struct and class?

Answer:- The differences between struct and class are :-

Structures:- 1) A structure is a user-defined data type which contains variables of dissimilar data types.
2)The variables of a structure are stored in the stack memory.
3)We cannot initialize the variables directly.
4) If access specifier is not specified, then by default the access specifier of the variable is "public".
5) The structure does not support the inheritance. 6) A structure is declared by using a struct keyword.
class:- 1) The class is a user-defined data type which contains member variables and member functions.
2)The variables of a class are stored in the heap memory.
3)We can initialize the member variables directly.
4) If access specifier is not specified, then by default the access specifier of a variable is "private".
5) The class supports the concept of inheritance.
6) The class is declared by using a class keyword.


26. What is a class template?

Answer:- Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.
Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.


27. What is concept of templates?

Answer:- The concept of templates can be used in two different ways:

Function Templates:-A function template works in a similar to a normal function, with one key difference.A single function template can work with different data types at once but, a single normal function can only work with one set of data types.
Class Templates:-Like function templates, you can also create class templates for generic class operations.Sometimes, you need a class implementation that is same for all classes, only the data types used are different.


28. What is a virtual destructor?

Answer:- Destructors in the Base class can be Virtual. Whenever Upcasting is done, Destructors of the Base class must be made virtual for proper destrucstion of the object when the program exits.

Pure Virtual Destructors in C++
1) Pure Virtual Destructors are legal in C++. Also, pure virtual Destructors must be defined, which is against the pure virtual behaviour.
2) The only difference between Virtual and Pure Virtual Destructor is, that pure virtual destructor will make its Base class Abstract, hence you cannot create object of that class.
3) There is no requirement of implementing pure virtual destructors in the derived classes.


29. Explain what is the use of void main () in C++ language?

Answer:- To run the C++ application it involves two steps, the first step is a compilation where conversion of C++ code to object code take place. While second step includes linking, where combining of object code from the programmer and from libraries takes place. This function is operated by main () in C++ language.


30. Explain what is Loop function? What are different types of Loops?

Answer:- In any programming language, to execute a set of statements repeatedly until a particular condition is satisfied Loop function is used. The loop statement is kept under the curly braces { } referred as Loop body.
In C++ language, three types of loops are used.
• While loop
• For loop
• Do-while loop







Also check :


Discussion



* You must be logged in to add comment.