References(C++ Programming ) Questions and Answers
Question 1.
What will be the output of the following program?
#include
enum xyz
{
a, b, c
};
int main()
{
int x = a, y = b, z = c;
int &p = x, &q = y, &r = z;
p = z;
p = ++q;
q = ++p;
z = ++q + p++;
cout
- 2 3 6
- 4 4 7
- 4 5 8
- 3 4 6
Explanation:-
Answer: Option B. -> 4 4 7
Explanation:-
Answer: Option D. -> It will result in a compile time error.
Question 3.
Which of the following statement is correct about the program given below?
#include
enum xyz
{
a, b, c
};
int main()
{
int x = a, y = b, z = c;
int &p = x, &q = y, &r = z;
p = ++x;
q = ++y;
r = ++c;
cout
- The program will print the output 1 2 3.
- The program will print the output 2 3 4.
- The program will print the output 0 1 2.
- It will result in a compile time error.
Explanation:-
Answer: Option D. -> It will result in a compile time error.
Explanation:-
Answer: Option E. -> It will result in a compile time error.
Explanation:-
Answer: Option C. -> The program will print output 4 8.
Explanation:-
Answer: Option A. -> we can not create the array of reference.None.
Explanation:-
Answer: Option A. -> Hello worldIn this program we used the concept of constant casting to cast the variable and printing it.
Output:
$ g++ ref2.cpp
$ a.out
Hello world
Question 8.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int a = 9;
6.
int & aref = a;
7.
a++;
8.
cout
- 9
- 10
- error
- 11
Explanation:-
Answer: Option B. -> 10The value is declared and it is post incremented, so it's value is 10.
$ g++ ref1.cpp
$ a.out
10
Explanation:-
Answer: Option B. -> Alternate name for the variableBecause we are pointing memory address using temp variable
Question 10.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
void swap(int &a, int &b);
4.
int main()
5.
{
6.
int a = 5, b = 10;
7.
swap(a, b);
8.
cout
- In swap 105 In main 105
- In swap 105 In main 510
- In swap 510 In main 105
- none of the mentioned
Explanation:-
Answer: Option A. -> In swap 105 In main 105As we are calling by reference the values in the address also changed. So the main and swap
values also changed.
Output:
$ g++ ref.cpp
$ a.out
In swap 105 In main 105