Test Series - cpp

Test Number 17/102

Q: What does a reference provide?
A. Alternate name for the class
B. Alternate name for the variable
C. Alternate name for the pointer
D. Alternate name for the object
Solution: Because we are pointing memory address using the temp variable.
Q: Identify the correct sentence regarding inequality between reference and pointer.
A. we can not create the array of reference
B. we can create the Array of reference
C. we can use reference to reference
D. we can use variable
Solution: 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.
Q: What are the references in C++?
A. An alternative name for already existing variables
B. A pointer to a variable
C. A new type of variables
D. A new type of constant variable
Solution: References are an alternative name for an already defined variable. They are different from pointers.
Q: What is the difference between references and pointers?
A. References are an alias for a variable whereas pointer stores the address of a variable
B. References and pointers are similar
C. References stores address of variables whereas pointer points to variables
D. Pointers are an alias for a variable whereas references stores the address of a variable
Solution: References are an alias/another name for a variable whereas pointer stores the address of a variable. Pointers need to be deference before use whereas references need not.
Q: Pick the correct statement about references in C++.
A. References stores the address of variables
B. References and variables both have the same address
C. References use dereferencing operator(*) to access the value of variable its referencing
D. References were also available in C
Solution: 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++.
Q: Pick the correct statement about references.
A. References can be assigned value NULL
B. References once assigned cannot be changed to refer another variable
C. Reference should not be initialized when created
D. Reference is the same as pointers
Solution: References are should be initialized during its creation and once assigned cannot be changed to refer another variable. References cannot be assigned NULL value. References and pointers are two different concepts.
Q: Which of the following operator is used while declaring references?
A. *
B. &
C. ^
D. ->
Solution: & operator is used for assigning references.
Q: Which of the following function must use reference.
A. Assignment operator function
B. Copy Constructor
C. Destructor
D. Parameterized constructor
Solution: We don’t need references in case of assignment, destructor or constructor. But in case of a copy constructor, we need to call copy constructor because if we use pass by value then as copy constructor itself is a function. So if we pass an argument bypass by value method in a copy constructor, a call to copy constructor would be made to again call copy constructor which becomes an endless chain of calls. Therefore compiler doesn’t allow parameters to be passed by value.
Q: How a reference is different from a pointer?
A. A reference cannot be null
B. A reference once established cannot be changed
C. The reference doesn’t need an explicit dereferencing mechanism
D. All of the mentioned
Solution: References can never be NULL. It is not allowed to change a reference once allocated. Referencing does not need an explicit referencing operator.
Q: Which of the following statement(s) is/are correct?
A. * operator is used to declare a reference
B. A reference variable defined to refer a particular variable can refer to any other variable also
C. References must always be initialized inside classes
D. A variable can have more than one references
Solution: A variable can have multiple references as references are nothing just another name for a variable hence a variable can more than one references.

You Have Score    /10