C++ Programming Questions and Answers – Generalized Numeric Algorithms

This section on C++ Multiple Choice Questions focuses on “Generalized Numeric Algorithms”. One shall practice these questions 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++ questions comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ Questions & Answers focuses on “Generalized Numeric Algorithms” along with answers, explanations and/or solutions:

1. Which header file is used to operate on numeric sequences?
a) number
b) numeric
c) algorithm
d) digit
View Answer

Answer: b
Explanation: header file is used to operate on numeric sequences that support certain operations.

2. Which mathematics library is used for vector manipulation in c++?
a) cli++
b) vec++
c) blitz++
d) stac+++
View Answer

Answer: c
Explanation: Blitz++ is a high-performance vector mathematics library written in C++.

3. What is the use of accumulate function in a numeric library?
a) Returns the number
b) Returns the result of accumulating all the values in the range
c) Returns the number & result
d) Return the characters
View Answer

Answer: b
Explanation: Returns the result of accumulating all the values in the range from first to last.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric>
  4.     using namespace std;
  5.     int myop (int x, int y)
  6.     {
  7.         return x + y;
  8.     }
  9.     int main ()
  10.     {
  11.         int val[] = {1, 2, 3, 5};
  12.         int result[7];
  13.         adjacent_difference (val, val + 7, result);
  14.         for (int i = 0; i < 4; i++)
  15.             cout << result[i] <<' ';
  16.         return 0;
  17.     }

a) 1 1 1 2
b) 1 2 3 1
c) 1 2 3 5
d) 1 2 5 6
View Answer

Answer: a
Explanation: In this program, We are calculating the adjacent difference of the given range by using function adjacent_difference.
Output:

$ g++ gnl.cpp
$ a.out
1 1 1 2

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

advertisement
  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric>
  4.     using namespace std;
  5.     int myfunction (int x, int y) 
  6.     {
  7.         return x + 2 * y;
  8.     }
  9.     struct myclass 
  10.     {
  11.         int operator()(int x, int y) 
  12.         {
  13.             return x + 3 * y;
  14.         }
  15.     } myobject;
  16.     int main () 
  17.     {
  18.         int init = 100;
  19.         int numbers[] = {10, 20, 30};
  20.         cout << accumulate(numbers, numbers + 3, init);
  21.         cout << endl;
  22.     }

a) 100
b) 140
c) 160
d) 180
View Answer

Answer: c
Explanation: In this program, We are calculating the product of every number in the given range by using accumulate function.
Output:

advertisement
$ g++ gnl1.cpp
$ a.out
160

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

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric>
  4.     using namespace std;
  5.     int myop (int x, int y) 
  6.     {
  7.         return x + y + 1;
  8.     }
  9.     int main () 
  10.     {
  11.         int val[] = {1, 2, 3, 4, 5};
  12.         int result[5];
  13.         partial_sum (val, val + 5, result);
  14.         for (int i = 0; i < 5; i++)
  15.             cout << result[i] << ' ';
  16.         return 0;
  17.     }

a) 1 3 6
b) 1 3 6 10 15
c) 1 3 6 10 16
d) 1 10 5 6 4
View Answer

Answer: b
Explanation: In this program, We are calculating the sum of the given range by using partial_sum function.
Output:

$ g++ gnl2.cpp
$ a.out
1 3 6 10 15

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

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric>
  4.     using namespace std;
  5.     int myfunction (int x, int y) 
  6.     {
  7.         return x + 2 * y;
  8.     }
  9.     struct myclass 
  10.     {
  11.         int operator()(int x, int y) 
  12.         {
  13.             return x + 3 * y;
  14.         }
  15.     } myobject;
  16.     int main () 
  17.     {
  18.         int init = 100;
  19.         int numbers[] = {10, 20, 30};
  20.         cout << accumulate (numbers, numbers + 3, init, minus<int>() );
  21.         cout << endl;
  22.         return 0;
  23.     }

a) 40
b) 100
c) 140
d) 524
View Answer

Answer: a
Explanation: In this program, We are finding the difference between the init and the total of numbers range.
Output:

$ g++ gnl3.cpp
$ a.out
40

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

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <numeric> 
  4.     using namespace std;
  5.     int myaccumulator (int x, int y) 
  6.     {
  7.         return x - y;
  8.     }
  9.     int myproduct (int x, int y) 
  10.     {
  11.         return x + y;
  12.     }
  13.     int main () 
  14.     {
  15.         int a = 100;
  16.         int series1[] = {10, 20, 30};
  17.         int series2[] = {1, 2, 3};
  18.         cout << inner_product(series1, series1 + 3, series2, a ,myaccumulator, 
  19.         myproduct);
  20.         cout << endl;
  21.         return 0;
  22.     }

a) 40
b) 34
c) 32
d) 20
View Answer

Answer: b
Explanation: In this program, We are forming the custom function from two ranges by using inner_product function.
Output:

$ g++ gnl4.cpp
$ a.out
34

9. How many parameters are available in partial_sum function in c++?
a) 2
b) 3
c) 2 or 3
d) 3 or 4
View Answer

Answer: d
Explanation: There are three or four parameters available in partial_sum function in C++. They are first and last element, result and an optional binary operator.

10. What is the default operation of adjacent_difference function in numeric library?
a) Difference
b) Addition
c) Multiplication
d) Subtraction
View Answer

Answer: a
Explanation: The default operation is to calculate the difference, but some other operation can be specified as binary operator instead.

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.