The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+2 votes
3.8k views

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
asked in Algorithms by Veteran (406k points)
edited by | 3.8k views
0
Why bonus? I think the correct option is D. Because convert(int n) will be called recursively. As soon as n becomes 1, convert(0) will called and  it enter's infinite recursion.
0
but the correct answer would be it should.not.print anything and terminate
0
It will terminate due to stackoverflow exception. I'm not sure  whether that is in scope of this question. If yes then you are correct it answer should be "it should not print and terminate"(terminate due to stackoverflow".
0
on madeeasy it is declare as bonus
0

@mradul

you will discuss this point on https://gateoverflow.in/302822/gate2019-26

there is no need of posting this question again, right ?

+1
i think it is a bonus any one  who can make it run program in compiler and let's check what the compiler gives the answer.

As I made run on my compiler i conclude that there is a non option is matched and it is to be a bonus. lets see what gate overflow will conclude .

Max the option will be nothing which was mentioned over there so it will be bonus marks.

8 Answers

+8 votes
Best answer

As per the question, the function convert needs to be called with a positive integer n as argument.

So, when a positive int is passed as argument, function will compute$(n/2)$ which will return the quotient of integer division. 

Again this result of integer division will be passed through compute$(n/2)$ and so on. Each time the no will be divided by $2$ and will gradually become smaller and smaller and close to $0$ but it will never be less than $0$

Hence the program will not print anything ( as after every function call, the value returned is greater than $0$) and will not terminate. 

PS: Being a C function and hence when run on a computer each recursive call will require creation of activation record consuming memory space. So, eventually the stack memory will run out and program terminates. But such an option is not there. D is the best option here . Answer (D)

answered by Loyal (6.9k points)
edited by
0
yaa bro gate has to give the marks to every one
0
GATE official ans key had D as correct option
+19 votes

I think none of the option matches. 

Ans should be :- "It won't print anything and terminates abruptly".

Remember here infinite looping is not there. It will terminate abruptly because of stackoverflow.

And thus for this question no option matches.

answered by (195 points)
+4

Agreed. This question is similar to this :

https://gateoverflow.in/118319/gate2017-1-36

0
At last what will be the answer either it is to be a bonus or it will not print anything and terminate please confirm me bro

Thanks for your valuable response
+11 votes
The print statement will run at the time n value becomes negative or function calls are over.

Divide by 2 won't give a negative result for any positive integer. So theoretically it will not terminate. Also, it won't print anything because printf statement in else part placed after the function call.

It will terminate due to StackOverflow without printing anything.
answered by Veteran (59.7k points)
edited by
0
For which value of "n", print statement will run once ?
n will never be negative!!
0
It will terminate abruptly, only if we are running the program on a finite memory model. Question does not talk about the platform on whih it runs.
0
$C$ program it is. So it doesn't matter how much memory do you have. Ultimately it will terminate.
0
We can even run this program on a hypothetical machine with infinite memory. Any assumption of finiteness is just introducing data to the question, that does not exist.
0
the program will terminate with stackoverflow as infinite recursion is there and it won't print anything ..i think program will not terminate is not correct
+1 vote
If we keep dividing a no by 2 it will decrease continuously but will not reach neg.

So the (n<0) will never execute and as control never gets past convert() function because it calls convert again and gets inside. So even printf("%d", n%2) will not be executed ever.

The program will neither print anything nor will it ever terminate.
answered by Junior (827 points)
+1 vote
Actually the correct answer for this question must be it terminates by showing error message stackoverflow as there is a limit upto which function calls can be pushed onto the stack and also it doesnot prints anything.And also infinite looping and stackoverflow are two different things so none of the option matches according to the given scenario.
answered by (41 points)
0
will it be bonus??
+1

@mradul

It might be a bonus...can see what GATE give to all..

0 votes
Let's analyze how will the program will behave if the input is 1.
convert(1)
  convert(1/2) or convert(0):  now this call
       will not enter the base case since 0 < 0
        is false.
        convert (0/2) or convert (0).....
So hypothetically program will not terminate and it also doesn't print anything since it goes into infinite recursion. Best answer is D
answered by Loyal (9k points)
0 votes
i think if the following c program is run on a machine with infinite memory it will not terminate or if it is run by hand by a human (just a thought experiment).

So it will not print anything and it will not terminate..
answered by Active (2k points)
0 votes

Ans is D

answered by Junior (627 points)
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,541 questions
54,084 answers
187,213 comments
70,992 users