Output of following C++ program?
#include<iostream> using namespace std; int main() { int x = 10; int& ref = x; ref = 20; cout << "x = " << x << endl ; x = 30; cout << "ref = " << ref << endl; return 0; } |
chevron_right
filter_none
(A)
x = 20 ref = 30
(B)
x = 20 ref = 20
(C)
x = 10 ref = 30
(D)
x = 30 ref = 30
Answer: (A)
Explanation: ref is an alias of x, so if we change either of them, we can see the change in other as well.
Quiz of this Question
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- lvalues references and rvalues references in C++ with Examples
- C++ | References | Question 1
- C++ | References | Question 6
- C++ | References | Question 6
- C++ | References | Question 4
- C++ | References | Question 5
- Can references refer to invalid location in C++?
- References in C++
- Default Assignment Operator and References
- Pointers vs References in C++
- A comma operator question
- C | Input and Output | Question 13
- C | Input and Output | Question 2
- C | Input and Output | Question 3
- C | Operators | Question 2
- C | Operators | Question 3
- C | Operators | Question 4
- C | Input and Output | Question 4
- C | Loops & Control Structure | Question 1
- C | Arrays | Question 1

