Recently, I ran into an interesting job interview question in C++ which I want to share. Lets take a look at the following code:

// Option A.

char str1[] = "example"; 

str1[1] = 'a'; 

// Option B. 

char* str2 = "example"; 

str2[1] = 'a';

What would happen if we compile and run this code ?

  1. This code doesn’t compile.
  2. This code compiles but option A will produce a run time error.
  3. This code compiles but option B will produce a run time error.
  4. This code compiles and runs successfully.

Take your time and think about it…

question_mark

Before answering this question, lets first understand this code and discuss both options:

Option A:

Array of 8 characters (‘\0′ is added) is allocated on the stack.

Option B:

An unnamed, static array of characters is stored in the read only data segment. Str2 is a pointer that is allocated on the stack, but it points to the read only array’s first character.

So, when we try to change the data in the first option, every thing is fine and str1 becomes “eaample”. But when trying to change the data that is pointed by str2 (read only data), “Access violation writing location …” exception is raised.

The correct answer is: 3. This code compiles but option B will produce a run time error.

I must point out that some compilers have the option -fwritable-strings which makes the constant strings non-constant, but this is for compilation of old code.

So, what do you have to say? Do you have more interesting “job interview” questions? Would you like to see a weekly “Job Interview Challenge” in Dev102?

Tags :

20 Responses to “A Programming Job Interview Challenge”


  1. Jordan

    Said on April 28, 2008 :

    This was a good question. I have to admit I had to run it myself just to make sure. With the prevalence of STL strings, MFC’s CString and others, I can’t say I’ve used good old C-style strings in quite some time.

  2. commenter

    Said on April 28, 2008 :

    The correct answer is “Uhuh. See, this is one of the many reasons why you shouldn’t use C++”.

  3. Bob

    Said on April 28, 2008 :

    The great thing about software is that you can write it and click compile, then run and find the error.

  4. gigi

    Said on April 28, 2008 :

    Without specifying the compiler, the version and the compiler options this question can’t be answered :)

  5. signal9

    Said on April 28, 2008 :

    WOW Commenter, your clever. Can we delete this Trolls comment please.

    This was a good question for an interview. Its simple and ensures the new hire understands certain concepts.

  6. Shahar Y

    Said on April 28, 2008 :

    Hi gigi,

    As i pointed out in the post, some compilers have the option -fwritable-strings which makes the constant strings non-constant (for compilation of old code).
    As far as i know, the behaviour will be the same in all other cases.
    Please, correct me if i am wrong.

  7. Michael

    Said on April 28, 2008 :

    Dear gigi,
    IMHO Shahar is correct. This is well known and well defined – please read Stroustrup 3rd edition chapter 5.5.

    enjoy

  8. Jason

    Said on April 28, 2008 :

    This is a great question like Backstreet Boys are great songwriters…

    Here’s another great question that tests whether candidates know C++ welll

    for(int i = 0; i < 100; ++i);
    printf(“X”);

    How many X’s appear?

    Great question, I’m really clever

  9. OJ

    Said on April 28, 2008 :

    Commenter: You’re incorrect (and ignorant). Stick to managed code and languages which require you not to think — it’s better for all of us.

    Everyone else with a brain: Great question, one that I’ve faced before in an interview where they did specify compiler and version ;)

  10. CPlusPlusGuru

    Said on April 29, 2008 :

    Jason: The answer is 1

    Great question because it makes me feel smart

  11. Dave Regan

    Said on April 30, 2008 :

    Seems to me like the code will not compile. It should come up with a “str1 is undefined” compile error no? Since the definition of str1 is commented out:

    // Option A.char str1[] = “example”;

  12. Shahar Y

    Said on April 30, 2008 :

    Dave Regan,

    This was not my intention, of course.
    Thanks for pointing the typing mistake out.
    It is fixed now…

  13. scoopr

    Said on May 14, 2008 :

    Any decently modern compiler would warn about casting string literal to char* instead of const char*,
    foo.cpp:4: warning: deprecated conversion from string constant to ‘char*’

    So, it would not compile with warnings as errors switch, so I insist the code is badly formed, even if it might compile.

  14. Albus Brian

    Said on May 26, 2008 :

    hi it’s my first time to view this site,I’m an I.T student.Can someone tell me if there is a C# compiler available in any website?? thanx

    hi to all!!

  15. Shahar Y

    Said on May 26, 2008 :

    Hi Brian

    I would recommend using the Visual Studio Express Edition.
    It is free and you can download it here:
    http://www.microsoft.com/express/download/

    Good luck

  16. Angel

    Said on May 27, 2008 :

    @Jason, it will print 100 X… some time later, as output will be cached by stdio (unless you disabled it for stdout with setvbuf).
    So it’ll be output when on next line (if output is a terminal), the buffer fulls (but it’s larger than 100 bytes) or at the end of your program. I don’t think it’ll print if you abort() though, implementation defined anyway.

  17. Adam

    Said on June 6, 2008 :

    @Angel

    Thats actually incorrect. It will only print out 1 “X”, because of the semi colon after the for statement that it will loop, and once it finishes print out the X.

  18. Talk Binary

    Said on August 19, 2008 :

    I had a friend who had relatively about the same format of questions. She had questions on C++ using heaps, linked list, and such.

    She said they felt more like “trick” questions.

2 Trackback(s)

  1. May 2, 2008: BLOG STATS FOR APRIL 2008 | Dev102.com
  2. May 12, 2008: A PROGRAMMING JOB INTERVIEW CHALLENGE #3 | Dev102.com

Post a Comment