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
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
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
Explanation: Returns the result of accumulating all the values in the range from first to last.
4. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myop (int x, int y)
{
return x + y;
}
int main ()
{
int val[] = {1, 2, 3, 5};
int result[7];
adjacent_difference (val, val + 7, result);
for (int i = 0; i < 4; i++)
cout << result[i] <<' ';
return 0;
}
a) 1 1 1 2
b) 1 2 3 1
c) 1 2 3 5
d) 1 2 5 6
View Answer
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?
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myfunction (int x, int y)
{
return x + 2 * y;
}
struct myclass
{
int operator()(int x, int y)
{
return x + 3 * y;
}
} myobject;
int main ()
{
int init = 100;
int numbers[] = {10, 20, 30};
cout << accumulate(numbers, numbers + 3, init);
cout << endl;
}
a) 100
b) 140
c) 160
d) 180
View Answer
Explanation: In this program, We are calculating the product of every number in the given range by using accumulate function.
Output:
$ g++ gnl1.cpp $ a.out 160
6. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myop (int x, int y)
{
return x + y + 1;
}
int main ()
{
int val[] = {1, 2, 3, 4, 5};
int result[5];
partial_sum (val, val + 5, result);
for (int i = 0; i < 5; i++)
cout << result[i] << ' ';
return 0;
}
a) 1 3 6
b) 1 3 6 10 15
c) 1 3 6 10 16
d) 1 10 5 6 4
View Answer
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?
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myfunction (int x, int y)
{
return x + 2 * y;
}
struct myclass
{
int operator()(int x, int y)
{
return x + 3 * y;
}
} myobject;
int main ()
{
int init = 100;
int numbers[] = {10, 20, 30};
cout << accumulate (numbers, numbers + 3, init, minus<int>() );
cout << endl;
return 0;
}
a) 40
b) 100
c) 140
d) 524
View Answer
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?
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myaccumulator (int x, int y)
{
return x - y;
}
int myproduct (int x, int y)
{
return x + y;
}
int main ()
{
int a = 100;
int series1[] = {10, 20, 30};
int series2[] = {1, 2, 3};
cout << inner_product(series1, series1 + 3, series2, a ,myaccumulator,
myproduct);
cout << endl;
return 0;
}
a) 40
b) 34
c) 32
d) 20
View Answer
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
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
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]
- Check Programming Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C++ Internship
- Practice Programming MCQs