LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 9

stoi with a position pointer and a base Answer, explanation & discussion

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

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::size_t idx = 0;
    int a = std::stoi("365days", &idx);
    int b = std::stoi("1010", nullptr, 2);
    int c = std::stoi("0x1F", nullptr, 16);
    std::cout << a << ' ' << idx << ' ' << b << ' ' << c << '\n';
    return 0;
}
  • A 365 3 10 31 Correct
  • B 365 7 10 31
  • C 365 3 1010 31
  • D 365 3 10 0
Correct Answer: Option A

idx receives the number of characters used (3). Base 2 reads 1010 as 10; base 16 accepts an optional 0x prefix, so 0x1F is 31.

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