Saturday, September 17, 2005

Answers to questions in C

1.) The array is already sorted. Bubble sort will hence come out of the loop on the first iteration. So it will be the most efficient.

2.) Since both i and j are integers (i/j) and (j/i) will only produce an integer. Since i and j have not been initialized, they will have some junk values and hence most of the times they will not be equal. As a result either i/j or j/i will result in a decimal value between 0 and 1. This value when converted to an integer will become 0 and hence k will be zero. In the rarest of rare cases when the two junk values (i and j) have the same value k will evaluate to 1.

3.) This will result in a compilation error. z is declared as a void pointer. No arithmetic is possible on void pointers.

4.) There are three solutions to this.
a.) add a '-' before the 'i' in the comparison statement.
b.) change the 'i' in 'i--' to 'n--'.
c.) change the '<' in the comparison statement to '+'

5.) void main() { if( printf("Hello world") ) {} }

6.) Recursion is when a function calls itself. Mutual recursion is when a function A calls function B and B calls back A.

7.) 0 (atleast on linux systems.. not tested on windows platform)
Scratch your heads how...

8.) Semaphores implement what is called atomocity which application programmers will not by using the boolean. i.e.
the statement if (a == 1) in C will be a bunch of commands for the microprocessor
So pre-emptive processors might stop the programs execution in any of those commands and hence it will not be possible to implement the semaphore action by application level code. By using atomic function calls, semaphores will avoid pre-emptive scheduling microprocessors for critical sections of code.

No comments: