This section on C++ quiz on “Generalized Numeric Algorithms”. One shall practice these 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 “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
Answer: b
Clarification: 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+++
Answer: c
Clarification: 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
Answer: b
Clarification: 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
-
#include
-
#include
-
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
Answer: a
Clarification: 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
-
#include
-
#include
-
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
Answer: c
Clarification: 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
-
#include
-
#include
-
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
Answer: b
Clarification: 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
-
#include
-
#include
-
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
Answer: a
Clarification: 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
-
#include
-
#include
-
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
Answer: b
Clarification: 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
Answer: d
Clarification: 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
Answer: a
Clarification: The default operation is to calculate the difference, but some other operation can be specified as binary operator instead.