A ______ is considered as a special type of pointer which contains address of a memory location.
- AFunction
- BStructure
- CReference
- DClass
A reference is an alias implemented like a constant pointer that is automatically dereferenced.
MCQs, short and long questions from lecture 30.Lecture 30 ki MCQs, short aur long sawal.
9 MCQs · 7 short questions · 9 MCQs from past papers9 MCQs · 7 short sawal · 9 MCQs past papers se
A ______ is considered as a special type of pointer which contains address of a memory location.
A reference is an alias implemented like a constant pointer that is automatically dereferenced.
Which of the following will be the correct function call for function prototype given below? int func (int &);
A reference parameter is passed the variable itself, so the call is func(num); &num would pass an int* and not compile.
If there is a symbol (& sign) used with the variable name followed by data type then it refers to _____ and if & is being used with variable name then it refers to _____.
In a declaration (int &r) & makes a reference; in an expression (&x) it takes the address.
We can do arithmetic with references like pointers.
A reference is an alias; arithmetic on it changes the referred value, not an address.
Which of the following function calling mechanism is true for the function prototype given below? float func(float &);
float & declares a reference parameter.
In functions that return reference, use __________variables.
Returning a reference to a local would dangle; global or static variables outlive the call.
To avoid dangling reference, always return the reference of a local variable from a function.
Returning a reference to a local variable creates a dangling reference.
We cannot increment ____________.
A reference is a fixed alias; ++ on it changes the referred variable, not the reference itself.
We can _____________references.
A reference is bound once at initialization and cannot be reseated or moved.
No MCQs match this filter.Is filter mein koi MCQ nahi.
How can we overcome the issue of dangling reference in a program?
A dangling reference refers to memory or an object that no longer exists (e.g. a reference to a local variable returned from a function, or to deleted memory). Avoid it by never returning a reference (or pointer) to a local/automatic variable, returning by value or referring only to objects whose lifetime is longer than the reference (static, global or caller-owned objects), and by not using references/pointers to memory after it has been deleted (set pointers to NULL after delete).
What will be the output of the following code snippet? int x = 10; int &y = x; y+=5; cout << x;
15. y is a reference (alias) to x, so y += 5 changes x to 15.
Identify and correct the error in the given code segment. main() { int val = 30; int &ref = val; &ref = &ref++; }
Error: &ref = &ref++; is invalid. &ref is the address of val, not a modifiable variable, so it cannot be assigned to; ref++ gives a temporary value whose address cannot be taken; and a reference cannot be made to refer to another variable after initialization. Correction: use the reference like a normal variable, e.g. int main() { int val = 30; int &ref = val; ref++; // val becomes 31 return 0; }
Suppose we want to pass a variable by reference to a function and we also don't want the function to change the original value of that variable. How can we achieve this in C++?
Pass it as a constant reference, for example void show(const int &x);. The variable is not copied (it is passed by reference), but because the reference is const the compiler gives an error if the function tries to modify it.
Is it possible to define two functions as given below? Justify your answer. func(int x, int y) func(int &x, int &y)
Not in a useful way. The two declarations have different parameter types (int and int&), so the compiler accepts them, but they cannot be called with variables: for int a, b; the call func(a, b) matches both functions equally well (passing by value and binding a reference are the same rank), so the compiler reports an ambiguous call error. Only a call with literals such as func(2, 3) would compile (it chooses the value version). So these two should not be used as overloaded functions; overloads must differ in a way that lets the call be resolved. (Both also need a return type, e.g. void.)
What is the use of reference data type?
A reference is an alias (another name) for an existing variable, declared as int &ref = x;. It is used mainly for call by reference: a function can modify the caller's variables without pointer syntax, and large objects can be passed (e.g. as const references) without copying. It is also used to return values by reference (e.g. in operator overloading, cout chaining).
If we want to send the data by reference and don't want that original data should be affected then what can we do to prevent any change?
Pass it as a const reference, e.g. void show(const Student &s);. The data is not copied (efficient), but the compiler gives an error if the function tries to modify it, so the original stays unchanged.
No long questions added yet.Abhi koi long sawal shamil nahi.