NEW
Font size
WorksheetsQuiz 9: Pointer
Total questions: 12
Worksheet time: 6mins
The expression:
int *p = new int[20];
allocates 20 integers and assigns the base address to p
allocates 20 integers off the stack local to a block
reads in 20 integer values from cin
allocates an integer of size 20 bytes
Which form of the operator delete would you use to deallocate free store memory allocated by this statement:
Students *student_list = new Students[size];
delete [] student_list
delete student_list
delete [] students
delete students[size]
Which of the following correctly displays the memory address of the variable named menu?
int menu = 23;
cout << *menu;
cout << menu;
cout << &menu;
cout << &menu*;
What does the variable i contain after the following code executes?
int i = 17;
int *p = &i;
*p = 98;
98
17
81
0
Allocation of memory storage for statically allocated variables occurs during ________
while memory allocation for dynamically allocated variables occurs during ___________.
(load time) and (compile time)
(load time) and (run time)
(run time) and (compile time)
(compile time) and (load time)
Given:
int *p1;
int *p2;
p1 = new int;
p2 = p1;
delete p1;
p1 = NULL;
Which of the following best describe the current situation with p2?
p2 is pointing at where p1 is pointing at.
p2 is not pointing at any memory location.
p2 is a dangling pointer.
p2 is also assigned value NULL.
Suppose the following structure and variables are declared:
struct Student {
string name;
string email;
};
Student john;
Student* p = &john;
Which of the following statements print the student john's email?
cout << p.email;
cout << p[email];
cout << john->email;
cout << p->email;
Given the following pointer variables:
int x;
int* p;
p = &x;
Which of the following code segments will cause segmentation fault?
*p = 100.12;
p = NULL;
cout << *p;
p = NULL;
cout << p;
p = NULL;
cout << &p;
In C++11, the _______ keyword was introduced to represent the address 0.
NULL
null
nullptr
weak_ptr
Every byte in the computer's memory is assigned a unique:
pointer
address
dynamic allocation
name
If a variable uses more than one byte of memory, for pointer purposes its address is:
the address of the last byte of storage
the average of all the addresses used to store that variable
the address of the first byte of storage
the address of the second byte of storage
What will the following code output?
int *numbers = new int[5];
for (int i = 0; i <= 4; i++)
*(numbers + i) = i;
cout << numbers[2] << endl;
five memory addresses
0
1
2
