Study Guide 4: Pointers

  1. A pointer is
    1. an abstract concept but is not supported by C++
    2. an indication of the variable to be accessed next.
    3. a variable for storing addresses.
    4. an address.
    5. the data type of an address variable.
  2. An address is a _____, while a pointer is a _____.
    1. constant, variable
    2. variable, constant
    3. constant, constant
    4. variable, variable
  3. In an expression, an ampersand (i.e, &) placed in front of a variable means
    1. defines a pointer variable
    2. contents of the variable pointed to by (dereferences a variable)
    3. the address of the variable
  4. In an expression, an asterisk placed between a data type (e.g., int, double, etc.) and a variable name means _____.
    1. define a pointer variable
    2. contents of the variable pointed to by (dereferences a variable)
    3. the address of the variable
  5. An asterisk placed in front of a variable name, without a data type, means ____.
    1. define a pointer variable
    2. contents of the variable pointed to by (dereferences a variable)
    3. the address of the variable
  6. The expression *test (not appearing as a part of a variable definition) can be said to (mark all that apply)
    1. be a pointer to test.
    2. indirectly refer to the variable pointed to by test.
    3. dereference test.
    4. indirectly refer to the value stored at the memory location whose address is stored in the variable test
    5. represent the address of test.
  7. Which of the following may be used in a C++ program to represent the null-value (mark all that apply)?
    1. null
    2. NULL
    3. 0
    4. nullptr
  8. Which of the following is/are the preferred notation to represent a null-value in a C++ program (mark all that apply)?
    1. null
    2. NULL
    3. 0
    4. nullptr

  9. For the following questions: Given the following code fragment:

    	int	x = 10;
    	int*	p = &x;

  10. What does cout << x << endl; print?
    1. 10
    2. the address of p
    3. the address of x
    4. the second statement will not compile
  11. What does cout << &x << endl; print?
    1. 10
    2. the address of p
    3. the address of x
    4. the second statement will not compile
  12. What does cout << *p << endl; print?
    1. 10
    2. the address of p
    3. the address of x
    4. the second statement will not compile
  13. What does cout << p << endl; print?
    1. 10
    2. the address of p
    3. the address of x
    4. the second statement will not compile
  14. What does cout << &p << endl; print?
    1. 10
    2. the address of p
    3. the address of x
    4. the second statement will not compile

  15. Choose the best expression to calculate the address of a variable named foo.
    1. &foo
    2. foo&
    3. *foo
    4. foo*
    5. foo* f = &foo;
    6. foo& f = foo*;
  16. Choose the best statement to define a variable named chr_ptr that points to a char.
    1. char chr_ptr;
    2. char* chr_ptr;
    3. char& chr_ptr;
    4. char chr_ptr *;
    5. char chr_ptr &;
  17. 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.
    1. &int_ptr;
    2. int_ptr&;
    3. *int_ptr;
    4. int_ptr*;
  18. Fill in the blank by writing an expression to calculate the address of the variable testvar.
    	cout << ________ << endl;
  19. Define a variable named char_pointer whose data type is a pointer to a char.
  20. Define a variable named double_pointer whose type is a pointer to a double.
  21. 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.
  22. The new operator (mark all that apply)
    1. returns a pointer to a previously defined variable.
    2. creates a variable called new.
    3. allocates memory for a new variable on the heap.
    4. allocates memory for a new variable on the stack.
    5. tells how much memory is available.
  23. The algorithms that the computer uses to manage the stack and the heap are the same.
    1. True
    2. False
  24. In C++, memory allocated with the new operator must be deallocated with the delete operator.
    1. True
    2. False
  25. C++ has an automatic garbage collector just like Java.
    1. True
    2. False
  26. Given the code fragment
    	double* scores = new double[30];
    Which of the following statements is correct?
    1. delete scores;
    2. delete scores[];
    3. delete[] scores;
    4. delete scores[3];
    5. delete[30] scores;
    6. None of them are correct.
  27. What does the following code fragment print?
    	char c;
    	char* cp = &c;
    	cout << sizeof(c) << " " << sizeof(cp) << endl;
    1. 1 1
    2. 1 4
    3. 1 8
    4. 4 4
    5. 4 8
    6. It's not possible to say because c is not initialized.
  28. 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.
  29. 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.