Pointers and References # MCQs Practice set

Q.1 What is the value of the expression `*p` if `int x = 10; int *p = &x;`?

Address of x
10
Garbage value
Compiler error
Explanation - The * operator dereferences the pointer, giving the value stored at the memory address pointed to by p, which is 10.
Correct answer is: 10

Q.2 Which operator is used to get the address of a variable in C/C++?

*
&
#
%
Explanation - The '&' operator returns the memory address of a variable.
Correct answer is: &

Q.3 If `int *p = nullptr;`, what does `p` point to?

An integer
Memory address 0
Garbage value
A random variable
Explanation - A nullptr pointer is a pointer that points to address 0, indicating it points to nothing.
Correct answer is: Memory address 0

Q.4 What will be the output of the following code? ```cpp int x = 5; int &ref = x; ref = 10; std::cout << x; ```

5
10
Garbage
Compiler error
Explanation - A reference acts as an alias. Assigning 10 to ref changes x to 10.
Correct answer is: 10

Q.5 Which of the following is NOT true about references in C++?

Must be initialized
Cannot be NULL
Can be reseated to another variable
Acts as an alias
Explanation - Once initialized, a reference cannot be made to refer to another variable.
Correct answer is: Can be reseated to another variable

Q.6 Given `int arr[3] = {1,2,3}; int *p = arr;`, what does `*(p+1)` return?

1
2
3
Garbage
Explanation - Pointer arithmetic: *(p+1) points to the second element of the array, which is 2.
Correct answer is: 2

Q.7 What is the difference between a pointer and a reference?

Pointer can be NULL; reference cannot
Reference can be NULL; pointer cannot
Pointers cannot be dereferenced
References require explicit memory allocation
Explanation - Pointers can point to nothing (NULL), but references must always refer to an object.
Correct answer is: Pointer can be NULL; reference cannot

Q.8 What is printed by the following code? ```cpp int x = 7; int *p = &x; *p += 3; std::cout << x; ```

7
3
10
Compiler error
Explanation - Dereferencing p and adding 3 changes the value of x from 7 to 10.
Correct answer is: 10

Q.9 Which of the following correctly declares a pointer to a pointer?

int **p;
int &p;
int p*;
int p**;
Explanation - int **p declares a pointer to a pointer to int.
Correct answer is: int **p;

Q.10 If `int x = 5; int *p = &x;`, what is the type of `p`?

int
int*
int&
int**
Explanation - p is a pointer to int, so its type is int*.
Correct answer is: int*

Q.11 What will happen if you dereference a NULL pointer?

Access the value 0
Access garbage value
Segmentation fault
It is allowed safely
Explanation - Dereferencing NULL leads to undefined behavior and usually causes a segmentation fault.
Correct answer is: Segmentation fault

Q.12 Which operator is used to access members of a structure through a pointer?

.
->
*
&
Explanation - The '->' operator is used to access structure members via a pointer.
Correct answer is: ->

Q.13 Can you have a reference to a pointer in C++?

Yes
No
Only for int pointers
Only for arrays
Explanation - You can have a reference to a pointer, e.g., int* &ref = ptr;
Correct answer is: Yes

Q.14 What does the following code print? ```cpp int x = 3; int y = 4; int *p = &x; p = &y; std::cout << *p; ```

3
4
Garbage
Compiler error
Explanation - Pointer p is reassigned to &y, so *p gives the value of y, which is 4.
Correct answer is: 4

Q.15 Which of the following statements about pointers is FALSE?

Pointers can be incremented
Pointers can store memory addresses
Pointers can point to functions
Pointers can store variable names
Explanation - Pointers store memory addresses, not variable names.
Correct answer is: Pointers can store variable names

Q.16 What will be the output? ```cpp int a = 5; int &r = a; r++; std::cout << a; ```

5
6
Undefined
Compiler error
Explanation - Incrementing the reference r also increments a, since r is an alias to a.
Correct answer is: 6

Q.17 Which of the following declares a constant pointer?

int * const p;
const int *p;
int const *p;
int *p const;
Explanation - int * const p is a pointer whose address cannot change, though the value pointed can be modified.
Correct answer is: int * const p;

Q.18 Which is the correct way to pass a variable by reference to a function?

void func(int x);
void func(int &x);
void func(int *x);
void func(&x);
Explanation - The '&' in the parameter indicates passing by reference.
Correct answer is: void func(int &x);

Q.19 What is the difference between `int * const p` and `const int * p`?

First is pointer to constant, second is constant pointer
First is constant pointer, second is pointer to constant
No difference
Both are invalid
Explanation - `int * const p` means pointer cannot change, but value can; `const int *p` means value cannot change but pointer can.
Correct answer is: First is constant pointer, second is pointer to constant

Q.20 What will the following code output? ```cpp int x = 2; int y = 3; int &r = x; r = y; std::cout << x; ```

2
3
Undefined
Compiler error
Explanation - Assigning y to reference r changes the value of x to 3.
Correct answer is: 3

Q.21 Can a pointer point to a reference variable?

Yes
No
Only in C
Only if initialized
Explanation - A reference is an alias, so a pointer can point to the variable that the reference refers to.
Correct answer is: Yes

Q.22 Which is correct to declare a pointer to a function returning int?

int *f();
int (*f)();
int f*();
int &f();
Explanation - int (*f)() declares f as a pointer to a function returning int.
Correct answer is: int (*f)();

Q.23 What does `sizeof(int*)` return?

Size of int
Size of pointer
Depends on compiler
Always 4 bytes
Explanation - sizeof(int*) returns the size of the pointer variable, which depends on architecture (32-bit or 64-bit).
Correct answer is: Size of pointer

Q.24 What is printed? ```cpp int x = 5; int *p = &x; int **q = &p; std::cout << **q; ```

5
Address of x
Address of p
Compiler error
Explanation - **q dereferences twice: first gives p, second gives value of x.
Correct answer is: 5

Q.25 Which of the following is true about NULL and nullptr?

NULL is integer 0; nullptr is type-safe pointer
NULL and nullptr are identical
nullptr is older than NULL
NULL is safer than nullptr
Explanation - In C++, nullptr is preferred because it is type-safe, unlike NULL which is an integer 0.
Correct answer is: NULL is integer 0; nullptr is type-safe pointer