Exam 2 Practice Questions

Mon Nov 10 2025
Updated: Wed Nov 12 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Definitions

  1. Match the term on the left to the definition on the right (Repetition).
___Repetitiona.
The block of statements executed each time the loop iterates.
___Loop Bodyb.
A single execution of the loop body.
___Iteration of a Loopc.
Variables whose value determines whether a loop should execute another iteration or not.
___Loop Control Variabled.
A type of control structure that allows a block of code to be executed multiple times.
  1. Match the term on the left to the definition on the right (File I/O).
___Filea.
A named portion of secondary memory used to permanently store data.
___Output Fileb.
Files that contain data to be read by a program.
___Input Filec.
Files that have data written to them by a program.
  1. Match the term on the left to the definition on the right (Arrays).
___1D Arraya.
index < 0 or index ≥ size
___In Bounds Index (1D Array)b.
A list of elements of the same data type contiguous in main memory.
___Out of Bounds Index (1D Array)c.
Two or more arrays of the same size used to store records, where each array holds a field of the individual records in the same index across the arrays.
___Segmentation Faultd.
An array of arrays, storing elements in rows and columns like a table or grid.
___Recorde.
A collection of data, possibly of different types, fixed in number and sequence.
___Parallel Arrayf.
An error which occurs when an invalid location in memory has been accessed or edited.
___2D Arrayg.
0 ≤ index ≤ size - 1
  1. Match the term on the left to the definition on the right (Functions).
___User-Defined Functiona.
The first line of a function definition that specifies a function’s return type, identifier, and parameters.
___Parametersb.
Formal parameter that receives a copy of the value of the corresponding actual parameter.
___Return Valuec.
Formal parameter that receives the address of the corresponding actual parameter.
___Return Typed.
A declaration that tells the compiler a function exists and how it should be used before the function is fully defined later in a program.
___Function Headere.
Variables declared in a function header to act as inputs to receive values the function will process.
___Function Bodyf.
The region of a program where an identifier can be accessed or used.
___Function Callg.
The kind of data (if any) a return value is.
___Function Prototypeh.
Tells the program to execute the code inside a function’s body.
___Function Signaturei.
A function the programmer creates to perform a specific task.
___Scopej.
Actual values passed to a function when it is called.
___Local Scopek.
The actual data (if any) sent back when a function completes.
___Global Scopel.
A unique fingerprint made up of a function’s identifier and types of parameters.
___Formal Parametersm.
Variables and constants defined in the function heading to be used in the function body.
___Actual Parametersn.
Identifiers declared inside a block of code that can be accessed only within the block in which they were created.
___Value Parametero.
The set of instructions (a block of code) that execute when a function is called.
___Reference Parameterp.
Identifiers created outside of a block of code that can be accessed anywhere within a program.

Short Answer

  1. What are the 3 different types of loops and how do they differ?

  2. When is it best to use a while loop?

  3. When is it best to use a do...while loop?

  4. When is it best to use a for loop?

  5. What is a loop control variable?

  6. What happens if a loop control variable is not properly updated?

  7. How do infinite loops present themselves in programs at runtime?

  8. In general, what are the valid indices of a 1D array?

  9. In 1 line of code, create a 1D array of strings and fill it with the 3 elements: luke skywalker, leia organa, and obi-wan kenobi.

  10. What are the inbound indices of the 1D array created in the previous question?

  11. What error can happen if an invalid index is accessed in an array?

  12. In general, what does a segmentation fault mean?

  13. What is a record of data?

  14. How can 1000 records of data be stored in a program?

  15. Explain how parallel arrays are created and maintained in a program.

  16. How does a 2D array differ from a 1D array?

  17. In 1 line create a 3x3 2D array of characters and fill it with the 9 elements: row 1: r, b, w, row 2: g, g, g, and row 3: y, r, r.

  18. When accessing a 2D array element what 2 pieces of information need to be provided?

  19. What is the difference between a function header and prototype?

  20. What happens if a function prototype is forgotten?

  21. What does a function prototype do?

  22. What is a function signature?

  23. Provide the signatures for the following functions:

    • int countOpenRides(string rides[], bool isOpen[])
    • void displayRides(string rides[], bool isOpen[])
    • string concatenate(string left, string right)
    • double area(double radius)
    • char continue()
  24. Where does the code that will be executed when the function is called go?

  25. What are parameters?

  26. What is the difference between actual and formal parameters?

  27. What is the difference between value and reference parameters?

  28. What is the difference between a void and value-returning functions?

  29. How are functions used?

  30. What is the point of writing functions?


Error Code

  1. In the following program fix all the errors (write the fixes directly on the code)

    #include <iostream> using namespace std; int main() { int end = 0, i = 0; // get valid number do { cout << “End:; cin >> end; if (cin.fail() || i < 0) { cout << “Invalid number\n”; cin.clear(); cin.ignore(256, ‘\n’); } } while (cin.fail() || i < 0); // output numbers between 0 and input number for (i <= end) { cout << i << endl; i++; } return 0; }
  2. In the following program fix all the errors (write the fixes directly on the code)

    using namespace std; int main() { ifstream iFile; iFile >> variable; iFile.close(); return 0; }
  3. In the following program fix all the errors (write the fixes directly on the code)

    #include <iostream> using namespace std; int main() { int SIZE = 10; int arr[SIZE] = 0; // Print all array elements cout << arr << endl; return 0; }
  4. In the following program fix all the errors (write the fixes directly on the code)

    #include <iostream> using namespace std; int main() { int ROWS = 5, COLUMNS = 3; int arr[ROWS][COLUMNS] = {}; // Print all array elements for (int i = 0; i < ROWS; i++) { cout << arr[i] << endl; } return 0; }
  5. In the following program fix all the errors (write the fixes directly on the code)

    #include <iostream> using namespace std; int main() { int i = multiplyBy3AndPrint(); return 0; } void multiplyBy3AndPrint(int i); { int j = i * 3; cout << i <<* 3 =<< j << endl; }

Write a Function That...

  1. Reads an integer from the user between a passed minimum and maximum value, then returns it.
    Prototype: int readInt(string prompt, int min, int max);

  2. Calculates the volume of a sphere using the formula:

    v=43πr3v = \frac{4}{3} \pi r^3

    Prototype: double volume(double r);

  3. Swaps 2 values in an array at the given indices.
    Prototype: void swap(int arr[], int left, int right);

  4. Splits a string on a character. Return the part before the character from the function using the return keyword, and place the part after the character in the parameter updating it via reference.
    Prototype: string split(string &toSplit)


Practice Program

  1. Write all preprocessor directives needed for the functions below.

  2. Write a function:

    string getString(const string prompt)

    That prompts the user to enter a string using the passed prompt, verifies the string’s length is in range 0 < length < 30 (if an invalid length string is entered, output an error and retry until a valid lengthed string is input), and then returns the entered string once a valid lengthed string has been entered.

  3. Write a function:

    string concatenate(const string str1, const string str2)

    Which will concatenate str1 to st2 and then return the new string.

  4. Write a function:

    void printStringToFile(const string toPrint)

    Which outputs the passed toPrint parameter to a file "output.txt". This function will always output to the same file.

  5. Using the functions written in the previous 3 questions, write a program that:

    a. Calls getString() passing it the prompt “\nEnter a string: ” and saving the result.

    b. Calls getString() passing it the prompt “\nEnter another string: ” and saving the result.

    c. Passes the 2 strings from (a) and (b) to concatenate() in that order and saves the result.

    d. Passes the string from (c) to printStringToFile().

    Example Program Interaction

    example $ ./a.out Enter a string: Error: invalid length Enter a string: This string is too long for this program to handle Error: invalid length Enter a string: A valid string My_prompt $ cat output.txt A valid string example $