- A pointer is
- an abstract concept but is not supported by C++
- an indication of the variable to be accessed next.
- a variable for storing addresses.
- an address.
- the data type of an address variable.
- An address is a _____, while a pointer is a _____.
- constant, variable
- variable, constant
- constant, constant
- variable, variable
- In an expression, an ampersand (i.e, &) placed in front of a variable means
- defines a pointer variable
- contents of the variable pointed to by (dereferences a variable)
- the address of the variable
- In an expression, an asterisk placed between a data type (e.g., int, double, etc.) and a variable name means _____.
- define a pointer variable
- contents of the variable pointed to by (dereferences a variable)
- the address of the variable
- An asterisk placed in front of a variable name, without a data type, means ____.
- define a pointer variable
- contents of the variable pointed to by (dereferences a variable)
- the address of the variable
- The expression *test (not appearing as a part of a variable definition) can be said to (mark all that apply)
- be a pointer to test.
- indirectly refer to the variable pointed to by test.
- dereference test.
- indirectly refer to the value stored at the memory location whose address is stored in the variable test
- represent the address of test.
- Which of the following may be used in a C++ program to represent the null-value (mark all that apply)?
- null
- NULL
- 0
- nullptr
- Which of the following is/are the preferred notation to represent a null-value in a C++ program (mark all that apply)?
- null
- NULL
- 0
- nullptr
For the following questions: Given the following code fragment:
int x = 10;
int* p = &x;
- What does
cout << x << endl;
print?
- 10
- the address of p
- the address of x
- the second statement will not compile
- What does
cout << &x << endl;
print?
- 10
- the address of p
- the address of x
- the second statement will not compile
- What does
cout << *p << endl;
print?
- 10
- the address of p
- the address of x
- the second statement will not compile
- What does
cout << p << endl;
print?
- 10
- the address of p
- the address of x
- the second statement will not compile
- What does
cout << &p << endl;
print?
- 10
- the address of p
- the address of x
- the second statement will not compile
- Choose the best expression to calculate the address of a variable named foo.
- &foo
- foo&
- *foo
- foo*
- foo* f = &foo;
- foo& f = foo*;
- Choose the best statement to define a variable named chr_ptr that points to a char.
- char chr_ptr;
- char* chr_ptr;
- char& chr_ptr;
- char chr_ptr *;
- char chr_ptr &;
- The variable int_ptr is a pointer variable that points to an int. If int_ptr points to an int variable named bar, choose the best expression that represents the value stored in variable bar but does not use the name bar.
- &int_ptr;
- int_ptr&;
- *int_ptr;
- int_ptr*;
- Fill in the blank by writing an expression to calculate the address of the variable testvar.
cout << ________ << endl;
- Define a variable named char_pointer whose data type is a pointer to a char.
- Define a variable named double_pointer whose type is a pointer to a double.
- Given:
int testvar;
// value assigned to testvar here
int* testptr = &testvar;
cout << _________ << endl;
Fill in the blank by writing an expression that represents the contents of testvar without using the name testvar.
- The new operator (mark all that apply)
- returns a pointer to a previously defined variable.
- creates a variable called new.
- allocates memory for a new variable on the heap.
- allocates memory for a new variable on the stack.
- tells how much memory is available.
- The algorithms that the computer uses to manage the stack and the heap are the same.
- True
- False
- In C++, memory allocated with the new operator must be deallocated with the delete operator.
- True
- False
- C++ has an automatic garbage collector just like Java.
- True
- False
- Given the code fragment
double* scores = new double[30];
Which of the following statements is correct?
- delete scores;
- delete scores[];
- delete[] scores;
- delete scores[3];
- delete[30] scores;
- None of them are correct.
- What does the following code fragment print?
char c;
char* cp = &c;
cout << sizeof(c) << " " << sizeof(cp) << endl;
- 1 1
- 1 4
- 1 8
- 4 4
- 4 8
- It's not possible to say because c is not initialized.
- Assume that bar is the name of a class. Write a statement that defines a variable named bar_ptr that can point to a bar object.
- Assume that bar is the name of a class. Write C++ code that creates an instance of bar on the heap. You may choose any variable name for the object.