Students can Download Computer Science Chapter 15 Polymorphism Questions and Answers, Notes Pdf, Samacheer Kalvi 11th Computer Science Book Solutions Guide Pdf helps you to revise the complete Tamilnadu State Board New Syllabus and score more marks in your examinations.
Tamilnadu Samacheer Kalvi 11th Computer Science Solutions Chapter 15 Polymorphism
Samacheer Kalvi 11th Computer Science Polymorphism Text Book Back Questions and Answers
PART – 1
I. Choose The Correct Answer
Question 1.
Which of the following refers to a function having more than one distinct meaning?
(a) Function Overloading
(b) Member overloading
(c) Operator overloading
(d) Operations overloading
Answer:
(a) Function Overloading
Question 2.
Which of the following reduces the number of comparisons in a program?
(a) Operator Overloading
(b) Operations overloading
(c) Function overloading
(d) Member overloading
Answer:
(c) Function overloading
Question 3.
void dispchar(char ch = ‘$\ int size=10)
{
for(int i = 1;i< = size;i ++)
cout << ch;
}
How will you invoke the function dispchar() for the following input?
To print $ for 10 times
(a) dispchar();
(b) dispchar(ch,size);
(c) dispchar($,10);
(d) dispchar(T, 10 times);
Answer:
(c) dispchar($,10);
Question 4.
Which of the following is not true with respect to function overloading?
(a) The overloaded functions must differ in their signature.
(b) The return type is also considered for overloading a function.
(c) The default arguments of overloaded functions are not considered for overloading.
(d) Destructor function cannot be overloaded.
Answer:
(d) Destructor function cannot be overloaded.
Question 5.
Which of the following is invalid prototype for function overloading
(a) void fun (intx);
void fun (char ch);
(b) void fun (intx);
void fun (inty);
(c) void fun (double d);
void fun (char ch);
(d) void fun (double d);
void fun (inty);
Answer:
(a) void fun (intx);
void fun (char ch);
Question 6.
Which of the following function(s) combination cannot be considered as overloaded function(s) in the given snippet?
void print(char A,int B); // F1
void printprint(int A, float B); // F2
void Print(int P=10); // F3
void printQ; // F4
(a) F1, F2, F3, F4
(b) F1, F2, F3
(c) F1, F2, F4
(d) F1, F3, F4
Answer:
(d) F1, F3, F4
Question 7.
Which of the following operator is by default overloaded by the compiler?
(a) *
(b) +
(c) + =
(d) = =
Based on the following program answer the questions (8) to (10)
#include
using namespace std;
class Point {
private:
int x, y; .
public:
Point(int x1,int y1)
{
x=x1;y=y1;
}
void operator+(Point & pt3);
void show() {cout << “x =” << x << “y =” << y; }
};
void Point: :operator + (Point & pt3)
{
x+=pt3.x;
y += pt3.y;
}
int main()
{
Point pt1(3, 2),pt2(5, 4);
pt1+pt2;
pt1.show();
return 0;
}
Answer:
(b) +
Question 8.
Which of the following operator is overloaded?
(a) +
(b) Operator
(c) : :
(d) =
Answer:
(a) +
Question 9.
Which of the following statement invoke operator overloading?
(a) pt1 + pt2;
(b) Point pt1(3, 2),pt2(5, 4);
(c) ptl.show();
(d) return 0;
Answer:
(d) return 0;
Question 10.
What is the output for the above program?
(a) x = 8, y = 6
(b) x = 14, y = 14
(c) x = 8, y = 6
(d) x = 5, y = 9
Answer:
(d) x = 5, y = 9
PART – 2
II. Answers to all the questions
Question 1.
What is function overloading?
Answer:
- The ability of the function to process the message or data in more than one form is called function overloading.
- In other words, function overloading means two or more functions in the same scope share the same name but their parameters are different.
Question 2.
List the operators that cannot be overloaded.
Answer:
Operator that are not overloaded is as follows:
- scope operator (::)
- sizeof
- member selector.
- member pointer selector *
- ternary operator ?:
Question 3.
class add{int x; public: add(int)}; Write an outline definition for the constructor.
Answer:
add (int temp)
{
x = temp;
}
Question 4.
Does the return type of function help in overloading a function?
Answer:
No. The return type of a function does not help in overloading a function. Only arguments are considered.
Question 5.
What is the use of overloading a function?
Answer:
Function overloading is not only implementing polymorphism but also reduces the number of comparisons in a program and makes the program execute faster. It also helps the programmer by reducing the number of function names to be remembered.
PART – 3
III. Answers to all the questions
Question 1.
What are the rules for function overloading?
Answer:
Rules for function overloading:
- The overloaded function must differ in the number of its arguments or data types.
- The return type of overloaded functions is not considered for overloading the same data type.
- The default arguments of overloaded functions are not considered as part of the parameter list in function overloading.
Question 2.
How does a compiler decide as to which function should be invoked when there are many functions? Give an example.
Answer:
When you call an overloaded function (when there are many functions with same name), the compiler determines the most appropriate definition to use by comparing the argument types used to call the function with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.
Example:
#include using namespace std; void print (int i)
{
cout<< “It is integer” <<i<< endl;
}
void print (string c)
{
cout<< “It is string”<< c << endl;
}
int main ()
{
print (10);
print (“Good”);
return 0;
}
Output:
It is integer 10
It is string Good
Question 3.
What is operator overloading? Give some example of operators which can be overloaded.
Answer:
The term operator overloading refers to giving additional functionality to the normal C++ operators like It is also a type of polymorphism in which an operator is overloaded to give user-defined meaning to it.
For example, ‘+’ operator can be overloaded to perform addition on various data types, like for Integer, String (concatenation), etc.
Question 4.
Discuss the benefit of constructor overloading?
Answer:
- Function overloading can be applied for constructors, as constructors are special functions of classes.
- A class can have more than one constructor with a different signature.
- Constructor overloading provides the flexibility of creating multiple type of objects for a class.
Question 5.
class sale ( int cost, discount; public: sale(sale &); Write a non-inline definition for constructor specified;
Answer:
class sale
{
int cost, discount;
public:
sale (sale&);
};
// non inline constructor
sale: : sale(sale&s)
{
cost = s.cost;
discount = s.discount;
}
PART – 4
IV. Answers to all the questions
Question 1.
What are the rules for operator overloading?
Answer:
Rules for operator overloading:
- The precedence and associativity of an operator cannot be changed.
- No new operators can be created, only existing operators can be overloaded.
- Cannot redefine the meaning of an operator’s procedure, we cannot change how integers are added. Only additional functions can be to an operator.
- Overloaded operators cannot have default arguments.
- When binary operators are overloaded, the left-hand object must be an object of the relevant class.
Question 2.
Answer the question (i) to (v) after going through the following class.
classBook
{
intBookCode ; char Bookname[20];float fees;
public:
Book() //Function 1
{
fees = 1000;
BookCode = 1;
strcpy (Bookname, “C++”);
}
void display(float C) //Function 2
{
cout << BookCode << “:”<< Bookname << “:”<< fees << endl;
~ Book() //Function 3
{
cout << “End of Book Object”<< endl;
}
Book (intSC, char S[ ], float F); //Function 4
};
1. In the above program, what are Function 1 and Function 4 combined together referred to?
2. Which concept is illustrated by Function3? When is this function called/ invoked?
3. What is the use of Function3?
4. Write the statements in main to invoke function1 and function2
5. Write the definition for Function4.
Answer:
- Constructor overloading (function 1 and function 4 are constructors with different signatures in the class book)
- Function 3 is destructor of the class. Function 3 is executed when the object of the class book goes out of scope.
- Function 3 is destructor of the class.
- Destructor (function3) will free resources if any that the object may have acquired during its lifetime
- Destructor function removes the memory of an object which was allocated by the constructor at the time of creating an object. Thus frees the unused memory.
- book b;
b.display (4.5); - Book: :Book (int sc.char.s[], float, F) // Function 4
{
Book Code = SC
strcpy (Book name, S);
fees = F;
}
Question 3.
Write the output of the following program.
Answer:
include
using namespace std;
class Seminar
{
int Time;
public:
Seminar()
{
Time = 30; cout << “Seminar starts now” << endl;
}
void Lecture()
{
cout << “Lectures in the seminar on”<< endl;
}
Seminar(int Duration)
{
Time=Duration;cout<< “Welcome to Seminar”<<endl;
}
Seminar(Seminar &D)
{
Time=D.Time;cout<< “Recap of Previous Seminar Content”<<endl;
}
~Seminar()
{
cout<< “Vote of thanks”<<endl;
}
};
int main()
{
Seminar s1, s2(2), s3(s2);
s1.Lecture();
return 0;
}
Output:
Seminar starts now
Welcome to seminar
Recap of previous seminar content
Lectures in the seminar on
Vote of thanks
Vote of thanks
Vote of thanks
Question 4.
Debug the following program.
Answer:
Corrected Program
Output
Enter string: God
Enter string: Bless
The first string is: God
The Second-string is: Bless
The concatenated string is: God Bless
Question 5.
Answer the questions based on the following program
#include
#include
using namespace std;
class comp
{
public:
char[10];
void getstring(char str[10])
{
strcpy(s,str);
}
void operator = = (comp);
};
void comp::operator = = (comp ob)
{
if(strcmp(s,ob.s) = = 0)
cout << “\nStrings are Equal”;
else
cout<< “\nStrings are not Equal”;
}
int main()
{
comp ob, ob1;
char stringl[10], string2[10];
cout << “Enter First String:”; cin >> string1;
ob.getstring(string1);
cout<< “\n Enter Second String:”; cin >> string2;
ob1.getstring(string2);
ob = = obi;
return 0;
}
- Mention the objects which will have the scope till the end of the program.
- Name the object which gets destroyed in between the program.
- Name the operator which is overloaded and write the statement that invokes it.
- Write out the prototype of the overloaded member function.
- What types of operands are used for the overloaded operator?
- Which constructor will get executed? Write the output of the program.
Answer:
- Objects: ob and obi of main ()
- Object ob of void operator = = (comp ob);
- Overloaded operator: = =
Statement that invokes: ob = = ob1; - void operator = = (comp ob):
- Operands used are the objects of the class comp
- The default constructor generated by the compiler will be executed.[comp();]
Output 1
Enter first string: hello Enter second string: hello String are equal
Output 2
Enter first string: hello Enter second string: fine String are not equal.
Samacheer Kalvi 11th Computer Science Arrays and Structures Additional Questions and Answers
PART – 1
I. Choose the correct answer
Question 1.
The number and types of a function’s parameters are called the …………………
(a) overload resolution
(b) function’s signature
(c) function overloading
(d) operator overloading
Answer:
(b) function’s signature
Question 2.
The process of selecting the most appropriate overloaded function or operator is called …………………
(a) overload resolution
(b) prototype
(c) polymorphism
(d) operator overload
Answer:
(a) overload resolution
Question 3.
The return type of overloaded functions are not considered for overloading same …………………
(a) polymorphism
(b) prototype
(c) data type
(d) overloading
Answer:
(c) data type
Question 4.
………………… cannot have default arguments.
(a) Operator overloading
(b) Overloaded operators
(c) Function overloading
(d) prototype
Answer:
(b) Overloaded operators
Question 5.
The mechanism of giving special meaning to an operator is known as …………………
(a) operator overloading
(b) parameter
(c) function overloading
(d) polymorphism
Answer:
(a) operator overloading
Question 6.
Operator overloading provides new definitions for most of the ………………… operators.
(a) *
(b) + = =
(c) +
(d) C++
Answer:
(d) C++
Question 7.
The overloaded operator is given using the keyword ………………… followed by an operator symbol.
(a) operator
(b) data type
(c) object
(d) function
Answer:
(a) operator
PART – 2
II. Very Short Answers
Question 1.
Give the syntax for operator overloading.
Answer:
RetumType classname :: Operator Operator Symbol (argument list)
{
\\ Function body
}
Example: Deposit Deposit: : operator + (Deposit dl);
Question 2.
Write a program for function overloading.
C++ Program to demonstrate function overloading
Answer:
#include
using namespace std;
void print(int i)
{cout << “It is integer” << i <<endl;}
void print(double f)
{ cout << “It is float” << f <<endl;}
void print(string c)
{ cout << “It is string” << c <<endl;}
int main() {
print(10);
print(10.10);
print(“Ten”);
return 0;
}
Output:
It is integer 10
It is float 10.1
It is string Ten
PART – 3
III. Short Answers
Question 1.
What is function signature?
Answer:
The ability of the function to process the message or data in more than one form is called as function overloading. It implies that two or more functions in the same scope share the same name but their parameters are different. In this situation, the functions that share the same name are said to be overloaded and the process is called function overloading. The number and types of a function’s parameters are called the function’s signature.
Question 2.
Explain overload resolution.
Answer:
When you call an overloaded function, the compiler determines the most appropriate definition . to use, by comparing the argument types you have used to callthe function with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.
PART – 4
IV. Explain in Detail
Question 1.
Give the output of the following program.
Answer:
Output:
value of x is 7
value of x is 9.132
value of x and y is 85,64
Question 2.
Write the coding for the following output using constructor overloading.
Output:
Constructor without parameters..
Parameterized constructor…
Copy Constructor…
Enter data… 20 30
Object a:
The numbers are..20 30
The sum of the numbers are.. 50
Object b :
The numbers are..10 20
The sum of the numbers are.. 30
Object c..
The numbers are..10 20
The sum of the numbers are.. 30
Answer:
#include
using namespace std;
class add
{
int num1, num2, sum;
public:
add()
{
cout << “\n Constructor without parameters…”;
num1 = 0;
num2 = 0;
sum = 0;
}
add (int s1, int s2 )
{
cout << “\n Parameterized constructor…”;
num1= s1;
num2=s2;
sum=0;
}
add (add &a)
{
cout << “\n Copy Constructor…”; ‘
num1 = a.num1;
num2 = a.num2;
sum = 0;
}
void getdata()
{
cout << “\n Enter data …”; cin>>num 1 >> num2;
}
void addition()
{
sum=num 1 + num2;
}
void putdata()
{
cout << “\n The numbers are..”;
cout < cout << “\n The sum of the numbers are..” << sum; }
};
int main()
{
add a, b (10, 20), c(b);
a. getdata();
a. addition();
b. addition();
c. addition();
cout << “\n Object a : “;
a. putdata();
cout << “\n Object b : “;
b. putdata();
cout << “\n Object c..”;
c. putdata();
return 0;
}
Question 3.
Write a program to find the area of a rectangle using constructor overloading in a class.
Answer:
Output:
Enter the value of length and breadth 10 20
Non parameterized constructor
area of Rectangle is 60
Parameterized constructor with one argument
area of Rectangle 8
Parameterized constructor with two argument
area of Rectangle 10
copy constructor
area of Rectangle 10