C++ Ch 7 Arrays Quiz

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Tcarteronw
T
Tcarteronw
Community Contributor
Quizzes Created: 38 | Total Attempts: 30,183
Questions: 29 | Attempts: 4,074

SettingsSettingsSettings
C++ Ch 7 Arrays Quiz - Quiz

Quiz over Arrays in C++


Questions and Answers
  • 1. 

    Name 3 types of sorts used with arrays.

  • 2. 

    The locations of the various indexed variables in an array can be spread out all over the memory.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The locations of the various indexed variables in an array cannot be spread out all over the memory. In an array, the indexed variables are stored in contiguous memory locations. This means that the memory locations for the variables are consecutive and adjacent to each other. Therefore, the correct answer is False.

    Rate this question:

  • 3. 

    If a function is expecting a pass by reference parameter, you can pass an index variable from an array of the same base type to that function.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    When a function is expecting a pass by reference parameter, it means that the function will modify the value of the parameter directly. In this case, if the function is expecting a pass by reference parameter and you pass an index variable from an array of the same base type, the function will be able to modify the value of that specific element in the array. This is because the index variable will point to the memory location of that element, allowing the function to directly access and modify it. Therefore, the statement is true.

    Rate this question:

  • 4. 

    When you have a function that expects an array, it should also expect the size of the array or the number of indexed variables with valid data.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    When a function expects an array, it is important for it to also expect the size of the array or the number of indexed variables with valid data. This is because the function needs to know the length or size of the array in order to properly handle and manipulate its elements. Without this information, the function may encounter errors or unexpected behavior when trying to access or modify the array's elements. Therefore, it is true that a function should expect the size of the array or the number of indexed variables with valid data.

    Rate this question:

  • 5. 

    The following function declaration guarantees the values in the array argument are not changed. void function1(int array[], int numElements);

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    This function declaration does not guarantee that the values in the array argument will not be changed. The absence of any const keyword or other modifiers in the declaration means that the function is free to modify the values in the array. Therefore, it is possible for the values to be changed within the function.

    Rate this question:

  • 6. 

    The following function will work with any size integer array. void function1(int array[], int numElements);

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The given function, function1, is designed to work with any size integer array. It takes two parameters: an integer array called "array" and an integer called "numElements". This means that the function can handle arrays of any length, as the size of the array is not specified or limited. Therefore, the statement "True" is correct.

    Rate this question:

  • 7. 

    What are the valid indexes for the array shown below? int myArray[25];

    • A.

      0-25

    • B.

      0-24

    • C.

      1-25

    • D.

      1-24

    Correct Answer
    B. 0-24
    Explanation
    The valid indexes for the array shown are from 0 to 24. Arrays in most programming languages are zero-indexed, meaning the first element is at index 0 and the last element is at index n-1, where n is the size of the array. In this case, the array has a size of 25, so the valid indexes range from 0 to 24.

    Rate this question:

  • 8. 

    Why should you use a named constant for the size of an array?

    • A.

      Readability of code

    • B.

      Makes changes to the program easier

    • C.

      Helps reduce logic errors

    • D.

      All answers are correct

    Correct Answer
    D. All answers are correct
    Explanation
    Using a named constant for the size of an array is beneficial for several reasons. Firstly, it improves the readability of the code by making it clear and understandable. Secondly, it makes changes to the program easier as the size of the array can be modified by simply changing the value of the constant in one place. This saves time and effort in searching for and updating multiple instances of the array size. Lastly, using a named constant helps reduce logic errors by ensuring that the array size remains consistent throughout the program.

    Rate this question:

  • 9. 

    What is wrong with the following code fragment? const int SIZE =5; float scores[SIZE]; for(int i=0; i<=SIZE;i++) { cout << "Enter a score\n"; cin >> scores[i]; }

    • A.

      Array indexes start at 1 not 0

    • B.

      Arrays must be integers

    • C.

      Array indexes must be less than the size of the array

    • D.

      Should be cin >> scores[0];

    Correct Answer
    C. Array indexes must be less than the size of the array
    Explanation
    The code has an error in the for loop condition. The condition should be "i < SIZE" instead of "i

    Rate this question:

  • 10. 

    Which of the following declare an array of 5 characters, and initializes them to some known values?

    • A.

      Char array[5]={'a','b','c','d','e'};

    • B.

      Char array[4]={'a','b','c','d','e'};

    • C.

      Char array[5]={' '};

    • D.

      Char array[]={'a','b','d','e'};

    • E.

      Char array[5]={'a','b','c','d','e'}; and char array[5]={' '};

    Correct Answer
    E. Char array[5]={'a','b','c','d','e'}; and char array[5]={' '};
    Explanation
    The correct answer is "char array[5]={'a','b','c','d','e'}; and char array[5]={' '};". These two options declare an array of 5 characters and initialize them to some known values. The first option initializes the array with the characters 'a', 'b', 'c', 'd', and 'e'. The second option initializes the array with a space character.

    Rate this question:

  • 11. 

    Arrays are always passed to a function using

    • A.

      Pass by value

    • B.

      Pass by reference

    • C.

      Pass by array

    • D.

      You cannot pass arrays to a function

    Correct Answer
    B. Pass by reference
    Explanation
    When an array is passed to a function using pass by reference, the memory address of the array is passed to the function. This means that any changes made to the array within the function will also affect the original array outside of the function. This is different from pass by value, where a copy of the array is made and any changes made within the function do not affect the original array. Therefore, pass by reference is the correct answer because it accurately describes how arrays are passed to a function.

    Rate this question:

  • 12. 

    Which of the following function declarations correctly guarantee that the function will not change any values in the array argument?

    • A.

      Void f1(int array[], int size) const;

    • B.

      Void f1(int array[], int size);

    • C.

      Void f1(int &array, int size);

    • D.

      Void f1(const int array[], int size);

    Correct Answer
    D. Void f1(const int array[], int size);
    Explanation
    The correct answer is "void f1(const int array[], int size)". This function declaration guarantees that the function will not change any values in the array argument because the "const" keyword before the "int" data type specifies that the array is a constant and cannot be modified within the function.

    Rate this question:

  • 13. 

    If we want a search function to search an array for some value and return either the index where the value was found, or -1 if not found, which of the following prototypes would be appropriate?

    • A.

      Void search(const int array, int target, int numElements);

    • B.

      Void search(const int array, int target);

    • C.

      Int search(const int array[], int numElements);

    • D.

      Int search(const int array[], int target, int numElements

    Correct Answer
    D. Int search(const int array[], int target, int numElements
    Explanation
    The correct answer is "int search(const int array[], int target, int numElements)". This prototype is appropriate because it takes in an array, a target value to search for, and the number of elements in the array. It returns an integer, which will be the index where the target value was found, or -1 if the target value was not found in the array.

    Rate this question:

  • 14. 

    Given the following function definition, will repeated calls to the search function for the same target find all occurrences of that target in the array? int search(const int array[], int target, int numElements) { int index=0; bool found=false; while((!found) && (index < numElements)) { if(array[index] == target) found=true; else index++; } if(found==true) return index; else return -1; }

    • A.

      Yes

    • B.

      No

    • C.

      Impossible to tell without looking at the values of the array

    • D.

      It depends on the value of target.

    Correct Answer
    B. No
    Explanation
    The given function will not find all occurrences of the target in the array. It will only find the first occurrence of the target and return its index. If the target appears multiple times in the array, the function will not continue searching for the remaining occurrences after finding the first one. Therefore, repeated calls to the search function for the same target will not find all occurrences.

    Rate this question:

  • 15. 

    Which of the following function declarations will accept the following two- dimension array? int pages[10][30];

    • A.

      Void f1(int pages[][], int size);

    • B.

      Void f1(int pages[][30], int size);

    • C.

      Void f1(int pages[10][], int size);

    • D.

      Void f1(int& pages, int size);

    Correct Answer
    B. Void f1(int pages[][30], int size);
    Explanation
    The correct answer is "void f1(int pages[][30], int size)". This function declaration accepts a two-dimensional array "pages" with 10 rows and 30 columns. The first dimension is left empty to indicate that the size is not specified and can vary. The second dimension is explicitly specified as 30 to match the size of the second dimension of the given array. The "size" parameter is used to indicate the size of the array.

    Rate this question:

  • 16. 

    If you need a function that will handle multi-dimensional arrays, you must specify the following sizes inside the square brackets.

    • A.

      All the sizes

    • B.

      All sizes except the last dimension

    • C.

      All sizes except the first dimension

    • D.

      None of the sizes

    Correct Answer
    C. All sizes except the first dimension
    Explanation
    When working with multi-dimensional arrays, specifying all sizes except the first dimension is necessary. This is because the first dimension represents the number of rows or sub-arrays, while the remaining dimensions represent the number of columns or elements within each sub-array. By specifying all sizes except the first dimension, the function will be able to handle arrays of any length in the remaining dimensions, allowing for flexibility and versatility in working with multi-dimensional arrays.

    Rate this question:

  • 17. 

    What is the output of the following code fragment? int array[4][4], index1, index2; for(index1=0;index1<4;index1++) for(index2=0;index2<4;index2++) array[index1][index2]=index1 + index2; for(index1=0;index1<4;index1++) { for(index2=0;index2<4;index2++) cout << array[index1][index2] << " "; cout << endl; }

    • A.

      0 1 2 3 1 2 3 4 2 3 4 5 3 4 5 6

    • B.

      0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3

    • C.

      0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3

    • D.

      0 0 0 0 0 1 2 3 0 2 4 6 0 3 6 9

    Correct Answer
    A. 0 1 2 3 1 2 3 4 2 3 4 5 3 4 5 6
    Explanation
    The code fragment initializes a 4x4 array called "array" and two variables "index1" and "index2". It then uses nested for loops to assign values to each element of the array based on the sum of "index1" and "index2". Finally, it uses another set of nested for loops to print the values of each element in the array. The output of the code will be a 4x4 matrix where each element is the sum of its row and column indices.

    Rate this question:

  • 18. 

    Which of the following function declarations can be passed the following array? char myArray[6][8];

    • A.

      Void f1(char a[][], int sizeOfFirst);

    • B.

      Void f1(char a[][8], int sizeOfFirst);

    • C.

      Void f1(char a[6][8], int sizeOfFirst);

    • D.

      Void f1(char a[][8], int sizeOfFirst); and void f1(char a[6][8], int sizeOfFirst);

    Correct Answer
    D. Void f1(char a[][8], int sizeOfFirst); and void f1(char a[6][8], int sizeOfFirst);
    Explanation
    The correct answer is void f1(char a[][8], int sizeOfFirst); and void f1(char a[6][8], int sizeOfFirst). These two function declarations can be passed the given array because they both specify the correct dimensions of the array. The first declaration uses the syntax of a two-dimensional array with an unspecified number of rows and 8 columns, which matches the dimensions of the given array. The second declaration explicitly specifies the dimensions of the array as 6 rows and 8 columns, which also matches the dimensions of the given array.

    Rate this question:

  • 19. 

    A two dimension array can also be thought of as

    • A.

      A table

    • B.

      An array of arrays

    • C.

      A file

    • D.

      A table and an array of arrays

    Correct Answer
    D. A table and an array of arrays
    Explanation
    A two-dimensional array can be visualized as a table because it has rows and columns, similar to how a table is structured. Each element in the array represents a cell in the table. Additionally, a two-dimensional array can also be considered as an array of arrays because it is essentially an array where each element is another array. This means that each row in the table is represented as an array within the main array. Therefore, the correct answer is that a two-dimensional array can be thought of as both a table and an array of arrays.

    Rate this question:

  • 20. 

    Memory for the formal parameters and variables declared in the body of a function is a / an

    • A.

      Local variable

    • B.

      Local identifier

    • C.

      Automatic variable

    Correct Answer
    A. Local variable
    Explanation
    The correct answer is "Local variable". In programming, a local variable is a variable that is declared and used within a specific function or block of code. It is only accessible within that function or block and its memory is allocated and deallocated automatically when the function or block is entered and exited, respectively. The term "local" indicates that the variable is limited to a specific scope and is not accessible from outside that scope.

    Rate this question:

  • 21. 

    What refers to where in the program an identifier is accessible (visible)?

    • A.

      Scope of an identifier

    • B.

      Reference parameter

    • C.

      Scope resolution operator

    • D.

      Static Variable

    Correct Answer
    A. Scope of an identifier
    Explanation
    The term "scope" refers to the part of a program where a particular identifier, such as a variable or function, is accessible or visible. It determines where in the program the identifier can be used. Therefore, the correct answer is "Scope of an identifier."

    Rate this question:

  • 22. 

    What type of variable do you have when the memory for the variable remains allocated as long as the program executes?

    • A.

      Static

    • B.

      Automatic

    • C.

      Instance

    • D.

      Local

    Correct Answer
    A. Static
    Explanation
    When the memory for a variable remains allocated as long as the program executes, it means that the variable has a static type. Static variables are allocated memory at the start of the program and retain their values throughout the entire execution. They are useful when a variable needs to maintain its value across multiple function calls or when it needs to be accessed by different parts of the program.

    Rate this question:

  • 23. 

    What is the correct way to create an array named golfScores that witll hold 5 game scores.

    • A.

      Int golfScores[5];

    • B.

      Float golfScores[5];

    • C.

      Int golfScores;

    • D.

      String golfScores[]

    Correct Answer
    A. Int golfScores[5];
    Explanation
    The correct way to create an array named golfScores that will hold 5 game scores is by using the syntax "int golfScores[5];". This declares an array named golfScores of type int and specifies that it can hold 5 elements.

    Rate this question:

  • 24. 

    Int testScores[] = {100, 95, 60, 75, 85, 55};   // Add 5 points to each test score for (int i = 0; i < 6; i++)    testScores[i] += 5;   // Display the test scores for (int i = 0; i < 6; i++)    cout << testScores[i] << endl; This code results in the following output: Output: 100 105 60 85 95 66

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The given code adds 5 points to each test score in the testScores array using a for loop. After that, it displays the updated test scores. The output shown in the answer is incorrect. The correct output should be:10510065809060Therefore, the correct answer is False.

    Rate this question:

  • 25. 

    When passing an array as a parameter, in the calling function youn need two things.  They are:

    • A.

      Name of the array

    • B.

      Size of the array

    • C.

      Number of items in the array

    Correct Answer(s)
    A. Name of the array
    B. Size of the array
    Explanation
    When passing an array as a parameter in the calling function, you need to provide the name of the array so that the function knows which array to work with. Additionally, you need to specify the size of the array so that the function knows the length of the array and can access its elements correctly. The number of items in the array is also necessary information for the function to handle the array appropriately.

    Rate this question:

  • 26. 

    The correct syntax for writing a 2-D array is: dataType arrayName[numCols][numRows];

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    dataType arrayName[numRows][numCols];

    Rate this question:

  • 27. 

    Give the following declarations, which of the following is a legal call to this function? int myFunction(int myValue); int myArray[1000];

    • A.

      Cout << myFunction(myArray);

    • B.

      Cout << myFunction(myArray[0]);

    • C.

      MyArray[1] = myFunction(myArray[0]);

    • D.

      Cout << myFunction(myArray[0]); and myArray[1] = myFunction(myArray[0]);

    Correct Answer
    C. MyArray[1] = myFunction(myArray[0]);
    Explanation
    The legal call to the function int myFunction(int myValue) is myArray[1] = myFunction(myArray[0]). This assigns the result of calling myFunction with myArray[0] as its argument to myArray[1]. The function expects an integer argument, and myArray[0] is an integer value, making this call valid.

    Rate this question:

  • 28. 

    Which of the following will correctly assign all the values in one array to the other array? (Assume both arrays are of the same type and have SIZE elements)

    • A.

      Array1=array2;

    • B.

      Array1[]=array2;

    • C.

      Array1[]=array2; for(i=0;i Continue to next questionSubmit QuizWhich of the followi...What refers to where...If we want a search ...When passing an arra...Given the following ...Which of the followi...If you need a functi...What type of va...What is the output o...Which of the followi...A two dimension arra...  a. Index ...the correct syntax f...Name 3 types of sort...What is the correct ...Memory for the forma...The locations of the...int testScores[] = {...If a function is exp...When you have a func...The following functi...The following functi...What are the valid i...Why should you use a...What is wrong with t...Which of the followi...Arrays are always pa...Give the following d...

    • D.

      For(i=0;i Continue to next questionSubmit QuizWhich of the followi...What refers to where...If we want a search ...When passing an arra...Given the following ...Which of the followi...If you need a functi...What type of va...What is the output o...Which of the followi...A two dimension arra...  a. Index ...the correct syntax f...Name 3 types of sort...What is the correct ...Memory for the forma...The locations of the...int testScores[] = {...If a function is exp...When you have a func...The following functi...The following functi...What are the valid i...Why should you use a...What is wrong with t...Which of the followi...Arrays are always pa...Give the following d...

    Correct Answer
    B. Array1[]=array2;
    Explanation
    The correct answer is "array1[]=array2;". This statement will correctly assign all the values in array2 to array1. The square brackets indicate that the entire array will be assigned, rather than just a single element. The for loop statements provided in options c and d are incomplete and do not make sense in this context.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 08, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Nov 20, 2013
    Quiz Created by
    Tcarteronw
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.