30 Lecture
CS201
Midterm & Final Term Short Notes
Reference data type
In C++, a reference is a data type that refers to another variable. It is used to create an alias or alternate name for an existing variable, allowing the original variable to be accessed or modified using a different name. Unlike pointers, refe
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is a reference data type in C++? a) It is a variable that stores the memory address of another variable b) It is a variable that refers to another variable by name c) It is a variable that can be assigned a null value d) It is a variable that cannot be passed to a function
Answer: b
- What is the difference between a pointer and a reference in C++? a) Pointers can be reassigned to point to different variables, while references cannot. b) References can be null, while pointers cannot. c) Pointers are used to pass variables by reference, while references are used to pass variables by value. d) There is no difference between a pointer and a reference in C++.
Answer: a
- Can a reference be declared without being initialized? a) Yes, a reference can be declared without being initialized. b) No, a reference must be initialized when it is declared. c) It depends on the data type of the reference. d) It depends on the scope of the reference.
Answer: b
- What is the benefit of passing parameters by reference in a function? a) It saves memory by not creating a copy of the variable. b) It allows the function to modify the original variable. c) It makes the code more readable. d) It makes the code faster.
Answer: b
- What happens if a reference is assigned to a new variable? a) The original variable is deleted. b) The new variable becomes an alias for the original variable. c) A new copy of the original variable is created. d) The program crashes.
Answer: b
- Can a reference be used as a return type for a function? a) Yes, a reference can be used as a return type for a function. b) No, a reference cannot be used as a return type for a function. c) It depends on the data type of the reference. d) It depends on the scope of the reference.
Answer: a
- What is the syntax for declaring a reference variable in C++? a) int& x; b) int* x; c) int x&; d) int& x = y;
Answer: d
- Can a reference refer to a const variable? a) Yes, a reference can refer to a const variable. b) No, a reference cannot refer to a const variable. c) It depends on the data type of the reference. d) It depends on the scope of the reference.
Answer: a
- What is the difference between a const reference and a non-const reference? a) A const reference cannot be modified, while a non-const reference can. b) A non-const reference cannot be modified, while a const reference can. c) There is no difference between a const reference and a non-const reference. d) A const reference cannot refer to a non-const variable.
Answer: a
- Can a reference refer to a temporary object? a) Yes, a reference can refer to a temporary object. b) No, a reference cannot refer to a temporary object. c) It depends on the data type of the reference. d) It depends on the scope of the reference.
Answer: a
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a reference variable in C++? How is it different from a pointer? Answer: A reference variable in C++ is an alias to an already existing variable. It is declared using an ampersand (&) symbol. A reference variable is different from a pointer in that it cannot be null and cannot be reassigned to point to another object.
Can a reference be returned from a function in C++? Answer: Yes, a reference can be returned from a function in C++. This can be useful in cases where we want to modify the value of an existing variable using a function.
What is the purpose of using a reference as a function parameter in C++? Answer: Using a reference as a function parameter in C++ allows us to modify the value of the original variable that is being passed to the function, rather than just making a copy of the variable.
How is a reference different from a constant reference in C++? Answer: A constant reference in C++ is a reference that cannot be modified. This means that any attempt to modify the value of the referenced variable will result in a compilation error.
What is a reference variable in C++ used for? Answer: A reference variable in C++ is typically used to provide an alternative name for an existing variable. It can also be used to pass variables by reference to functions, which can be more efficient than passing by value.
Can a reference be used to refer to a pointer variable in C++? Answer: Yes, a reference can be used to refer to a pointer variable in C++. This can be useful in cases where we want to modify the value of the pointer variable using a function.
How is a reference variable initialized in C++? Answer: A reference variable in C++ must be initialized when it is declared. This initialization binds the reference to the variable being referenced.
What is the difference between a reference and a copy in C++? Answer: A reference in C++ is an alias to an existing variable, while a copy is a separate instance of the variable. Modifying a reference modifies the original variable, while modifying a copy does not affect the original variable.
Can a reference be used to refer to an object in C++? Answer: Yes, a reference can be used to refer to an object in C++. This can be useful in cases where we want to modify the value of an object using a function.
What is the difference between a reference and a pointer in C++? Answer: A reference in C++ is an alias to an existing variable, while a pointer is a variable that stores the memory address of another variable. A reference cannot be null and cannot be reassigned to point to another object, while a pointer can be null and can be reassigned to point to another object.