LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Predict the Output · Question 3

Which overload runs for int, double, float, char and bool? Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

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

main.cpp
#include <iostream>

void f(int)    { std::cout << 'I'; }
void f(double) { std::cout << 'D'; }

int main() {
    f(5);
    f(5.0);
    f(5.0f);
    f('5');
    f(true);
    f(5L * 1.0);
    std::cout << '\n';
    return 0;
}
  • A IDDIID Correct
  • B IDIIID
  • C IDDDID
  • D IDDIDD
Correct Answer: Option A

5 → int; 5.0 → double; float promotes to double; char and bool promote to int; 5L × 1.0 is a double.

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