LinkedIn C++ quiz: It’s a trap!

Today LinkedIn suggested me to take C++ quiz to test my declared skill. I have not used C++ in “professional life” almost 4 years. Last standard I looked closely was long-awaited C++ 11. Later versions C++14 and C++17 are barely reached me. But LinkedIn comforted me that if I fail, result won’t be visible to others. Even I believed that these data will be used in matching algorithm for recruiter’s premium accounts I accepted challenge!

Question: “What is the output of the code”

#include <cstdio>
using namespace std;
 
int main()
{
  char c = 255;
  if (c < 10) {
      printf("c = %i, which is greater than 10", c);
  } else {
      printf("c = %i, which is less than 10", c);
  }
  return(0);
}

Question only looks simple of course. Obviously quiz is testing your knowledge of signed/unsigned types and min/max values. I also think that quiz tries to confuse you in:

– we don’t use printf often in C++ and %i instead of traditional %d is summoned to test you. Trick is that i,d are the same for printf but match differently for fscanf/scanf according to C11.

return(0) in the original C specification parenthesis were required around the return value but nowadays it reveals old-school fan.

Select answer

  • c=-1, which is greater than 10
  • c=255, which is greater than 10
  • c=-1, which is less than 10
  • c=255, which is less than 10

Is it a Trap?You as owner of C++ skill could see the following problems

Problem 1

The question is incomplete really! It doesn’t specify platform that is critical in this question.

Max value of char std::numeric_limits<char>::max() , equivalent to  CHAR_MAXSCHAR_MAX , as standards specifies:

SCHAR_MAX, maximum value for an object of type signed char 127 (2 7 -1) or greater*.

* the actual value depends on the particular system and library implementation, but shall reflect the limits of these types in the target platform.

Yes, char is platform specific and can be greater than 8bit. But well, sure we supposed to assume common “x86_64”, family of Linux, Windows, OSX.

Let’s assume

SCHAR_MAX == 127

but there is another greater problem

Problem 2

C++ standard says (link to early draft, C++17 standard can be purchased ) :

https://eel.is/c++draft/expr#pre-4

If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

What about C? Standard C11

6.3.1.3 Signed and unsigned integers

3  Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised

The right answer “the behavior is undefined” and I didn’t see it in options.

After time expired, it suggested me to proceed with Next. I decided to stop quiz because it looks like a Trap really!

Advertisements

我来评几句
登录后评论

已发表评论数()

相关站点

热门文章