Thursday, July 15, 2010

Wipro C/C++ Interview Questions

1. What is the main difference between the C++ class and the C structure?


Answer : Both in C++ class and C structure you can have member functions and member variables, but in C++ the member functions are inside the memory allocated for the object, but in C structures they will reside out the memory allocated for the structure.  C++ the member functions are passed with 'this' pointer which pointer to the objects and rest of the things are taken care by compiler with the help of 'this' pointer. This is done by compiler during compile time, and this remains the main difference and helps in achieving the object oriented nature of C++.

2. What is the difference between the two declarations below using volatile keyword.
volatile struct reg_map {
          int reg_r1;
          int reg_r2;
};

struct reg_map {
        volatile int reg_r1;
        volatile int reg_r2;
};

Answer : In the first case it is up to the compiler to make all the variables as volatile, but in second case the compiler will always make the members as volatile, so that the purpose is served. In first case we need to check if the compiler really makes all members volatile or not, and if it does not, then it does not serve the purpose.

3. I have a union declared as below    union  ex_union {
    int a;
    short int ar[2];
   }
 
if I assign the value 0x112233 to a, what would be the values of elements ar[0] and ar[1]

Answer: Considering it to be little endian machine, the value would be, 0x2233 and 0x0011 respectively, Because in Little endian end byte will be in little address and first byte will be in high address.

4. Assume you have a function with two params a and b, and you need to return an int after processing like below.

   int compare(int a, int b)
   {
       if a>b return 1
       if b>a return -1
       if a = b, return 0
   }
you need to write this function without using if else, switch and ternary operators, how do you do it?
Answer: The purpose of this question is, to reduce the pipe line flush during branch, since here there is 50% chance of branch, we need to avoid any branch instructions.
int compare(int a, int b)
{
    int x, m, s;
    x = a - b;             // find the difference
    m = x || 1;            // if magnitude not zero make it 1
    s = (x & (1 << 31));   // get the sign
    __asm sar s, 31        // extend the sign to make it -1
    return s | m;          // or sign and magnitude
}

This can also be written like below, but it will introduce branch in the generated x86 code, check it in visual studio.
int compare (int a, int b)
{
   int x  = 0;
   (a>b) && (x = 1) || (a<b) && (x = -1);
   return x;
}

Update:
In both the above cases, the compiler will generate the the branch instruction, which is same as the if, else and thus defeating the purpose, I have an solution for it, after the subtraction find the sign of the result using below function (code) and it wont generate the branch instruction.

int IntSign(int num)
{
    int sign = (num !=0) | -(int)((unsigned int)num >> (sizeof(int)*8 - 1));
    return sign;
}


The assembly code generate for the calculation is as below (visual studio for x86)

    int sign = (num !=0) | -(int)((unsigned int)num >> (sizeof(int)*8 - 1));
0041140E  xor         eax,eax 
00411410  cmp         dword ptr [num],0 
00411414  setne       al   
00411417  mov         ecx,dword ptr [num] 
0041141A  shr         ecx,1Fh 
0041141D  neg         ecx  
0041141F  or          eax,ecx 
00411421  mov         dword ptr [sign],eax 
    return sign;
00411424  mov         eax,dword ptr [sign] 

2 comments:

  1. hi prasad, how are you . long time :) i was just going through your blog and stumbled upon this -

    >>but in C++ the member functions are inside the memory allocated for the object, but in C structures they will reside out the memory allocated for the structure.

    i guess you need to re-check this. C++ member functions are like any other C functions or C++ non-member functions. The difference is that member functions has implicit "this" variable which point to the current object and compiler does all the backend work of linking the object on which the function is called to this pointer.

    ReplyDelete
  2. Hey Charan, I also have the same opinion, but I have got not enough time to check on it, this was the answer given by an interviewer when I said what you have said. I just updated it for reference. Because memory wise, I am also not sure about whether it would reside inside the object. I will update this info about this pointer. Thanks for leaving a comment. If you do know about if the functions memory would be same as in C, please do update me.

    ReplyDelete