Open In App

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

Last Updated : 11 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

C++




#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;
}


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?  

C++




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


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?  

C++




#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;
}


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?  

C++




#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;
}


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 reverse function getting the address of the array and and address of the address p the reverse it but output is not mention output is 5 4 3 2 1

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

C++





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.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads