Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 7 Single-Dimensional Arrays and C-Strings


Section 7.2 Array Basics
7.1  What is the representation of the third element in an array called a?
A. a[2]
B. a(2)
C. a[3]
D. a(3)

7.2  Which of the following is incorrect?
A. int a[2];
B. int a[];
C. int a(2);
D. int a = new int[2];
E. int a() = new int[2];

7.3  If you declare an array double list[] = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
A. 3.4
B. 2.0
C. 3.4
D. 5.5
E. undefined

7.4  If you declare an array double list[] = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.
A. 0
B. 1
C. 2
D. 3
E. 4

7.5  How many elements are in array double list[5]?
A. 4
B. 5
C. 6
D. 0

7.6  What is the correct term for numbers[99]?
A. index
B. index variable
C. indexed variable
D. array variable
E. array

7.7  Suppose int i = 5, which of the following can be used as an index for array double t[100]?
A. i
B. rand() % 100
C. i + 10
D. i + 6.5
E. rand() / 100

7.8  Which of the following statements are true?
A. Every element in an array has the same type.
B. The array size is fixed after it is created.
C. The array size used to declare an array must be a constant expression.
D. The array elements are initialized when an array is created.

7.9  Analyze the following code.

 #include <iostream>
 using namespace std;

 int main()
 {
   int x[3];
   cout << "x[0] is " << x[0];

   return 0;
 }
A. The program has a compile error because the size of the array wasn't specified when declaring the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x[0] is 0.
D. The program has a runtime error because the array element x[0] is not defined.
E. x[0] has an arbitrary value.

7.10  Which of the following statements is valid?
A. int i(30);
B. double d[30];
C. int i[] = {3, 4, 3, 2};
D. int[] i = {3, 4, 3, 2};
E. int i[4] = {3, 4, 3, 2};

7.11  How can you initialize an array of two characters to 'a' and 'b'?
A. char[] charArray = {'a', 'b'};
B. char[2] charArray = {'a', 'b'};
C. char charArray[] = {'a', 'b'};
D. char charArray[2] = {'a', 'b'};

7.12  Given the following two arrays:

char s1[] = {'a''b''c'};
char s2[] = "abc";

Which of the following statements is correct?
A. s1 has three characters
B. s2 has three characters
C. s1 has four characters
D. s2 has four characters

7.13  Which of the following statements are correct?
A. A string literal is a C-string.
B. A C-string is a sequence of characters ending with a null terminator.
C. An array of characters is a C-string.
D. A C-string is a sequence of characters.

7.14  What is the output of the following code?

double myList[] = {155551};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < 6; i++) 
{
  if (myList[i] > max)
  {
    max = myList[i];
    indexOfMax = i;
  }
}
cout << indexOfMax << endl;
A. 0
B. 1
C. 2
D. 3
E. 4

7.15  What is the output of the following code?

     int myList[] = {1, 2, 3, 4, 5, 6};
 
     for (int i = 4; i >= 0; i--)
     {
       myList[i + 1] = myList[i];
     }

     for (int i = 0; i < 6; i++)
       cout << myList[i] << " ";
A. 1 2 3 4 5 6
B. 6 1 2 3 4 5
C. 6 2 3 4 5 1
D. 1 1 2 3 4 5
E. 2 3 4 5 6 1

7.16  Assume int t[] = {1, 2, 3, 4}. What is t[4]?
A. 0
B. 3
C. 4
D. 5
E. accessing t[4] may result in a runtime error

7.17  Analyze the following code:

 #include <iostream>
 using namespace std;
 
 int main()
 {
   int x[5];
   int i;
   for (i = 0; i < 5; i++)
     x[i] = i;
     cout << x[i] << " ";

   return 0;
 }
A. The program displays 0 1 2 3 4.
B. The program displays 4.
C. The program may have a runtime error because the last statement in the main function has the out of bound index for the array.
D. The program has a compile error because i is not defined in cout << x[i] << " ".

7.18  What is the output of the following code:

 #include <iostream>
 using namespace std;

 int main()
 {
   int x[] = {120, 200, 16};
   for (int i = 0; i < 3; i++)
     cout << x[i] << " ";

   return 0;
 }
A. 120 200 16
B. 16 200 120
C. 200 120 16
D. 16 120 200

7.19  Can you copy an array as follows:

  int x[] = {120, 200, 16};
  int y[3];

  y = x;
A. Yes
B. No

7.20  What is the output of the following code:

    int list[] = {1, 2, 3, 4, 5, 6};

    for (int i = 1; i < 6; i++)
      list[i] = list[i - 1];
    
    for (int i = 0; i < 6; i++)
      cout << list[i] << " ";
A. 1 2 3 4 5 6
B. 2 3 4 5 6 6
C. 2 3 4 5 6 1
D. 1 1 1 1 1 1

Section 7.5 Passing Arrays to Functions
7.21  When you pass an array to a function, __________ is passed to the array parameter in the function.
A. a copy of the array
B. a copy of the first element
C. the starting address of the array
D. the length of the array

Section 7.6 Preventing Changes of Array Arguments in Functions
7.22  Analyze the following code:

  #include <iostream>
  using namespace std;
  
  void reverse(const int list[], const int size, int newList[])
  {
    for (int i = 0; i < size; i++)
      newList[i] = list[size - 1 - i];
  }
  
  int main()
  {
    int list[] = {1, 2, 3, 4, 5};
    int newList[5];
  
    reverse(list, 5, newList);
    for (int i = 0; i < 5; i++)
      cout << newList[i] << " ";

    return 0;
  }
A. The program displays 1 2 3 4 6.
B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.
C. The program displays 5 4 3 2 1.
D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

Section 7.7 Returning Arrays from Functions
7.23  Show the output of the following code:

 #include <iostream>
 using namespace std;

 void increase(int x[], int size)
 {
   for (int i = 0; i < size; i++)
     x[i]++;
 }

 void increase(int y)
 {
   y++;
 }

 int main()
 {
   int x[] =
   {
     1, 2, 3, 4, 5
   };

   increase(x, 5);

   int y[] =
   {
     1, 2, 3, 4, 5
   };

   increase(y[0]);

   cout << x[0] << " " << y[0];

   return 0;
 }
A. 0 0
B. 1 1
C. 2 2
D. 2 1
E. 1 2

Section 7.9 Searching Arrays
7.24  For the binarySearch method in Section 7.8.2, what is low and high after the first iteration of the while loop for the list {1, 4, 6, 8, 10, 15, 20} and key 11?
A. low is 0 and high is 6
B. low is 0 and high is 3
C. low is 3 and high is 6
D. low is 4 and high is 6
E. low is 0 and high is 5

Section 7.10 Sorting Arrays
7.25  Use the selectionSort method presented in this section to answer this question. Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the content of list after the first iteration of the outer loop in the method?
A. 3.1, 3.1, 2.5, 6.4, 2.1
B. 2.5, 3.1, 3.1, 6.4, 2.1
C. 2.1, 2.5, 3.1, 3.1, 6.4
D. 3.1, 3.1, 2.5, 2.1, 6.4
E. 2.1, 3.1, 2.5, 6.4, 3.1

7.26  Use the selectionSort method presented in this section to answer this question. What is list1 after executing the following statements?

double list1[] = {3.13.12.56.4};
selectionSort(list1, 4);
A. list1 is 3.1, 3.1, 2.5, 6.4
B. list1 is 2.5 3.1, 3.1, 6.4
C. list1 is 6.4, 3.1, 3.1, 2.5
D. list1 is 3.1, 2.5, 3.1, 6.4

Section 7.11 C-Strings
7.27  Suppose you declare

char city[7] = "Dallas"

How many characters are stored in city?
A. 5
B. 6
C. 7
D. 8

7.28  Are the following two declarations the same?

char city[7] = "Dallas"
char city[] = "Dallas"
A. yes
B. no

7.29  Are the following two declarations the same?

char city[8] = "Dallas"
char city[] = "Dallas"
A. yes
B. no

7.30  Are the following two declarations the same?

char city[] = {'D''a''l''l''a''s'}; 
char city[] = "Dallas"
A. yes
B. no

7.31  Suppose char city[7] = "Dallas"; what is the output of the following statement?

cout << city; 
A. D
B. Dallas
C. Dallas0
D. nothing printed

7.32  Show the output of the following code:

char city[7] = "Dallas"
cout << strlen(city);
A. 5
B. 6
C. 7
D. 8

7.33  What is wrong in the following code?

char city[7] = "Dallas"
char s1[7];
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[7] to char s1[], it will work correctly.
C. If you change char city[7] to char city[], it will work correctly.
D. If you change char s1[7] to char s1[17], it will work correctly.

7.34  What will be displayed by the following code?

char s2[7] = "Dallas"
char s1[14] = "Dallas";
strcat(s1, s2);
cout << s1;
A. Dallas
B. DallasDallas
C. D
D. DD

7.35  What will be displayed by the following code?

char s2[7] = "Dallas"
char s1[14] = "Dallas";

cout << strcmp(s1, s2);
A. 1
B. 0
C. -1
D. 2

7.36  THe ? mark in the following code should be replaced by ___________.

 #include <iostream>
 #include <cstring>
 using namespace std;

 int countLetters(const char s[])
 {
   int count = 0;
   for (int i = 0; i < ?; i++)
   {
     if (isalpha(s[i])) count++;
   }

   return count;
 }

 int main()
 {
    // Prompt the user to enter a string
    cout << "Enter a string: ";
    char s[80];
    cin.getline(s, 80);

    cout << "The number of letters in " << s << " is " <<
      countLetters(s) << endl;

    return 0;
 }
A. 80
B. 79
C. strlen(s)
D. strlen(s) - 1