C++ Interview Question and Answers Part 3

C++ Interview Question and Answers Part 3

Here you can find the most frequently asked C++ Interview Question asked in interviews of reputed multinational IT companies with Answers (part 3).

1. When should you use multiple inheritance?

There are three acceptable answers: “Never,” “Rarely,” and “When the problem domain cannot be accurately modeled any other way.”

2. Explain the ISA and HASA class relationships. How would you implement each in a class design?

A specialized class “is” a specialization of another class and, therefore, has an ISA relationship with the other class. An Employee ISA Person. This relationship is best implemented with inheritance. The employee is derived from Person. A class may have an instance of another class. For example, an employee “has” a salary, therefore the Employee class has a HASA relationship with the Salary class. This relationship is best implemented by embedding an object of the Salary class in the Employee class.

3. When is a template a better solution than a base class?

When you are designing a generic class to contain or otherwise manage objects of other types when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the generosity) to the designer of the container or manager class.

READ  Series Pattern Program for (1) + (1+2) + (1+2+3) + (1+2+3+4) + … + (1+2+3+4+…+n)

4. What is a mutable member?

One that can be modified by the class even when the object of the class or the member function doing the modification is const.

5. What do you mean by inline function?

The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application’s performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.

6. What is the difference between an object and a class?

Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.

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.

READ  Number Pattern Programs in Python Java C and CPP

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.

7. Write a short code using C++ to print out all odd numbers from 1 to 100 using a for a loop.

for( unsigned int i = 1; i < = 100; i++ )
	if( i & 0x00000001 )
		cout << i << \",\";

8. 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.

9. Tell how to check whether a linked list is circular.

Create two pointers, each set to the start of the list. Update each as follows:

while (pointer1)
	pointer1 = pointer1->next;
	pointer2 = pointer2->next; 
	if (pointer2)
	if (pointer1 == pointer2)
		print (\"circular\n\");

10. What is friend function?

As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition.

READ  C++ Interview Question and Answers Part 1

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it‟s either 1 or 2 jumps until they meet.

This tutorial discusses the most frequently asked C++ Interview Question and Answers (Part -3). Subscribe to our YouTube channel for more videos and like the Facebook page for regular updates.

Leave a Comment

Your email address will not be published. Required fields are marked *