The address-of operator (&) gives the memory address of a variable. It tells where the variable is stored in memory.
Click to reveal answer
beginner
What is the purpose of the dereference operator (*) in C++?
The dereference operator (*) accesses the value stored at the memory address held by a pointer. It lets you read or change the value the pointer points to.
Click to reveal answer
beginner
Explain the difference between a pointer and a normal variable.
A normal variable holds a value directly. A pointer holds the memory address of another variable. You use * to get the value from the pointer's address.
Click to reveal answer
intermediate
What happens if you dereference a pointer that is not initialized?
Dereferencing an uninitialized pointer leads to undefined behavior. It may cause a crash or access random memory, which is unsafe.
Click to reveal answer
beginner
How do you declare a pointer to an integer in C++?
You declare a pointer to an integer like this: int* ptr; This means ptr can hold the address of an int variable.
Click to reveal answer
What does the expression &x return if x is an int variable?
AThe memory address of x
BThe value of x
CA copy of x
DAn error
✗ Incorrect
The & operator returns the memory address of the variable x.
If int* p = &x;, what does *p represent?
AThe address of x
BAn uninitialized variable
CThe pointer p itself
DThe value stored in x
✗ Incorrect
The * operator dereferences the pointer p to get the value stored at the address it holds, which is x's value.
Which operator is used to get the address of a variable?
A%
B*
C&
D#
✗ Incorrect
The & operator is used to get the address of a variable.
What will happen if you try to dereference a null pointer?
AIt causes a runtime error or crash
BIt returns zero
CIt returns the pointer's address
DIt compiles but does nothing
✗ Incorrect
Dereferencing a null pointer causes a runtime error or crash because it points to no valid memory.
How do you declare a pointer to a double variable?
Adouble ptr;
Bdouble* ptr;
Cptr double*;
D*double ptr;
✗ Incorrect
The correct syntax to declare a pointer to double is: double* ptr;
Explain how the address-of (&) and dereference (*) operators work together with pointers in C++.
Think about how you get a variable's address and then use that address to get or change the variable's value.
You got /4 concepts.
Describe what happens if you try to use the dereference operator on an uninitialized or null pointer.
Consider what memory the pointer points to when it is not set properly.
You got /4 concepts.
Practice
(1/5)
1. What does the & operator do in C++?
easy
A. It dereferences a pointer to get the value.
B. It multiplies two numbers.
C. It declares a pointer variable.
D. It gives the memory address of a variable.
Solution
Step 1: Understand the & operator
The & operator is used to get the address of a variable in memory.
Step 2: Differentiate from other operators
It does not multiply, declare pointers, or dereference pointers; those are different operations.
Final Answer:
It gives the memory address of a variable. -> Option D
Quick Check:
& operator = address retrieval [OK]
Hint: Remember & means 'address of' in C++ [OK]
Common Mistakes:
Confusing & with multiplication operator
Thinking & declares a pointer
Mixing & with dereference * operator
2. Which of the following is the correct way to declare a pointer to an integer in C++?
easy
A. int *ptr;
B. int &ptr;
C. int ptr*;
D. *int ptr;
Solution
Step 1: Recall pointer declaration syntax
In C++, a pointer to an int is declared as int *ptr; where * indicates pointer type.
Step 2: Check other options for syntax errors
int &ptr; declares a reference, not a pointer. int ptr*; and *int ptr; are invalid syntax.
Final Answer:
int *ptr; -> Option A
Quick Check:
Pointer declaration = int *ptr; [OK]
Hint: Pointer syntax: type *name; [OK]
Common Mistakes:
Using & instead of * for pointers
Placing * after variable name incorrectly
Confusing pointers with references
3. What is the output of the following code?
int x = 10;
int *p = &x;
*p = 20;
std::cout << x << std::endl;
medium
A. Address of x
B. 10
C. 20
D. Compilation error
Solution
Step 1: Understand pointer assignment
Pointer p stores the address of x. Using *p = 20; changes the value at that address.
Step 2: Check the effect on x
Since *p points to x, changing *p to 20 updates x to 20.
Final Answer:
20 -> Option C
Quick Check:
Dereferencing pointer changes original variable [OK]
Hint: Changing *p changes the value of x [OK]
Common Mistakes:
Thinking *p is the address, not the value
Expecting output to be original x (10)
Confusing pointer variable with pointed value
4. Identify the error in this code snippet:
int x = 5;
int *p;
*p = x;
std::cout << *p << std::endl;
medium
A. Variable x is not declared.
B. Pointer p is not initialized before dereferencing.
C. Syntax error in pointer declaration.
D. Cannot assign value to *p.
Solution
Step 1: Check pointer initialization
Pointer p is declared but not assigned an address before *p = x;.