HN2new | past | comments | ask | show | jobs | submitlogin
The Hardest Test I Ever Gave (C++) (lbrandy.github.com)
32 points by aroman on Feb 11, 2013 | hide | past | favorite | 57 comments


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.


In fairness, the author does recognize that he failed in giving a good test.

The thing is, those questions demonstrate a certain type of playfulness in dealing with the subject. It is not uncommon for good students to think of this type of questions when they design their first tests, precisely because those are the kinds of questions that they like to think about themselves. What they fail to recognize is that those questions are incredibly hard for others precisely because of their playfulness.

So those questions are useful for quickly recognizing really good students (students that are so good that they transcend the requirements of the course and can deal with the subject on this playful level), but they should only make up a very small portion of any test. And teachers should probably warn their students about this type of question...


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.


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


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?


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.


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.


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.


Questioning what the -Wall -Wextra -pedantic arguments mean would be better. This is how you find those typos in the real world.

Ask them about compiler errors. For example, what does "undefined reference to `vtable for Foo'" mean?

Then some style guide [0] might be helpful. Make them write the rvalue expression to the left of comparisons, because "if (3 = x)" is an error then. Or some "assert (this != NULL)" in methods.

Ask about semantics, like "What is the difference between a move and a copy constructor?" or "Write a program to demonstrate the difference between any two of static_cast,dynamic_cast,reinterpret_cast,const_cast,implicit cast" or "When should you use unique_ptr instead of shared_ptr?"

[0] http://google-styleguide.googlecode.com/svn/trunk/cppguide.x...


Not really. To be honest, if anything it's good training. How many times have you seen an assignment operator in an if statement where a comparison operator was meant to be used?

Actually, now I think of it... all the questions were related to operators, except the last one which was a trick question. If all the questions bar one were in regards to operators only, then this isn't a particularly good test.


I disagree. Question#5 and bonus Question#1 are good questions. Production bugs are often close to these.

It tests if a student is able to follow a program's logic with rigor. It tests if you don't interpret the code but actually read it "as is".

Question#1 and Question#2 are less intelligent.

Also, no template test? Are you testing C with classes or C++ ? ;)


I found Question #1 to be bad, #2 is alright but coming close to just a 'trick' question. #5 is just a simple trick question, and I can see where people went wrong with it. Especially if they skimmed over the code, and in a high pressure test environment. The bonus question is reasonable and can be worked through, if it wasn't printing the string literals, which makes it just a mean trick question.

I think that's the problem with a lot of first time test (or interview) makers, they make their questions into trick/puzzle questions where they want the test taker to guess what the writer had in their mind.

--edited because I skimmed over the last question and assumed it was printing the values of the variables not the strings--


Screw that, I loved it when we'd get a "bonus test" that was actually really difficult.


I'm truly sorry that learning a real language takes some more effort than Ruby-loving ADHD rockstar developers can muster.


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.


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.


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


1. I learned the two languages contemporaneously. They were commonly referred to as C / C++. I used C mostly for embedded/systems programming. I don't know what "real" C++ means... but to be fair, the last time I really used it was pre-STL.

2. OK. (I didn't make that claim, did I?) C / C++ has it's uses. I use it when I have to (embedded systems, systems programming, and performance). But I don't use it willingly.

I TA'd a course in C for EE's. It was fine. I think it probably helped them in many ways. Anecdote: My favorite course as an undergrad was building our own wire-wrapped 8086 computer. You quickly learn to appreciate assembly (versus manually loading registers), and then quickly learn to appreciate the C compiler.


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".


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


> 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).



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)???


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


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


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.


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.


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';


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.


(a = b) inside an if (inside a test) is never a typo!!

That get me to think "this is the evillest thing I can imagine" =)


These are great tests... for a compiler.


Nonsense test.


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.


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


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.


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.


Not only that, but you can set an assignment operator to x for 1e, then print the result to the screen.

Hardly the point of the test. Though not sure the point of the test...


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


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


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


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


You are cruel!!


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


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

But yeah, gcc throws an error

If you put the parenthesis it works


He didn't put it into parenthesis.


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


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


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

x+ +3 works.

x++3 doesn't.


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://hackernews.hn/item?id=5200523

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


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.)


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


> but it could equally see it as x ++3

What it could see is not a valid argument. A compiler could "see" all kinds of absolutely valid situations as invalid, but it doesn't.


Give me an example.


It could see

    5//3 
as two division operators and throw an error, but it considers it a comment (to be fair, comments are probably removed by a pre-processor).


No, it couldn't. That's a comment, and only a comment. It can't be anything else. Do you have any other examples?


It also works if you add a space:

    x+ +3;
I always thought the unary plus operator is a complete waste, and shouldn't be implemented. I can't imagine a situation when it's needed.

EDIT: technically, it's a bug in GCC. It sees "x++" and assumes it's a unary increment, followed by a hardcoded 3. It definitely should consider that it's an addition followed by a unary plus (which has higher precedence than addition). But I guess it's an ambiguous syntax, and GCC is just being strict.


This is not a bug in GCC. The compiler is required to interpret this as the syntactically-invalid "y = x ++ 3 ;" by the common so-nicknamed maximal-munch property of lexers for various languages. In the 1998 C++ standard, this is in section 2.4/3:

"If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail."

This is the same reason why (prior to C++11) nested templates have to be written with an excess space: "std::vector<std::list<int> > foo". (C++11 has a lexer hack to allow a >> token to be valid here and equivalent to two > > tokens.)

For this ++ example, if you want the valid parse, you get to put the space in yourself, just like we're used to for nested templates. This is the trade-off for having a fast mostly conceptually-separate lexer.

Unary plus is nice if you want to write out the symmetry in things like "int signum[] = { -1, 0, +1 }" and I expect there are meaningful uses in ensuring desired conversions occur in some overloading situations (though there would of course be a case for being explicit...).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: