# CMPT 111 Mock Midterm 2

Document Sample

```					CMPT 111
Mock
Midterm 2
2010-2011 Term 1

This practice exam was
prepared for you by your SSS
yourself on the topics covered
in class and should not be
considered as a preview of the
actual midterm.
CMPT 111 Mock Midterm 2                      2010-2011              University Learning Centre

Section 1: Multiple Choice
Circle the correct answer to the following questions.                                                    2

1. What type of array would you use to store your first name?
a. Integer Array
b. String Array
c. Character Array
d. Name Array
2. What type of errors will the compiler identify for you?
a. Syntax Errors
b. Logic Errors
c. Erroneous Errors
3. A function can have no parameters.
a. True
b. False
4. A function can have no return value.
a. True
b. False
5. Can a function call itself?
a. Yes
b. No
6. Which of these will result in an infinite loop?
a. A recursive function without a recursive case
b. A recursive function without a base case
c. A recursive function with a recursive case and a base case
7. Everything you can do with a loop you can do with recursion.
a. True
b. False
8. What would be the base case for a recursive function to calculate the sum of all numbers from 0
to an arbitrary number n?
a. If (n == 1) return 1;
b. If (n == 0) return 0;
c. If (n == 1) return 0;
d. If (n == 0) return 1;
e. If (n < 1) return 1;
9. What’s the best way to understand recursion?
b. Trace it out on paper
10. Bonus: What was the first computer “bug”?
a. Cricket
b. Moth
c. Mosquito
d. Housefly
CMPT 111 Mock Midterm 2                     2010-2011                University Learning Centre

1. The square bracket operator [ ] can be used in two different ways. Name these ways and give
an example for each.

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

Translate pseudo-code into code.
Test the program.
Figure out how to perform the task.
Write down the step-by-step method.

3. What is the difference between pass-by-value and pass-by-reference functions? Give examples of
each one.
CMPT 111 Mock Midterm 2                         2010-2011   University Learning Centre

Fill in the function headers for the following functions.                                4

1. C++

{

if (purple)
cout << “Purple”;

return a % b;

}

2. Pseudocode

for (int i  0; i < size; i++)

array[i] = size – i

return

3. Pseudocode

int a = 5

int b = 6

int c[5] = {1, 2, 3, 4, 5}

if (true)

return a + b + 1.2

else

return c[0] + c[4] + b
CMPT 111 Mock Midterm 2                    2010-2011              University Learning Centre

Section 4: Program Output
Write down what would be output to the screen if you were to run this program.                 5

1. C++

#include <iostream>
using namespace std;

int average(int numbers[], int size) {

int sum = 0;

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

sum += numbers[i];
cout << “Sum so far is: ” << sum;

}

return sum / size;

}

int main() {

int descending[6] = {5, 4, 3, 2, 1, 0}

cout << “Average: ” << average(descending, 3 * 2);

cout << “Nozero average: ” << average(descending, 3 + 2);

return 0;

}

Output (Write in the space below)

> g++ my_program.cpp
> ./a.out
CMPT 111 Mock Midterm 2                    2010-2011         University Learning Centre

Section 5: C++ Errors
Cross out the errors in this C++ program and correct them.                                6

using namespace std;

float main() {

PrintNumAndChar(‘8’, 9);

int myArray[6] = {1, 2, 3, 4, 5}

myArray[6] = 6;

myArray[1] = 1.5;

return 7;

}

printNumAndChar(a, b) {

cout << (a + 1) << b;

}

int[][] seven(int array [5][]) {

for (int i = 0; i < size; i++)

array[0][i] = six(6);

return array;

}

void six()

return 6;
CMPT 111 Mock Midterm 2                      2010-2011                 University Learning Centre

Section 6: Function Creation
Create a function to accomplish the following problems. Use the header given.                       7

1. Pseudocode

Write a function to figure out the factorial of a number.
Example: factorial(5) = 5! = 5 * 4 * 3 * 2 * 1 = 120

int factorial(int n)

2. C++

Write a function to initialize a two-dimensional array with all zeroes.

void initialize2dArray(int array[][10], int size) {
CMPT 111 Mock Midterm 2                    2010-2011                 University Learning Centre

3. Pseudocode
8
Write a function that checks whether or not two arrays have all the same elements, regardless
of the order.

For instance:
Array 1 – 1, 4, 6, 7 and Array 2 – 4, 6, 1, 7 both have the same elements in a different order.

Please note: This is a large task, so it is recommended to use abstraction to break it into several
smaller problems. Therefore, write additional functions to help make this problem easier.

bool sameElements(int array1[], int array1size, int array2[], int array2size)
CMPT 111 Mock Midterm 2                         2010-2011                 University Learning Centre

Section 7: Recursion (BONUS!)
Write a recursive function in C++ to solve the task listed below.                                               9

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.
This should be a clue as to how I envision the problem being solved.

Good luck, this is very difficult.

void sort(__________________________________________________________________________) {

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 9 posted: 3/26/2012 language: English pages: 9
How are you planning on using Docstoc?