wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Quiz 9: Pointer

Total questions: 12

Worksheet time: 6mins

Name
Class
Date
1.

The expression:


int *p = new int[20];

a)

allocates 20 integers and assigns the base address to p

b)

allocates 20 integers off the stack local to a block

c)

reads in 20 integer values from cin

d)

allocates an integer of size 20 bytes

2.

Which form of the operator delete would you use to deallocate free store memory allocated by this statement:


Students *student_list = new Students[size];

a)

delete [] student_list

b)

delete student_list

c)

delete [] students

d)

delete students[size]

3.

Which of the following correctly displays the memory address of the variable named menu?


int menu = 23;

a)

cout << *menu;

b)

cout << menu;

c)

cout << &menu;

d)

cout << &menu*;

4.

What does the variable i contain after the following code executes?


int i = 17;

int *p = &i;

*p = 98;

a)

98

b)

17

c)

81

d)

0

5.

Allocation of memory storage for statically allocated variables occurs during ________

while memory allocation for dynamically allocated variables occurs during ___________.

a)

(load time) and (compile time)

b)

(load time) and (run time)

c)

(run time) and (compile time)

d)

(compile time) and (load time)

6.

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?

a)

p2 is pointing at where p1 is pointing at.

b)

p2 is not pointing at any memory location.

c)

p2 is a dangling pointer.

d)

p2 is also assigned value NULL.

7.

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?

a)

cout << p.email;

b)

cout << p[email];

c)

cout << john->email;

d)

cout << p->email;

8.

Given the following pointer variables:


int x;

int* p;

p = &x;


Which of the following code segments will cause segmentation fault?

a)

*p = 100.12;

b)

p = NULL;

cout << *p;

c)

p = NULL;

cout << p;

d)

p = NULL;

cout << &p;

9.

In C++11, the _______ keyword was introduced to represent the address 0.

a)

NULL

b)

null

c)

nullptr

d)

weak_ptr

10.

Every byte in the computer's memory is assigned a unique:

a)

pointer

b)

address

c)

dynamic allocation

d)

name

11.

If a variable uses more than one byte of memory, for pointer purposes its address is:

a)

the address of the last byte of storage

b)

the average of all the addresses used to store that variable

c)

the address of the first byte of storage

d)

the address of the second byte of storage

12.

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;

a)

five memory addresses

b)

0

c)

1

d)

2