26.07.2013 Views

CMPT 111 Mock Midterm 2 - Homepage Usask

CMPT 111 Mock Midterm 2 - Homepage Usask

CMPT 111 Mock Midterm 2 - Homepage Usask

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>CMPT</strong> <strong>111</strong><br />

<strong>Mock</strong><br />

<strong>Midterm</strong> 2<br />

2010-2011 Term 1<br />

This practice exam was<br />

prepared for you by your SSS<br />

coach (not your professor!).<br />

It is designed to help you test<br />

yourself on the topics covered<br />

in class and should not be<br />

considered as a preview of the<br />

actual midterm.


<strong>CMPT</strong> <strong>111</strong> <strong>Mock</strong> <strong>Midterm</strong> 2 2010-2011 University Learning Centre<br />

Section 1: Multiple Choice<br />

Circle the correct answer to the following questions.<br />

1. What type of array would you use to store your first name?<br />

a. Integer Array<br />

b. String Array<br />

c. Character Array<br />

d. Name Array<br />

2. What type of errors will the compiler identify for you?<br />

a. Syntax Errors<br />

b. Logic Errors<br />

c. Erroneous Errors<br />

3. A function can have no parameters.<br />

a. True<br />

b. False<br />

4. A function can have no return value.<br />

a. True<br />

b. False<br />

5. Can a function call itself?<br />

a. Yes<br />

b. No<br />

6. Which of these will result in an infinite loop?<br />

a. A recursive function without a recursive case<br />

b. A recursive function without a base case<br />

c. A recursive function with a recursive case and a base case<br />

7. Everything you can do with a loop you can do with recursion.<br />

a. True<br />

b. False<br />

8. What would be the base case for a recursive function to calculate the sum of all numbers from 0<br />

to an arbitrary number n?<br />

a. If (n == 1) return 1;<br />

b. If (n == 0) return 0;<br />

c. If (n == 1) return 0;<br />

d. If (n == 0) return 1;<br />

e. If (n < 1) return 1;<br />

9. What’s the best way to understand recursion?<br />

a. Don’t think about it<br />

b. Trace it out on paper<br />

10. Bonus: What was the first computer “bug”?<br />

a. Cricket<br />

b. Moth<br />

c. Mosquito<br />

d. Housefly<br />

2


<strong>CMPT</strong> <strong>111</strong> <strong>Mock</strong> <strong>Midterm</strong> 2 2010-2011 University Learning Centre<br />

Section 2: Short Answer<br />

Answer the following short answer questions in the space below.<br />

1. The square bracket operator [ ] can be used in two different ways. Name these ways and give<br />

an example for each.<br />

2. The problem solving process is listed below in the wrong order. Put it in the right order.<br />

Translate pseudo-code into code.<br />

Understand the task.<br />

Check your method critically.<br />

Test the program.<br />

Figure out how to perform the task.<br />

Write down the step-by-step method.<br />

3. What is the difference between pass-by-value and pass-by-reference functions? Give examples of<br />

each one.<br />

3


<strong>CMPT</strong> <strong>111</strong> <strong>Mock</strong> <strong>Midterm</strong> 2 2010-2011 University Learning Centre<br />

Section 3: Function Headers<br />

Fill in the function headers for the following functions.<br />

1. C++<br />

// Put function header here<br />

{<br />

}<br />

2. Pseudocode<br />

3. Pseudocode<br />

if (purple)<br />

cout


<strong>CMPT</strong> <strong>111</strong> <strong>Mock</strong> <strong>Midterm</strong> 2 2010-2011 University Learning Centre<br />

Section 4: Program Output<br />

Write down what would be output to the screen if you were to run this program.<br />

1. C++<br />

#include <br />

using namespace std;<br />

int average(int numbers[], int size) {<br />

}<br />

int sum = 0;<br />

for (int i = 0; i < size; i++) {<br />

}<br />

Output (Write in the space below)<br />

sum += numbers[i];<br />

cout


<strong>CMPT</strong> <strong>111</strong> <strong>Mock</strong> <strong>Midterm</strong> 2 2010-2011 University Learning Centre<br />

Section 5: C++ Errors<br />

Cross out the errors in this C++ program and correct them.<br />

using namespace std;<br />

float main() {<br />

}<br />

PrintNumAndChar(„8‟, 9);<br />

int myArray[6] = {1, 2, 3, 4, 5}<br />

myArray[6] = 6;<br />

myArray[1] = 1.5;<br />

return 7;<br />

printNumAndChar(a, b) {<br />

}<br />

cout


<strong>CMPT</strong> <strong>111</strong> <strong>Mock</strong> <strong>Midterm</strong> 2 2010-2011 University Learning Centre<br />

Section 6: Function Creation<br />

Create a function to accomplish the following problems. Use the header given.<br />

Please note: Do not use recursion in your answers.<br />

1. Pseudocode<br />

Write a function to figure out the factorial of a number.<br />

Example: factorial(5) = 5! = 5 * 4 * 3 * 2 * 1 = 120<br />

int factorial(int n)<br />

2. C++<br />

Write a function to initialize a two-dimensional array with all zeroes.<br />

void initialize2dArray(int array[][10], int size) {<br />

7


<strong>CMPT</strong> <strong>111</strong> <strong>Mock</strong> <strong>Midterm</strong> 2 2010-2011 University Learning Centre<br />

3. Pseudocode<br />

Write a function that checks whether or not two arrays have all the same elements, regardless<br />

of the order.<br />

For instance:<br />

Array 1 – 1, 4, 6, 7 and Array 2 – 4, 6, 1, 7 both have the same elements in a different order.<br />

Please note: This is a large task, so it is recommended to use abstraction to break it into several<br />

smaller problems. Therefore, write additional functions to help make this problem easier.<br />

bool sameElements(int array1[], int array1size, int array2[], int array2size)<br />

8


<strong>CMPT</strong> <strong>111</strong> <strong>Mock</strong> <strong>Midterm</strong> 2 2010-2011 University Learning Centre<br />

Section 7: Recursion (BONUS!)<br />

Write a recursive function in C++ to solve the task listed below.<br />

Sort an array of integers. The array will always be a size that is a power of 2, such as 1, 2, 4, 8, 16, etc.<br />

This should be a clue as to how I envision the problem being solved.<br />

Good luck, this is very difficult.<br />

void sort(__________________________________________________________________________) {<br />

9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!