Prerequisite: C++ Pointers
The best way you can learn C++ Pointers concepts is by hands-on practice. This C++ Pointers Quiz is perfect for both beginners and experienced programmers who want to master pointers in C++. Here you'll find questions that covers everything from basic pointer concepts to more advanced topics like dynamic memory allocation. Each question comes with a clear explanation, helping you understand not just the correct answer but why it's correct.
Ready to boost your C++ Pointers skills? Let's get started!
Question 1
What is a pointer in C++?
A variable that stores a data type
A function that points to a variable
A variable that stores the memory address of another variable
A reference variable
Question 2
Which of the following is the correct syntax to declare a pointer?
int ptr;
int &ptr;
ptr *int
int *ptr
Question 3
What is the output of the following C++ code?
#include<iostream>
using namespace std;
int main() {
int var = 5;
int *ptr = &var;
cout << *ptr;
return 0;
}
0
5
Address of var
Garbage value
Question 4
How do you dynamically allocate memory for an array in C++ using pointers?
int arr[10];
int *arr = new int[10];
int *arr = malloc(10 * sizeof(int));
int *arr = int[10];
Question 5
What does the following C++ code do?
#include<iostream>
using namespace std;
int main() {
int *ptr = NULL;
ptr = new int;
*ptr = 7;
cout << *ptr;
delete ptr;
return 0;
}
Outputs 0
Outputs 7
Causes a compile-time error
Causes a segmentation fault
Question 6
What happens if you dereference a NULL pointer in C++?
Outputs 0
Undefined behavior
Causes a compile-time error
Outputs NULL
Question 7
How can you release the memory allocated by a pointer in C++?
delete pointer;
free(pointer);
release(pointer);
remove(pointer);
Question 8
Which operator is used to access the value stored at the address stored in a pointer variable?
&
->
*
::
Question 9
What is the correct way to declare a constant pointer to an integer in C++?
int const *ptr;
const int * const ptr;
const int *ptr;
int * const ptr;
Question 10
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
void updateValue(int *ptr) {
*ptr = 20;
}
int main() {
int var = 10;
updateValue(&var);
cout << var;
return 0;
}
10
0
20
Garbage value
There are 25 questions to complete.