Q. What is the output of the following C++ code?

Code:
#include<iostream>
  
using namespace std; 
  
class MyClass { 
    int &val; 
public: 
    MyClass (int &v) { val = v; } 
    int getValue() { return val; } 
}; 
  
int main() 
{ 
    int v = 10; 
    MyClass obj(v); 
    cout << obj.getValue() << " "; 
    v = 20; 
    cout << obj.getValue() << endl; 
    return 0; 
}
  • (A) 10
  • (B) 20
  • (C) 10 20
  • (D) Compilation error
πŸ’¬ Discuss
βœ… Correct Answer: (D) Compilation error

You must be Logged in to update hint/solution

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
230
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Dharmendra Sir
Publisher
πŸ“ˆ
96%
Success Rate