Tuesday, October 2, 2012

Interview Questions BB 2


4. int count = 0;
for(int i=0; i < 10; ++i)
count = count++;
std::cout << count;
what is the output for the above c++ code?
The behaviour of this type of self-assignment is undefined and dependent on the compiler implementation. So, the result may be either 0, or 10, or even a not-compilable code. The undefined behaviour happens due to missing sequence point in the evaluation of expression "count = count++;"You can read it @en.cppreference.com/w/cpp/language/eval_order
5. find the 2nd largest number in an integer array, what do you return if the array has only 1 element?
static int secondLargest(int *arr, int size)
{
int secondL, largest;
largest = arr[0];
secondL = 0;
for(int i=1; i < size; i++)
{
if(arr[i] > largest)
{
secondL = largest;
largest = arr[i];
}
else if(arr[i] > secondL)
{
secondL = arr[i];
}
}
return secondL;
}
6. What is better(why and how): multi processes OR single process with multiple threads?
if the threads share common code & data, then single process with multiple threads would be better as all threads share the same virtual address space.
But, if the threads are independent, then single process with multiple threads will be overkill because a thread may make the entire process crash or go down.


No comments:

Post a Comment