Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ interview question

C++ interview question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++career
16 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M minkowski

    Hi, I was asked the below in an interview:- What does the following code below do? char nextChar(); int main() { char ch; ch = nextChar() != '\0'; std::cout << (int) ch; return 0; } I said it would not compile since there is no definition (only a declaration) of the function nextChar() but the answer was 0 or 1. I am not sure why and also how can you use the != operator outside a if or while statement (i.e. something that expects true or false ? Thank you for any input.

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #2

    minkowski wrote:

    nextChar() != '\0'

    This part of the code actually "returns" something (true or false). You then assign the result of the comparison into the ch variable. true is usually 1 and false is 0. So, your character will contain either 0 or 1. If you try to ouptut as it is (so, without the casting to an integer), you will end up printing the character whose value is 0 or 1 (so, not printable character), that's why you need to cast it to an integer. I guess that having a body for your function was not really important for the interview question: they simply have a function that returns a character...

    Cédric Moonen Software developer
    Charting control [v2.0] OpenGL game tutorial in C++

    M 1 Reply Last reply
    0
    • M minkowski

      Hi, I was asked the below in an interview:- What does the following code below do? char nextChar(); int main() { char ch; ch = nextChar() != '\0'; std::cout << (int) ch; return 0; } I said it would not compile since there is no definition (only a declaration) of the function nextChar() but the answer was 0 or 1. I am not sure why and also how can you use the != operator outside a if or while statement (i.e. something that expects true or false ? Thank you for any input.

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #3

      Yes you should get a linker error, due to the missing function definition

      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

      M 1 Reply Last reply
      0
      • M minkowski

        Hi, I was asked the below in an interview:- What does the following code below do? char nextChar(); int main() { char ch; ch = nextChar() != '\0'; std::cout << (int) ch; return 0; } I said it would not compile since there is no definition (only a declaration) of the function nextChar() but the answer was 0 or 1. I am not sure why and also how can you use the != operator outside a if or while statement (i.e. something that expects true or false ? Thank you for any input.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #4

        minkowski wrote:

        I said it would not compile since there is no definition (only a declaration) of the function nextChar()

        Your point is correct (please note, however, that you would get a linker error, not a compiler one). Now suppose nextChar is defined, somewhere (it's actual implementation doesn't matter).

        minkowski wrote:

        but the answer was 0 or 1. I am not sure why and also how can you use the != operator outside a if or while statement (i.e. something that expects true or false ?

        nextChar() != '\0';

        the above expression evaluates true whenever nextChar return a value different from '\0', false otherwise. Since true is 1 [^] (false is 0) an implicit cast from bool to char (that is an integer type) happens and you get ch=1 (or ch=0). :) BTW Are you that Minkowski [^]? :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        T 1 Reply Last reply
        0
        • M minkowski

          Hi, I was asked the below in an interview:- What does the following code below do? char nextChar(); int main() { char ch; ch = nextChar() != '\0'; std::cout << (int) ch; return 0; } I said it would not compile since there is no definition (only a declaration) of the function nextChar() but the answer was 0 or 1. I am not sure why and also how can you use the != operator outside a if or while statement (i.e. something that expects true or false ? Thank you for any input.

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #5

          minkowski wrote:

          ch = nextChar() != '\0';

          This is what the question is aiming at. It's a matter of operator precedence.

          «_Superman_» I love work. It gives me something to do between weekends.

          1 Reply Last reply
          0
          • C Cedric Moonen

            minkowski wrote:

            nextChar() != '\0'

            This part of the code actually "returns" something (true or false). You then assign the result of the comparison into the ch variable. true is usually 1 and false is 0. So, your character will contain either 0 or 1. If you try to ouptut as it is (so, without the casting to an integer), you will end up printing the character whose value is 0 or 1 (so, not printable character), that's why you need to cast it to an integer. I guess that having a body for your function was not really important for the interview question: they simply have a function that returns a character...

            Cédric Moonen Software developer
            Charting control [v2.0] OpenGL game tutorial in C++

            M Offline
            M Offline
            minkowski
            wrote on last edited by
            #6

            Hi ya, Thanks for your reply. Yes as you correctly said the answer is 0 or 1. I got confused because of the lack of function definition. Can I ask how it is possible that you can use the != operator without being enclosed in a statement that expects true / false ? e.g. while() , if() Thanks for any information.

            C 1 Reply Last reply
            0
            • M minkowski

              Hi ya, Thanks for your reply. Yes as you correctly said the answer is 0 or 1. I got confused because of the lack of function definition. Can I ask how it is possible that you can use the != operator without being enclosed in a statement that expects true / false ? e.g. while() , if() Thanks for any information.

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #7

              minkowski wrote:

              Can I ask how it is possible that you can use the != operator without being enclosed in a statement that expects

              != is a binary operator that may be used in any expression. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              1 Reply Last reply
              0
              • _ _AnsHUMAN_

                Yes you should get a linker error, due to the missing function definition

                You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                M Offline
                M Offline
                molesworth
                wrote on last edited by
                #8

                Most interview questions just use code fragments, and you're supposed to assume functions exist in other libraries (that's generally spelled out in the interview). Mind you, this is a very simple example question and probably meant as a warm up, not actually a real test. You should see the kinds of questions we ask in our interviews (although coding tests are only a small part of them).

                There are three kinds of people in the world - those who can count and those who can't...

                M 1 Reply Last reply
                0
                • M molesworth

                  Most interview questions just use code fragments, and you're supposed to assume functions exist in other libraries (that's generally spelled out in the interview). Mind you, this is a very simple example question and probably meant as a warm up, not actually a real test. You should see the kinds of questions we ask in our interviews (although coding tests are only a small part of them).

                  There are three kinds of people in the world - those who can count and those who can't...

                  M Offline
                  M Offline
                  minkowski
                  wrote on last edited by
                  #9

                  Hi ya, Thanks for your post, was wondering if you could put up some of your interview code questions? :)

                  M 1 Reply Last reply
                  0
                  • C CPallini

                    minkowski wrote:

                    I said it would not compile since there is no definition (only a declaration) of the function nextChar()

                    Your point is correct (please note, however, that you would get a linker error, not a compiler one). Now suppose nextChar is defined, somewhere (it's actual implementation doesn't matter).

                    minkowski wrote:

                    but the answer was 0 or 1. I am not sure why and also how can you use the != operator outside a if or while statement (i.e. something that expects true or false ?

                    nextChar() != '\0';

                    the above expression evaluates true whenever nextChar return a value different from '\0', false otherwise. Since true is 1 [^] (false is 0) an implicit cast from bool to char (that is an integer type) happens and you get ch=1 (or ch=0). :) BTW Are you that Minkowski [^]? :-D

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #10

                    CPallini wrote:

                    W Are you that Minkowski [^]?

                    or that one[^] ?!

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    M 1 Reply Last reply
                    0
                    • T toxcct

                      CPallini wrote:

                      W Are you that Minkowski [^]?

                      or that one[^] ?!

                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                      M Offline
                      M Offline
                      minkowski
                      wrote on last edited by
                      #11

                      Ha ha, nope I am not that Minkowski although that was quite a story ! :)

                      C 1 Reply Last reply
                      0
                      • M minkowski

                        Ha ha, nope I am not that Minkowski although that was quite a story ! :)

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #12

                        :doh: I was awaiting for some geometry hints... :-D

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        U 1 Reply Last reply
                        0
                        • C CPallini

                          :doh: I was awaiting for some geometry hints... :-D

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          U Offline
                          U Offline
                          UserNameless
                          wrote on last edited by
                          #13

                          CPallini wrote:

                          I was awaiting for some geometry hints...

                          :suss: shhhh. dont' tell anyone, but i'm letting it leak out - a triangle... has 3 sides. shhh. don't let anyone know though

                          C 1 Reply Last reply
                          0
                          • M minkowski

                            Hi ya, Thanks for your post, was wondering if you could put up some of your interview code questions? :)

                            M Offline
                            M Offline
                            molesworth
                            wrote on last edited by
                            #14

                            LOL - I wouldn't be allowed to do that, just in case anyone reading here comes in for an interview (which is quite possible) :-D

                            There are three kinds of people in the world - those who can count and those who can't...

                            1 Reply Last reply
                            0
                            • U UserNameless

                              CPallini wrote:

                              I was awaiting for some geometry hints...

                              :suss: shhhh. dont' tell anyone, but i'm letting it leak out - a triangle... has 3 sides. shhh. don't let anyone know though

                              C Offline
                              C Offline
                              CPallini
                              wrote on last edited by
                              #15

                              UserNameless wrote:

                              a triangle... has 3 sides.

                              That's the reason geometry is soooo difficult to grasp: triangle => three sides...while plain common sense would suggest triangle => three angles, or, at least, triside => three sides. :~ :-D

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              M 1 Reply Last reply
                              0
                              • C CPallini

                                UserNameless wrote:

                                a triangle... has 3 sides.

                                That's the reason geometry is soooo difficult to grasp: triangle => three sides...while plain common sense would suggest triangle => three angles, or, at least, triside => three sides. :~ :-D

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                [My articles]

                                M Offline
                                M Offline
                                minkowski
                                wrote on last edited by
                                #16

                                so it should be a quad - right - angle for a rectangle ? :)

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups