frequently asked C and C++ interview questions along with their answers

Discussion in 'C++' started by rohi, Jul 17, 2004.

  1. rohi

    rohi Guest

    dear friends,

    I have applied for a job in a software company.
    a friend of mine told me that the written test will basically contain
    questions from C & C++.I would be happy if someone could please send some
    basic interview questions asked in C and C++ along with their answers.

    Thanking you.


     
    rohi, Jul 17, 2004
    #1
    1. Advertising

  2. On Sat, 17 Jul 2004 03:11:02 -0400, rohi
    <> wrote:

    > dear friends,
    >
    > I have applied for a job in a software company.
    > a friend of mine told me that the written test will basically contain
    > questions from C & C++.I would be happy if someone could please send some
    > basic interview questions asked in C and C++ along with their answers.
    > Thanking you.
    >
    >


    I searched on google for C++ interview questions

    http://www.google.com/search?ie=UTF-8&oe=UTF-8&sourceid=deskbar&q=C++ interview questions

    You could have done the same.

    John
     
    John Harrison, Jul 17, 2004
    #2
    1. Advertising

  3. rohi

    Stone Lan Guest

    seems that the interviewers are always liking the questions about Pointer
    "rohi" <> дÈëÏûÏ¢ÐÂÎÅ
    :...
    > dear friends,
    >
    > I have applied for a job in a software company.
    > a friend of mine told me that the written test will basically contain
    > questions from C & C++.I would be happy if someone could please send some
    > basic interview questions asked in C and C++ along with their answers.
    >
    > Thanking you.
    >
    >
    >
    >
    >
    >
     
    Stone Lan, Jul 17, 2004
    #3
  4. "rohi" <> wrote in message
    news:...
    > dear friends,
    >
    > I have applied for a job in a software company.
    > a friend of mine told me that the written test will basically contain
    > questions from C & C++.I would be happy if someone could please send some
    > basic interview questions asked in C and C++ along with their answers.

    ....

    The 3 that I was asked at an interview the other day were these. I don't
    necessarily consider them good quesitons.
    Infact I won't post the 3rd one. Interview questions vary a lot, and
    sometimes arn't really about C. ( i.e. what is a thread and what
    compications arise in using them )


    1) These files are compiled and linked toghether.

    /* File: c1.c */

    #include <stdio.h>

    extern char *str;

    int main()
    {
    printf("%s\n", str );
    getchar();
    return 0;
    }

    /* File: c2.c */

    char str[] = "Hello world";

    What's wrong here?


    2) If an int x, is known to have the value 1 or 2. Assign x the value 1 to x
    when x is 2, and 2 when x is 1. Use the shortest possible code snippet.

    2a) If x is a float, do the same as above


    Answers:

    1) extern char *str is a declaration that specifies str as external and is a
    pointer to str, whereas infact str is a character array, so use extern char
    str[];


    2) X ^=3;
    2a) x = 3 - x;
     
    Spacen Jasset, Jul 17, 2004
    #4
  5. On Sat, 17 Jul 2004 12:51:30 +0100, Spacen Jasset
    <> wrote:

    > "rohi" <> wrote in message
    > news:...
    >> dear friends,
    >>
    >> I have applied for a job in a software company.
    >> a friend of mine told me that the written test will basically contain
    >> questions from C & C++.I would be happy if someone could please send
    >> some
    >> basic interview questions asked in C and C++ along with their answers.

    > ...
    >
    > The 3 that I was asked at an interview the other day were these. I don't
    > necessarily consider them good quesitons.
    > Infact I won't post the 3rd one. Interview questions vary a lot, and
    > sometimes arn't really about C. ( i.e. what is a thread and what
    > compications arise in using them )
    >
    >
    > 1) These files are compiled and linked toghether.
    >
    > /* File: c1.c */
    >
    > #include <stdio.h>
    >
    > extern char *str;
    >
    > int main()
    > {
    > printf("%s\n", str );
    > getchar();
    > return 0;
    > }
    >
    > /* File: c2.c */
    >
    > char str[] = "Hello world";
    >
    > What's wrong here?
    >
    >
    > 2) If an int x, is known to have the value 1 or 2. Assign x the value 1
    > to x
    > when x is 2, and 2 when x is 1. Use the shortest possible code snippet.
    >
    > 2a) If x is a float, do the same as above
    >


    [snip]


    What beats me is why anyone would think that this sort of trivia can
    distinguish good programmers from bad programmers.

    What about (off the top of my head)

    1) Under what circumstances would you consider using a global variable?

    2) One of your classes needs to be able to access an instance of another
    of your classes. What are the benefits and costs of the following two
    possibilites

    class A
    {
    ...
    private:
    B* b_ptr;
    };

    class A
    {
    ...
    private:
    B b_obj;
    };

    3) Would you ever use public data members? If so when?

    You know questions about stuff that actually matters when you are
    programming.

    john
     
    John Harrison, Jul 17, 2004
    #5
  6. On Sat, 17 Jul 2004 03:11:02 -0400, "rohi" <> wrote:

    >dear friends,
    >
    > I have applied for a job in a software company.
    >a friend of mine told me that the written test will basically contain
    >questions from C & C++.I would be happy if someone could please send some
    >basic interview questions asked in C and C++ along with their answers.
    >
    > Thanking you.
    >


    int main()
    {
    int * x;

    *x = 10;
    return 0;
    }

    // What's wrong here?

    They have asked this in _every_ interview that I did,
    and if you don't know the answer, don't apply for
    a software job :)

    --
    Lorenzo Dieryckx
    mail: lorenzodotdieryckxatpandoradotbe
     
    Lorenzo Dieryckx, Jul 17, 2004
    #6
  7. "John Harrison" <> wrote in message
    news:eek:psa97fra5212331@andronicus...

    ....

    > What beats me is why anyone would think that this sort of trivia can
    > distinguish good programmers from bad programmers.
    >

    ....

    Yes, it's true. I think the best interviews are those that ask a variety of
    questions. 3 questions arn't really enough. And in my opinion good C
    knowledge is not necessarily enough -- but on the other hand if you have
    good C knoweledge then you are likly to be more rounded since you've had to
    spend time learning about all sort of things while putting C to use.

    Some interview questions I don't agree with either -- or I mean don't quite
    agree with the answer.
     
    Spacen Jasset, Jul 17, 2004
    #7
  8. rohi

    Julie Guest

    Re: frequently asked C and C++ interview questions along withtheir answers

    rohi wrote:
    >
    > dear friends,
    >
    > I have applied for a job in a software company.
    > a friend of mine told me that the written test will basically contain
    > questions from C & C++.I would be happy if someone could please send some
    > basic interview questions asked in C and C++ along with their answers.


    You may consider this a little harsh, but with an attitude like this, you won't
    or shouldn't get hired.

    Being a successful programmer means that you dedicate yourself to personal (and
    team) improvement -- a 'go-getter' attitude is key.

    What you should be doing in this case is: making up questions on your own --
    really think about the questions and why or why they aren't appropriate, then
    research the various answers.
     
    Julie, Jul 17, 2004
    #8
  9. rohi

    Julie Guest

    Re: frequently asked C and C++ interview questions along withtheir answers

    Lorenzo Dieryckx wrote:
    >
    > On Sat, 17 Jul 2004 03:11:02 -0400, "rohi" <> wrote:
    >
    > >dear friends,
    > >
    > > I have applied for a job in a software company.
    > >a friend of mine told me that the written test will basically contain
    > >questions from C & C++.I would be happy if someone could please send some
    > >basic interview questions asked in C and C++ along with their answers.
    > >
    > > Thanking you.
    > >

    >
    > int main()
    > {
    > int * x;
    >
    > *x = 10;
    > return 0;
    > }
    >
    > // What's wrong here?


    Absolutely nothing is wrong. All of the above is perfectly legal C++.
     
    Julie, Jul 17, 2004
    #9
  10. rohi

    osmium Guest

    Julie writes:

    > > int main()
    > > {
    > > int * x;
    > >
    > > *x = 10;
    > > return 0;
    > > }
    > >
    > > // What's wrong here?

    >
    > Absolutely nothing is wrong. All of the above is perfectly legal C++.


    Only a fool would give that answer if he wanted the job. If you don't want
    to work there, why bother with the interview?

    They are hiring programmers, not language lawyers.
     
    osmium, Jul 17, 2004
    #10
  11. rohi

    JKop Guest

    osmium posted:

    > Julie writes:
    >
    >> > int main()
    >> > {
    >> > int * x;
    >> >
    >> > *x = 10;
    >> > return 0;
    >> > }
    >> >
    >> > // What's wrong here?

    >>
    >> Absolutely nothing is wrong. All of the above is

    perfectly legal C++.
    >
    > Only a fool would give that answer if he wanted the job.

    If you don't
    > want to work there, why bother with the interview?
    >
    > They are hiring programmers, not language lawyers.



    What the hell is going on here?

    Julie, yes, the code will compile; is that your definition
    of "perfectly legal C++"? The code contains undefined
    behaviour.

    osmium, the question is very clear and straightforward. In
    answering it correctly, one shows an understanding of
    what's going on. Anyone with more than an hour's C++
    knowledge knows what's going on there, let alone a language
    lawyer.

    int main()
    {
    int * x; //1

    //1: Defines a local variable named "x" of type "int*".
    The variable is un-initialized and as such contains white
    noise - it may contain any value

    *x = 10; //2

    //2: The aforementiond variable is dereferenced: the
    address which this variable contains is written to with the
    value 10. As the variable contains any value, any piece of
    memory is written to.

    return 0;

    }


    -JKop
     
    JKop, Jul 17, 2004
    #11
  12. rohi

    David Harmon Guest

    On Sat, 17 Jul 2004 03:11:02 -0400 in comp.lang.c++, "rohi"
    <> wrote,
    >dear friends,
    >
    > I have applied for a job in a software company.
    >a friend of mine told me that the written test will basically contain
    >questions from C & C++.I would be happy if someone could please send some
    >basic interview questions asked in C and C++ along with their answers.


    Many questions asked here are answered in bits and pieces in Marshall
    Cline's C++ FAQ, but the answer to this one is the whole thing. It is
    always good to check the FAQ before posting. You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/

    Also Steve Summit's C FAQ. http://www.eskimo.com/~scs/C-faq/top.html
     
    David Harmon, Jul 17, 2004
    #12
  13. rohi

    Julie Guest

    Re: frequently asked C and C++ interview questions along withtheir answers

    osmium wrote:
    >
    > Julie writes:
    >
    > > > int main()
    > > > {
    > > > int * x;
    > > >
    > > > *x = 10;
    > > > return 0;
    > > > }
    > > >
    > > > // What's wrong here?

    > >
    > > Absolutely nothing is wrong. All of the above is perfectly legal C++.

    >
    > Only a fool would give that answer if he wanted the job. If you don't want
    > to work there, why bother with the interview?
    >
    > They are hiring programmers, not language lawyers.


    I'm not a fool, nor a language lawyer. However, I do answer the question
    _asked_. What do you do? Answer what isn't asked?!

    In answer to the question, nothing is wrong. How can you determine what is
    wrong when what is expected isn't defined? Suppose the requirement was to
    "create a program that exhibits undefined behavior" --

    The interviewer should ask "what is the result?" if that is what they are
    after. The answer to _that_ question is "undefined behavior due to an
    uninitialized pointer dereference."

    Osimum, perhaps you don't realize it (yet!), but when interviewing for a job,
    there are actually two interviews that _should_ be taking place: one is the
    interviewing of perspective candidate by the company representative(s), the
    second is by the candidate of the company and its representative(s). Only a
    foolish candidate wouldn't consider the second.
     
    Julie, Jul 17, 2004
    #13
  14. rohi

    Julie Guest

    Re: frequently asked C and C++ interview questions along withtheir answers

    JKop wrote:
    >
    > osmium posted:
    >
    > > Julie writes:
    > >
    > >> > int main()
    > >> > {
    > >> > int * x;
    > >> >
    > >> > *x = 10;
    > >> > return 0;
    > >> > }
    > >> >
    > >> > // What's wrong here?
    > >>
    > >> Absolutely nothing is wrong. All of the above is

    > perfectly legal C++.
    > >
    > > Only a fool would give that answer if he wanted the job.

    > If you don't
    > > want to work there, why bother with the interview?
    > >
    > > They are hiring programmers, not language lawyers.

    >
    > What the hell is going on here?
    >
    > Julie, yes, the code will compile; is that your definition
    > of "perfectly legal C++"? The code contains undefined
    > behaviour.
    >
    > osmium, the question is very clear and straightforward. In
    > answering it correctly, one shows an understanding of
    > what's going on. Anyone with more than an hour's C++
    > knowledge knows what's going on there, let alone a language
    > lawyer.
    >
    > int main()
    > {
    > int * x; //1
    >
    > //1: Defines a local variable named "x" of type "int*".
    > The variable is un-initialized and as such contains white
    > noise - it may contain any value
    >
    > *x = 10; //2
    >
    > //2: The aforementiond variable is dereferenced: the
    > address which this variable contains is written to with the
    > value 10. As the variable contains any value, any piece of
    > memory is written to.
    >
    > return 0;
    >
    > }
    >
    > -JKop


    You didn't answer the question though. "What's wrong here?"

    You can't answer the question, because you don't know the requirement.
     
    Julie, Jul 17, 2004
    #14
  15. rohi

    JKop Guest

    Re: frequently asked C and C++ interview questions along with their answers

    Julie posted:

    > JKop wrote:
    >>
    >> osmium posted:
    >>
    >> > Julie writes:
    >> >
    >> >> > int main()
    >> >> > {
    >> >> > int * x;
    >> >> >
    >> >> > *x = 10;
    >> >> > return 0;
    >> >> > }
    >> >> >
    >> >> > // What's wrong here?
    >> >>
    >> >> Absolutely nothing is wrong. All of the above is

    perfectly legal
    >> >> C++.
    >> >
    >> > Only a fool would give that answer if he wanted the

    job. If you
    >> > don't want to work there, why bother with the

    interview?
    >> >
    >> > They are hiring programmers, not language lawyers.

    >>
    >> What the hell is going on here?
    >>
    >> Julie, yes, the code will compile; is that your

    definition
    >> of "perfectly legal C++"? The code contains undefined

    behaviour.
    >>
    >> osmium, the question is very clear and straightforward.

    In
    >> answering it correctly, one shows an understanding of
    >> what's going on. Anyone with more than an hour's C++
    >> knowledge knows what's going on there, let alone a

    language lawyer.
    >>
    >> int main()
    >> {
    >> int * x; //1
    >>
    >> //1: Defines a local variable named "x" of type

    "int*".
    >> The variable is un-initialized and as such contains

    white
    >> noise - it may contain any value
    >>
    >> *x = 10; //2
    >>
    >> //2: The aforementiond variable is dereferenced: the
    >> address which this variable contains is written to with

    the
    >> value 10. As the variable contains any value, any piece

    of
    >> memory is written to.
    >>
    >> return 0;
    >>
    >> }
    >>
    >> -JKop

    >
    > You didn't answer the question though. "What's wrong

    here?"
    >
    > You can't answer the question, because you don't know the

    requirement.

    Q: What's wrong here?

    A: Un-allocated memory is accessed and written to.


    Is that the kind of answer you're looking for?

    (And to which requirement to you refer?)

    -JKop
     
    JKop, Jul 17, 2004
    #15
  16. Re: frequently asked C and C++ interview questions along with their answers

    > osmium wrote:
    > >
    > > Julie writes:
    > >
    > > > > int main()
    > > > > {
    > > > > int * x;
    > > > >
    > > > > *x = 10;
    > > > > return 0;
    > > > > }
    > > > >
    > > > > // What's wrong here?
    > > >
    > > > Absolutely nothing is wrong. All of the above is perfectly legal C++.

    > >
    > > Only a fool would give that answer if he wanted the job. If you don't

    want
    > > to work there, why bother with the interview?
    > >
    > > They are hiring programmers, not language lawyers.

    >
    > I'm not a fool, nor a language lawyer. However, I do answer the question
    > _asked_. What do you do? Answer what isn't asked?!


    Answer to osmium:

    Oh, but you are wrong. Answering that question, in a job interview for a
    programmer, whould mark you as a fool. It is a foolish, quibbling answer. As
    a manager, I wouldn't want an employee who couldn't speak "in the
    vernacular" and had to be given an iron-clad contractual specification for
    everything wanted. That, of course, is absurd. No job for you, unless I
    really want a headache.
    [Before it becomes a flame war, notice I am not calling you a fool. That
    answer, in that situation is foolish. I'm sure, based on previous posts
    here, that you are no fool, but perhaps too "clever" for me.]

    >
    > In answer to the question, nothing is wrong. How can you determine what

    is
    > wrong when what is expected isn't defined? Suppose the requirement was to
    > "create a program that exhibits undefined behavior" --
    >
    > The interviewer should ask "what is the result?" if that is what they are
    > after. The answer to _that_ question is "undefined behavior due to an
    > uninitialized pointer dereference."


    And, yes, you answer what was meant. As I learned many years ago:

    Don't tell me all about your indigestion.
    "How are you?" is a greeting, not a question.
    --
    Gary
     
    Gary Labowitz, Jul 17, 2004
    #16
  17. rohi

    Walter Guest

    Re: frequently asked C and C++ interview questions along with their answers

    "Julie" <> wrote in message
    news:...
    > You didn't answer the question though. "What's wrong here?"
    >
    > You can't answer the question, because you don't know the requirement.


    It's hard to see a candidate being offered the job after trying that tactic.
     
    Walter, Jul 17, 2004
    #17
  18. rohi

    Julie Guest

    Re: frequently asked C and C++ interview questions along withtheir answers

    Walter wrote:
    >
    > "Julie" <> wrote in message
    > news:...
    > > You didn't answer the question though. "What's wrong here?"
    > >
    > > You can't answer the question, because you don't know the requirement.

    >
    > It's hard to see a candidate being offered the job after trying that tactic.


    Well then, that is the misfortune of the company.

    I find it somewhat dichotomous how we programmers deal with extreme precision,
    something that isn't precise usually doesn't work or worse, yet we allow
    ourselves to be treated as morons and never attempt to establish what is really
    intended by those that we work for.

    My original response was somewhat facetious, intended to bring up some points
    about the interviewing process and the capacity of the interviewer.

    Honestly, if I were in an interview like that:

    interviewer: <code snippet>, "what's wrong with this code?"

    me: "Without knowing the specific requirements, it is hard to really determine
    what is really 'wrong' as it looks like it would compile just fine to me. I do
    see it resulting in undefined behavior when executing, however. Can you tell me
    what the requirements are?."

    interviewer: "I'll get to the requirements in a sec; what do you mean by
    undefined behavior?"

    me: "Well, there is a problem when derefrencing x because it hasn't been
    initialized. The standard indicates that doing such results in 'undefined
    behavior', so just about anything can happen at that point, but will probably
    result in a crash."

    interviewer: "By the way, the requirement was to 'write a program that exhibits
    undefined behavior' -- nice job, you have been the only candidate to realize
    that requirements are an important part to the development process."
     
    Julie, Jul 17, 2004
    #18
  19. rohi

    DaKoadMunky Guest

    >
    >/* File: c1.c */
    >
    >#include <stdio.h>
    >
    >extern char *str;
    >
    >int main()
    >{
    > printf("%s\n", str );
    > getchar();
    > return 0;
    >}
    >
    >/* File: c2.c */
    >
    >char str[] = "Hello world";


    Should this result in an executable program?

    I was expecting a linker error.

    I am aware that in certain contexts the name of an array can be converted to a
    pointer and used as though if it were a pointer. I did not expect this to be
    one of them though.

    When executed the program crashes.

    If the declaration in c1.c and the definition in c2.c are made to match then
    the program executes without error.

    Was MSVC++.NET correct in allowing this program to build?
     
    DaKoadMunky, Jul 18, 2004
    #19
  20. Re: frequently asked C and C++ interview questions along withtheir answers

    Julie wrote:
    > interviewer: "By the way, the requirement was to 'write a program that exhibits
    > undefined behavior' -- nice job, you have been the only candidate to realize
    > that requirements are an important part to the development process."


    that's clever.

    personally, i wouldn't want to work for an employer so machiavellian though.

    besides, the narrow-mindedness behind that thinking is very dangerous.
    an analogy:

    "copilot, i just got a light on my board, is engine #2 ok?"
    "yes sir, it looks fine"
    (ten seconds later as the plane is about to crash)
    "copilot, i thought you said engine #2 was fine!"
    "it was sir. of course, it wasn't connected to the plane at that point.
    but i watched it fall, and it was running beautifully."

    i have never hired a programmer myself, but i have hired engineers. when
    i hire an engineer, i expect them to be smart enough to make reasonable
    extrapolations of the broader impact of their design decisions. given
    that the candidate was specifically not given the requirements, i would
    expect any decent engineer to attempt to extrapolate them from the code.
    personally, i don't think it's such a leap to assume that "don't crash"
    is one such requirement.

    if i handed an engineer something like that program, i would *expect*
    them to come back and tell me it's wrong - because otherwise they were
    either too lazy or too stupid to assume a reasonable requirement set.
    maybe programming is different from engineering, but i hire engineers
    because they make reasonable logical leaps and think outside the box.
    otherwise, i could just hire matlab.

    mark
     
    Mark A. Gibbs, Jul 18, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Snyke
    Replies:
    3
    Views:
    1,025
    Julie
    Jul 19, 2004
  2. Minti
    Replies:
    0
    Views:
    4,174
    Minti
    Jul 19, 2004
  3. Steve Summit
    Replies:
    0
    Views:
    1,159
    Steve Summit
    Jul 1, 2003
  4. Steve Summit
    Replies:
    0
    Views:
    926
    Steve Summit
    Jul 1, 2003
  5. Steve Summit
    Replies:
    0
    Views:
    590
    Steve Summit
    Aug 1, 2003
Loading...

Share This Page