CS304- Midterm Solved Past Papers

Contents

Object Oriented Programming

CS304 – Midterm Solved Past Papers

Q.1 Can constant object access the none constant member function of each class.

Answer:- “Const objects can access only const member functions so chances of change of state of const objects once they are created are eliminated.”

Q2. Give c++ code to overloaded unary “–” operators to complex member class.

Answer:- 
class Complex{

Complex operator — (int);
// friend Complex operator –(const Complex &, int);
}
Complex Complex::operator — (int){
complex t = *this;
real – = 1;
return t;
}
Complex operator — (const
Complex & h, int){
complex t = h;
h.real – = 1;
return t;
}

Q3. What is simple association? Explain it with the help of example.

Answer:-  The two interacting objects have no intrinsic relationship with other object. It is the weakest link between  objects. It is a reference by which one object can interact with some other object.

  • Customer gets cash from cashier
  • Employee works for a company

Q4. Explain the deference between the static variable of a class with none static variable with the help of example

Answer:-  The difference between static and non static members is only that a non static member is tied to an instance of a class although a static member is tied to the class, and not to a particular instance. That is, a static member is shared by all instances of a class although a non static member exists for each instance of class.

Q.5. Write c++ code for operator() 

Advertisement

Answer:- 
Function Operator
class String{

public:
char & operator()(int);

};
char & String::operator()
(int pos){
assert(pos>0 && pos<=size);
return bufferPtr[pos-1];
}
int main(){
String s1(“Ping”);
char g = s1(2); // g = „i‟
s1(2) = „o‟;
cout << g << “\n”;
cout << str.GetString();
return 0;
}

Q.6. Write c++ code for operator [] using string class.

Answer:-
Subscript operator must be overloaded as member function of the class with one parameter of integer type,

class String{

public:
char & operator[](int);

};
char & String::operator[]( int pos){
assert(pos>0 && pos<=size);
return stringPtr[pos-1];
}
int main() {
String s1(“Ping”);
cout <<str.GetString()<< endl;
s1[2] = „o‟;
cout << str.GetString();
return 0;
}

Q.7. Static data members.

Answer:- 
“A variable that is part of a class, yet is not part of any object of that class, is called static data member”

Q.8. What is interface give two real life examples?

Answer:- 
Interface is a set of functions of an object that he wants to expose to other objects.
Interface of a Car is set of these functions

  • Accelerate
  • Change Gear
  • Apply Brakes
  • Turn Lights On/Off
  • Interface of a Phone is set of these functions

Input Number• Place Call • Disconnect Call • Add number to address book • Remove number • Update
number

Q.9. What is aggregation give example?

Answer:- 
An object may contain a collection (aggregate) of other objects, the relationship between the container and the contained object is called aggregation, For Example: A room and its components like table, chair and bed etc are example of aggregation.

Q.10. What is an abstract class?

Answer:-  Taking part of the objects and their functions that is related to the problem and ignore all other irrelative characteristic called abstraction.

Q.11. Can we create an array of objects for a class having user defined constructor? Justify your answer.

Answer:-  There must always be a default constructor if we want to create array of objects

Q.12. Friend functions increase „Programming bugs‟. What is your opinion?

Answer:- Yes friend functions can increase programming bugs some time.

Q.13. Explain two benefits of setter functions.

 They help to define the variable in an interface
 To make a variable read-only
 notify other methods / classes when the value is changed

Q.14. What are binary operators? Give an example of binary operators overloading using any class.

Answer:- 

Binary Operators Overloading:
Binary operators act on two quantities.
Examples:
Overloading + operator:
class Complex{
private:
double real, img;
public:

Complex operator +(const Complex & rhs);
};
Complex Complex::operator +( const Complex & rhs){
Complex t;
t.real = real + rhs.real;
t.img = img + rhs.img;
return t;
}
The binary operator is always called with reference to the left hand argument.

Q.15. Give c++ code to overload assignment operator for string class.

Answer:- 
class String{
int size;
char * bufferPtr;
public:
String(); // default constructor
String(char *); // overloaded constructor
String(const String &); // copy constructor

};
String::String(){
bufferPtr = NULL;
size = 0;
}
String::String(char * ptr){
if(ptr != NULL){
size = strlen(ptr);
bufferPtr = new char[size+1];
strcpy(bufferPtr, ptr);
}

else{
bufferPtr = NULL;
size = 0;
}
}
String::String(const String & rhs){
size = rhs.size;
if(rhs.size != 0){
bufferPtr = new char[size+1];
strcpy(bufferPtr, ptr);
}
else
bufferPtr = NULL;
}
int main(){
String str1(“Hello”);
String str2(“World”);
str1 = str2;78
return 0;
}

Q.16. Detect and correct compile time error(s) in the following code.

class Exam
{
char *ExamName;
int No_of_paper;
public:
Exam()
{
ExamName = “Final Term”;
No_of_paper = 5;
}
void setname( char* name) const
{
ExamName = name;
}
void setpaper(int paper) const
{
No_of_paper = paper;

}
char* getname()
{
return ExamName;
}
int getpaper()
{
return No_of_paper;
}
};
int main()
{
const Exam exam1;
cout << ” Exam = “<<exam1.getname()<<endl;
cout << ” Numbe of paper = ” << exam1.getpaper();
getch();
return 0;
}

Answer:-
Corrected Code
class Exam
{
char *ExamName;
int No_of_paper;
public:
Exam()
{
ExamName = “Final Term”;
No_of_paper = 5;
}
void setname( char* name)
{
ExamName=name;
}
void setpaper(int paper)
{
No_of_paper = paper;
}
char* getname()

{
return ExamName;
}
int getpaper()
{
return No_of_paper;
}
};
int main()
{
Exam exam1;
cout << ” Exam = “<<exam1.getname()<<endl;
cout << ” Numbe of paper = ” << exam1.getpaper();
getch();
return 0;
}

Q. 17. If, within a class, da is a member variable, will the statement this.da=25; assign 25 to da? False

Answer:- “this” is pointer, so using pointer to object we use the – > operator to access object member function and data member,

Q.18. Explain the scope of „private‟ member of a class with one example.

Answer:- The scope of private data member of a class is that, only an access is allowed within the class. Only member function of class can access and modify the value of data member value. Derived class or class instance/object can‟t directly access. Data Member will created for each separate object in the memory and will eliminated when object will be destroy. Object could be destroy by calling its destructor.

Example
Class arithmetic
{
Private:
int a,b;
Public:
Add()
{

A+b; // direct access by member function Correct Access
}
};
Int main()
{
Arithmetic c1;
C1.a=4;// is illegal access to class private data member error will generate by compiler
}

Q.19. Write c++ of overloading ^ operator in complex numbers class.
If we have two objects of complex number class as follows,
Complex obj1,obj2;
and we write statement,
Complex obj3 = obj1 ^ obj2;
obj3 real and imaginary parts will be,
obj3.real = (obj1.real) obj2.real and obj3.img = (obj1.img) obj2.img
Hint: You can use c++ built in function power(x,y) that returns the result of x y.

Answer:-
Class complex
{
Private:
Double val;
Complex operator ^(complex c1)
{
Return Power(val,c1.val);
}
};

Q.20. Write c++ of overloading – operator in complex numbers class which can work in these give situation.

If we have two objects of complex number class as follows,
Complex obj1;
Complex obj3 = 47.4- obj2;
Complex obj4 = obj2-47.4;
Answer:- We have made them as friend so that we can write them as non member functions and they ar not called with respect to complex no. Class object instead we pass both arguments (complex no. object and double value) to this function compiler invoke them according to arguments passed. Their implementation is similar as give below.
Class complex
{
private:
double real, img;
public:
friend complex operator -(double c1, complex obj);
Complex operator -(double c1)
{
Complex temp;
temp.real=real-c1;
temp.img=img-c1;
return temp;
}
};
//Friend function Definition
Complex operator -( double c1, complex obj)
{
Complex temp;
temp.real=c1-real;
temp.img=c1-img;
return temp;
}

Q21. What are Accessor Functions, Explain with an example?

Answer:-  Accessor functions are used to access private data of the object, we provide accessor functions to get and set private data members of the class.
Example
class Student{

int rollNo;
public:

void setRollNo(int aRollNo){
rollNo = aRollNo;
}
};
Avoiding Error
void Student::setRollNo(int aRollNo){
if(aRollNo < 0){
rollNo = 0;
}
else
{
rollNo = aRollNo;
}
}

Q.22. What is difference between simple association and composition?

Answer:-
In Association, interacting objects have no intrinsic relationship with other object. It is the weakest link between objects. While in Composition An object may be composed of other smaller objects, the relationship between the “part” objects and the “whole”.

Q.23. Friend functions minimize “Encapsulation”, What is your opinion?

Answer:- 
Friend functions minimize encapsulation as we can access private data of any class
using friend functions,
This can result in:
 Data vulnerability
 Programming bugs
 Tough debugging

Q.24. Write three important properties of constructors?

1. Tell the reason why we can not overload the following four operators in c++
.       ,       .*     ,      ::     ,     ?:
2. Considering the complex number class can we subtract two complex numbers by overloading plus
“+ “Operator. Justify your answer as well.
Answer:-
Constructor Properties
 Constructor is a special function having same name as the class name
 Constructor does not have return type
 Constructors are commonly public members

Q.25. How we resolve the following problems in overloading of assignment operator in string class, (explain with the help of c++ code)

Self referencing
Assigning a string value to more than one strings in a single line like, stringobject1 = string object2 =
stringobject3 = stringobject4
Answer:-
Handout Page:150
class String{

public:

String & operator = (const String &);
};
String & String :: operator = (const String & rhs){
size = rhs.size;
delete [] bufferPtr;
if(rhs.size != 0){
bufferPtr = new char[rhs.size+1];
strcpy(bufferPtr,rhs.bufferPtr);
}
else bufferPtr = NULL;
return *this;
}

Q.26. Friend functions increase „Data vulnerability‟, what is your opinion?

Answer:- 
Friend functions minimize encapsulation as we can access private data of any class using friend functions,
This can result in:
 Data vulnerability
 Programming bugs
 Tough debugging

Q.27. Explain two benefits of setter functions.

Answer:- Setters and getters functions are provided by class to access the its members it also minimizes the changes to move the objects in inconsistent state as we can write checks in our setter functions for example we can check that whether the user has entered correct age value and has not entered negative value for age.

Q.28. Consider the class given below what will be the values in variables x,y and z after creating object of this class,

class ABC{
int x;
int y;
int z;
public:
ABC();
};
ABC::ABC():x(10),z(x),y(x)
{

}
Answer:-
X=10,z=10 and y=10

Q.29. What is composition? Explain it with the help of an example.

Answer:- An object may be composed of other smaller objects, the relationship between the “part” objects and the “whole” object is known as Composition, and Composition is represented by a line with a filled-diamond head towards the composer object. For example a man and his body parts have the relation of “part” objects and the “whole” object is known as Composition of man.

Q30. How we can overload Stream Extraction and Insertion Operators in c++? Give example code for Complex Number Class.

Answer:-
Overloading Stream Insertion Operator
class Complex{

friend ostream & operator << (ostream & os, const Complex & c);
};
Stream Insertion operator
// we want the output as: (real, img)
ostream & operator << (ostream & os, const Complex & c){
os << „(„ << c.real
<< „,„
<< c.img << „)‟;
return os;
}
Overloading Stream Extraction Operator
class Complex{

friend istream & operator >> (istream & i, Complex & c);
};