LearnFrenzy
Sign in Take a free test

Pointers · Exercise 2 of 4

Pointers 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 here was compiled and run, so the answers are the real output. Assume a 64-bit system (int 4 bytes, pointers 8 bytes).

Question 1 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 10;
    int *p = &x;
    *p = 20;
    printf("%d %d\n", x, *p);
    return 0;
}
A 10 20
B 20 20
C 10 10
D Compilation error
Question page & discussion
Question 2 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[] = {10, 20, 30, 40, 50};
    int *p = a;
    p += 2;
    printf("%d %d\n", *p, *(p + 1));
    return 0;
}
A 12 13
B 30 40
C 20 30
D 30 31
Question page & discussion
Question 3 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[] = {5, 10, 15};
    int *p = a;
    int x = *p++;
    int y = (*p)++;
    printf("%d %d %d\n", x, y, a[1]);
    return 0;
}
A 5 10 10
B 6 10 11
C 5 10 11
D 5 11 11
Question page & discussion
Question 4 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[] = {1, 2, 3};
    int *p = a;
    printf("%d ", ++*p);
    printf("%d ", *++p);
    printf("%d\n", a[0]);
    return 0;
}
A 2 2 2
B 2 3 1
C 2 2 1
D 1 2 2
Question page & discussion
Question 5 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

void fake_swap(int a, int b) { int t = a; a = b; b = t; }
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }

int main(void) {
    int x = 3, y = 7;
    fake_swap(x, y);
    printf("%d %d ", x, y);
    swap(&x, &y);
    printf("%d %d\n", x, y);
    return 0;
}
A 7 3 3 7
B 3 7 7 3
C 7 3 7 3
D 3 7 3 7
Question page & discussion
Question 6 Medium

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a[10];
    int *p = &a[2], *q = &a[7];
    printf("%td\n", q - p);
    return 0;
}
A 20
B 5
C 7
D Compilation error
Question page & discussion
Question 7 Medium

What is the output of the following program on a 64-bit system?

main.c
#include <stdio.h>

int main(void) {
    int a[5];
    double *d;
    char *c;
    printf("%zu %zu %zu\n", sizeof a, sizeof d, sizeof c);
    return 0;
}
A 8 8 8
B 20 8 1
C 20 8 8
D 5 8 8
Question page & discussion
Question 8 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 5;
    int *p = &x;
    int **pp = &p;
    **pp = 50;
    printf("%d %d %d\n", x, *p, **pp);
    return 0;
}
A 5 5 50
B 5 50 50
C 50 50 50
D Compilation error
Question page & discussion
Question 9 Easy

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    char s[] = "LearnFrenzy";
    char *p = s;
    p += 5;
    printf("%s %c\n", p, *(s + 1));
    return 0;
}
A Frenzy e
B LearnFrenzy L
C nFrenzy e
D Frenzy L
Question page & discussion
Question 10 Hard

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int (*row)[3] = m;
    printf("%d %d\n", *(*(row + 1) + 2), **row);
    return 0;
}
A 6 1
B 3 1
C 5 4
D 6 4
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links