Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
622 views
in Information Technology by (88.5k points)
closed by

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

1 Answer

0 votes
by (85.4k points)
selected by
 
Best answer
Correct Answer - Option 4 : It will not print anything and will not terminate

Let us consider n = 1

Since n > 0 else part gets executed

convert(1/2) = convert(0) // integer division in C

Now, n = 0

But base condition is n < 0 again else part gets executed

convert(0/2) = convert(0) // recursive calls until stack memory gets exhausted

This code will never satisfy base condition and will not terminate until stack memory gets exhausted

Since this option is not available choose the best possible option

Option d → “It will not print anything and will not terminate”

It is partially correct since it will not print anything but also gets terminated

Note:
Answer key in GATE 2019: D

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...