C++ Programming Questions and Answers – Vector Arithmetic

This section on C++ quiz focuses on “Vector Arithmetic”. One shall practice these online quizzes to improve their C++ programming skills needed for various interviews (campus interviews, walkin 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.

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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: As we are using the vector and it will give accurate result if we use const reference.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class vec
  4.     {
  5.         public:
  6.         vec(float f1, float f2) 
  7.         {
  8.             x = f1;
  9.             y = f2;
  10.         }
  11.         vec()
  12.         {
  13.         }
  14.         float x;
  15.         float y;
  16.     };
  17.     vec addvectors(vec v1, vec v2);
  18.     int main() 
  19.     {
  20.         vec v1(3, 6);
  21.         vec v2(2, -2);
  22.         vec v3 = addvectors(v1, v2);
  23.         cout << v3.x << ", " << v3.y << endl;
  24.     }
  25.     vec addvectors(vec v1, vec v2)
  26.     {
  27.         vec result;
  28.         result.x = v1.x + v2.x;
  29.         result.y = v1.y + v2.y;
  30.         return result;
  31.     };

a) 4, 5
b) 4, 4
c) 5, 4
d) 5, 5
View Answer

Answer: c
Explanation: 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?

advertisement
  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<bool> mask;
  7.         mask.push_back(true);
  8.         mask.flip();
  9.         cout << boolalpha;
  10.         for (unsigned i = 0; i < mask.size(); i++)
  11.         cout << mask.at(i);
  12.         return 0;
  13.     }

a) false
b) true
c) false & true
d) alpha
View Answer

Answer: a
Explanation: In this program, We are flipping the vector values by using flip function.
Output:

advertisement
$ g++ vecar1.cpp
$ a.out
false

6. What will be the type of output of vector cross product?
a) Scalar
b) Vector
c) Both Scalar & Vector
d) Linear
View Answer

Answer: b
Explanation: Cross product of two vectors results into a vector.

7. Which function is used to optimize the space in vector?
a) at
b) bool
c) operator
d) operand
View Answer

Answer: b
Explanation: This is a specialized version of vector, which is used for elements of type bool and optimizes for space.

8. What is the use of vector arithmetic in c++?
a) Computer graphics
b) Computer booting
c) Both Computer graphics & Computer booting
d) Computer Networks
View Answer

Answer: a
Explanation: Computer graphics is the use of vector arithmetic in c++.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.