LearnFrenzy
Sign in Take a free test

Functions · Exercise 2 of 4

Functions Predict the Output

  • 12 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each program and choose what it prints. Every program was compiled and run, so the answers are the real output. Assume a 64-bit system.

Question 1 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

void add_one(int n) {
    n++;
}

int main(void) {
    int x = 5;
    add_one(x);
    printf("%d\n", x);
    return 0;
}
A 5
B 6
C 0
D Compilation error
Question page & discussion
Question 2 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int counter(void) {
    static int c = 0;
    int d = 0;
    c++;
    d++;
    return c * 10 + d;
}

int main(void) {
    printf("%d ", counter());
    printf("%d ", counter());
    printf("%d\n", counter());
    return 0;
}
A 11 21 31
B 11 11 11
C 11 22 33
D 10 20 30
Question page & discussion
Question 3 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

void show(int n) {
    if (n == 0)
        return;
    printf("%d ", n);
    show(n - 1);
    printf("%d ", n);
}

int main(void) {
    show(3);
    printf("\n");
    return 0;
}
A 3 2 1 1 2 3
B 3 2 1 3 2 1
C 1 2 3 3 2 1
D 3 2 1
Question page & discussion
Question 4 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int f(int n) {
    return n < 10 ? n : n % 10 + f(n / 10);
}

int main(void) {
    printf("%d %d\n", f(12345), f(f(9875)));
    return 0;
}
A 15 11
B 15 29
C 6 2
D 15 2
Question page & discussion
Question 5 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int x = 10;

void show(void) {
    printf("%d ", x);
}

int main(void) {
    int x = 20;
    show();
    printf("%d ", x);
    {
        int x = 30;
        printf("%d ", x);
    }
    printf("%d\n", x);
    return 0;
}
A 10 20 30 20
B 20 20 30 30
C 10 20 30 30
D 20 20 30 20
Question page & discussion
Question 6 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }

int main(void) {
    int (*ops[])(int, int) = {add, sub, mul};
    printf("%d\n", ops[2](ops[0](2, 3), ops[1](9, 4)));
    return 0;
}
A 25
B 10
C 30
D 5
Question page & discussion
Question 7 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

static int calls = 0;

int fib(int n) {
    calls++;
    return n < 2 ? n : fib(n - 1) + fib(n - 2);
}

int main(void) {
    int r = fib(6);
    printf("%d %d\n", r, calls);
    return 0;
}
A 8 25
B 8 6
C 13 25
D 8 15
Question page & discussion
Question 8 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

struct point {
    int x, y;
};

struct point move(struct point p, int dx) {
    p.x += dx;
    return p;
}

int main(void) {
    struct point a = {1, 2};
    struct point b = move(a, 5);
    printf("%d %d %d\n", a.x, b.x, b.y);
    return 0;
}
A 1 6 2
B 6 6 2
C 1 1 2
D 6 1 2
Question page & discussion
Question 9 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

void reset(int *p) { p = NULL; }
void zero(int *p)  { *p = 0; }

int main(void) {
    int x = 5;
    int *q = &x;
    reset(q);
    printf("%d ", q != NULL);
    zero(q);
    printf("%d\n", x);
    return 0;
}
A 1 0
B 0 0
C 1 5
D 0 5
Question page & discussion
Question 10 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int is_odd(int n);

int is_even(int n) { return n == 0 ? 1 : is_odd(n - 1); }
int is_odd(int n)  { return n == 0 ? 0 : is_even(n - 1); }

int main(void) {
    printf("%d %d %d\n", is_even(10), is_odd(7), is_even(3));
    return 0;
}
A 1 1 0
B 1 0 1
C 0 1 0
D Compilation error
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links