All Exams Test series for 1 year @ ₹349 only
Question

Consider the function in C code :

Cal(a, b) {

    if (b != 1) {

        if (a != 1) {

            printf("*");

            Cal(a/2, b);

        }

        else {

            b=b-1;

            Cal(10, b);

        }

        }}

How many times * is going to be printed, if the function is called with Cal(10, 10); ?

This question was previously asked in
UGC NET 2024 Management Question Paper (02-Sep-2024) (Shift 2)
The correct answer is

27

The function halves a (integer division) and prints one * per call while a is not 1. When a reaches 1, b is decremented and a is reset to 10. Everything stops once b becomes 1.

Step 1 — For a fixed b (b ≠ 1), starting from a = 10: a = 10 prints *, a = 5 prints *, a = 2 prints *, and a = 1 triggers the else branch. So each value of b produces 3 stars.

Step 2 — b takes the values 10, 9, 8, …, 2 while stars are printed; when b becomes 1 the outer condition fails and the recursion ends. That is 9 values of b.

Step 3 — Total stars = \(9 \times 3 = 27\).

24 counts only 8 values of b (forgetting b = 10 or b = 2); 23 and 25 come from miscounting the halving chain 10 → 5 → 2 → 1, which always gives exactly 3 prints.

27 option is correct.

Was this answer helpful?

Similar Questions

  1. In general, in a recursive and non-recursive implementation of a problem (program) :

  2. Consider the following C code :

    #include<stdio.h>

    int temp=0;

    int fun(int x, int y)

    {

        int z; temp++;

        if (y==3) return(x*x*x);

        else {

            z=fun(x, y/3);

            return(z*z*z);

            }

    }

    int main()

    {

        fun(4, 81);

        printf("%d", temp);

    }

    What will be the output of the above code ?


Important Questions from Function Recursion

  1. #include <stdio.h>
    
    int foo(int S[], int size){
        if(size == 0) return 0;
        if(size == 1) return 1;
        if(S[0] != S[1]) return 1 + foo(S + 1, size - 1);
        return foo(S + 1, size - 1);
    }
    
    int main(){
        int A[] = {0, 1, 2, 2, 2, 0, 0, 1, 1};
        printf("%d", foo(A, 9));
        return 0;
    }
    

    The value printed by the given C program is _______ . (Answer in integer)

  2. Consider the following ANSI-C function.

    int func(int start, int end){
    int length=end+1-start;
    if((length < 1)||(start < 0)||(end < 0)){ return(0); }
    if(length%3==0){
    return(func(start+1, end));
    } else if(length%3==1){
    return(1+func(start, end-1));
    } else {
    return(func(start+2, end));
    }
    }


    The maximum possible value that can be returned from this function is ____________. (answer in integer)

    Note: Ignore syntax errors (if any) in the function.
  3. What is the output of the given C language code snippet? 

    #include<stdio.h>

     void f(int n)

    {

     if(n==0)

     return; 

    printf("%d ",n); 

    f(n-1); 

    } 

    int main()

    { 

    f(3); 

    return 0; 

    }

  4. What is the value returned by the function f given below when n = 100 ?
    int f (int n)
    { if (n == 0) then return n;
    else
    return n + f(n-2);
    }
  5. Consider the recursive functions represented by the following code segment:
    int bar(int n){
    if (n == 1) return 0;
    else return 1 + bar(n/2);
    }
    int foo(int n){
    if (n == 1) return 1;
    else return 1 + foo(bar(n));
    }

    The smallest positive integer n for which foo(n) returns 5 is ______. (answer in integer)
    Note: Ignore syntax errors (if any) in the function.
Need Expert Advice?
Upcoming Exams
CTET
December 12, 2026
CBSE UGC NET
December 14, 2026
CSIR UGC NET
December 19, 2026
Test Series
UGC NET img
Teaching
UGC NET (Paper 2) 2026 Mock Test Series
796 Tests 2 Tests Free
219 Attempts
4.4(46)
English
More Questions from UGC NET

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App