You are on page 1of 9

C++ Programming From Problem Anal-

ysis to Program Design 6th Edition Ma-


lik
Full download at link:

Test bank: https://testbankpack.com/p/test-bank-for-c-programming-


from-problem-analysis-to-program-design-6th-edition-by-malik-isbn-
1133626386-9781133626381/

Solution Manual: https://testbankpack.com/p/solution-manual-for-c-


programming-from-problem-analysis-to-program-design-6th-edition-
by-malik-isbn-1133626386-9781133626381/

Chapter 8: Arrays and Strings

TRUE/FALSE

1. All components of an array are of the same data type.

ANS: T PTS: 1 REF: 507

2. The array index can be any integer less than the array size.

ANS: F PTS: 1 REF: 509

3. The statement int list[25]; declares list to be an array of 26 components, since the array
index starts at 0.

ANS: F PTS: 1 REF: 509

4. Given the declaration int list[20]; the statement list[12] = list[5] + list[7];
updates the content of the twelfth component of the array list.

ANS: F PTS: 1 REF: 509

5. Suppose list is a one dimensional array of size 25, wherein each component is of type int. Further,
suppose that sum is an int variable. The following for loop correctly finds the sum of the elements
of list.

sum = 0;

for (int i = 0; i < 25; i++)


sum = sum + list;

ANS: F PTS: 1 REF: 512

6. If an array index goes out of bounds, the program always terminates in an error.

ANS: F PTS: 1 REF: 515

7. Arrays can be passed as parameters to a function by value, but it is faster to pass them by reference.

ANS: F PTS: 1 REF: 518

8. When you pass an array as a parameter, the base address of the actual array is passed to the formal
parameter.

ANS: T PTS: 1 REF: 523

9. The one place where C++ allows aggregate operations on arrays is the input and output of C-strings.

ANS: T PTS: 1 REF: 539

10. In a two-dimensional array, the elements are arranged in a table form.

ANS: T PTS: 1 REF: 557

MULTIPLE CHOICE

1. Which of the following statements declares alpha to be an array of 25 components of the type int?
a. int alpha[25]; c. int alpha[2][5];
b. int array alpha[25]; d. int array alpha[25][25];
ANS: A PTS: 1 REF: 507-508

2. Assume you have the following declaration char nameList[100];. Which of the following
ranges is valid for the index of the array nameList?
a. 0 through 99 c. 1 through 100
b. 0 through 100 d. 1 through 101
ANS: A PTS: 1 REF: 509

3. Assume you have the following declaration int beta[50];. Which of the following is a valid
element of beta?
a. beta['2'] c. beta[0]
b. beta['50'] d. beta[50]
ANS: C PTS: 1 REF: 509

4. Assume you have the following declaration double salesData[1000];. Which of the
following ranges is valid for the index of the array salesData?
a. 0 through 999 c. 1 through 1001
b. 0 through 1000 d. 1 through 1000
ANS: A PTS: 1 REF: 509
5. Suppose that sales is an array of 50 components of type double. Which of the following correctly
initializes the array sales?
a. for (int 1 = 1; j <= 49; j++)
sales[j] = 0;
b. for (int j = 1; j <= 50; j++)
sales[j] = 0;
c. for (int j = 0; j <= 49; j++)
sales[j] = 0.0;
d. for (int j = 0; j <= 50; j++)
sales[j] = 0.0;
ANS: C PTS: 1 REF: 512

6. Suppose that list is an array of 10 components of type int. Which of the following codes correctly
outputs all the elements of list?

a. for (int j = 1; j < 10; j++)


cout << list[j] << " ";
cout << endl;

b. for (int j = 0; j <= 9; j++)


cout << list[j] << " ";
cout << endl;

c. for (int j = 1; j < 11; j++)


cout << list[j] << " ";
cout << endl;

d. for (int j = 1; j <= 10; j++)


cout << list[j] << " ";
cout << endl;

ANS: B PTS: 1 REF: 512

7. What is the output of the following C++ code?

int list[5] = {0, 5, 10, 15, 20};


int j;

for (j = 0; j < 5; j++)


cout << list[j] << " ";
cout << endl;

a. 0 1 2 3 4 c. 0 5 10 15 20
b. 0 5 10 15 d. 5 10 15 20
ANS: C PTS: 1 REF: 512

8. What is the value of alpha[2] after the following code executes?

int alpha[5];
int j;

for (j = 0; j < 5; j++)


alpha[j] = 2 * j + 1;

a. 1 c. 5
b. 4 d. 6
ANS: C PTS: 1 REF: 512

9. What is the output of the following C++ code?

int alpha[5] = {2, 4, 6, 8, 10};


int j;

for (j = 4; j >= 0; j--)


cout << alpha[j] << " ";
cout << endl;

a. 2 4 6 8 10 c. 8 6 4 2 0
b. 4 3 2 1 0 d. 10 8 6 4 2
ANS: D PTS: 1 REF: 512

10. What is the output of the following C++ code?

int list[5] = {0, 5, 10, 15, 20};


int j;

for (j = 1; j <= 5; j++)


cout << list[j] << " ";
cout << endl;

a. 0 5 10 15 20 c. 5 10 15 20 20
b. 5 10 15 20 0 d. Code results in index out-of-bounds
ANS: D PTS: 1 REF: 515-516

11. Suppose that gamma is an array of 50 components of type int and j is an int variable. Which of the
following for loops sets the index of gamma out of bounds?
a. for (j = 0; j <= 49; j++)
cout << gamma[j] << " ";
b. for (j = 1; j < 50; j++)
cout << gamma[j] << " ";
c. for (j = 0; j <= 50; j++)
cout << gamma[j] << " ";
d. for (j = 0; j <= 48; j++)
cout << gamma[j] << " ";
ANS: C PTS: 1 REF: 515-516

12. Consider the following declaration: int alpha[5] = {3, 5, 7, 9, 11};. Which of the
following is equivalent to this statement?
a. int alpha[] = {3, 5, 7, 9, 11};
b. int alpha[] = {3 5 7 9 11};
c. int alpha[5] = [3, 5, 7, 9, 11];
d. int alpha[] = (3, 5, 7, 9, 11);
ANS: A PTS: 1 REF: 516

13. In C++, the null character is represented as ____.


a. '\0' c. '0'
b. "\0" d. "0"
ANS: A PTS: 1 REF: 535

14. Which of the following correctly declares name to be a character array and stores "William" in it?
a. char name[6] = "William";
b. char name[7] = "William";
c. char name[8] = "William";
d. char name[8] = 'William';
ANS: C PTS: 1 REF: 536

15. Consider the following declaration: char str[15];. Which of the following statements stores
"Blue Sky" into str?
a. str = "Blue Sky";
b. str[15] = "Blue Sky";
c. strcpy(str, "Blue Sky");
d. strcpy("Blue Sky");
ANS: C PTS: 1 REF: 537

16. Consider the following declaration:


char charArray[51];
char discard;

Assume that the input is:


Hello There!
How are you?

What is the value of discard after the following statements execute?

cin.get(charArray, 51);
cin.get(discard);

a. discard = ' ' (Space) c. discard = '\n'


b. discard = '!' d. discard = '\0'
ANS: C PTS: 1 REF: 540

17. Consider the following statement: double alpha[10][5];. The number of components of
alpha is ____.
a. 15 c. 100
b. 50 d. 150
ANS: B PTS: 1 REF: 544

18. Consider the statement int list[10][8];. Which of the following about list is true?
a. list has 10 rows and 8 columns.
b. list has 8 rows and 10 columns.
c. list has a total of 18 components.
d. list has a total of 108 components.
ANS: A PTS: 1 REF: 544

19. Consider the following statement: int alpha[25][10];. Which of the following statements
about alpha is true?
a. Rows of alpha are numbered 0...24 and columns are numbered 0...9.
b. Rows of alpha are numbered 0...24 and columns are numbered 1...10.
c. Rows of alpha are numbered 1...24 and columns are numbered 0...9.
d. Rows of alpha are numbered 1...25 and columns are numbered 1...10.
ANS: A PTS: 1 REF: 544

20. Which of the following correctly declares and initializes alpha to be an array of four rows and three
columns with the component type int?
a. int alpha[4][3] = {{0,1,2} {1,2,3} {2,3,4} {3,4,5}};
b. int alpha[4][3] = {0,1,2; 1,2,3; 2,3,4; 3,4,5};
c. int alpha[4][3] = {0,1,2: 1,2,3: 2,3,4: 3,4,5};
d. int alpha[4][3] = {{0,1,2}, {1,2,3}, {2,3,4}, {3,4,5}};
ANS: D PTS: 1 REF: 546

21. After the following statements execute, what are the contents of matrix?

int matrix[3][2];
int j, k;

for (j = 0; j < 3; j++)


for (k = 0; k < 2; k++)
matrix[j][k] = j + k;

a. 0 0 c. 0 1
1 1 1 2
2 2 2 3
b. 0 1 d. 1 1
2 3 2 2
4 5 3 3
ANS: C PTS: 1 REF: 548-550

22. Given the following declaration:

int j;
int sum;
double sale[10][7];

which of the following correctly finds the sum of the elements of the fifth row of sale?
a. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sale[5][j];
b. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sale[4][j];
c. sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sale[5][j];
d. sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sale[4][j];
ANS: B PTS: 1 REF: 550

23. Given the following declaration:

int j;
int sum;
double sale[10][7];

which of the following correctly finds the sum of the elements of the fourth column of sale?
a. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sale[j][3];
b. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sale[j][4];
c. sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sale[j][4];
d. sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sale[j][3];
ANS: D PTS: 1 REF: 551

24. In row order form, the ____.


a. first row is stored first c. first column is stored first
b. first row is stored last d. first column is stored last
ANS: A PTS: 1 REF: 552

25. A collection of a fixed number of elements (called components) arranged in n dimensions (n>=1) is
called a(n) ____.
a. matrix c. n-dimensional array
b. vector d. parallel array
ANS: C PTS: 1 REF: 557

COMPLETION

1. A data type is called ____________________ if variables of that type can store only one value at a
time.

ANS: simple

PTS: 1 REF: 506

2. In a(n) ____________________ data type, each data item is a collection of other data items.

ANS: structured

PTS: 1 REF: 506


3. Complete the following statement so that it outputs the array sales.

double sales[10];
int index;

for (index = 0; index < 10; index++)


cout << ____________________ << " ";

ANS: sales[index]

PTS: 1 REF: 512

4. The word ____________________ is used before the array declaration in a function heading to
prevent the function from modifying the array.

ANS: const

PTS: 1 REF: 519

5. The ____________________ of an array is the address (that is, the memory location) of the first array
component.

ANS: base address

PTS: 1 REF: 521

6. The ____________________ sort algorithm finds the location of the smallest element in the unsorted
portion of the list and moves it to the top of the unsorted portion of the list.

ANS: selection

PTS: 1 REF: 530-531

7. For a list of length n, the ____________________ sort makes exactly (n(n - 1))/2 key
comparisons and 3(n-1) item assignments.

ANS: selection

PTS: 1 REF: 535

8. The declaration char str[] = "Hello there"; declares str to be a string of


____________________ characters.

ANS:
12
twelve

PTS: 1 REF: 535-536

9. The function ____________________ returns the length of the string s, excluding the null character.

ANS: strlen(s)
PTS: 1 REF: 537

10. The statement strlen("Marylin Stewart"); returns ____________________.

ANS: 15

PTS: 1 REF: 537-538

11. The following statements store the value ____________________ into len.

int len;
len = strlen("Sunny California");

ANS: 16

PTS: 1 REF: 537-538

12. The header file string contains the function ____________________,which converts a value of type
string to a null-terminated character array.

ANS: c_str

PTS: 1 REF: 541

13. Two (or more) arrays are called ____________________ if their corresponding components hold
related information.

ANS: parallel

PTS: 1 REF: 542

14. The following statement creates alpha to be a two-dimensional array with


____________________ rows.

int alpha[10][25];

ANS:
10
ten

PTS: 1 REF: 544

15. In the following declaration, the array gamma has ____________________ components.

int gamma[5][6][10];

ANS:
300
three hundred

PTS: 1 REF: 558

You might also like