LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Exercise 3 of 4

Functions and Default Arguments Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each C++ program contains a function-related mistake that the compiler rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int total(int price = 100, int qty) {
    return price * qty;
}

int main() {
    std::cout << total(200, 3) << '\n';
    return 0;
}
A qty must be unsigned
B Default arguments must be trailing: qty, after the defaulted price, needs a default too (or reorder the parameters)
C total must be declared first
D The call needs only one argument
Question page & discussion
Question 2 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

void beep(int times = 1);

void beep(int times = 1) {
    for (int i = 0; i < times; ++i) std::cout << "beep ";
    std::cout << '\n';
}

int main() {
    beep();
    return 0;
}
A beep must return int
B A default argument may be given only once; repeating it in the definition is a redefinition error
C The loop needs braces
D void functions cannot have defaults
Question page & discussion
Question 3 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

void store(int x)  { std::cout << "int " << x << '\n'; }
void store(long x) { std::cout << "long " << x << '\n'; }

int main() {
    store(3.75);
    return 0;
}
A 3.75 is too large
B double converts equally well to int and to long, so the call is ambiguous
C Overloads must be in different files
D long cannot be printed
Question page & discussion
Question 4 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int  value() { return 1; }
double value() { return 1.5; }

int main() {
    std::cout << value() << '\n';
    return 0;
}
A double cannot be returned
B Functions cannot be overloaded on return type alone; both have the same parameter list
C value is a keyword
D main must call value twice
Question page & discussion
Question 5 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

void add_bonus(int& marks) { marks += 5; }

int main() {
    add_bonus(80);
    return 0;
}
A marks must be double
B A non-const reference cannot bind to the literal 80 – it needs a variable to modify
C add_bonus must return a value
D 80 is out of range
Question page & discussion
Question 6 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    std::cout << cube(3) << '\n';
    return 0;
}

int cube(int x) { return x * x * x; }
A cube must be inline
B cube is used before any declaration of it; add the prototype int cube(int); above main or move the definition up
C cube needs a default argument
D main must be last in the file
Question page & discussion
Question 7 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

void percentage(int got, int total) {
    return got * 100 / total;
}

int main() {
    std::cout << percentage(45, 50) << '\n';
    return 0;
}
A The division must use doubles
B A void function cannot return a value (and its result cannot be printed); the return type should be int or double
C got must be const
D percentage must take references
Question page & discussion
Question 8 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

consteval int square(int x) { return x * x; }

int main() {
    int n;
    std::cin >> n;
    std::cout << square(n) << '\n';
    return 0;
}
A square must be recursive
B A consteval function must be evaluated at compile time, but n is only known at run time; use constexpr instead
C cin cannot read int
D consteval functions cannot return int
Question page & discussion
Question 9 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>

void shout(const std::string& msg) {
    msg += "!";
    std::cout << msg << '\n';
}

int main() {
    shout("Hello");
    return 0;
}
A "Hello" is not a std::string
B msg is a const reference, so += cannot modify it; work on a copy or take the parameter by value
C shout must return std::string
D += does not work on strings
Question page & discussion
Question 10 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    int helper(int x) {
        return x + 1;
    }
    std::cout << helper(5) << '\n';
    return 0;
}
A helper must be declared static
B C++ does not allow function definitions inside other functions; define helper outside main or use a lambda
C x must be a reference
D helper must return void
Question page & discussion
Up next · Exercise 4Function Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links