Ask Question         Q & A         Articles        
Skip Navigation LinksHome > Articles > Article Detail

C++ Questions for Campus Placement Preparation - 1

 By : Sourabh Khanna   |  Views : 81454
0 0 0 0 0

 

Question 1:   Difference between a “ assignment” and a “copy constructor”
Ans : Two major differences between copy constructor and assignment  are:
(i) Copy constructor doesn’t return any value (not even void) whereas assignment operator returns a value.
(ii) Copy constructor will be called only when a new object is being instantiated whereas assignment operator is called for an existing object to which a new value is being assigned. 
Question 2: What are the inline functions?
Ans: An inline function is a function for which compiler is requested to insert the complete body of the function in every place that the function is called, rather than generating code (as it happens with general functions) to call the function in the one place it is defined. 
Question 3: What will happen if I allocate memory using “new” and free it using: “free()” or allocate it using “calloc()” and free it using “delete”?
Ans: This will be invalid situation since delete operator is meant to free the space allocated only by new operator and free() is meant to free the space allocated by calloc().
Question 4: What are the different types of polymorphism?
Ans:  There are two type of polymorphism: 
(i) Compile time (or Static) polymorphism
(ii) Runtime (or Dynamic) polymorphism
Question 5:   Should we prefer incline functions over macros? 
Ans: Yes. Because in case of macro, macro code will always get inserted at the calling place but in inline function compiler decides on the basis of size of function and compiler settings that whether to insert the code or to call the function.
Question 6:  What is the difference between “overloading” and “overriding?
Ans: Two major differences between overloading and overriding are: 
(i) In overloading same function name is used with different parameters for different tasks within a class whereas in overriding same function name with same parameters of the base class in the derived class.
(ii) In overloading, there is a relationship between different methods (functions) available in the same class where as in overriding; there is relationship between a super class method and subclass method.
Question 7 : What is abstraction?
Ans:  Abstraction is a process of representing the essential features without including background details and explanations. For example, in the seating area in a car only parts needed for the user are displayed and almost all the internal machinery part (which are meant for the mechanic) are not displayed to the user.
Question 8: Can we overload all operators available in C++? 
Ans: No 
Question  9: What are friend functions?
Ans: Friend function is a special non-member function that has been granted access to all the private members of the class. 
Major features of friend functions are:
(i) It can access the private members of a class which is a violation of the rules of encapsulation. Therefore its use is recommended only when proper justification is there. 
(ii) Its scope is not limited to a class for which it has been declared as friend function.
(iii) It has objects as arguments. 
Question 10: What is encapsulation?
Ans: Encapsulation is a process of wrapping up of data and functions into single unit (class). It binds both together and keeps both safe from outside interface and misuse. Encapsulation is a way to implement data abstraction. 
Question 11: Can we have “Virtual Constructors”?
Ans:  No
Question 12: When is an object created and what is its lifetime?
Ans: An object is created when a class is instantiated. This is the time when memory is allocated to the object and the class constructor is called to initialize the data members. Object lifetime (or life cycle) of an object in object-oriented programming is the time between an object's creation (also known as instantiation or construction) till the object is no longer used, and is destructed or freed.

Question: What are the inline functions?

Ans: An inline function is a function for which compiler is requested to insert the complete body of the function in every place that the function is called, rather than generating code (as it happens with general functions) to call the function in the one place it is defined. 

Question:   Difference between  assignment operator and copy constructor

Ans : Two major differences between copy constructor and assignment  are:

(i)Copy constructor doesn’t return any value (not even void) whereas assignment operator returns a value.

(ii)  Copy constructor will be called only when a new object is being instantiated whereas assignment operator is called for an existing object to which a new value is being assigned. 

 

Question: What will happen if I allocate memory using “new” and free it using: “free()” or allocate it using “calloc()” and free it using “delete”?

Ans: This will be invalid situation since delete operator is meant to free the space allocated only by new operator and free() is meant to free the space allocated by calloc().

Question: What are the different types of polymorphism?

Ans:  There are two type of polymorphism: 

i) Compile time (or Static) polymorphism

(ii) Runtime (or Dynamic) polymorphism

Question:   Should we prefer inline functions over macros? 

Ans: Yes. Because in case of macro, macro code will always get inserted at the calling place but in inline function compiler decides on the basis of size of function and compiler settings that whether to insert the code or to call the function.

Question:  What is the difference between “overloading” and “overriding?

Ans: Two major differences between overloading and overriding are: 

(i) In overloading same function name is used with different parameters for different tasks within a class whereas in overriding same function name with same parameters of the base class in the derived class.

(ii) In overloading, there is a relationship between different methods (functions) available in the same class where as in overriding; there is relationship between a super class method and subclass method.

Question: What is abstraction?

Ans:  Abstraction is a process of representing the essential features without including background details and explanations. For example, in the seating area in a car only parts needed for the user are displayed and almost all the internal machinery part (which are meant for the mechanic) are not displayed to the user.

Question: Can we overload all operators available in C++? 

Ans: No 

Question: What are friend functions?

Ans: Friend function is a special non-member function that has been granted access to all the private members of the class. Major features of friend functions are:

(i) It can access the private members of a class which is a violation of the rules of encapsulation. Therefore its use is recommended only when proper justification is there. 

(ii) Its scope is not limited to a class for which it has been declared as friend function.

(iii) It has objects as arguments. 

Question: What is encapsulation?

Ans: Encapsulation is a process of wrapping up of data and functions into single unit (class). It binds both together and keeps both safe from outside interface and misuse. Encapsulation is a way to implement data abstraction. 

Question: Can we have “Virtual Constructors”?

Ans:  No

Question: When is an object created and what is its lifetime?

Ans: An object is created when a class is instantiated. This is the time when memory is allocated to the object and the class constructor is called to initialize the data members. Object lifetime (or life cycle) of an object in object-oriented programming is the time between an object's creation (also known as instantiation or construction) till the object is no longer used, and is destructed or freed.

Liked this Article? Share with your Friends.
Related Content
arrow  C Questions for Campus Placement Preparation - 1  
arrow  C Questions for Campus Placement Preparation - 2  
arrow  C++ Questions for Campus Placement Preparation - 2  
arrow  Important C Interview Questions with Answers - 1  
arrow  Important C Interview Questions with Answers - 2  
arrow  Important C Interview Questions with Answers - 3  
Comments
Post Comment
Ask a Question