0% found this document useful (0 votes)
301 views3 pages

Midterm 2 - Comp 3010

This document contains a midterm test for a COMP 3010 course. It has 3 sections worth a total of 100 points: 1) Definitions worth 27 points, 2) Code Comprehension worth 52 points, and 3) Complete a Function worth 21 points. For section 1, students must define programming concepts. Section 2 involves analyzing code snippets for compilation, output, and syntax errors. Section 3 requires completing a function to calculate powers of 2.

Uploaded by

Richard Ruiz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
301 views3 pages

Midterm 2 - Comp 3010

This document contains a midterm test for a COMP 3010 course. It has 3 sections worth a total of 100 points: 1) Definitions worth 27 points, 2) Code Comprehension worth 52 points, and 3) Complete a Function worth 21 points. For section 1, students must define programming concepts. Section 2 involves analyzing code snippets for compilation, output, and syntax errors. Section 3 requires completing a function to calculate powers of 2.

Uploaded by

Richard Ruiz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

student name, id page 1 of 3

COMP 3010 Midterm Test 2

Problem 1. 2. 3. Total

Marks

out of 27 52 21 100

1. (27 total points) Definitions

a. (9 points) How are function declarations, function definitions and function


calls related?

b. (9 points) Explain the similarities and the differences between call--by--value


and call--by--reference.

c. (9 points) What is the purpose of the pre--condition and the post--condition


of a function?

2. (52 total points) Code Comprehension

a. (17 points)
Does Program1.cpp compile? If you answer yes explain what it will output.
If you answer no explain why it does not compile.
Program1.cpp:

#include <iostream>
using namespace std;

int main() {
int j = 0, i = 5, total = 0;
while (i < 10) {
j = i;
j = (j * 3) % 9;
total += j;
cout << j << " + ";
i++;
}
total += j;
cout << j << "= " << total;
return 0;
}
student name, id page 2 of 3

b. (17 points) In the code Fragment3.cpp underline all classes, circle all ob-
jects, draw boxes around all member functions, and draw a line through all
arguments.
Fragment3.cpp:

ifstream in_stream;
ofstream out_stream;

in_stream.open("Input.dat");
if (in_stream.fail()) {
cout << "Failed to open input file\n";
exit(1);
}

out_stream.open("Output.dat");
if (out_stream.fail()) {
cout << "Failed to open output file\n";
exit(1);
}

cout << "Relax, it’s only an exam...\n";

in_stream >> number;

switch (number) {
case 0:
out_stream << "relaxed\n";
break;
case 1:
out_stream << "truly calm\n";
break;
case 2:
out_stream << "calmly focused\n";
break;
default:
out_stream << "and enjoying it\n";
}

c. (18 points)
Does Fragment2.cpp contain a syntax error? If you answer no explain what
it will output. If you answer yes explain where the syntax error is.
student name, id page 3 of 3

Fragment2.cpp:

int i, j;
// some other code here

for (i = 1; i < 8; i += 3)
for (j = 8; j > i; j -= 2)
cout << j << " ";

3. (21 points) Complete a function

Complete the body of function pow2 (without using the built--in function pow)
to calculate and return the value 2n .

int pow2(int n)
{

End of last page.

You might also like