0
0
C++programming~5 mins

Reference declaration in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a reference in C++?
A reference in C++ is an alias for another variable. It allows you to create a new name for an existing variable, so both names refer to the same memory location.
Click to reveal answer
beginner
How do you declare a reference variable in C++?
You declare a reference by using the & symbol after the type. For example: int &ref = originalVariable; creates a reference named ref to originalVariable.
Click to reveal answer
intermediate
Can a reference be changed to refer to another variable after initialization?
No, once a reference is initialized to a variable, it cannot be changed to refer to another variable. It always refers to the original variable it was bound to.
Click to reveal answer
beginner
What happens if you modify a reference variable?
Modifying a reference variable changes the value of the original variable it refers to, because both names point to the same memory location.
Click to reveal answer
intermediate
Why use references instead of pointers in C++?
References are safer and easier to use because they cannot be null and do not require dereferencing syntax. They provide a simpler way to alias variables without pointer complexity.
Click to reveal answer
How do you declare a reference to an int variable named num?
Aint &ref = num;
Bint *ref = #
Cint ref = #
Dint &ref;
Can a reference be null in C++?
ANo, references must always refer to a valid variable.
BOnly for pointer references.
COnly if initialized with nullptr.
DYes, references can be null.
What happens if you assign a new value to a reference variable?
AThe reference starts pointing to a new variable.
BA new variable is created.
CThe original variable's value changes.
DCompilation error.
Which symbol is used to declare a reference in C++?
A*
B&
C#
D%
Can you declare a reference without initializing it?
AOnly inside functions.
BYes, references can be declared without initialization.
COnly for const references.
DNo, references must be initialized when declared.
Explain what a reference is in C++ and how it differs from a pointer.
Think about how you use a nickname for a friend instead of their full name.
You got /4 concepts.
    Describe how to declare and use a reference variable in C++ with an example.
    Show how to create a new name for an existing variable and change its value.
    You got /4 concepts.