C++ quiz focuses on “Function Call”. One shall practice these quizzes to improve their C++ programming skills needed for various interviews (campus interviews, walk in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ quiz comes with detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ quiz on “Function Call” along with answers, explanations and/or solutions:
Q 1. What is the use of function call operator?
A. overloading the methods
B. overloading the objects
C. overloading the parameters
D. overloading the string
Show Answer
Answer:-B. overloading the objectsExplanation
Overloading the objects is the use of function call operator.Q 2. Pick out the correct statement.
A. virtual functions does not give the ability to write a templated function
B. virtual functions does not give the ability to rewrite a templated function
C. virtual functions does give the ability to write a templated function
D. virtual functions does not give the ability to rewrite a simple function
Show Answer
Answer:-A. virtual functions does not give the ability to write a templated functionExplanation
Virtual functions does not give the ability to write a templated function.Q 3. What will happen when the function call operator is overloaded?
A. It will modify the operator to be interpreted
B. It will modify the functions
C. It will modify the object
D. It will not modify the functions
Show Answer
Answer:-A. It will modify the operator to be interpretedExplanation
It will modifies how the operator is to be interpreted when applied to objects of a given type.4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches = i;
}
Distance operator()(int a, int b, int c)
{
Distance D;
D.feet = a + c + 10;
D.inches = b + c + 100 ;
return D;
}
void displayDistance()
{
cout << feet << inches << endl;
}
};
int main()
{
Distance D1(11, 10), D2;
cout << “First Distance : “;
D1.displayDistance();
D2 = D1(10, 10, 10);
cout << “Second Distance :”;
D2.displayDistance();
return 0;
}
A. First Distance : 110
Second Distance :3020
B. First Distance : 1110
Second Distance :30120
C. First Distance : 1115
Second Distance :30125
D. pre> First Distance : 100
Second Distance :30120
Show Answer
Answer:-B. First Distance : 1110 Second Distance :30120Explanation
We are calculating the foot and inches by using the function call operator. Output: $ g++ call.cpp $ a.out First Distance : 1110 Second Distance :30120Q 5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c)
{
a *= 2;
b *= 2;
c *= 2;
}
int main ()
{
int x = 1, y = 3, z = 7;
duplicate (x, y, z);
cout << x << y << z;
return 0;
}
A. 1468
B. 2614
C. 2713
D. 2812
Show Answer
Answer:-B. 2614Explanation
We are passing the values by reference and modified the data on the function block. Output: $ g++ call1.cpp $ a.out 2614Q 6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class three_d
{
int x, y, z;
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) { x = i; y = j; z = k; }
three_d operator()(three_d obj);
three_d operator()(int a, int b, int c);
friend ostream &operator<<(ostream &strm, three_d op);
};
three_d three_d::operator()(three_d obj)
{
three_d temp;
temp.x = (x + obj.x) / 2;
temp.y = (y + obj.y) / 2;
temp.z = (z + obj.z) / 2;
return temp;
}
three_d three_d::operator()(int a, int b, int c)
{
three_d temp;
temp.x = x + a;
temp.y = y + b;
temp.z = z + c;
return temp;
}
ostream &operator<<(ostream &strm, three_d op) {
strm << op.x << “, ” << op.y << “, ” << op.z << endl;
return strm;
}
int main()
{
three_d objA(1, 2, 3), objB(10, 10, 10), objC;
objC = objA(objB(100, 200, 300));
cout << objC;
return 0;
}
A. 55, 106
B. 55, 106, 156
C. 55, 106, 159
D. 55, 106, 158
Show Answer
Answer:-B. 55, 106, 156Explanation
In this program, We are using the function call operator to calculate the value of objc. Output: $ g++ call2.cpp $ a.out 55, 106, 156Q 7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex():real(0), imag(0){}
Complex operator ()(float re, float im)
{
real += re;
imag += im;
return *this;
}
Complex operator() (float re)
{
real += re;
return *this;
}
void display()
{
cout << “(” << real << “,” << imag << “)” << endl;
}
};
int main()
{
Complex c1, c2;
c2 = c1(3.2, 5.3);
c1(6.5, 2.7);
c2(1.9);
cout << “c2=”;c1.display();
cout << “c2=”;c2.display();
return 0;
}
A. c2=(9.7,8)
c2=(5.1,5.3)
B. c2=(9,8)
c2=(5,5)
C. c2=(4.7,8)
c2=(2.1,5.3)
D. c2=(5.7,8)
c2=(5.1,5.6)
Show Answer
Answer:-A. c2=(9.7,8) c2=(5.1,5.3)Explanation
In this program, We are returning the real and imaginary part of the complex number by using function call operator. Output: $ g++ call3.cpp $ a.out c2=(9.7,8) c2=(5.1,5.3)Q 8. In which form does the function call operator can be overloaded?
A. static member function
B. static_cast
C. dynamis_cast
D. non-static member function
Show Answer
Answer:-D. non-static member functionExplanation
In non-static member function, the function call operator can be overloaded.Q 9. What is the use of functor?
A. It makes the object “callable” like a function
B. It makes the class “callable” like a function
C. It makes the attribute “callable” like a function
D. It makes the argument “callable” like a function
Show Answer
Answer:-A. It makes the object “callable” like a functionExplanation
functor makes the object “callable” like a function.Q 10. What will be the output of the following C++ code?
#includeusing namespace std;
int operate (int a, int b)
{
return (a * b);
}
float operate (float a, float b)
{
return (a / b);
}
int main ()
{
int x = 5, y = 2;
float n = 5.0, m = 2.0;
cout << operate (x, y);
cout << operate (n, m);
return 0;
}
A. 119
B. 102.5
C. 123.4
D. 128.4
Leave a Reply