1 Crore+ students have signed up on EduRev. Have you? |
What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char const *argv[])
{
int &q = 5;
cout<<q;
return 0;
}
References require are other names for variables not for a constant literal. No such assignment are allowed in C++.
Reference is a thing which points to the valid memory address, so it can’t be redesigned.
What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char const *argv[])
{
int a = 5;
int &p = a;
cout<<p;
return 0;
}
In this program, every thing is correct so the program runs perfectly and prints the 5 as output.
C++ allows references to refer to a constant value by making constant references. For example:
const int a = 5;
const int &ref = a;
is an example of that.
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void swap(int &a, int &b);
int main()
{
int a = 5, b = 10;
swap(a, b);
cout << "In main " << a << b;
return 0;
}
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << "In swap " << a << b;
}
As the function is called by reference i.e. all the changes are done directly into the memories of a and b. Therefore changes made to a and b in swap function is reflected back to main function. Hence the values of a and b in swap as well as in main function is changed.
Output:
$ g++ ref.cpp
$ a.out
In swap 105 In main 105
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
A function returning value by reference can be used as lvalue i.e. it can be used on the left side of an expression. Here when we are doing fun() = 30 then we are changing the value of x(i.e. value returning) to and as x is static therefore it will not be initialized again so the value of x becomes 30 hence the output is 30.
What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char const *argv[])
{
int a = 5;
int *p = &a;
int *(&q) = p;
cout<<q;
return 0;
}
The program is correct so the the program runs perfectly. It is way to assign pointers to references. The program prints the address of a because it is an alias for pointer p and pointer p stores the address of a therefore answer is address of a.
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 9;
int & aref = a;
a++;
cout << "The value of a is " << aref;
return 0;
}
The value is declared and it isincrementedrement, so it’s value is 10.
$ g++ ref1.cpp
$ a.out
10
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << x << endl ;
x = 30;
cout << ref << endl;
return 0;
}
As we know references are alias for a variable so the value of a variable can be changed using alias hence both ref and x are same therefore changing the value of one effects the value of other.
References and variable it is referring to shares the same address. References do not consume extra address. References do not store the address of other variables. No dereferencing operator required while using references. References are not available in C++.
Identify the correct sentence regarding inequality between reference and pointer.
It is not allowed in C++ to make an array of references. To test check following array:
int &arr[] = {&a, &b, &c};
This will give an error.
A variable can have multiple references as references are nothing just another name for a variable hence a variable can more than one references.
References are an alternative name for an already defined variable. They are different from pointers.
If it can be assigned with a null value means, it is a copy of the pointer.
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int f(int &x, int c)
{
c = c - 1;
if (c == 0) return 1;
x = x + 1;
return f(x, c) * x;
}
int main(int argc, char const *argv[])
{
int a = 4;
cout<<f(a,a);
return 0;
}
In this program as one parametere is passed by value and other is passed by reference so after 4 calls when c == 0, then the value of x = 7 and as x is passed by reference so all the changes will be reflected back in all the previous calls hence the answer 1*7*7*7 = 343.
15 videos|20 docs|13 tests
|
Use Code STAYHOME200 and get INR 200 additional OFF
|
Use Coupon Code |
15 videos|20 docs|13 tests
|
|
|
|
|
|
|
|
|