Embercode
✕
C++ Functions and ScopeQuick quiz

Parameters

7 questions. Covers Varargs and parameter packs.

Questions 1 to 5 of 7

  1. 1. This program does not compile. Which statement explains the error? ``cpp struct Score { int points; int get() { return points; } }; int show(const Score& s) { return s.get(); } ``
  2. 2. This program does not compile. What is the bug? ``cpp #include <iostream> template <typename... Values> auto total(Values... values) { return (... + values); } int main() { std::cout << total() << '\n'; } ``
  3. 3. Predict the output of this program. ``cpp #include <iostream> template <typename... Values> auto product(Values... values) { return (1 * ... * values); } int main() { std::cout << product(2, 3, 4) << ' ' << product() << '\n'; } ``
  4. 4. Predict the output of this program. ``cpp #include <iostream> struct Box { int v; inline static int copies = 0; Box(int x) : v(x) {} Box(const Box& o) : v(o.v) { ++copies; } }; void by_value(Box b) { b.v += 1; } void by_ref(const Box& b) { (void)b; } int main() { Box a(5); by_value(a); by_ref(a); by_value(a); std::cout << Box::copies << ' ' << a.v << '\n'; } ``
  5. 5. Write the fold expression from the lesson that adds a starting value of 0 to every item in a pack named values. Type it exactly as it appears in code.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.