Q:

C++ Reference Variable | Find output programs | Set 1

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Reference Variable (set 1).

Program 1:

#include <iostream>
using namespace std;

int main()
{
    int A = 10;
    int& A1 = A;

    int B = 20, &B1 = B;
    int* ptr;

    ptr = &B1;
    ptr++;

    ptr = &A1;
    ptr++;

    cout << A << " " << B << " ";

    return 0;
}

Program 2:

#include <iostream>
using namespace std;

int main()
{
    int A = 10;
    int& A1 = A;

    int B = 20, &B1 = B;
    int C = 0;

    C = B1 & A + A1 & B;

    cout << C;

    return 0;
}

Program 3:

#include <iostream>
using namespace std;

int main()
{
    int A = 10;

    int& A1 = A;
    int& B1 = A1;

    int C = 0;

    C = sizeof((A, A1, B1)) + 5;

    cout << A << " " << A1 << " " << B1 << " " << C;

    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

10 20

Explanation:

Here, we took two variables A and B, created the reference variable for both A1 and B1, and declared an integer pointer, initially, it points to B1, it means it contains the address of B. then we increased the pointer (It will not increase the value).

After that, pointer ptr is pointing to A1, it will contain the address of A1 and A, and will increase the pointer in the same way, values of B or B1 will not increase.

Then the final values of A and B remain the same. then "10 20" will be printed on the console screen.

Answer Program 2:

Output:

20

Explanation:

Here, we took two variables A and B, and created the reference variable for both A1 and B1. The initial value of A and B are 10 and 20 respectively.

We declared one more variable called C.

Now evaluate the below expression,

C = B1 & A + A1 & B;
C = 20 & 10 + 10 & B;

Here we used two operators + and &, according to operator priority table, + plus will execute before bitwise AND (&). Thus,

C = 20 & 20 & 20;

Now, perform bitwise AND operation, the binary value of 20 is "0001 0100".

0001 0100
0001 0100
=========
0001 0100

The result of the Bitwise AND operation will be 20. Then,

C = 20 & 20;
C = 20;

 

Then the final output will be 20.

Answer Program 3:

Output:

10 10 10 9

Explanation:

Here, we declared a variable with initial value 10, and then we created a reference variable of A that is A1 and we created one more reference variable B1 of A1, so we can say that A1 and B1 are alias name of variable A.

We created one more variable C with initial value 0.

Now, evaluate the expression,

C = sizeof((A,A1,B1)) +5;

Now, first we evaluate (A, A1, B1), here all three have the same value because A1 and B1 are alias of A.

(10, 10 , 10)

If we evaluate the above expression it will return last-most 10.

Now,

C= sizeof(10) + 5;

As we already mentioned we are using a 32-bit compiler then the size of any integer will be 4 bytes.

C = 4+5;
C = 9;

 

Then the final result will be: 10 10 10 9

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Reference Variable | Find output programs | Se... >>
<< C++ Manipulators | Find output programs | Set 2...