Which of the following declares a pointer to an integer?
A
int p; B
int *p; C
int &p; D
pointer p;
Analysis & Theory
`int *p;` declares a pointer to an integer.
What does the `&` operator do in C++?
A
Dereferences a pointer B
Returns the value of a variable C
Gets the address of a variable D
Declares a reference
Analysis & Theory
`&` is the address-of operator, used to get the memory address of a variable.
Which operator is used to access the value pointed to by a pointer?
A
& B
* C
% D
#
Analysis & Theory
`*` is the dereference operator, used to access the value stored at a memory address.
What will this code output?
int a = 5;
int *p = &a;
cout << *p;
A
Address of a B
5 C
*p D
Error
Analysis & Theory
`*p` dereferences the pointer and prints the value of `a`, which is 5.
What is the output of the following code?
int a = 10;
int *p = &a;
*p = 20;
cout << a;
A
10 B
20 C
*p D
Compilation error
Analysis & Theory
Since `*p = 20;` modifies the value at address of `a`, `a` becomes 20.
Which of the following is true about pointers in C++?
A
They store variable names B
They store the memory address of another variable C
They hold constant values only D
They cannot be modified
Analysis & Theory
Pointers are used to store the memory address of another variable.
What is the result of this code?
int *p;
cout << *p;
A
Garbage value or runtime error B
0 C
NULL D
Compilation error
Analysis & Theory
`p` is an uninitialized pointer. Dereferencing it leads to undefined behavior or crash.
How do you assign the address of a variable `x` to pointer `p`?
A
p = x; B
*p = x; C
p = &x; D
&p = x;
Analysis & Theory
Use `p = &x;` to assign the address of `x` to pointer `p`.
Which of the following correctly declares and initializes a null pointer?
A
int *p = NULL; B
int *p = 0; C
int *p = nullptr; D
All of the above
Analysis & Theory
`NULL`, `0`, and `nullptr` are all valid ways to initialize a null pointer (C++11 introduced `nullptr`).
Which keyword is used to dynamically allocate memory in C++?
A
malloc B
alloc C
new D
create
Analysis & Theory
`new` is the C++ keyword used to dynamically allocate memory on the heap.