StreakPeaked· Practice

ExamsGATETechnical

Consider the following C function. void convert(int n){ if(n<0) printf("%d",n); else{ convert(n/2); printf("%d",n%2); } } Which one of the following will happen when the function convert is called with any positive integer n as argument?

  1. It will print the binary representation of n and terminate
  2. It will print the binary representation of n in the reverse order and terminate
  3. It will print the binary representation of n but will not terminate
  4. It will not print anything and will not terminate

Correct answer: It will not print anything and will not terminate

Solution

For any positive n the recursion reaches convert(0), which takes the else branch and calls convert(0/2)=convert(0) endlessly, never returning, so no printf in the else branch ever executes. The function prints nothing and does not terminate (index 3), not the stored index 2.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →