LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Predict the Output · Question 13

Return types deduced with auto Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

auto half(int x) { return x / 2; }
auto halfd(int x) { return x / 2.0; }
auto twice(char c) { return c + c; }

int main() {
    std::cout << half(7) << ' ' << halfd(7) << ' ' << twice('a') << ' '
              << sizeof(half(7)) << sizeof(halfd(7)) << sizeof(twice('a')) << '\n';
    return 0;
}
  • A 3 3.5 194 484 Correct
  • B 3.5 3.5 194 884
  • C 3 3.5 aa 481
  • D 3 3.5 194 444
Correct Answer: Option A

x / 2 is int, x / 2.0 is double, and char + char is promoted to int (97 + 97 = 194). Their sizes are 4, 8 and 4.

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