I’m not really a fan of this kind of question. Especially if there’s enough questions that time will be an issue for most. Because at first glance it’s easy to think the answer might be the length of a day.
There shouldn’t be a need to try to trick people into the wrong answer on an open question. Maybe with multiple choice but not an open answer question.
I get your point about it being a trick question but I think in this case it’s pretty reasonable that you would see code like this in real life. Where the programming metaphor and your understanding of the real world clash. It’s a very important skill to be able to spot the difference.
The compiler or interpreter does that for you. There’s no point in these “gotcha’s”. They are cute brain teasers that belong on those useless “are you a programmer” quizzes you find on random meme websites, not an exam.
In the error shown a compiler would be just fine and run as usual but the person programming it would be expecting a different result so a compiler wouldn’t do this for you since it’s a logical error and not a syntax error.
If it’s a statically typed language and x is of type Date, it’s for sure throw a type error when trying to assign a string to it. If it had autoboxing / auto type conversion from String to Date, length could return a number or a string.
If this were Javascript on NodeJS, it would fail at print(x) because that doesn’t exist in JS. If it were Python it would fail at x.length because that has to be len(x). And so on.
If this were all to pass, at the latest at runtime, when the programmer sees the output “6”, they would know something’s up.
It’s the length of the string. The number of characters is 6. It’s a play on words and a question.
I’m not really a fan of this kind of question. Especially if there’s enough questions that time will be an issue for most. Because at first glance it’s easy to think the answer might be the length of a day.
There shouldn’t be a need to try to trick people into the wrong answer on an open question. Maybe with multiple choice but not an open answer question.
I get your point about it being a trick question but I think in this case it’s pretty reasonable that you would see code like this in real life. Where the programming metaphor and your understanding of the real world clash. It’s a very important skill to be able to spot the difference.
The compiler or interpreter does that for you. There’s no point in these “gotcha’s”. They are cute brain teasers that belong on those useless “are you a programmer” quizzes you find on random meme websites, not an exam.
CC BY-NC-SA 4.0
In the error shown a compiler would be just fine and run as usual but the person programming it would be expecting a different result so a compiler wouldn’t do this for you since it’s a logical error and not a syntax error.
If it’s a statically typed language and
x
is of typeDate
, it’s for sure throw a type error when trying to assign a string to it. If it had autoboxing / auto type conversion fromString
toDate
, length could return a number or a string.If this were Javascript on NodeJS, it would fail at
print(x)
because that doesn’t exist in JS. If it were Python it would fail atx.length
because that has to belen(x)
. And so on.If this were all to pass, at the latest at runtime, when the programmer sees the output “6”, they would know something’s up.
As I said, cute, but worthless test.
CC BY-NC-SA 4.0