Quiz on Polymorphism in C++
Polymorphism refers to the fact that there are many forms of something. As a simple explanation, polymorphism is the capability for displaying a message more than once. Please give this topic your special attention since it is very important. Let’s begin the quiz on Polymorphism in C++ without wasting any more time. Happy Learning!
0 of 15 Questions completed Questions: You have already completed the quiz before. Hence you can not start it again. Quiz is loading… You must sign in or sign up to start the quiz. You must first complete the following: 0 of 15 Questions answered correctly Your time: Time has elapsed You have reached 0 of 0 point(s), (0) Earned Point(s): 0 of 0, (0) Types of polymorphism in C++ are: Types of compile time polymorphism in C++ are: #include<iostream> using namespace std; class Student { public : int marks; void display() { cout<<“Base class”<<endl; } }; class Topper: public Student { public: void display() { cout<<“Derived class”; } }; int main() { Student s; Topper t; s.display(); t.display(); return 0; } #include <bits/stdc++.h> using namespace std; class Students { public: void display(int x) { cout << x <<” “; } void display(double x) { cout << x << ” “; } void display(int x, int y) { cout << x << “, ” << y << endl; } }; int main() { Students obj1; obj1.display(7); obj1.display(9.132); return 0; } #include <iostream> using namespace std; class Parent { public: void display() { cout << “Parent”; } }; class Child:public Parent { public: void display() { cout << “Child”; } }; int main() { Parent* p; Child c; p = &c; p->display(); return 0; } #include <iostream> using namespace std; class Parent { public: void display() { cout << “Parent”; } }; class Child:public Parent { public: void display() { cout << “Child”; } ~Child() { cout<<“Child class Destructor called”; } }; int main() { Parent* p = new Child; p->display(); delete p; return 0; } using namespace std; class Addition { public: static int add(int a,int b){ return a + b; } static int add(int a, int b, int c) { return a + b + c; } static int add(int a, int b, int c, int d) { return a+b+c+d; } }; int main() { Addition a; cout<<a.add(5, 10)<<” “; cout<<a.add(6, 12, 18)<<” “; cout<<a.add(2,3,4,5); return 0; } #include <iostream> using namespace std; class Multiplication { public: static int mul(int a,int b){ return a * b; } static int mul(int a, int b, int c) { return a * b * c; } }; float mul(double x, int y) { return x*y; } int main() { Multiplication m; cout<<m.mul(5, 10)<<” “; cout<<m.mul(2, 3, 4)<<” “; cout<<mul(0.3,5); return 0; } #include <iostream> using namespace std; class Multiplication { public: static int mul(int a=2,int b=5){ return a * b; } static int mul(int a, int b, int c) { return a * b * c; } }; float mul(double x, int y) { return x*y; } int main() { Multiplication m; cout<<m.mul(5)<<” “; return 0; } #include <iostream> using namespace std; void mul(int a){ cout<<a<<” “; } void mul(int &a) { cout<<a<<” “; } int main() { int number=5; mul(number); return 0; } #include <iostream> using namespace std; class Multiplication { int x; public: Multiplication(){} Multiplication(int i) { x=i; } void operator*(Multiplication); void display(); }; void Multiplication :: operator*(Multiplication m) { int mul = x*m.x; cout<<mul; } int main() { Multiplication m1(5); Multiplication m2(4); m1*m2; return 0; } What is the output of the following code snippet? #include <iostream> using namespace std; class Superclass { public: virtual void display()=0; }; class Subclass:public Superclass { public: void display() { cout<<“virtual function”<<endl; } }; int main() { Superclass *super; Subclass sub; super = ⊂ sub.display(); super->display(); return 0; } #include <iostream> using namespace std; class Superclass { public: virtual void display() { cout<<“Superclass”<<endl; } }; class Subclass:public Superclass { public: void display() { cout<<“Subclass”<<endl; } }; int main() { Superclass a; Subclass b; Superclass c; Superclass *pa[]={&a,&b,&c}; for ( int j=0;j<3;j++) pa[j]->display(); return 0; } Quiz Summary
Information
Results
Results
0 Essay(s) Pending (Possible Point(s): 0) Categories
1. Question
2. Question
3. Question
An abstract class in C++ can contain
4. Question
5. Question
6. Question
7. Question
8. Question
9. Question
10. Question
11. Question
12. Question
13. Question
14. Question
15. Question
Summary
Polymorphism is one of the important features of object-oriented programming. In the above quiz, you have learned a lot of important concepts of polymorphism. We recommend moving forward only after you understand each concept of polymorphism in detail. Have a good learning experience!
Static fuctions is object independent
The output for question 12 will be compile time error, because there are undefined reference for display() function