Рубрики

ВОРОТА | GATE-CS-2016 (набор 1) | Вопрос 45

Каковы будут результаты следующей C-программы?

void count(int n)

{

    static int d = 1;

    printf("%d ", n);

    printf("%d ", d);

    d++;

    if(n > 1) count(n-1);

    printf("%d ", d);

}

int main()

{

    count(3);

}

(А) 3 1 2 2 1 3 4 4 4
(B) 3 1 2 1 1 1 2 2 2
(С) 3 1 2 2 1 3 4
(D) 3 1 2 1 1 1 2

Ответ: (А)
Объяснение:

count(3) will print value of n and d. So 3 1 will be printed 
and d will become 2. 

Then count(2) will be called. It will print value of n and d. 
So 2 2 will be printed and d will become 3. 

Then count(1) will be called. It will print value of n and d.
So 1 3 will be printed and d will become 4. 

Now count(1) will print value of d which is 4. count(1) will 
finish its execution. 

Then count(2) will print value of d which is 4. 

Similarly, count(3) will print value of d which is 4. 
So series will be A.

Тест на этот вопрос

Рекомендуемые посты:

ВОРОТА | GATE-CS-2016 (набор 1) | Вопрос 45

0.00 (0%) 0 votes