Pointers Questions
Practice Pointers MCQs with answers and explanations. Page 2 of 2.
Category
C Programming
Topic
Pointers
Page
2 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
In C, precedence of unary * and multiplication: evaluate this expression.
#include<stdio.h>
int main()
{
int i = 3, j, k;
j = &i;
printf("%d
", i**ji + *j);
return 0;
}
Open
View answer
In C, using a[b] == (a+b) symmetry: what character is printed?
#include<stdio.h>
int main()
{
printf("%c
", 7["CuriousTab"]);
return 0;
}
Open
View answer
In C programming, what will be the output of the following code? Carefully consider pointer arithmetic on a string and the postfix increment.
#include <stdio.h>
int main()
{
char str[] = "peace";
char s = str;
printf("%s
", s++ + 3);
return 0;
}
Open
View answer
In C (assume pointer size = 4 bytes), what will this program print for the sizes of NULL and an empty string literal?
#include <stdio.h>
int main()
{
printf("%d, %d
", sizeof(NULL), sizeof(""));
return 0;
}
Open
View answer
In C, what is the output of the following pointer-to-pointer calculation? Pay attention to operator precedence and tokenization.
#include <stdio.h>
int power(int );
int main()
{
int a = 5, aa; / Suppose &a is 1000 (illustrative only) /
aa = &a;
a = power(&aa);
printf("%d
", a);
return 0;
}
int power(int **ptr)
{
int b;
b = ptrptr; / parsed as (**ptr) * (**ptr) /
return b;
}
Open
View answer
In C, consider this function that modifies an array through a pointer. What is printed after the loop and why?
#include <stdio.h>
void change(int b, int n)
{
int i;
for (i = 0; i < n; i++)
(b + 1) = (b + i) + 5; / note the constant +1 on the left /
}
int main()
{
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
for (i = 0; i <= 4; i++)
printf("%d, ", a[i]);
return 0;
}
Open
View answer
In C, given a 3D array and two pointers, what values are printed? Understand row-major layout and element addresses.
#include <stdio.h>
int main()
{
int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
int *p, q;
p = &arr[1][1][1];
q = (int)arr;
printf("%d, %d
", *p, q);
return 0;
}
Open
View answer
In C, a const pointer to char is used to modify a string buffer. What gets printed after changing the first character?
#include <stdio.h>
int main()
{
char str[20] = "Hello";
char const p = str; / const pointer, modifiable characters */
*p = 'M';
printf("%s
", str);
return 0;
}
Open
View answer
In C, arrays of pointers and multi-level pointers: what substring is printed by this code?
#include <stdio.h>
int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char *ptr[] = {s + 3, s + 2, s + 1, s}, p;
p = ptr;
++p; / now points to (s + 2) /
printf("%s", p + 1);
return 0;
}
Open
View answer
In C, identify the correct diagnostic for using the keyword static on function parameters and the result of returning their addresses.
#include <stdio.h>
int *check(static int, static int);
int main()
{
int *c;
c = check(10, 20);
printf("%d
", c);
return 0;
}
int *check(static int i, static int j)
{
int *p, *q;
p = &i;
q = &j;
if (i >= 45)
return p;
else
return q;
}
Open
View answer
In C, using void* to reference different types: what does this program print when casting to the correct types before dereferencing?
#include <stdio.h>
int main()
{
void vp;
char ch = 74, cp = "JACK";
int j = 65;
vp = &ch;
printf("%c", (char)vp);
vp = &j;
printf("%c", (int)vp);
vp = cp;
printf("%s", (char)vp + 2);
return 0;
}
Open
View answer
In C, modify the recursive factorial function so that the computed factorial is stored back through the pointer argument. Which statement should be added where indicated?
#include <stdio.h>
void fact(int*);
int main()
{
int i = 5;
fact(&i);
printf("%d
", i);
return 0;
}
void fact(int *j)
{
static int s = 1;
if (*j != 0)
{
s = s * *j;
*j = j - 1;
fact(j);
/ Add a statement here */
}
}
Open
View answer
In C on a typical PC, consider aliasing a float as bytes and printing the first byte's numeric value. What does this program output?
#include<stdio.h>
int main()
{
float a = 3.14;
char j;
j = (char)&a; // alias the float's storage as bytes
printf("%d
", *j); // print the first byte as an integer
return 0;
}
Open
View answer
In C, analyze multi-dimensional array initialization and deep dereferencing. What does this print?
#include<stdio.h>
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d
", (((arr))));
return 0;
}
Open
View answer
C strings copy loop: which statement must be added to properly terminate the destination so that it prints "CuriousTab"?
#include<stdio.h>
int main()
{
char s[] = "CuriousTab";
char t[25];
char *ps, *pt;
ps = s;
pt = t;
while (*ps)
*pt++ = *ps++;
/* Add a statement here */
printf("%s
", t);
return 0;
}
Open
View answer
Pointers to pointers: add one statement inside fun so that j in main receives the address of local a (demonstration of double-pointer assignment).
#include<stdio.h>
int main()
{
int j;
void fun(int*);
fun(&j);
return 0;
}
void fun(int *k)
{
int a = 10;
/ Add a statement here */
}
Open
View answer
Function declaration syntax: choose the correct prototype for a function that takes float*** and returns float****.
Open
View answer
Pointer declaration meaning: what does the declaration "char k;" represent in C?
Open
View answer
Pointer basics: what do the variables represent here, and which statement is correct?
#include<stdio.h>
int main()
{
int i = 10;
int *j = &i;
return 0;
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.