What The ... ?

Posted by alastair
on April 06, 2007 11:51

Question #24 in the otherwise quite reasonable C/C++ programming quiz at Cprogramming.com is as follows:

C is to C++ as 1 is to

a) What the heck?
b) 2
c) 10

I think I have a new favourite interview question.

Anyway, have a guess what they give as an answer.

That’s right, b).

Apparently they think C is almost a strict subset of C++, in exactly the same way that 1 is almost a strict subset of 2.

7 Responses
  1. Julian said on April 06, 2007 @ 07:45 PM

    It seems perfectly clear to me. You just need to go back to definitions.

    What is C++? The Jargon File defines it as “Designed by Bjarne Stroustrup of AT&T Bell Labs as a successor to C. ”

    What is the number 2? Peano defined it as the successor to 1.

    Ergo, Cis to C++ as 1 is to 2.

    Q.E.D.

    Do I get the job?

  2. Alastair said on April 06, 2007 @ 10:54 PM

    Ergo, C is to C++ as 1 is to 2.

    Nicely done, except it probably should be the other way around. C99 is the most recent standardisation of C, dating back to 1999 (obviously). C++ was ratified as a standard in 1998.

    Hence, C : C++ :: 2 : 1

    And don’t call us, we’ll call you.

  3. bjkeefe said on April 07, 2007 @ 08:19 AM

    I think you just treat C as a regular variable, which means the expression C++ evaluates to 2. (You would of course soundly thrash any programmer who defined a variable using a capital letter.)

    By this incremental reasoning, answer (c) is also correct, since any computer geek worth hiring looks at “10” and says “two.”

  4. Alastair said on April 07, 2007 @ 09:23 AM

    I think you just treat C as a regular variable, which means the expression C++ evaluates to 2.

    No it doesn’t! If C is 1, then C++ evaluates to 1 also. ++C evaluates to 2.

    I’m sorry, the position has been filled!

  5. bjkeefe said on April 10, 2007 @ 04:17 AM

    I agree that in the expression “C++,” if C is referenced, it returns the value 1 before incrementing C. But the entire expression evaluates to 2; else the following would never end:

    for (C=1; C<=10; C++) { … }

    Another way to say it: What’s the output of this?

    int main() { int C = 1; while (C++ < 3) { printf(“%d\n”, C); } return 0; }

  6. Eric TF Bat said on April 23, 2007 @ 11:36 AM

    I’d take a leaf from the new series of Doctor Who, where they occasionally travel to the year five billion when numbers are augmented by other symbols – so the episode The End Of The World happens in the year 5.5/Apple/26. So I’d say that C++ is a partial superset of C with some incompatibilities, and therefore C is to C++ as 1 is to 1.943turnip7442gorilla04.

    Do I win?

  7. Donkey said on December 18, 2007 @ 12:30 PM

    Sorry folks, but unlike you nerds, computers think in BINARY, not decimal. The binary representation for what you call “2” is “10”. So the correct answer is c) “10”.

Comment