C++ Interview Questions and Answers

Started by Kalyan, Jan 10, 2009, 01:19 AM

previous topic - next topic
Go Down


C++ Interview Questions and Answers

   1. Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at:
          * const char *
          * char const *
          * char * const

      Note: Ask the candidate whether the first declaration is pointing to a string or a single character. Both explanations are correct, but if he says that it's a single character pointer, ask why a whole string is initialized as char* in C++. If he says this is a string declaration, ask him to declare a pointer to a single character. Competent candidates should not have problems pointing out why const char* can be both a character and a string declaration, incompetent ones will come up with invalid reasons.

   2. You're given a simple code for the class BankCustomer. Write the following functions:
          * Copy constructor
          * = operator overload
          * == operator overload
          * + operator overload (customers' balances should be added up, as an example of joint account between husband and wife)

      Note:Anyone confusing assignment and equality operators should be dismissed from the interview. The applicant might make a mistake of passing by value, not by reference. The candidate might also want to return a pointer, not a new object, from the addition operator. Slightly hint that you'd like the value to be changed outside the function, too, in the first case. Ask him whether the statement customer3 = customer1 + customer2 would work in the second case.
   3. What problems might the following macro bring to the application?

          #define sq(x) x*x

   4. Consider the following struct declarations:

          struct A { A(){ cout << \"A\"; } };
          struct B { B(){ cout << \"B\"; } };
          struct C { C(){ cout << \"C\"; } };
          struct D { D(){ cout << \"D\"; } };
          struct E : D { E(){ cout << \"E\"; } };
          struct F : A, B
             C c;
             D d;
             E e;
             F() : B(), A(),d(),c(),e() { cout << \"F\"; }

      What constructors will be called when an instance of F is initialized? Produce the program output when this happens.
   5. Anything wrong with this code?

          T *p = new T[10];
          delete p;

      Note: Incorrect replies: "No, everything is correct", "Only the first element of the array will be deleted", "The entire array will be deleted, but only the first element destructor will be called".
   6. Anything wrong with this code?

          T *p = 0;
          delete p;

      Note: Typical wrong answer: Yes, the program will crash in an attempt to delete a null pointer. The candidate does not understand pointers. A very smart candidate will ask whether delete is overloaded for the class T.

   7. Explain virtual inheritance. Draw the diagram explaining the initialization of the base class when virtual inheritance is used.

Note: Typical mistake for applicant is to draw an inheritance diagram, where a single base class is inherited with virtual methods. Explain to the candidate that this is not virtual inheritance. Ask them for the classic definition of virtual inheritance. Such question might be too complex for a beginning or even intermediate developer, but any applicant with advanced C++ experience should be somewhat familiar with the concept, even though he'll probably say he'd avoid using it in a real project. Moreover, even the experienced developers, who know about virtual inheritance, cannot coherently explain the initialization process. If you find a candidate that knows both the concept and the initialization process well, he's hired.

   8. What's potentially wrong with the following code?

          long value;
          //some stuff
          value &= 0xFFFF;

      Note: Hint to the candidate about the base platform they're developing for. If the person still doesn't find anything wrong with the code, they are not experienced with C++.
   9. What does the following code do and why would anyone write something like that?

          void send (int *to, int * from, int count)
             int n = (count + 7) / 8;
             switch ( count  %  8)
                case 0: do { *to++ = *from++;
                case 7: *to++ = *from++;
                case 6: *to++ = *from++;
                case 5: *to++ = *from++;
                case 4: *to++ = *from++;
                case 3: *to++ = *from++;
                case 2: *to++ = *from++;
                case 1: *to++ = *from++;
                } while ( --n > 0 );

  10. In the H file you see the following declaration:

          class Foo {
          void Bar( void ) const ;

      Tell me all you know about the Bar() function.

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Gift your Kids Memories :: Book Website @ 349 Rs monthly
Copyright © 2005 - 2021 :: IT Acumens :: All Rights Reserved. :: XML Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Pro Dedicated Server

My Kids Diary