Home » C++ Programming » Questions and Answers » Question
  1. What is the output of this program?
    #include <iostream>
    #include <new>
    using namespace std;
    int main ()
    {
    int k, num;
    int * ptr;
    num = 3;
    ptr= new (nothrow) int[num];
    if (ptr == 0)
    cout << "Error: memory could not be allocated";
    else
    {
    for (k=0; k {
    ptr[num] = 7;
    }
    for (k = 0; k < num; k++)
    cout << ptr[num] << " ";
    delete[] ptr;
    }
    return 0;
    }
    1. 3
    2. 7
    3. 7 7
    4. 7 7 7
    5. None of these
Correct Option: D

As we had given k value as 3, It will print the 7 for three times.



Your comments will be displayed only after manual approval.