C++ is a programming language that is implemented on a wide variety of operating system platforms and used for hardware design. 

This language has following features:
Compiled, Statically typed, Multi-paradigm and Free-form.

This Language also consists of both high-level and low-level language features.

cpp-interview

Here is a list of C++ Interview questions with answers which are asked at the time of campus interviews, placements.

Top C++ Interview Questions and Answers for freshers and experienced are below are below ::


1. What is C++?
C++ is created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is an object-oriented computer language used in the development of enterprise and commercial applications.
Microsoft’s Visual C++ became the premier language of choice among developers and programmers.

2. What is polymorphism? Explain with an example?
"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object.

Example: function overloading, function overriding, virtual functions. Another example can be a plus ‘+’ sign, used for adding two integers or for using it to concatenate two strings.

3. What is the difference between C & C++?
C++ is an object oriented programing but c is a procedure oriented programing. C is super set of C++. 
C can’t support inheritance, function overloading, method overloading etc. but C++ can do this. 
In c-program the main function could not return a value but in the C++ the main function should return a value.

4. What is a class?
Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class.

5. What is the difference between class and structure?
By default, the members of structures are public while that tor class is private.
Structures doesn’t provide something like data hiding which is provided by the classes.
Structures contains only data while class bind both data and member functions.


6. Define Constructors?
A constructor is a member function with the same name as its class. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class.

7. What is the difference between a copy constructor and an overloaded assignment operator?
A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

8. What do you mean by implicit conversion?
Whenever data types are mixed in an expression then C++ performs the conversion automatically.
Here smaller type is converted to wider type.

Example : in case of integer and float integer is converted into float type.

9. What is dynamic binding?
Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run time. It is associated with polymorphism and inheritance.

10. What is the difference between an object and a class?
I. Classes and objects are separate but related concepts.
Every object belongs to a class and every class contains one or more related objects.

II. A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.

The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.

III. An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.


11. What is public, protected, private?
Public, protected and private are three access specifiers in C++.
Public data members and member functions are accessible outside the class.
Protected data members and member functions are only available to derived classes.
Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.

12. In C++, what is the difference between method overloading and method overriding?
Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

13. What is difference between function overloading and operator overloading?
A function is overloaded when same name is given to different function.
While overloading a function, the return type of the functions need to be the same.

Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types.
Operator overloading allows existing C++ operators to be redefined so that they Customer is king inc.,

work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).

14. Define inheritance?
The mechanism of deriving a new class (derived) from an old class (base class) is called inheritance. It allows the extension and reuse of existing code without having to rewrite the code from scratch. Inheritance is the process by which objects of one class acquire properties of objects of another class.

advantages of inheritance : It permits code reusability. Reusability saves time in program development. Itencourages the reuse of proven and debugged high-quality software, thus reducing
problem after a system becomes functional.

15. What is friend function?
The function declaration should be preceded by the keyword friend. The function definitions does not use either the keyword or the scope operator ::. The functions that are declared with the keyword friend as friend function. Thus, a friend function is an ordinary function or a member of another class.


16. What are virtual functions?
A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure 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. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived
class.

17. What do you mean by pure virtual functions?
A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.
class Shape { public: virtual void draw() = 0; };

18. What is an iterator?
Iterators are like pointers. They are used to access the elements of containers thus providing a link between algorithms and containers. Iterators are defined for specific containers and used as arguments to algorithms.

19. What are the differences between new and malloc?
New initializes the allocated memory by calling the constructor. Memory allocated with new should be released with delete.

Malloc allocates uninitialized memory.

The allocated memory has to be released with free. New automatically calls the constructor while malloc (doesn’t).

20. What is an explicit constructor?
A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. It’s purpose is reserved explicitly for construction. Explicit constructors are simply constructors that cannot take part in an implicit conversion.


21. What do you mean by inline function?
An inline function is a function that is expanded inline when invoked.ie. the compiler replaces the function call with the corresponding function code. An inline function is a function that is expanded in line when it is invoked. That is the compiler replaces the function call with the corresponding function code (similar to macro).

22. What is the difference between a copy constructor and an overloaded assignment operator?
A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

23. What is class invariant?
A class invariant is a condition that defines all valid states for an object. It is a logical condition to ensure the correct working of a class. Class invariants must hold when an object is created, and they must be preserved under all operations of the class. In particular all class invariants are both preconditions and post-conditions for all operations or member functions of the class.

24. What is a scope resolution operator?
A scope resolution operator (::), can be used to define the member functions of a class outside the class.

25. What does extern mean in a function declaration?
It tells the compiler that a variable or a function exists, even if the compiler hasn’t yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.

26. Describe the main characteristics of static functions.
The main characteristics of static functions include,

It is without the a this pointer,
It can't directly access the non-static members of its class
It can't be declared const, volatile or virtual.
It doesn't need to be invoked through an object of its class, although for convenience, it may.

Post a Comment

 
Top