C++ Reference Variable Aptitude Questions and Answers

C++ Reference Variable Aptitude: This section contains C++ Reference Variable Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 21, 2021

1) There are the following statements that are given below, which of them are correct about reference variables in C++?

  1. Reference variables are used to provide the new name to the existing variables.
  2. The size of the reference variable is fixed, any type of reference variable occupies 4 bytes in memory.
  3. If we made any changes in the reference variable, it will also reflect in the original variable.
  4. The reference variables do not occupy space in memory.

Options:

  1. A and B
  2. A and C
  3. A, B, and C
  4. A, C, and D

2) Which of the following symbol is used to create a reference variable in C++?

  1. $
  2. *
  3. &
  4. @

3) Can we create multiple reference variables of an existing variable?

  1. Yes
  2. No

4) Can we bind a reference variable with multiple existing variables?

  1. Yes
  2. No

5) Which of following is correct syntax, if we want to create a reference variable of an existing integer variable?

  1. int a=10; int r = &a;
  2. int a=10; int &r = a;
  3. int a=10; int &r = &a;
  4. int a=10; int &r; r = a;

6) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    int& r1 = a;
    int& r2 = b;

    cout << r1 << " " << r2 << endl;

    return 0;
}

Options:

  1. 10 20
  2. 20 10
  3. Compile-time error
  4. Runtime error

7) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    int& r1 = a;
    int& r2 = b;

    r1 = 30;
    r2 = 40;

    cout << b << " " << a << endl;

    return 0;
}

Options:

  1. 30 40
  2. 40 30
  3. Compile-time error
  4. Runtime error

8) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    int& r1 = a;

    r1 = b;

    cout << r1 << endl;

    return 0;
}

Options:

  1. 10
  2. 20
  3. Compile-time error
  4. Runtime error

9) What is the correct output of the given code?

#include <iostream>
using namespace std;

int main()
{
	int a=10;
	int b=20;
	
	int &r1 = a;

	&r1 = b;
	
	r1 =30;
	
	cout << a <<" "<<b<< endl;
			 
	return 0;
}

Options:

  1. 10 30
  2. 30 20
  3. Compile-time error
  4. Runtime error

10) What is the correct output of the given code?

#include <iostream>
using namespace std;

int main()
{
    int a = 10;

    int* p;

    p = &a;

    int& r = p;

    r = 20;

    cout << a << " " << *p << " " << r << endl;

    return 0;
}

Options:

  1. 20 20 20
  2. 20 20
  3. Compile-time error
  4. Runtime error

11) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int main()
{
    int a = 10;

    int* p;

    p = &a;

    int& r = *p;

    r = 20;

    cout << a << " " << *p << " " << r << endl;

    return 0;
}

Options:

  1. 20 20 20
  2. 20 20
  3. Compile-time error
  4. Runtime error

12) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int main()
{
    short a = 10;

    short* p;

    p = &a;

    long& r = *p;

    r = 20;

    cout << a << " " << *p << " " << r << endl;

    return 0;
}

Options:

  1. 20 20 20
  2. 20 20
  3. Compile-time error
  4. Runtime error

13) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

int main()
{
    short a = 10;

    short& r = a;

    if (&r == &a)
        cout << "Same address";
    else
        cout << "Different address";

    return 0;
}

Options:

  1. Same address
  2. Different address
  3. Compile-time error
  4. Runtime error

14) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

void fun(short& X)
{
    X = 20;
}

int main()
{
    short a = 10;

    fun(a);

    cout << a << endl;

    return 0;
}

Options:

  1. 10
  2. 20
  3. Compile-time error
  4. Runtime error

15) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

void fun(static short& X)
{
    X = 20;
}

int main()
{
    static short a = 10;

    fun(a);

    cout << a << endl;

    return 0;
}

Options:

  1. 10
  2. 20
  3. Compile-time error
  4. Runtime error

16) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

void fun(short& X)
{
    X = 20;
}

int main()
{
    static short a = 10;

    fun(a);

    cout << a << endl;

    return 0;
}

Options:

  1. 10
  2. 20
  3. Compile-time error
  4. Runtime error






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.