Skip to content
Related Articles

Related Articles

Output of C++ programs | Set 47 (Pointers)
  • Difficulty Level : Easy
  • Last Updated : 15 Sep, 2017

Prerequisite : Pointers in C/C++

1. 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;
}

Options:
a. 32, A
b. 32, a
c. 129, a
d. 129, A

Answer: c. 129, a

Explanation: The “ptr” variable is a pointer which holds the address of variable “a”. And “*ptr” returns the value of “a” variable. “cho” is a reference variable to “ch”. So any change made to “cho” will be reflected to “ch”. As such, when “cho” is increased by 32, it adds to the ASCII value of “A”(which is 65), and this results to 97 which is the ASCII value of “a”(from the alphabet). So this “a” gets stored in “ch”. As for when “*ptr” is incremented by “ch”, it gives value 97+32=129.

2. What will be the output of the following program?






#include <iostream>
using namespace std;
  
int main()
{
    const int i = 20;
    const int* const ptr = &i;
    (*ptr)++;
    int j = 15;
    ptr = &j;
    cout << i;
    return 0;
}

Options:
a. 20
b. 21
c. 15
d. Compile error

Answer: d. Compile error

Explanation:Here “ptr” has been defined as a constant pointer that holds address of a constant integer “i”. So, neither can the value of “ptr” be changed, nor can the value held by it can be changed. Thus the lines “(*ptr)++” and “ptr=&j” are invalid, as they are trying to modify the variable i’s content and the value of the pointer respectively. This gives an error.

3. What will be the output of the following program?




#include <iostream>
using namespace std;
int main()
{
    int num[5];
    int* p;
    p = num;
    *p = 10;
    p++;
    *p = 20;
    p = &num[2];
    *p = 30;
    p = num + 3;
    *p = 40;
    p = num;
    *(p + 4) = 50;
    for (int i = 0; i < 5; i++)
        cout << num[i] << ", ";
    return 0;
}

Options:
a. 10, 20, 30, 40, 50
b. 10, 20, 30, 40, 50,
c. compile error
d. runtime error

Answer: b. 10, 20, 30, 40, 50, 

Explanation: Its simple. The values are being assigned to the array, and de-referenced to print them.

4. What will be the output of the following program?




#include <iostream>
using namespace std;
int main()
{
    int arr[] = { 4, 5, 6, 7 };
    int* p = (arr + 1);
    cout << *arr + 10;
    return 0;
}

Options:
a. 12
b. 15
c. 14
d. error

Answer: c. 14

Explanation : ‘arr’ works as a pointer to the array. So ‘arr’ points to the first location in the array. Similarly ‘arr + 1’ points to the second location in the array, and so on. Therefore, *arr accesses the first value in the array, which is 4, and adds it to 10. This gives 14 as output.

5. What will be the output of the following program?




#include <iostream>
using namespace std;
int main()
{
    int a = 10, *pa, &ra;
    pa = &a;
    ra = a;
    cout << "a=" << ra;
    return 0;
}

Options:
a. 10
b. no output
c. compile error
d. runtime error

Answer : c. compile error

Explanation: Reference variables are different from pointers. Pointer can be initialized as well as assigned, but references can only be initialized. Here, ‘ra’ reference variable is being assigned the address of ‘a’ instead of initializing it when it is declared. This throws a compile error – ‘ra’ declared as reference but not initialized.

This article is contributed by Nihar Ranjan Sarkar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :