Dynamic Memory Management & Pointers

Last Updated :
Discuss
Comments

Question 1

How to create a dynamic array of pointers (to integers) of size 10 using new in C++? Hint: We can create a non-dynamic array using int *arr[10]
  • int *arr = new int *[10];
  • int **arr = new int *[10];
  • int *arr = new int [10];
  • Not Possible

Question 2

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 3

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

  • int ptr;

  • int &ptr;

  • ptr *int;

  • int *ptr;

Question 4

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;
}
  • 0

  • 5

  • Address of var

  • Garbage value

Question 5

What is dynamic memory allocation in C++?

  • Management of memory automatically

  • Allocation of memory at compile time

  • Allocation of memory during runtime

  • Increasing allocated memory

Question 6

Which of the following is not a valid way to allocate memory dynamically for an integer in C++?

  • int* ptr = new int;

  • int* ptr = new;

  • int* ptr = new int(10);

  • int* ptr = new int[1];

Question 7

What of the following is the correct way to use 'new' and 'delete' for dynamic memory allocation of an object in C++ ?

  • MyClass* obj = new MyClass(); delete obj;

  • MyClass obj = new MyClass(); delete obj;

  • MyClass* obj = new MyClass(); delete[] obj;

  • MyClass* obj = new MyClass[1]; delete obj;

Question 8

What is the initial value of dynamically allocated memory in C++?

  • 0

  • 1

  • Null

  • Indeterminate

Question 9

What is the effect of assigning a dynamically allocated pointer to nullptr after deletion?

  • Causes a memory leak

  • Increases memory usage

  • Throws an exception

  • Prevents dangling pointer issues

Question 10

What will be the output of the following program?

#include <iostream>
using namespace std;
int main()
{
int a = 32, *ptr = &a;
char ch = 'A', &cho = ch;
cho += a;
*ptr += ch;
cout << a << ", " << ch << endl;
return 0;
}
  • 32, A

  • 32, a

  • 129, a

  • 129, A

Tags:

There are 12 questions to complete.

Take a part in the ongoing discussion