COSC 1315

Programming Fundamentals

 Practice Text #190

One-Dimensional Arrays

Revised: February 5, 2007
By Richard G. Baldwin

File Pfsg00190.htm
Practice Text Index


Welcome

The practice tests in this series were written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  They consists of questions, answers, and explanations.  The questions are based on the material covered in my series of online lecture notes for the course.  Each practice test is keyed to a specific lecture.  This practice test is keyed to lecture #190 titled One-Dimensional Arrays.

Questions



190-1.  True or False.  An array is a list of variables, all of the same type, all of which have the same name.  Each variable in the array is called an element.  Each element in the array has a subscript or index that differentiates it from the other elements in the array.

Answer and Explanation

190-2.  True or False.  The first element in a C++ array always has an index value of 1.

Answer and Explanation

190-3.  True or False.  You must declare a C++ array before you can use it.

Answer and Explanation

190-4.  True or False.  When you declare an array in C++, you create a data structure that contains the list of variables.

Answer and Explanation

190-5.  True or False.  The size of a C++ array is automatically initialized to a single element, and grows automatically on an as-needed basis.

Answer and Explanation

190-6.  True or False.  If the size of a C++ array is N, the index values for the elements in the array always extend from 0 to N-1.

Answer and Explanation

190-7.  True or False.  You access an individual element in a C++ array by:

Answer and Explanation

190-8.  True or False.  Because you can use the name of a variable as the index when accessing an element from an array, it is possible to:

Answer and Explanation

190-9.  True or False.  When you declare an array in C++, all of the element values are automatically initialized to a value of zero.

Answer and Explanation

190-10.  True or False.  When programming in C++, you should always make certain that you purposely store a value into each array element before you access and use the value stored in that element.

Answer and Explanation

190-11.  True or False.  It is not possible to store or fetch data from outsides the bounds of an array in C++.

Answer and Explanation

190-12.  True or False.  Accessing an array element that is outside the bounds of the array is a serious logic error in C++ programming.

Answer and Explanation

190-13.  True or False.  If you fetch and use six element values from consecutive indices in a five-element array, one of those element values will be garbage because it really isn't part of the array.

Answer and Explanation

190-14.  True or False.  The size of a C++ array must be established at compile time and cannot be established at runtime.

Answer and Explanation

190-15.  True or False.  The code shown in Listing 190-15 will declare a C++ array designed to store five values of type long.

Listing 190-15.
    int size = 5;
    long[] values = new long[size];

Answer and Explanation

190-16.  True or False.  The declaration syntax for a c++ array consists of:

Answer and Explanation

190-17.  True or False.  The conversation between the program and the user shown in Listing 190-17b is valid for the program shown in Listing 190-17a.

Listing 190-17a.
/*File:  Array01a.cpp
Revised 02/04/07
************************************************/

#include <iostream>
using namespace std;

class Array01a{ 
  public:
  static void classMain(){
    Array01a* ptrToObject = new Array01a();
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Array01a class
  void doSomething(){
    const int size = 3;
    long values[size];
    getData(values,size);
    cout << "Array contents:" << endl;
    displayData(values,size);
  }//end doSomething function
  //-------------------------------------------//

  void getData(long array,int size){
    int count = size - 1;
    while(count >= 0){
      cout << "Enter an integer value: ";
      cin >> array[count];
      count = count - 1;
    }//end while loop
  }//end getData
  //-------------------------------------------//

  void displayData(long array,int size){
    int count = 0;
    while(count < size){
      cout << array[count] << " ";
      count = count + 1;
    }//end while loop
    cout << endl;
  }//end displayData
  //-------------------------------------------//

};//End Array01a class
//---------------------------------------------//

int main(){
  Array01a::classMain();
  return 0;
}//end main

Listing 190-17b.
Enter an integer value: 0
Enter an integer value: 1
Enter an integer value: 2
Array contents:
2 1 0

Answer and Explanation

190-18.  What output is produced by the program shown in Listing 190-18?

Listing 190-18.
/*File:  Array01b.cpp
Revised 02/05/07
************************************************/
#include <iostream>
using namespace std;

class Array01b{ 
  public:
  static void classMain(){
    Array01b* ptrToObject = new Array01b();
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//
  void doSomething(){
    const int size = 3;
    long values[size];
    for(int cnt = size;cnt > 0;cnt = cnt-1){
      values[cnt-1] = cnt;
    }//end for loop
    displayData(values,size);
  }//end doSomething function
  //-------------------------------------------//
  void displayData(long array[],int size){
    int count = 0;
    while(count < size){
      cout << array[count] << " ";
      count = count + 1;
    }//end while loop
    cout << endl;
  }//end displayData
  //-------------------------------------------//
};//End Array01b class
//---------------------------------------------//
int main(){
  Array01b::classMain();
  return 0;
}//end main

Answer and Explanation



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com


Answers and Explanations

Answer 18

C.  1 2 3

Back to Question 18

Explanation 18

Answer 17

False

Back to Question 17

Explanation 17

The type specification for the first parameter in the formal parameter list for the functions named getData and displayData is incorrect.  An empty subscript operator [] must be used in the declaration of the first parameter to specify that the first parameter is an array.  The program won't compile.

Answer 16

True

Back to Question 16

Explanation 16

Answer 15

False

Back to Question 15

Explanation 15

This is not the correct syntax for declaring a C++ array.

Answer 14

False

Back to Question 14

Explanation 14

Answer 13

True

Back to Question 13

Explanation 13

Answer 12

True

Back to Question 12

Explanation 12

Answer 11

False

Back to Question 11

Explanation 11

Answer 10

True

Back to Question 10

Explanation 10

Answer 9

False

Back to Question 9

Explanation 9

Answer 8

True

Back to Question 8

Explanation 8

Answer 7

False

Back to Question 7

Explanation 7

Use the subscript operator [] instead of parentheses () to enclose the index.

Answer 6

True

Back to Question 6

Explanation 6

Answer 5

False

Back to Question 5

Explanation 5

You specify the number of elements (the size) of a C++ array when you declare it.  Once created, the size of a C++ array cannot be changed.

Answer 4

True

Back to Question 4

Explanation 4

Answer 3

True

Back to Question 3

Explanation 3

Answer 2

False

Back to Question 2

Explanation 2

The first element in a C++ array always has an index value of 0.

Answer 1

True

Back to Question 1

Explanation 1



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-