Which of the following correctly creates a pointer to an integer variable?
A
int *ptr; B
int &ptr; C
pointer ptr; D
int ptr = &x;
Analysis & Theory
`int *ptr;` declares a pointer to an integer.
What does the `*` symbol represent when declaring a pointer?
A
A reference operator B
A multiplication operator C
A pointer declaration or dereference operator D
A memory allocator
Analysis & Theory
`*` is used in pointer declaration and dereferencing (accessing the value at an address).
Which of the following is a valid way to assign the address of a variable `x` to a pointer `p`?
A
p = x; B
*p = x; C
p = &x; D
int p = &x;
Analysis & Theory
Use `p = &x;` to assign the memory address of variable `x` to pointer `p`.
How do you declare a pointer and initialize it to NULL?
A
int *p = 0; B
int p = NULL; C
int *p = NULL; D
Both 1 and 3
Analysis & Theory
Both `int *p = 0;` and `int *p = NULL;` are valid ways to initialize a null pointer in C++.
What is the value of `*p` after this code?
int x = 10; int *p = &x;
A
10 B
&x C
0 D
Garbage
Analysis & Theory
`*p` gives the value stored at the memory address pointed to by `p`, which is 10.
Which keyword is used to allocate memory dynamically in C++?
A
malloc B
new C
create D
alloc
Analysis & Theory
The `new` keyword is used for dynamic memory allocation in C++.
What is the type of a pointer to a float?
A
float * B
int * C
char * D
double *
Analysis & Theory
`float *` is a pointer that can store the address of a float variable.
Which of the following creates a pointer to a character?
A
char *p; B
int *p; C
string *p; D
float *p;
Analysis & Theory
`char *p;` is used to declare a pointer to a character.
What does this code do?
int *p; *p = 5;
A
Assigns 5 to memory location pointed by p B
Creates a pointer C
Results in undefined behavior (uninitialized pointer) D
Compiles successfully and sets p to 5
Analysis & Theory
Using `*p = 5;` without initializing `p` causes undefined behavior.
Which is the correct way to print the address of a variable `a`?
A
cout << a; B
cout << *a; C
cout << &a; D
cout << address(a);
Analysis & Theory
`&a` gives the memory address of variable `a`.