Quiz on C++ Pointers

Last Updated :
Discuss
Comments

Question 1

What is a pointer in C++?

  • A

    A variable that stores a data type

  • B

    A function that points to a variable

  • C

    A variable that stores the memory address of another variable

  • D

    A reference variable

Question 2

Which of the following is the correct syntax to declare a pointer?

  • A

    int ptr;

  • B

    int &ptr;

  • C

    ptr *int;

  • D

    int *ptr;

Question 3

What is the output of the following C++ code?

C++
#include<iostream>
using namespace std;

int main() {
    int var = 5;
    int *ptr = &var;
    cout << *ptr;
    return 0;
}
  • A

    0

  • B

    5

  • C

    Address of var

  • D

    Garbage value

Question 4

How do you dynamically allocate memory for an array in C++ using pointers?

  • A

    int arr[10];

  • B

    int *arr = new int[10];

  • C

    int *arr = malloc(10 * sizeof(int));

  • D

    int *arr = int[10];

Question 5

What does the following C++ code do?

C++
#include<iostream>
using namespace std;

int main() {
    int *ptr = NULL;
    ptr = new int;
    *ptr = 7;
    cout << *ptr;
    delete ptr;
    return 0;
}
  • A

    Outputs 0

  • B

    Outputs 7

  • C

    Causes a compile-time error

  • D

    Causes a segmentation fault

Question 6

What happens if you dereference a NULL pointer in C++?

  • A

    Outputs 0

  • B

    Undefined behavior

  • C

    Causes a compile-time error

  • D

    Outputs NULL

Question 7

How to release the memory allocated by a pointer in C++?

  • A

    delete pointer;

  • B

    free(pointer);

  • C

    release(pointer);

  • D

    remove(pointer);

Question 8

Which operator is used to access the value stored at the address stored in a pointer variable?

  • A

    &

  • B

    ->

  • C

    *

  • D

    ::

Question 9

What is the correct way to declare a constant pointer to an integer in C++?

  • A

    int const *ptr;

  • B

    const int * const ptr;

  • C

    const int *ptr;

  • D

    int * const ptr;

Question 10

What will be the output of the following C++ code?

C++
#include<iostream>
using namespace std;

void updateValue(int *ptr) {
    *ptr = 20;
}

int main() {
    int var = 10;
    updateValue(&var);
    cout << var;
    return 0;
}
  • A

    10

  • B

    0

  • C

    20

  • D

    Garbage value

There are 25 questions to complete.

Take a part in the ongoing discussion