LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Predict the Output · Question 11

A default argument that reads a global variable Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following C++ program?

main.cpp
#include <iostream>

int rate = 5;

int interest(int amount, int r = rate) { return amount * r / 100; }

int main() {
    std::cout << interest(1000) << ' ';
    rate = 8;
    std::cout << interest(1000) << ' ' << interest(1000, 10) << '\n';
    return 0;
}
  • A 50 80 100 Correct
  • B 50 50 100
  • C 80 80 100
  • D 50 80 80
Correct Answer: Option A

A default argument expression is evaluated at every call, so it sees the current value of rate.

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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links