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

C++ Questions for Campus Placement Preparation - 2

 By : Sourabh Khanna   |  Views : 61583
0 0 0 0 0

Question: What are Virtual Functions?

Ans:  Virtual function is a member function declared in the base class whose functionality is redefined (same function name) by its derived class. To implement a virtual function simply precede function name with keyword virtual. A virtual function declared in the base class represents the single interface and its redefinition by the derived class implements operations specific to each derived class.

Question: What is a Namespace?

Ans:  A namespace is a declarative region that attaches an additional identifier to any names declared inside it. It overcomes the problem of name conflicts (of variable, functions, classes or types) that may occour independently developed functions. It is possible to use the same name in separate namespaces without conflict even if the names appear in the same translation unit. It allows to group entities like classes, objects and functions under a name.

Question: Explain the need for “Virtual Destructor”.

Ans: Virtual destructor is needed to make destructor as dynamically bounded. When we destroy object of a derived class with non-virtual destructor explicitly, only destructor of base class is executed and not of the derived class. Reason of non-execution of the derived class is that the compiler uses static binding when calling the destructor. this problem can be overcome by making the destructor to be dynamically bounded which is possible by making the base class of destructor virtual. 

Question: Can we have private constructor in a class?

Ans : Yes. When we want that object of a class can be created only from inside the class then we can have private constructor.

Question: What is the use of Operator Overloading?

Ans:  Operator overloading is a property of giving additional meaning to existing operator so that they can work with variables of user-defined data types also in addition to specific built-in data types.

Major uses of operator overloading are:

(i)    It enhances the capability of existing operators by making them to work on user-defined data types in addition to built-in data types.

(ii)   It is used in large and complex programs involving multiple objects of different classes as it provides a common interface to a operator.

Question: What is the main difference between new and malloc()?

Ans :New operator allocates memory and call the constructor of the class whereas malloc() only allocates memory.

Question: What is the main difference between delete and free()?

Ans: Delete operator calls the destructor and then deallocates memory whereas free() only deallocates memory and not call the destructor?

Question: What is the difference between an object and a class?

Ans:  Object is an entity with identification, with some characteristics and behavior whereas class is a group of objects that have common properties and relationship. In fact objects are variables of data type class.

Question: What is the difference between class and structure?

Ans:  Major differences between a class and a structure are:

(i) Class is collection of objects of similar objects whereas structure is collection of members of different data types.

(ii) By default all the members inside the class are private whereas all declarations inside a structure are public by default.

(iii) The structures are value types and the classes are reference types.

(iv) Classes can be inherited whereas structures can not be.

(v)  Fields can be directley instanciated but not in classes.

Question: What is a scope resolution operator?

Ans:  For the ease of user, some  member function(generally lengthy) are declared in the  class but they are defined outside the class. Now to bind such function with class scope resolution operator ( denoted as :: ) is used . It is an operator that informs compiler that the given function is a member of a class even though function has been defined outside the class. 

Question: What is name mangling?

Ans: Name mangling is a technique used by compiler for calling overloaded function by changing their names.

Question: What is function overloading and operator overloading?

Ans:  Function overloading is a property by which a family of functions performing similar multiple related activity share a common name (but different arguments) . When a overloaded function is called, it is the responsibility of the compiler to select the appropriate function depending upon the number of arguments.

Operator overloading is a property of giving additional meaning to an existing operator so that it is defined for some user-defined data-types in addition to its earlier defined data types. It does not change the original meaning of the operator, rather it extends the existing functionality of the operator. 

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