VU CS304- Object Oriented Programming LATEST SOLVED SUBJECTIVES FROM MIDTERM PAPERS 2010 Part1
MIDTERM EXAMINATION 2010
Question No: 17 ( Marks: 2 )
Can we create an array of objects for a class having user defined constructor? Justify your answer.
Answer:- (Page 114)
There must always be a default constructor if we want to create array of objects
Question No: 17 ( Marks: 2 )
Can we create an array of objects for a class having user defined constructor? Justify your answer.
Answer:- (Page 114)
There must always be a default constructor if we want to create array of objects
Question No: 18 ( Marks: 2 )
Friend functions increase „Programming bugs‟. What is your opinion?
Answer:-
Yes friend functions can increase programming bugs some time.
Friend functions increase „Programming bugs‟. What is your opinion?
Answer:-
Yes friend functions can increase programming bugs some time.
Question No: 19 ( Marks: 2 )
Explain two benefits of setter functions.
Answer:- click here for detail5
They help to define the variable in an interface
To make a variable read-only
notify other methods / classes when the value is changed
Question No: 20 ( Marks: 3 )
What are binary operators? Give an example of binary operators overloading using any class.
Answer:- (Page 142)
Binary Operators Overloading:
Binary operators act on two quantities.
Examples of binary operators:
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. (Page 145)
Explain two benefits of setter functions.
Answer:- click here for detail5
They help to define the variable in an interface
To make a variable read-only
notify other methods / classes when the value is changed
Question No: 20 ( Marks: 3 )
What are binary operators? Give an example of binary operators overloading using any class.
Answer:- (Page 142)
Binary Operators Overloading:
Binary operators act on two quantities.
Examples of binary operators:
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. (Page 145)
Question No: 21 ( Marks: 3 )
Give c++ code to overload assignment operator for string class.
Answer:- (Page 148)
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);
}6
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;
}
Question No: 22 ( Marks: 5 )
Writ c++ code to overload subscript [] operator for String class.
Answer:- (Page 160)
Give c++ code to overload assignment operator for string class.
Answer:- (Page 148)
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);
}6
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;
}
Question No: 22 ( Marks: 5 )
Writ c++ code to overload subscript [] operator for String class.
Answer:- (Page 160)
Question No: 23 ( Marks: 5 )
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;
}
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;7
}
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();
{
ExamName = name;
}
void setpaper(int paper) const
{
No_of_paper = paper;7
}
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;
}
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() 8
{
return ExamName;
}
int getpaper()
{
return No_of_paper;
}
};
int main()
{
Exam exam1;
cout << " Exam = "<<exam1.getname()<<endl;
cout << " Numbe of paper = " << exam1.getpaper();
{
ExamName=name;
}
void setpaper(int paper)
{
No_of_paper = paper;
}
char* getname() 8
{
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;
}
return 0;
}
No comments:
Post a Comment