Revised: February 5, 2007
By Richard G. Baldwin
File Pfsg00190.htm
Practice Text Index
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.
190-2. True or False. The first element in a C++ array always has an index value of 1.
190-3. True or False. You must declare a C++ array before you can use it.
190-4. True or False. When you declare an array in C++, you create a data structure that contains the list of variables.
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.
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.
190-7. True or False. You access an individual element in a C++ array by:
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:
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.
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.
190-11. True or False. It is not possible to store or fetch data from outsides the bounds of an array in C++.
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.
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.
190-14. True or False. The size of a C++ array must be established at compile time and cannot be established at runtime.
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]; |
190-16. True or False. The declaration syntax for a c++ array consists of:
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 |
Enter an integer value: 0 Enter an integer value: 1 Enter an integer value: 2 Array contents: 2 1 0 |
190-18. What output is produced by the program shown in 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 |
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.
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.
This is not the correct syntax for declaring a C++ array.
Use the subscript operator [] instead of parentheses () to enclose the index.
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.
The first element in a C++ array always has an index value of 0.
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.
-end-