Question 11
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <array>
#include <iostream>
constexpr int square(int x) { return x * x; }
consteval int cube(int x) { return x * x * x; }
constexpr std::array<int, 5> make_table() {
std::array<int, 5> t{};
for (int i = 0; i < 5; ++i) t[i] = square(i) + cube(2);
return t;
}
int main() {
constexpr auto table = make_table();
static_assert(table[3] == 17);
int n = 6;
std::cout << square(n) << ' ' << cube(3) << ' ' << table[4] << '\n'; // square at run time, cube at compile time
return 0;
}
- A 36 27 24 Correct
- B 36 27 16
- C 36 9 24
- D Compilation error
Correct Answer: Option A
square can run at run time (n is a variable); cube(3) has a constant argument, as consteval requires. table[4] = 16 + 8.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!