Pointer
6.What will happen if you dereference a null pointer in C++?
-
A. It will cause a segmentation fault or runtime error
-
B. It will return a random value
-
C. It will return 0
-
D. It will return NULL
It will cause a segmentation fault or runtime error
It will cause a segmentation fault or runtime error
7.What is the purpose of the delete keyword in C++ when used with pointers?
-
A. It deallocates memory allocated by new
-
B. It initializes a pointer to NULL
-
C. It checks if the pointer is valid
-
D. It allocates memory for the pointer
It deallocates memory allocated by new
It deallocates memory allocated by new
8. Which of the following pointers should be used when passing an argument by reference to a function in C++?
-
A. Pointer to a constant
-
B. Void pointer
-
C. Pointer to a variable
-
D. Reference pointer
Pointer to a variable
Pointer to a variable
9.How do you allocate memory dynamically for an array of 5 integers in C++?
-
A. int arr = new int[5];
-
B. int* arr = new int(5);
-
C. int* arr = new int[5];
-
D. int* arr = malloc(5 * sizeof(int));
int* arr = new int[5];
int* arr = new int[5];
10.What does the following code do in C++?
int x = 5;
int *ptr = &x;
ptr++;
cout << *ptr;
-
A. Outputs 5
-
B. Outputs 6
-
C. Outputs the address of x
-
D. Outputs an undefined value (likely an invalid address)
Outputs an undefined value (likely an invalid address)
Outputs an undefined value (likely an invalid address)