Output of C++ programs | Set 38 (Pointers)

Prerequisite : Pointers in C/ C++
QUE.1 What would be printed from the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int x[5] = { 1, 2, 3, 4, 5 };
    // p points to array x
    int* p = x;
    int i;
    // exchange values using pointer
    for (i = 0; i < 2; i++) {
        int temp = *(p + i);
        *(p + i) = *(p + 4 - i);
        *(p + 4 - i) = temp;
    }
    // output the array x
    for (i = 0; i < 5; i++)
        cout << x[i] << " ";
    return 0;
}

chevron_right


a) 5 4 3 2 1
b) 1 2 3 4 5
c)Address of the elements
d) Can’t say

Answer: a

Explanation : p points to array x then exchange values using pointer in the for loop to after exchanging value the last loop print the value

QUE.2 What would be printed from the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
void fun(int x)
{
    x = 30;
}
  
int main()
{
    int y = 20;
    fun(y);
    printf("%d", y);
    return 0;
}

chevron_right


a) 10
b) 20
c) 30
d) Run Time Error

Answer: b 

Explanation: Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using the function fun(). So how do we modify the value of a local variable of a function inside another function. Pointer is the solution to such problems. Using pointers, we can modify a local variable of a function inside another function. See the next question. Note that everything is passed by value in C. We only get the effect of pass by reference using pointers.

QUE.3 What would be printed from the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
    int arri[] = { 1, 2, 3 };
    int* ptri = arri;
  
    char arrc[] = { 1, 2, 3 };
    char* ptrc = arrc;
  
    printf("sizeof arri[] = %d ", sizeof(arri));
    printf("sizeof ptri = %d ", sizeof(ptri));
  
    printf("sizeof arrc[] = %d ", sizeof(arrc));
    printf("sizeof ptrc = %d ", sizeof(ptrc));
  
    return 0;
}

chevron_right


a) sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4
b) sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1
c) sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1
d) sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4

Answer: d 

Explanation: Size of an array is number of elements multiplied by the type of element, that is why we get sizeof arri as 12 and sizeof arrc as 3. Size of a pointer is fixed for a compiler. All pointer types take same number of bytes for a compiler. That is why we get 4 for both ptri and ptrc.

QUE.4 What is the output of the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int* reverse(int*);
int main()
{
    int x[5] = { 1, 2, 3, 4, 5 };
    int i, *p;
  
    // exchange values
    p = reverse(x);
  
    // output the array x
    for (i = 0; i < 5; i++)
        cout << *(p + i) << " ";
  
    return 0;
}
  
int* reverse(int* p)
{
  
    int i;
    // exchange values
    for (i = 0; i < 2; i++) {
        int temp = *(p + i);
        *(p + i) = *(p + 4 - i);
        *(p + 4 - i) = temp;
    }
    return p;
}

chevron_right


a) 1 2 3 4 5
b) Address of the elements
c) run time error
d) None of the mentioned

Answer: d 

Explanation:
p points to array x then exchange values using pointer in the for loop to after exchanging value the last loop print the value but in this program revers function getting the address of the array and and address of the address p the revers it but output is not mention output is 5 4 3 2 1

QUE.5 What is the output of the following C++ program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
int main()
{
    int x = 10, y = 15, i;
    for (i = 0; i < 1; i++) {
        int x = 20, y = 15;
    }
    int mul = x * y;
    cout << x << "*" << y << " = " << mul;
    return 0;
}

chevron_right


a) 10*15 = 150
b) 20*15 = 300
c) 15*15 = 225
d) None of the mentioned

Answer: a 

Explanation: Simple multiply but when it goes to loop value of x and y are not change because they are defined in other block.

This article is contributed by Ajay Puri. 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


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.