
The Hardest Test I Ever Gave (C++) - aroman
http://lbrandy.github.com/blog/2008/11/the-hardest-test-i-ever-gave-c/
======
adamnemecek
Oh, you came up with a shitty test (testing I'm not even sure what) to stroke
your ego while probably turning some kids away from programming? And then you
thought that a recollection of this experience was worth sharing with the
world? I sarcastically applaud your efforts. But seriously you are a douche.

~~~
10098
If the object of the course is to study C++, I say this is a good test, and
it's not even that hard tbh.

~~~
adamnemecek
I did not say it was. But it's a shitty test regardless.

~~~
10098
Again, it depends on what those kids were supposed to study. If it's a general
programming/algorithms class then yes this is testing the wrong knowledge. If
it's a C++ specific class, this is completely justified. How does one come out
of a C++ class and not know this stuff?

~~~
adamnemecek
It was "an intro to programming course for incoming engineering freshman".

Also I write C++ for living and I was not entirely sure if "y = x++3;" was
valid or not (turns out it is an error but "y = x+ +3;" is fine, which I would
have guessed but would not have been quite certain).

Edit: Now that I think about, I believe that "x = (x++ == 3 + x % (x=x=3));"
is undefined behavior. I guess the question is not asking about that but
regardless, that's a stupid thing to ask.

~~~
tolmasky
If you set aside the fact that the deeper issue is probably that C++ in
general is a bad intro language to begin with, then its not completely
unreasonable to test on this syntactic stuff, especially as you go farther
back when compilers/checkers were less friendly. For better or worse, one of
the lessons of programming is the unforgiving nature of the computer to do
exactly what you typed in, and not what you meant. I remember having a similar
test or quiz to this when I was in high school (~12 years ago or so). Its not
that big of a deal, the whole class is not just that, its just to kind of
drill this into your head. We were using pretty terrible Borland compilers
with rubbish error messages. At that particular point, being able to stare at
code and determine what minor thing you mistyped was unfortunately a crucial
skill. Again, this is arguably less important today (and arguably _all_
written tests are bad for CS).

Later in college, I remember hating the assembly tests where you had to keep
track of what numbers were in what address, and whether you were adding what
was on the stack or in some register or the address or what was in the address
and making a mistake in the very beginning could ruin the whole problem. Not
fun, not sure how useful, but certainly gave you an appreciation of what was
going on.

~~~
adamnemecek
I guess my main issue is with OP's attitude regardless of the test's content.
His sense of "lol, I sure showed the freshmen that I can put together a test
that they would fail" is just plain dumb.

------
beambot
Perfect reminder of why I don't use C / C++ anymore. I had a teacher
(highschool, IIRC) who made similar exams... He had the gall to call his
course "computer science." I called it a waste of time, effort, and
brainpower. I hate to think about how many computer-loving students he ruined.

OP, you did your students a disservice. You should've just assigned a
"practicum" exam instead.

~~~
bnegreve
1\. There is no such a thing as C / C++. "Real" C++ is closer to other OO
languages than it is from C.

2\. It is not because a language is not a good option for teaching that it
isn't worth using.

BTW After hours of debate, I still think that C is not a bad option for
teaching purposes, mainly because it has a very simple syntax and almost NO
api which generally introduce confusion.

~~~
pimentel
I learned programming in C. I really think it is important to learn all the
data structures internal functioning (coding your own linked lists or hash-
tables), learning how memory in a program really works, what is the difference
between a 4 byte-type variable versus a 1 byte one, etc

------
jre
This reminds me about an undergrad C course I had. One test was about printf
formatting. We were given a serie of crazy format strings (using format
specifier I've never encountered again since) and had to know what it would
output. This was the most stupid test I've ever had.

There was a similar one about pointers. We called it "Star Wars".

~~~
chris_wot
A test about pointers is not stupid. A test about printf formatters is.

~~~
_pmf_
> A test about pointers is not stupid. A test about printf formatters is.

Obviously you did not have the joy of working with people who write their own
byzantine 1k LoC formatting code for cases that would have been perfectly
covered by printf (and yes, this was on a system where a printf implementation
was available).

------
rmserror
This is crap.

1e: this is undefined behavior (-Wsequence-point) x can be literally anything.

3\. a typical mistake that the compiler both warns about and that anyone who
has taken a course on C should be able to immediately recognize

bonus 1. void main(int)???

~~~
DerekL
Question 1 asks about syntax. The behavior of the code is irrelevant.

~~~
TwoBit
Except for the the fact that the author writes: "for bonus points, if x is 1
before 1e, what is x after that line?"

~~~
epidemian
Then wouldn't an answer like "x could be anything because of undefined
behavior" be correct?

This test is indeed quite evil, especially considering it was for freshman
students. I have to admit, though, that the bonus question was great.

------
aroman
Just so everyone's clear, I am not the author of this blog post. I had
replaced the I in the title with [He], but a. Mod must have annoyingly edited
it out. I hope people realize that.

------
olalonde
I would probably have assumed that "question #5" and "Bonus Question #1" had
typos and failed those questions.

    
    
        if (y = z)
    

and

    
    
        cout << 'j' << 'k';

~~~
nhaehnle
You should always either ask to clarify or, if that is not possible, go with
the literal statement of the question, and explicitly say so on your test
paper. Any professor worth his/her salt will accept that and think even more
highly of you, because they will recognize that you are able to think about
the question on a higher level than most students.

If, on the other hand, you just silently "correct" the question in your mind,
you will fail - especially in a course on programming, because the point is
exactly that you have to learn to deal with computers that do _not_ silently
"correct" the program that you give them.

------
Toenex
These are great tests... for a compiler.

------
tommys
Nonsense test.

------
Sharlin
1c is cruel. One can hardly expect freshmen to understand the difference
between a syntax error and a semantic error. Depending on the context, the
line may be perfectly valid, or it may not.

------
leoh
Meh. I thought this was pretty easy. Where are the solutions?

~~~
10098
You can use a compiler to find the solutions. Except maybe for 1e bonus part,
the output won't tell you much in that case.

~~~
benihana
Can I ask what the point of this comment is? _Obviously_ one can use a
compiler to find the solutions. OP was most likely asking because he didn't
want to do that.

~~~
10098
What, he was too lazy to copypaste those into codepad and click a button?
Okay, I guess.

~~~
leoh
I think being on a mobile device is a decent excuse ;-)

------
friendly_chap
You are one of the reasons why education sucks in general.

------
chris_wot
Not a single question about templates. Your test was not hard enough.

------
ankitml
You are cruel!!

------
rorrr
Without a compiler, or google, haven't touched C++ in years:

1a) will give an error

2a) 2

2b) -10

2c) 4

2d) This is clever, haha. Answer: 5

2e) true (or 1)

5) "This is the answer." because of

    
    
        if (y = z)
    

Bonus) jk

~~~
raverbashing
1a) not so sure, because it could be x+(+3)

But yeah, gcc throws an error

If you put the parenthesis it works

~~~
chris_wot
He didn't put it into parenthesis.

~~~
raverbashing
I know that, I'm using parenthesis to explain what the compiler "might think"

~~~
chris_wot
No decent compiler would attempt to correct that by adding parenthesis!

~~~
rorrr
It's not about a parenthesis. It's about the compiler not understanding all
valid possibilities.

x+ +3 works.

x++3 doesn't.

~~~
chris_wot
OK, I take it back. That's logical.

Technically speaking though, the compiler could see it as x+ +3, but it could
equally see it as x ++3, which has two compiler errors: firstly, that's not
actually a valid expression, and secondly (at least as I checked in the C99
standard, under section 6.5.3.1 on postfix increment and decrement operators)
the lvalue must be modifiable.

All I guess I'm saying here is that it could technically be a typo which leads
to invalid syntax, or it could be technically correct, so most compilers will
treat it as a syntax error.

Someone above said that gcc was being "closer to the standard", but the
standard never attempts to cover this particular corner case. So the gcc devs
have done something, to my mind, quite sensible. They treat it as a possible
syntax error because it could either be:

    
    
      x+ +3;
      x++ 3;
      x ++3;
    

I'm more interested in how it handles x+++x; - I'd imagine that it's a syntax
error. It could be:

    
    
      x++ + x;
      x + ++x;
      x+ + +x;
    

The C99 standard doesn't actually explain how to handle this. Perhaps they
believe you shouldn't be so daft.

I'll stand by original statement that it's a syntax error and most good
compilers will treat it as such, even if it is logical to treat x++3 as x+ +3.

 _Edit:_ scratch that ALL completely. I forgot about operator precedence.

If you take x++3, then suffix increment is a higher operator than suffix
decrement and addition. In other words, the compiler will always try to
evaluate the expression as

    
    
      x++ 3;
    

In other words, it's a syntax error. It can never evaluate to x + +3, because
that's at a lower operator precedence.

Thus, it's a syntax error. Take _that_ , downvoter of my original comment.

 _Edit x2:_ Definitive answer is here:
<https://news.ycombinator.com/item?id=5200523>

Some days it just doesn't pay to get out of bed... :-)

~~~
jomar
Thanks for the pointer to my answer :-)

Similar text to that C++ text is in C99 at 6.4 [Lexical elements], paragraph
4. Paragraph 6 considers the example x+++++y (which is similar to your
example), noting that it is to be parsed as x ++ ++ + y. (I guess it uses
distinct variables x and y to avoid any distractions due to sequence point
issues -- which are immaterial here as the thing fails to parse anyway.)

~~~
chris_wot
Sometimes I think that the C and C++ standards authors entered into the IOCCC
before they wrote the examples :-)

