Here is a listing of C++ quiz on “Vector Arithmetic” along with answers, explanations and/or solutions:
1. Which of the following library is used to do vector arithmetic?
a) Boost
b) Time
c) OpenGL
d) OpenDL
Answer: a
Clarification: Boost package has a linear algebra package that may well suits for vector arithmetic.
2. Which header file is used to manipulate the vector algebra in c++?
a) math
b) cmath
c) vmath
d) dmath
Answer: c
Clarification: vmath is set of C++ classes for Vector and Matrix algebra used in the programs.
3. What type of reference should be used in vector arithmetic?
a) dynamic
b) const
c) both dynamic & const
d) static
Answer: b
Clarification: As we are using the vector and it will give accurate result if we use const reference.
4. What will be the output of the following C++ code?
-
#include -
using namespace std;
-
class vec -
{ -
public:
-
vec(float f1, float f2)
-
{ -
x = f1;
-
y = f2;
-
} -
vec()
-
{ -
} -
float x;
-
float y;
-
};
-
vec addvectors(vec v1, vec v2);
-
int main()
-
{ -
vec v1(3, 6);
-
vec v2(2, -2);
-
vec v3 = addvectors(v1, v2);
-
cout << v3.x << ", " << v3.y << endl;
-
} -
vec addvectors(vec v1, vec v2)
-
{ -
vec result; -
result.x = v1.x + v2.x;
-
result.y = v1.y + v2.y;
-
return result;
-
};
a) 4, 5
b) 4, 4
c) 5, 4
d) 5, 5
Answer: c
Clarification: In this program, We are adding two vectors by using standalone function and printing it.
Output:
$ g++ vecar.cpp $ a.out 5, 4
5. What will be the output of the following C++ code?
-
#include -
#include -
using namespace std;
-
int main ()
-
{ -
vector<bool> mask;
-
mask.push_back(true);
-
mask.flip();
-
cout << boolalpha;
-
for (unsigned i = 0; i < mask.size(); i++)
-
cout << mask.at(i);
-
return 0;
-
}
a) false
b) true
c) false & true
d) alpha
Answer: a
Clarification: In this program, We are flipping the vector values by using flip function.
Output:
$ g++ vecar1.cpp $ a.out false
