

InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
1. |
Modify the program 2 of Type C to include constructors for the three classes. |
Answer» class Account { public Account() { strcpy(name,"NULL"); ano=0; balance=0.0; } // same as above }; class Current public Account { public Current() { depo=0.0;with=0.0;pen=0.0; } // same as above }; class Savings:public Account { public Savings() { depo=0.0;with=0.0;intr=0.0; } // same as above }; void main() { // same as above } |
|
2. |
What is inheritance? Discuss its various forms. |
Answer» Inheritance is the capability of one class to inherit properties from another class. Forms of Inheritance: 1. Single Inheritance: One base class and one super class. 2. Multiple Inheritance: Multiple base class and one sub class. 3. Hierarchical Inheritance: One base class and multiple sub class. 4. Multi-level Inheritance: A subclass inherits from a class that itself inherits from another class. 5. Hybrid Inheritance: A subclass inherits from multiple base classes and all of its base classes inherit from a single base class. |
|
3. |
Imagine a publishing company that markets both books and audio-cassette versions of its works. Create a class publication that stores the title (a string) ad price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int); and tape, which adds a playing time in minutes (type float). Each of these three classes should have a getdata() function to get its data from the user at the keyboard, and a putdata() function to display its data.Write a main() program to test the book and tape classes by creating instances of them, asking the user to fill in their data with getdata(), and then displaying the data with putdata(). |
Answer» #include <iostream.h> #include<conio.h> #include<stdio.h> class publication { char title[20]; float price; public: void getdata() { cout<<"Enter title: "; gets(title); cout<<"Enter price: "; cin>>price; } void putdata() { cout<<"Title: "<>page_count; } void putdata() { publication::putdata(); cout<<"Page count: "<<page_count<<endl; } }; class tape:public publication { float play_time; public: void getdata() { publication::getdata(); cout<<"Enter Play time: "; cin>>play_time; } void putdata() { publication::putdata(); cout<<"Play time: "<<play_time<<endl; } }; void main() { clrscr(); book b; tape t; b.getdata(); b.putdata(); t.getdata(); t.putdata(); getch();\ } |
|
4. |
Answer the questions (i) to (iv) based on the following: class FaceToFace{char CenterCode[10];public:void Input( );void Output( );};class Online{char website[50];public:void SiteIn( ); void SiteOut( );};class Training: public FaceToFace, private online{ long Tcode; loat charge;int period;public:void Register( );void show( );};(i) Which type of inheritance is shown in the above example?(ii) Write names of all the member functions accessible from Show( ) function of class Training.(iii) Write name of all the member accessible through an object of class Training.(iv) Is the function Output( ) accessible inside the function SiteOut( )? Justify your answer? |
Answer» (i) Multiple Inheritance (ii) Register( ) Siteln( ), SiteOut( ), Input( ), Output( ) (iii) Register( ), Show( ), Input( ), Output( ). (iv) No, function Output( ) is not directly accessible inside the function SiteOut( ), because Output( ) is a member function of class FaceToFace and SiteOut( ) is a member function of class Online, and the classes FaceToFace and Online are two independent classes. |
|
5. |
Discuss various reasons that support the concept of inheritance in Object Oriented Languages. |
Answer» The reasons that support the existence of inheritance concept in OO Languages are: 1. ‘Inheritance’ is capable of expressing the inheritance relationship of real-world models. 2. ‘Inheritance’ facilitates the code reusability. 3. ‘Inheritance’ is capable of simulating the transitive nature of real-world’s inheritance |
|
6. |
Differentiate between public and protected visibility in context of Object Oriented Programming giving suitable examples for each. |
||||||||
Answer»
|
|||||||||
7. |
When does ambiguity arise in multiple inheritance? How can one resolve it? What are virtual base classes? What is their significance? |
Answer» An ambiguity can arise when several paths exist to a class from the same base class. This means that a child class could have duplicate sets of members inherited from a single base class. This can be solved by using a virtual base class. When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. When a class is made virtual, necessary care is taken so that the duplication is avoided regardless of the number of paths that exist to the child class. |
|
8. |
#include<iostream.h>class a{public: void something(){ cout<<"a"; }};class b{public;void something(){ cout<<"b"; }};class c:public a,public b { };int main(){c x;x.something();return 0;}(a) a::something() is called(b) b:: something() is called(c) Runtime Error(d) Syntax Error |
Answer» (d) Syntax Error |
|
9. |
What should be the structure of a class when it has to be a base class for other classes? |
Answer» class classname { private: //members not intended to be inherited public: //members intended to be inherited and available to every function protected: //members intended to be inherited but not intended to be public }; |
|
10. |
What will be the output of the following program? #include<iostream.h>class Student{public:Student (char pname[]=" "){strcpy(name,pname);average=semesterHours=0;}void addCourse(int hours,float grade){average=(semesterHours*average+grade); semesterHours+=hours;average=semesterHours;}int hours(){ return semesterHours; }float gpa(){ return average;}protected:char name[40];int semesterHours;float average;};class GradStudent:public Student{public:GradStudent(char pname[]=" "):Student(pname){ qual_grade=0;}int qualifier(){ return qual_grade; }protected:int qual_grade;};int main() { Student commerce("Saurabh 7/."); GradStudent gs;commerce.addCourse(4,2.5);gs.addCourse(4,3.0);return 0;} |
Answer» Above code will generate various compilation errors few of them are listed below– i. strcpy(name,pname); gives error due to missing string.h file ii. After adding the required header file code will execute but screen will appear blank due to missing output statements. |
|
11. |
Given the definition of following two classes. Write the constructor definition for both classes.class Alpha {int a;float b;char c;public.......... //constructor definition// has to become here :};class Beta:public Alpha {public:........//constructor definition}; |
Answer» class Alpha { int a; float b; char c; public: Alpha(int i, float j,char k) { a=i; b=j; c=k; } }; class Beta:public Alpha { public: Beta(int p,float q,char r):Alpha(p,q,r) {cout<<"Beta constructor..."; } }; |
|
12. |
Discuss a situation in which the private derivation will be more appropriate as compared to public derivation |
Answer» When we want that the derived class has the direct access privilege only to the non-private members of the base class, in that situation the private derivation will be more appropriate as compared to public derivation. |
|
13. |
Consider the following and answer the questions give below: class MNC{char Cname[25]; //Compay nameprotected:char Hoffice[25]; //Head officepublic:MNC();char Country[25];void EnterData();void DisplayData();};class Branch:publicMNC { long NOE //Number of employeeschar Ctry[25]; //Countryprotected:void Association();public:Branch();void Add();void Show();};class Outlet:public Branch{char State[25];public: Outlet();void Enter();void Output();};(i) Which class' constructor will be called first at the time of declaration of an object of class Outlet?(ii) How many bytes does a object belonging to class Outlet require?(iii) Name the member function(s), which are accessed from the object(s) of class Outlet.(iv) Name the data member(s), which are accessible from the object(s) of class Branch. |
Answer» (i) class MNC (ii) 129 Bytes (iii) Enter(), Output(), Add(), Show(), EnterData(), DisplayData() (iv) Country[25] |
|
14. |
Assume a class Derv derived from a base class Base. Both classes contain a member function func() that takes no arguments. Write the definition for a member function of Derv that calls both the func()s. |
Answer» class Base { public: void func() { cout<<"base class"; } }; class Derv:public Base { public: void func() { cout<<"derived class"; } void callAll() { Base::func(); func(); } }; |
|
15. |
How does the invocation of constructor differ in derivation of class and nesting of classes? |
Answer» In derivation of class first the base class constructor is invoked, followed by the derived class constructor, whereas in nested classes constructors of all the member objects are called before the constructors of the object enclosing other objects. |
|
16. |
A class One with data members int a and char b inherits from another class Two with data members float f and int x. Write definitions for One and Two for the following situations:(i) Objects of both the classes are able to access all the data members of both the classes.(ii) Only the members of class One can access all the data members of both the classes. |
Answer» (i) class Two { protected: float f; int x; }; class One:private Two { int a; char b; }; (ii) class Two { public: float f; int x; }; class One:public Two { public: int a; char b; }; |
|
17. |
Write a declaration for a class Person which has the following: data members name, phone set and get functions for every data member a display function a destructor(i) For the Person class above, write each of the constructor, the assignment operator, and the getName member function. Use member initialization lists as often as possible.(ii) Given the Person class above, write the declaration for a class Spouse that inherits from Person and does the following:has an extra data member spouseName redefines the display member function.(iii) For the Spouse class above, write each of the constructors and the display member function. Use member initialization lists as often as possible. |
Answer» class Person { char name[20]; long phone; public: void set() { strcpy(name,"NULL"); phone=7878963522; } void get() { cout<<"Enter name: "; gets(name); cout<<"Enter phone: "; cin>>phone; } void display() { cout<<"Name: "<<name<<end1; cout<<"Phone: "<<phone<<end1; } Person() { strcpy(name,"Rahul"); phone=9965869922; } Person(char na[20],long ph) { name=na; phone=ph; } void getName() { cout<<"Enter name:"; gets(name); } }; class Spouse:public Person { char spouseName[20]; public: void getName() { cout<<"Enter name:"; gets(spousename); } void display() { cout<<"Name: "<<name<<end1; cout<<"Phone: "<<phone<<end1; cout<<"spouse name: "<<spousename<<end1; } Spouse() { strcpy(spouseName,"NULL"); } Spouse(char sn[20]) { spouseName=sn; } }; |
|
18. |
Create the following class hierarchy in C++. |
Answer» class student { char name[20]; int age; public: void readData(); void display(); }; class Book { student Enrollno; char bookname[20],author[20]; int no_of_pages; public: void ReadB(); void displayB(); }; class PrimaryStudet:public student { char Activity[20]; int no_of_hrs; public: void ReadPrimary(); void DisplayPrimary(); }; class SecondaryStudet:public student {}; class EQUIPMENT { char name[20]; int role; public: void ReadEquip(); void Display(); }; |
|
19. |
Write a C++ to read and display information about employees and managers. Employee is a class that contains employee number, name, address ad department. Manager class contains all information of the employee class and a list of employees working under a manager. |
Answer» #include <iostream.h> #include<conio.h> class employee { public: int num,house ; char city[20], state[20], name[20],depart[20]; public: void input() { cout<<"Enter the employe's name"; cin>>name;\ cout<<"Enter the employe number"; cin>>num; cout<<"Enter the address including house number ,city ,state"; cin>>house>>city>>state; cout<<"enter the department"; cin>>depart; } void output() { cout<<"\nemploye's infomation:"; cout<<"\n"<<name<<"\n"<<num<<"\n"<<"address -:" <<"\n"<<house<<" "<<city<<"\n"<<state; cout<<"\n"<<depart; } }; class manager: public employee { char name[20]; int n ,i; public: void getdata() { cout<<"Enter the manager's name"; cin>>name; cout<<"enter the total number of employe's working under him"; cin>>n; } void info(); }; void manager::info() { getdata(); for(i=1;i<=n;i++) { input(); } cout<<name; cout<<"\nemploye's are-:n" ; for(i=1;i<=n;i++) { cout<<i<<" employe-:" ; output(); } } void main() { class manager M; clrscr(); M.info(); getch(); } |
|