Which operator is used to get the memory address of a variable in C++?
A
* B
& C
# D
%
Analysis & Theory
The ampersand (&) operator is used to get the memory address of a variable.
What will this code output?
int x = 10;
cout << &x;
A
10 B
Address of x C
Compilation error D
0
Analysis & Theory
The expression `&x` gives the memory address of variable x.
Which data type is used to store memory addresses in C++?
A
int B
float C
pointer D
None of the above
Analysis & Theory
C++ uses pointers (e.g., int*, float*) to store memory addresses.
How do you declare a pointer to an integer?
A
int p; B
int *p; C
int &p; D
pointer p;
Analysis & Theory
Use `int *p;` to declare a pointer to an integer.
What does the * (asterisk) operator do when used with a pointer?
A
Gets the address of a variable B
Declares a variable C
Dereferences the pointer to get its value D
Multiplies values
Analysis & Theory
The * operator dereferences a pointer to access the value stored at that memory address.
Which line correctly assigns the address of x to pointer p?
int x = 5;
A
int *p = x; B
int *p = &x; C
int &p = x; D
int p = *x;
Analysis & Theory
To assign the address of x to p, use `int *p = &x;`.
What will be printed?
int x = 20; int *p = &x;
cout << *p;
A
Memory address of x B
0 C
20 D
Pointer name p
Analysis & Theory
*p gives the value stored at the address held by p, which is 20.
What is the output of this code?
int a = 42;
int *ptr = &a;
*ptr = 100;
cout << a;
A
42 B
100 C
Garbage value D
Compilation error
Analysis & Theory
Dereferencing the pointer and assigning a value changes the original variable a to 100.
Which of the following best describes a pointer?
A
A function B
A variable that stores a memory address C
An alias D
A constant
Analysis & Theory
A pointer is a variable that stores the memory address of another variable.
What is the result of this code?
int *p;
cout << p;
A
Memory address B
0 C
Garbage value (undefined) D
Compilation error
Analysis & Theory
Uninitialized pointers hold garbage (random) values unless explicitly set.