V.Vidhya Logo

V.Vidhya

Pointers


Q1.

Create a int variable initialised with some value, then print -

Expected Output:

// int variable initialized with value 5

value of var (using variable identifier) : 5
address of var (using address-of operator) : 0027FEA0
value at the address of var (using dereference operator) : 5

Q2.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q3.

Declare a int variable initialised with some value, then create a pointer to that variable, now print -

Expected Output:

// int variable initialized with value 5 
// a pointer declared pointing to that variable

value of var : 5
address of var (using address-of operator) : 0027FEA0
value of ptr : 0027FEA0
value at the address pointed by the pointer : 5

Q4.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q5.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.

    #include <iostream>

    int main()
    {
        int x = 5;
        int& ref = x;
        int* ptr = &x;

        std::cout << x << " " << ref << " " << *ptr << '\n';

        x = 6;
        std::cout << x << " " << ref << " " << *ptr << '\n';

        ref = 7;
        std::cout << x << " " << ref << " " << *ptr << '\n';

        *ptr = 8;
        std::cout << x << " " << ref << " " << *ptr << '\n';

        return 0;
    }

Q6.

Analyze the following program:

#include <iostream>

int main()
{
    int x = 5;
    int& ref = x;
    int* ptr = &x;

    std::cout << "x: " << x << '\n';
    std::cout << "&x: " << &x << '\n';
    std::cout << "*x: " << *x << '\n';

    std::cout << "ref: " << ref << '\n';
    std::cout << "&ref: " << &ref << '\n';
    std::cout << "*ref: " << *ref << '\n';

    std::cout << "ptr: " << ptr << '\n';
    std::cout << "*ptr: " << *ptr << '\n';
    std::cout << "&ptr: " << &ptr << '\n';

    return 0;
}

Evaluate each cout statement. Identify whether they are valid and explain what each of them represents.

You have to predict the output result without running/executing the code.


Q7. (Adv.)

Consider the following program:

#include <iostream>

void printAddresses(int val, int& ref)
{
    std::cout << "The address of the value parameter is: " << &val << '\n';
    std::cout << "The address of the reference parameter is: " << &ref << '\n';
}

int main()
{
    int x = 5;
    std::cout << "The address of x is: " << &x << '\n';
    printAddresses(x, x);

    return 0;
}

Which of the following statement is correct for the above program?

You have to predict the output result without running/executing the code.

  1. The address value for all x, val, ref will be same.
  2. The address value for x and ref will be same but for val will be different.
  3. The address value for all x, val, ref will be different.
  4. This cannot be predetermined.

Q8.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q9. (Adv.)

Examine the following program:

#include <iostream>

int main()
{   
    int x = 5;
    int* ptr;
    
    std::cout << *ptr;
    
    if (x > 5)
    {
        int y = 6;
        ptr = &y;
        
        std::cout << *ptr;
    }
    
    std::cout << *ptr;
    
    ptr = nullptr;
    
    std::cout << *ptr;

    return 0;
}

Predict the program’s behavior upon execution.

You have to predict the output result without running/executing the code.

If you identify any issues, explain what causes them and propose how you would correct them.


Q10.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q11.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q12.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Prev Post
References
Next Post
Arrays