Quiz on Virtual, Inline and Friend Functions in C++
A function is a block of code that runs only when it is called. Virtual, inline and friend are special types of functions used for different purposes. Let’s test our understanding of these special Functions in C++ with this quick quiz. Enjoy 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) What is the output of the following code snippet? What is the output of the following code snippet? What is the output of the following code snippet? What is the output of the following program? Which keyword is used to declare the inline function? What is the output of the following code snippet? Which of the following functions may or may not be the members of other classes? What is the output of the following code snippet? What is the output of the following code snippet? } What is the output of the following code snippet? What is the output of the following code snippet? What is the output of the following code snippet? Where a function call can be substituted for its definition, it is referred to as? What is the output of the following code snippet? What is the output of the following code snippet? Quiz Summary
Information
Results
Results
0 Essay(s) Pending (Possible Point(s): 0) Categories
1. Question
#include<iostream>
using namespace std;
class Parent {
public:
Parent() { cout<<“Constructor: Parent class”<<endl; }
virtual ~Parent() { cout<<“Destructor : Parent class”<<endl; }
};
class Child: public Parent {
public:
Child() { cout<<“Constructor: Child class”<<endl; }
~Child() { cout<<“Destructor : Child class”<<endl; }
};
int main() {
Parent *pb=new Child();
delete pb;
return 0;
} 2. Question
#include<iostream>
using namespace std;
class Parent {
public:
Parent() { cout<<“Constructor: Parent class”<<endl; }
virtual ~Parent() { cout<<“Destructor : Parent class”<<endl; }
};
class Child: public Parent {
public:
Child() { cout<<“Constructor: Child class”<<endl; }
~Child() { cout<<“Destructor : Child class”<<endl; }
};
class SubChild:public Child
{
public:
SubChild()
{
cout<<“Constructor: Sub-Child class”<<endl;
}
~SubChild()
{
cout<<“Destructor : Sub-Child class”<<endl;
}
};
int main() {
Parent *pb=new SubChild();
delete pb;
return 0;
} 3. Question
#include<iostream>
using namespace std;
class Parent {
public:
Parent() { cout<<“Constructor: Parent class”<<endl; }
virtual ~Parent() { cout<<“Destructor : Parent class”<<endl; }
};
class Child{
public:
Child() { cout<<“Constructor: Child class”<<endl; }
~Child() { cout<<“Destructor : Child class”<<endl; }
};
class SubChild:public Child, public Parent
{
public:
SubChild()
{
cout<<“Constructor: Sub-Child class”<<endl;
}
~SubChild ()
{
cout<<“Destructor : Sub-Child class”<<endl;
}
};
int main() {
SubChild *pb=new SubChild();
delete pb;
return 0;
} 4. Question
#include<iostream>
using namespace std;
class University
{
public:
virtual void courses() {cout<<“University courses”; }
};
class Student: public University
{
public:
void courses()
{
cout<<“Please Enter No. of Courses available”;
}
};
int main()
{
Student s;
s.courses();
} 5. Question
6. Question
#include<iostream>
using namespace std;
class Parent {
public:
Parent()
{ cout<<“Parent Constructor \n”; }
virtual ~Parent()
{ cout<<“Parent Destructor\n”; }
};
class Child: public Parent {
public:
Child()
{ cout<<“Child Constructor \n”; }
~Child()
{ cout<<“Child Destructor\n”; }
};
int main(void)
{
Child *c = new Child();
Parent *p = c;
delete p;
return 0;
} 7. Question
8. Question
#include <iostream>
using namespace std;
class Student
{
private:
int x, y;
public:
void result()
{
x = 200;
y = 300;
}
friend int marks(Student s1);
};
int marks(Student s1)
{
return int(s1.x + s1.y) – 30;
}
int main()
{
Student s;
s.result();
cout << marks(s);
return 0;
} 9. Question
#include <iostream>
using namespace std;
class University
{
int dept1, dept2;
public:
int record()
{
dept1 = 400;
dept2 = 500;
return dept1*dept2;
}
friend float average(University u);
};
float average(University u)
{
return float(u.dept1 + u.dept2) / 2;
}
int main()
{
University obj;
obj.record();
cout << average(obj);
return 0; 10. Question
#include<iostream>
using namespace std;
class Parent
{
public:
virtual void display() { cout<<” In Parent Class \n”; }
};
class Child: public Parent
{
public:
void display() { cout<<“In Child Class \n”; }
};
int main()
{
Child c;
Parent &pb=c;
pb.display();
return 0;
} 11. Question
#include<iostream>
using namespace std;
class Parent
{
public:
virtual void display() { cout<<” In Parent Class \n”; }
};
class Child: public Parent
{
public:
void display() { cout<<“In Child Class \n”; }
};
int main()
{
Parent *pc = new Child;
pc->display();
Parent &pb = *pc;
pb.display();
return 0;
} 12. Question
13. Question
14. Question
#include <iostream>
using namespace std;
void inline records(int a, int b)
{
cout<<“a= “<< a<<“and b=”<< b<<”,”;
}
int inline squaredata(int x)
{
return x*x;
}
int main()
{
int data;
records(1,4);
data = squaredata(6);
cout<<” square val=”<< data;
return 0;
} 15. Question
#include <iostream>
using namespace std;
void inline records(char b)
{
cout<<“value of b is:”<<b<<“,”;
}
int inline addition(char b)
{
return b+b;
}
int main()
{
char val=’D’;
records(val);
cout<<” Addition of ASCII Values are:”<<addition(val);
return 0;
}
Summary
Programmers use functions as one of their most important tools. By reusing code and enhancing the quality of the code, they make it easier to maintain. You might have gained some insight into your knowledge of arguments and special kinds of functions by taking the quiz.