POINTERS: Interview Questions To Practice

Robin Kamboj
Sep 7, 2018 · 12 min read

THEORY TOPICS:

  1. Introduction to pointers
  2. Pointer Arithmentic
  3. Arrays & Pointers
  4. Characters & Pointers
  5. Pointers & Functions
  6. Double Pointer

QUESTIONS:

Pointer Declaration

Which of the following is the proper declaration of a pointer?

  • int x;
  • int &x;
  • int *x;
  • ptr x;

Answer: int *x;


Address of Variable

Which of the following gives the memory address of integer variable a ?

  • *a;
  • a;
  • &a; (ans)
  • address(a);

Answer: &a

Explanation: Variable a is integer, and not an integer pointer. So we use &a to get its address.


Address of Variable

Which of the following gives the memory address of variable ‘b’ pointed by pointer ‘a’ i.e.

int b = 10;
int *a = &b;
  • a
  • *a
  • &a
  • address(a)

Answer: a

Explanation: a is a pointer that stores the address of variable b.


Pointers Output

What will happen in this code?

int a = 100, b = 200;
int *p = &a, *q = &b;
p = q;
  • b is assigned to a
  • p now points to b
  • a is assigned to b
  • q now points to a

Answer: p now points to b

Explanation: a and b are two integer variables. p stores address of a and q stores address of b. by performing p = q, p now stores the address of b


Output

int a = 7;
int b = 17;
int *c = &b;
*c = 7;
cout << a << " “ << b << endl;
  • 7 17
  • 17 7
  • 7 7
  • 17 17

Answer: 7 7

Explanation: c stores the address of variable b. Then, value at the address stored in c (which is of b) is updated to 7 (that is b = 7).


Output

 int a = 50;
int *ptr = &a;
int *q = ptr;
(*q)++;
cout << a << endl;
  • 50
  • 51
  • Error
  • None of these

Answer: 51

Explanation: ptr points to a. q points to ptr directly and to a through ptr. *q = value of p = address of a. So, (*q)++ increments the value at the address of a. So a becomes 51 from 50.


Output

int *ptr = 0;
int a = 10;
*ptr = a;
cout << *ptr << endl;
  • 10
  • 0
  • Error
  • None of these

Answer: Error

Explanation: at the time of printing, *ptr points to the value at address a which is 10. There is no value stored at address 10.


Output

int a = 7;
int b = 17;
int *c = &b;
a = *c;
*c = *c + 1;
cout << a << " " << b << endl;
  • 18 18
  • 7 18
  • 17 17
  • 17 18

Answer: 17 18

Explanation: c points to b. a has the value at the address stored in c, which is of b (17). Value at address stored in c is incremented by 1 (b = 17 + 1 = 18).


Pointers Output

float f = 10.5;
float p = 2.5;
float* ptr = &f;
(*ptr)++;
*ptr = p;
cout << *ptr << “ “ << f << “ “ << p;
  • 2.5 10.5 2.5
  • 2.5 11.5 2.5
  • 2.5 2.5 2.5
  • 11.5 11.5 2.5

Answer: 2.5 2.5 2.5

Explanation: ptr points to f. f is incremented by 1 through ptr (f = 11.5 now). f = p using ptr (f = 2.5 now).


Pointers Output

int a = 7;
int *c = &a;
c = c + 1;
cout << a << " " << *c << endl;
  • Garbage_value 7
  • 7 Garbage_value
  • 8 8
  • 7 7

Answer: 7 Garbage_value

Explanation: c stores the address of a (and points to the value of a). address that c stores is incremented by 1, so now it points to an unknown value.


Output

Assume memory address of variable ‘a’ is : 400 (and an integer takes 4 bytes), what will be the output -

int a = 7;
int *c = &a;
c = c + 3;
cout << c << endl;

Answer: 412

Explanation: c stores address of a (and points to value of a). address that c stores is incremented by 3. since c is of type int, increment in bytes is 3 integer addresses, that is 3*4 = 12 bytes. therefore 400 + 12 = 412


Output

Assume integer takes 4 bytes and integer pointer 8 bytes.

int a[5];
int *c;
cout << sizeof(a) << “ “ << sizeof(c);
  • 8 8
  • 5 8
  • 20 8
  • 20 4

Answer: 20 8

Explanation: array a has size 5 and is of type int (4 bytes per int) so total size = 5*4 = 20. c is an integer pointer, so its size is 4 (for 32 bit system) or 8 (for 64 bit system).


Fill Output

int a[] = {1, 2, 3, 4};
cout << *(a) << " " << *(a+1);

Answer: 1 2

Explanation: *a points to first value of array a = 1. *(a+1) = *(second value of array a) = 2.


Fill Output

Assume that address of 0th index of array ‘a’ is : 200.

What is the output -

int a[6] = {1, 2, 3};
cout << a << “ “ << &a;
  • Error
  • 200 204
  • 200 200
  • 1 200
  • 200 1

Answer: 200 200

Explanation: a is the first value of array a. &a is the address of first value of array a, which is 200.


Fill Output

Assume that address of 0th index of array ‘a’ is : 200. What is the output -

int a[6] = {1, 2, 3};
cout << (a + 2);

Answer: 208

Explanation: a is of type int, so per value, address increases by 4. a+2 is the address of third value of array a. so 200 (1) — 204 (2) — 208 (3)


Output

Assume address of 0th index of array ‘a’ is 200.

What is the output -

int a[6] = {1, 2, 3};
int *b = a;
cout << b[2];
  • Error
  • 3
  • 1
  • 200
  • 212

Answer: 3

Explanation: b stores the address of first value of array a. b[2] will be 0,1,2th value of array a, which is 3.


Output

Assume address of 0th index of array ‘a’ is 200.

What is the output -

int a[] = {1, 2, 3, 4, 5};
cout << *(a) << " " << *(a + 4);
  • Error
  • 200 216
  • 1 5
  • None of these

Answer: 1 5

Explanation: *a gives the first value of array a (1). *(a+4) gives the fifth value of array a (5).


Output

int a[] = {1, 2, 3, 4};
int *p = a++;
cout << *p << endl;
  • 1
  • 2
  • Garbage value
  • Error

Answer: Error

Explanation: entire array’s address can not be shifted by four bytes


Pointers Output

char ch = 'a';
char* ptr = &ch;
ch++;
cout << *ptr << endl;
  • a
  • b
  • 97
  • 98

Answer: b

Explanation: small a ascii 97. ptr points to ch. ch is incremented and is now 98 (b).


Output

Assume address of 0th index of array ‘b’ is 200. What is the output -

char b[] = "xyz";
char *c = &b[0];
cout << c << endl;
  • 200
  • x
  • xyz
  • None of these

Answer: xyz

Explanation: c stores the address of start of array b (and not of it’s values). So the entire array is printed when c is printed.


Output

Assume address of 0th index of array ‘b’ is 200. What is the output -

char b[] = "xyz";
char *c = &b[0];
c++;
cout << c << endl;
  • 201
  • y
  • xyz
  • yz

Answer: yz

Explanation: c stores the address of start of array b (and not of it’s values). As it is of type char, one byte is increased for the address of c. So, all characters of array except the skipped/first one are printed.


Fill Output

char s[]= "hello";
char *p = s;
cout << s[0] << " " << p[0];

Answer: h h

Explanation: p points to starting value of array s. p[0] calls for the value at 0 characters skipped from it’s address, which is the value at the address itself, which is of s[0].


Pointers Output

void changeSign(int *p){
*p = (*p) * -1;
}
  • -10
  • 10
  • Error
  • None of the above

Answer: -10

Explanation: p of function stores the address of a. So it points to the actual value of a. When value that p points to is made negative, a also becomes negative and vice versa.


Fill Output

void fun(int a[]) {
cout << a[0] << " ";
}

Answer: 2 1

Explanation: a +1 gives the entire array except the first value of the array as parameter (2 3 4). So 0th value is 2.


Output

void square(int *p){
int a = 10;
p = &a;
*p = (*p) * (*p);
}
  • 100
  • 10
  • Error
  • None of these

Answer: 10

Explanation: p points to actual value of a since address of a is passed as a parameter. Within the function, p now points to the local a within the function. Updating the local a doesn’t change the a within the main.


Output

int a = 10;
int *p = &a;
int **q = &p;
int b = 20;
*q = &b;
(*p)++;
cout << a << " " << b << endl;
  • 10 21
  • 11 20
  • 11 21
  • 10 20

Answer: 10 21

Explanation: p points to a. q points to p directly and a through p (double pointer). *q — value stored in p is changed to address of b instead of that of a. (*p)++ — value that p points to, which now is of b, is incremented by 1 (b becomes 21). Value of a remains unchanged.


Output

int a = 100;
int *p = &a;
int **q = &p;
int b = (**q)++ + 4;
cout << a << " " << b << endl;
  • 100 104
  • 101 104
  • 101 105
  • 100 105

Answer: 101 104

Explanation: p points to a. q points to p directly and to a through p (double pointer). b stores value of a through p through q plus 4, which is 100 + 4 = 104. Value stored in b is incremented by 1 using post increment operator after the end of this statement.


Pointers Output

int a = 100;
int *p = &a;
int **q = &p;
int b = (**q)++;
int *r = *q;
(*r)++;
cout << a << " " << b << endl;

  • 102 100
  • 101 100
  • 101 101
  • 102 101

Answer: 102 100

Explanation: p points to a. q points to p directly and to a through p. b is assigned the value of a through q (b = 100) and value of a through q is post incremented (a = 101). r points to p. Value at p is post incremented through r (a = 102).


Pointers Output

void increment(int **p){
(**p)++;
}
  • 10
  • 11
  • Error
  • None of these

Answer: 11

Explanation: ptr points to num. p points to ptr directly and to num through ptr. value at num is incremented by 1 through q through p (num becomes 11).


Output

#include <iostream>
using namespace std;
int main()
{
int arr[] = {4, 5, 6, 7};
int *p = (arr + 1);
cout << *arr + 9;
return 0;
}
  • 12
  • 14
  • 13
  • error

Answer: 13

Explanation: p points to the address of starting index of array arr plus 4 bytes, which is the 1st index. *arr points to the first index which is 4, adding 9 to which yields 13.


Output

#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers;
*p = 10;
p = &numbers[2];
*p = 20;
p--;
*p = 30;
p = numbers + 3;
*p = 40;
p = numbers;
*(p+4) = 50;
for (int n=0; n<5; n++) {
cout << numbers[n] << ",";
}
return 0;
}
  • 10,20,30,40,50,
  • 50,40,30,20,10,
  • 10,30,20,40,50,
  • None of these

Answer: 10,30,20,40,50,

Explanation: p points to the first value of the numbers array, which is updated to 10 (numbers[0] = 10). p now points to the third value of numbers array, and updates it to 20 (numbers [2] = 20). p is decremented by 4 bytes, and updated to 30 (numbers[1]). p now points to the 4th value of numbers and updates it to 40 (numbers[3] = 40). p now points to the first value of numbers array, updates it’s 5th value to 50. Numbers = 10 30 20 40 50


Output

#include <iostream>
using namespace std;
int main()
{
char *ptr;
char Str[] = "abcdefg";
ptr = Str;
ptr += 5;
cout << ptr;
return 0;
}
  • fg
  • cdef
  • defg
  • abcd

Answer: fg

Explanation: ptr points to the entire string array. ptr now points to the sixth value of string array. All characters starting from sixth index are printed.


Characters & Pointers

#include <iostream>
using namespace std;
int main()
{
char arr[20];
int i;
for(i = 0; i < 10; i++) {
*(arr + i) = 65 + i;
}
*(arr + i) = '\0';
cout << arr;
return 0;
}
  • ABCDEFGHIJ
  • AAAAAAAAAA
  • JJJJJJJJ
  • none of the mentioned

Answer: ABCDEFGHIJ

Explanation: for the first iteration, *(arr + 0) is ascii(65 + 0) which is A, and so on…


Output

#include<iostream>
using namespace std;
void swap (char *x, char *y)
{
char *t = x;
x = y;
y = t;
}
  • geeksforgeeks geeksquiz geeksforgeeks geeksquiz
  • geeksquiz geeksforgeeks geeksquiz geeksforgeeks
  • geeksquiz geeksforgeeks geeksforgeeks geeksquiz
  • Compiler Error

Answer: geeksquiz geeksforgeeks geeksforgeeks geeksquiz

Explanation: since parameters for swap function are not passed by reference, so the actual values in them remains unchanged when swap function is called.


Output

#include <iostream>
using namespace std;
int main()
{
float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
float *ptr1 = &arr[0];
float *ptr2 = ptr1 + 3;
cout<<*ptr2<<" ";
cout<< ptr2 - ptr1;
return 0;
}
  • 90.5 3
  • 90.5 12
  • 10.0 12
  • undefined

Answer: 90.5 3

Explanation: ptr1 points to the first value of arr (12.5). ptr2 points to the fourth value of arr (90.5). So, *ptr2 is 90.5 and if ptr1 is x, ptr2 is x + 3. Subtracting these yields 3.


Output

#include<iostream>
using namespace std;
int main() {
char st[] = "ABCD";
for(int i = 0; st[i] != ‘\0’; i++) {
cout << st[i] << *(st)+i << *(i+st) << i[st];
}
return 0;
}
  • AAAABBBBCCCCDDDD
  • ABCD
  • A65AAB66BBC67CCD68DD
  • Compilation Error

Answer: A65AAB66BBC67CCD68DD

Explanation: for the first iteration: A << *(st[0]) + 0= A + 0 (cast to 65) << st[0] = A << 0th index value of st = A….. And so on…


Output

#include <iostream>
using namespace std;
void Q(int z)
{
z += z;
cout<<z << " ";
}
  • 7 6 14
  • 14 7 5
  • 14 7 6
  • 14 6 5

Answer: 14 7 6

Explanation: y in P() points to actual value of x in main. local x in P() has value of (5 + 2 = 7). Copy of local x (7) is passed to Q() and prints 7+7 = 14 is output. actual value of x is updated through y to local x: x-1 = 7–1 = 6. Local x (7) is printed at the end of P(). actual value of x (6) is output in main.


Output

#include<iostream>
using namespace std;
int main()
{
int ***r, **q, *p, i=8;
p = &i;
(*p)++;
q = &p;
(**q)++;
r = &q;
cout<<*p << " " <<**q << " "<<***r;
return 0;
}
  • 8 8 8
  • 10 10 10
  • 9 10 11
  • 9 10 10

Answer: 10 10 10

Explanation: p points to i. value of i++ through p (i = 9). q points to p directly and to i through p. i++ (i = 10) through q. r points to q directly, p through q, i through p through q. i, i, i are printed, which is 10, 10, 10.


Output

int f(int x, int *py, int **ppz) {
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
  • 21
  • 18
  • 19
  • 24

Answer: 19

Explanation: b points to c. a points to b directly and to c through b. copies of c, b, a are passed to function (same rules apply to the parameter variables). Value of x is incremented by 1 through **ppz (x = 4). z now stores **ppz = 4. value of x is incremented by 2 using *py (x = 6). = *py = 6. x is incremented by 3 (x = 9). x + y + x = 4 + 6 + 9 = 19.


Data Driven Investor

from confusion to clarity, not insanity

Robin Kamboj

Written by

Software Engineer by profession. Designer by force. Bibliophile by nature.

Data Driven Investor

from confusion to clarity, not insanity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade