Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
I have used gcc 4.7.1 compiler & the platform is GNU/Linux.
I didn't get any error. And output is 1 but I don't know why.
I think it will give you some random answer. It is checking for the element at position a[99][j] in a loop, now it depends that which element is at that memory location. If ever in the whole loop that element comes out to be smaller than k then k's value will be updated else it will print 99.
I don't know where you see 99th element of array being accessed. When the for loop begins, both i and j will be reset to zero.
The only tricky part in the code is the static array should have 12 elements but it only has 9 elements. So it depends on how compiler will align the memory so accomodate for the missing 3 elements.
I would say output be architecture dependent.
yeah zero is the corrct ans.
here basically we are finding the smallest element in the array..
and since the partial initialization of the array is dun so all the uninitialized values vud be zero and thus zero is the rite ans
(only if v consider the typo)
else the ans vud be segmentation fault
here the output is '0' or any garbage value,why because a[k][j] means a[99][j] means any garbage value which may be greater than 'k' or less than 'k' so "a[k][j]>k" may be true or false.(we may get output '0' in some cases because some values of array are not intialised ,by default they are '0').
Undefined behavior. I don't think segmentation fault would occur because the 2d array is allocated dynamically on the stack, not on the heap through malloc(). Could be wrong about this though.
It depends on the garbage values stored at a[99][0], a[99][1], a[99][2], and a[99][3]. If none of these values are less than 99, then k never gets updated. Otherwise it gets set to a[i][j]. Since 4<=a[i][j]<=12 and there are only 3 rows in the matrix, the new value of k will still produce garbage values.
Answer is undefined. The values up to the end of the array a[3][4] , i.e upto 12 values will be 0 if it is uniniitalized .However any value beyond that can give a runtime error as it will have a garbage value . The statement a[k][j] will make the array access unpredictable and hence cause a runtime error . I tested with vc++ a[99][j] had -8567433 so the program gave a runtime error .
isn't it error, its trying to access a[99][0] here initially?
- Vannu July 06, 2012