开发者

Proveit.com C++ test

开发者 https://www.devze.com 2023-01-23 14:15 出处:网络
Has anyone taken the C++ test on proveit.com ? I\'ve done it a few times, and there are always questions that I get wrong, but I can\'t help but assume it\'s the site and not me. The only example I ca

Has anyone taken the C++ test on proveit.com ? I've done it a few times, and there are always questions that I get wrong, but I can't help but assume it's the site and not me. The only example I can think of off the top of my head (its been a while) is

What is the value of x after the operation?

int x = 5;
++x;

And it gives a few answers, one of which being 6. I don't see how it could be anything but. Just to be sure I would compile the code and still get 开发者_运维百科the same answer, but the test would tell me I'm wrong. Wondering if anyone has any experience with this test/site.


Perhaps the test read what will this output:

int x = 5;
std::cout << x++;

And the question was:

What will be the output?

Because when you do this x will change value but will display 5 from cout because x is incremented after the original value of x is displayed. Otherwise, it should equal 6 in your case. (You said it had been a while...)


Strictly speaking this is implementation dependent, because there might an overflow here. But for all practical purpose, yes, the value of x should be 6 when the code is done execution.


You probably have an answer by now, but hopefully this will help someone else. Depending on how the question is stated you may have got the wrong answer:

int x = 5;
cout << x++ << endl;
cout << x << endl;

would display 5 and 6 because x is incremented AFTER it has been outputted. However,

int x = 5;
cout << ++x << endl;
cout << x << endl;

would display 6 and 6 because x is incremented BEFORE it has been outputted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号