Introduction to C++ Programming, Y. Daniel Liang
Chapter 7 Pointers and C-Strings
Section 7.2 Pointer Basics
1
Which of the following correctly declares pointer variables.
A.
int *x;
B.
int x;
C.
int &x;
D.
int **x;
2
Suppose you declare int count = 5; which of the following is true?
A.
&count is the address of count
B.
&count is 5
C.
*count is the address of count
D.
*count is 5
3
Suppose you declare int count = 5 and int *pCount = &count; which of the following is true?
A.
*count is the address of count
B.
&count is 5
C.
*pCount is 5
D.
pCount contains the address of count
4
The ampersand (&) used in the following statement is known as ___________.
  
   int count = 5;
   cout << &count;
A.
indirection operator
B.
dereference operator
C.
multiply operator
D.
address operator
5
The asterisk (*) used in the following statement is known as ___________.
   cout << *pCount;
A.
indirection operator
B.
dereference operator
C.
multiply operator
D.
address operator
6
Why the following pointer variable declaration is wrong?
int area = 1;
double *pArea = &area;
A.
double *pArea = &area should be double *pArea = area;
B.
the type of variable does not match the type of the pointer.
C.
double *pArea = &area should be float *pArea = area;
D.
double *pArea = &area should be int *pArea = area;
7
Which of the following statements are true?
A.
A local variable is assigned an arbitrary value if you don?t initialize it.
B.
A local pointer is assigned an arbitrary value if you don?t initialize it.
C.
An array element is assigned an arbitrary value if you don?t initialize it.
D.
Dereferencing a pointer that is not initialized could cause fatal runtime error or it could accidentally modify important data.
Section 7.3 Passing Arguments by References with Pointers
8
What is the output of the following code?
   #include <iostream>
   using namespace std;
  
   void swap(int *pValue1, int *pValue2)
   {
     cout << "swap 1 invoked" << endl;
   }
  
   void swap(int &pValue1, int &pValue2)
   {
     cout << "swap 2 invoked" << endl;
   }
  
   int main()
   {
     int num1 = 1;
     int num2 = 2;
  
     swap(&num1, &num2);
  
     return 0;
   }
A.
swap 1 invoked
B.
swap 2 invoked
C.
The program has a runtime error because swap is declared multiple times.
D.
The program has a compile error because swap is declared multiple times.
9
What is the output of the following code?
   #include <iostream>
   using namespace std;
  
   void swap(int *pValue1, int *pValue2)
   {
     cout << "swap 1 invoked" << endl;
   }
  
   void swap(int &pValue1, int &pValue2)
   {
     cout << "swap 2 invoked" << endl;
   }
  
   int main()
   {
     int num1 = 1;
     int num2 = 2;
  
     swap(num1, num2);
  
     return 0;
   }
A.
swap 1 invoked
B.
swap 2 invoked
C.
The program has a runtime error because swap is declared multiple times.
D.
The program has a compile error because swap is declared multiple times.
10
What is the output of the following code?
   #include <iostream>
   using namespace std;
   void swap(int pValue1, int pValue2)
   {
     cout << "swap 1 invoked" << endl;
   }
   void swap(int &pValue1, int &pValue2)
   {
     cout << "swap 2 invoked" << endl;
   }
   int main()
   {
     int num1 = 1;
     int num2 = 2;
     swap(num1, num2);
     return 0;
   }
A.
swap 1 invoked
B.
swap 2 invoked
C.
The program has a runtime error because swap is declared multiple times.
D.
The program has a compile error because swap(num1, num2) could match either swap(int pValue1, int pValue2) or swap(int &pValue1, int &pValue2).
Section 7.4 Arrays and Pointers
11
Suppose int list[6] = {11, 12, 13, 14, 15, 16}; Is *list the same as list[0]?
A.
yes
B.
no
12
What is the output of the following code?
   #include <iostream>
   using namespace std;
  
   int main()
   {
     int list[] = {10, 20, 30, 40};
     cout << *(list + 1) << " " << *list + 1 << endl;
  
     return 0;
   }
A.
10 10
B.
20 20
C.
30 30
D.
20 11
13
What is the output of the following code?
   #include <iostream>
   using namespace std;
  
   int main()
   {
     int list[] = {1, 1, 1, 1};
     *(list) = *(list) + 1;
     *(list + 1) = *(list + 1) + 2;
     *(list + 2) = *(list + 2) + 3;
     *(list + 3) = *(list + 3) + 4;
     cout << list[0] << " " << list[3] << endl;
  
     return 0;
   }
A.
1 2
B.
2 2
C.
3 4
D.
3 5
E.
2 5
14
Suppose you defined
  int list1[4], list2[4];
  int *p1, *p2;
Which of the following statements are correct?
A.
p1 = list1;
B.
p1 = p2;
C.
list1 = p1;
D.
list1 = list2;
15
Analyze the following code.
   #include <iostream>
   using namespace std;
  
   int main()
   {
     char *p;
     cout << "Enter a string: ";
     cin >> p;
     cout << p << endl;
  
     return 0;
   }
A.
If you run the program and enter abc, nothing will be displayed. The program runs without errors.
B.
If you run the program and enter abc, abc will be displayed.
C.
If you run the program and enter abc, unpredictable characters will be displayed.
D.
If you run the program and enter abc, a runtime error will occur, because p is used without being initialized.
16
Analyze the following code.
   #include <iostream>
   using namespace std;
  
   int main()
   {
     char t[10];
     char * p = t;
     cout << "Enter a string: ";
     cin >> p;
     cout << p << endl;
  
     return 0;
   }
A.
If you run the program and enter abc, nothing will be displayed. The program runs without errors.
B.
If you run the program and enter abc, abc will be displayed.
C.
If you run the program and enter abc, unpredictable characters will be displayed.
D.
If you run the program and enter abc, a runtime error will occur, because p is being used without initialized.
Section 7.5 Using const with Pointers
17
Suppose you declare the following:
double radius = 5;
double * const pValue = &radius;
Which of the following statements are allowed?
A.
radius++;
B.
(*pValue)++;
C.
pValue = &radius;
D.
*pValue = 0;
18
Suppose you declare the following:
double radius = 5;
const double * pValue = &radius;
Which of the following statements are allowed?
A.
radius++;
B.
(*pValue)++;
C.
pValue = &radius;
D.
*pValue = 0;
19
Suppose you declare the following:
double radius = 5;
const double const* pValue = &radius;
Which of the following statements are allowed?
A.
radius++;
B.
(*pValue)++;
C.
pValue = &radius;
D.
*pValue = 0;
E.
cout << *pValue;
Section 7.6 Returning Pointers from Functions
20
Which of the following function header declaration is correct?
A.
int int[] reverse(int const * list, const int size)
B.
int * reverse(int const * list, const int size)
C.
int * reverse(int const * list[], const int size)
D.
int * reverse(int const list[], const int size)
Section 7.7 Dynamic Memory Allocation
21
Which of the following declaration is correct?
A.
int *pValue = new double;
B.
int *pValue = new int;
C.
double *pValue = new double;
D.
double *pValue = new int;
22
Suppose list is declared as follows:
int *list = new int[10];
How should you destroy list?
A.
delete list;
B.
delete *list;
C.
delete [] list;
D.
delete [] *list;
23
Does the following code cause a memory leak?
int *pValue = new int;
*pValue = 45;
pValue = new int;
delete pValue;
A.
yes
B.
no
Section 7.9 Characters and Strings
24
To check whether char variable ch is a digit, use the function
A.
isdigit(ch)
B.
isalpha(ch)
C.
isalnum(ch)
D.
isprint(ch)
E.
islower(ch)
25
To return an uppercase letter from char variable ch, use
A.
isdigit(ch)
B.
isalpha(ch)
C.
toupper(ch)
D.
tolower(ch)
E.
islower(ch)
26
Suppose you declare
char city[7] = "Dallas";
How many characters are stored in city?
A.
5
B.
6
C.
7
D.
8
27
Are the following two declarations the same
char city[7] = "Dallas";
char * const city = "Dallas";
A.
yes
B.
no
28
Suppose char city[7] = "Dallas"; what is the output of the following statement?
cout << city;
A.
D
B.
Dallas
C.
Dallas0
D.
nothing printed
29
Show the printout of the following code
char city[7] = "Dallas";
cout << strlen(city);
A.
5
B.
6
C.
7
D.
8
30
What is wrong in the following code?
char city[7] = "Dallas";
char *s1;
strcpy(s1, city);
cout << s1;
A.
There is no memory allocated to s1. Thus, you cannot copy anything into it.
B.
If you change char *s1 to char *s1[7], it will work correctly.
C.
If you change char *s1 to char s1[7], it will work correctly.
D.
If you change char *s1 to char s1[6], it will work correctly.
31
What is the printout of the following code?
char s2[7] = "Dallas";
char s1[14] = "Dallas";
strcat(s1, s2);
cout << s1;
A.
Dallas
B.
DallasDallas
C.
D
D.
DD
32
What is the printout of the following code?
char s2[7] = "Dallas";
char s1[14] = "Dallas";
cout << strcmp(s1, s2);
A.
1
B.
0
C.
-1
D.
2