Department of Electrical and Computer Engineering Full Name: ____________________________________________
Concordia University
Student Number: _______________________________________
COEN 243 – Programming Methodology I - Fall 2018
Midterm Exam - Date: October 19, 2018 at 4:15p.m. Duration: 70 min.
Instructions: Closed-book. Concordia-approved calculators are allowed.
Professors: Dr. A. Hamou-Lhadj and Dr. D. Davis
For MCQ, select the best answer. There is only one best answer. Not all questions are MCQ. Some
questions require filling in the gaps. Total Marks = 15 (1 mark for each).
ANSWER
1. What is the output of this program?
#include <iostream>
#include <cmath>
using namespace std;
int main () {
int A,B, d,e;
float a,b,D,E;
A = 4.5;
a = 4+0.5;
B = a*A;
b = A*a;
d = sqrt(9);
D = sqrt(d);
e = d*D+A+a;
E = (d+D)/(E+e);
cout << E << " - " << e <<endl;
return 0;
}
A. 0- 0
B. 13 - 0.364004
C. 0.364004 - 13
D. 0 - 13
__________
E. 0.342224 - 0
2. What does this function return when calling it with n = 5?
int f(int n) {
if (n == 0)
return 1;
else
return f(n-1) - 1;
}
A. 5
B. -5
C. -4
D. -3 __________
E. 4
TYPE: A 1/7
3. Which of the following is not a valid identifier?
A. value
B. value_1
C. 23_value
D. _value
E. VALUE __________
4. How many times the cout statement in the following code fragment will be executed?
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
cout << “Hello world!” << endl;
A. 10
B. 100
C. 1000
D. 10000 __________
E. 100000
5. The following program computes the series 3 + 5/2 + 7/4 + 9/8 + 11/16 +… up to a term n,
entered by the user. Complete the program by choosing one of the options below.
//Program that computes 3 + 5/2 + 7/4 + 9/8 + 11/16 + . . .
#include <iostream>
using namespace std;
int main () {
int n;
int sum = 0;
int a = 3;
int b = 1;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i <= n; i++) {
_________________;
_________________;
_________________;
}
cout << "Sum is: " << sum;
return 0;
}
A. sum = sum + a/b
a += 2
b *= 2
B. sum = a/b __________
a += 2
b *= 2
C. sum = sum + a/b
a *= 2
b += 2
D. sum = i + a/b
a *= 2
b += 2
TYPE: A 2/7
6. Consider the following for loop:
for (int i = 1; i <= 5; i++)
for (int j = i; j <= 5; j++)
cout << i*j << endl;
Which while loop below behaves the same as the above for loop?
A.
int i = 1;
while (i <= 5) {
int j = 1;
while (j <= 5) {
cout << i*j << endl;
j++;
}
i++;
}
B.
int i = 1;
while (i <= 5) {
int j = i;
while (j <= 5) {
cout << i*j << endl;
j++;
}
i++;
}
C.
int i = 1;
while (i<=5) {
int j = i;
while (j <= 5) {
cout << i*j << endl;
}
j++;
i++;
}
D.
int i = 1;
while (i <= 5) { __________
int j = i;
while (j <= 5) {
cout << i*j << endl;
j++;
i++;
}
}
TYPE: A 3/7
7. What is the output of this program?
#include <iostream>
using namespace std;
void f(int);
int x = 93;
int main() {
int x = 10;
f(10);
cout << x << endl;
return 0;
}
void f(int a) {
x = a * 2;
} __________
A. 93 B. 20 C. 186 D. 10 E. 100
8. Consider the following flowchart:
cin >> g;
NO YES
g>=90
cout << “Very Good!”;
No YES
g>=80
cout << “Satisfactory!”; cout << “Good!”;
DO NOT
cout << “\nEnd of Evaluation!”; ANSWER
HERE
Write the equivalent code fragment using if-else statements:
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
_____________________________________________________________
_____________________________________________________________
TYPE: A 4/7
9. A run-time error happens:
A. during compilation a program
B. during execution of a program
C. both during compilation and execution
D. all the time when we forget to include a library __________
E. when special libraries used to represent time are used
10. Which statement is valid, given the following function prototype?
int f(int, float);
A. cin >> f(y);
B. cin >> f(2, 2.5);
C. cout << f(f(7), 15);
D. cout << f(f(3, 3.5), 15.5);
E. cout << f(f(3), 15.5);
__________
11. What is the output of this program?
#include <iostream>
#include <array>
using namespace std;
int main() {
const size_t arraySize{5};
array<int, arraySize> alpha{234, 23, 50, 12, 900};
int x;
int y;
x = alpha[0];
y = alpha[0];
for (int i{1}; i < alpha.size(); ++i) {
if (x > alpha[i]) x = alpha[i];
if (y < alpha[i]) y = alpha[i];
}
cout << x << “-” << y;
return 0; __________
}
A. 234-23 B. 23-234 C. 50-900 D. 12-900 E. 900-1000
12. What is the output of this program?
#include <iostream>
using namespace std;
int try_this (int);
void try_that (int&);
int main() {
int d;
int c = 10;
d = try_this(c);
try_that(d);
cout << d << endl;
TYPE: A 5/7
return 0;
}
int try_this (int x) {
return x + 4;
}
void try_that (int& x) { __________
x = x + 3;
}
A. 10 B. 14 C. 17 D. 24 E. 4
13. Complete the following program, which computes the Fibonacci series and displays
the result for the first 10 terms. Fibonacci series is defined as follows:
Fibonacci [0] = 1
Fibonacci [1] = 1
Fibonacci [2] = Fibonacci [0] + Fibonacci [1]
…
Fibonacci [n] = Fibonacci [n-2] + Fibonacci [n-1]
#include <iostream>
using namespace std;
int main () {
int n;
const int arraySize = 10;
int fib[arraySize];
fib[0] = 0;
fib[1] = 1;
cout << "Seq n = " << 0 << " is : " << fib[0] << endl;
cout << "Seq n = " << 1 << " is : " << fib[1] << endl;
for (__________________________________________) { // Your answer DO NOT
fib[i] = fib[i-1] + fib[i-2]; ANSWER
cout <<"Seq n = " << i << " is : " << fib[i]; HERE
}
return 0;
}
14. What is the drawback of using call by value when passing parameters to a function?
A. The changes made by the function will affect the parameters using during the call
B. The function creates copies, which impacts program execution time
C. The parameters may not be passed properly causing improper execution
D. The function may delete the parameters, making them unavailable to the callee _________
15. What is the output of this program?
#include <iostream>
#include <array>
using namespace std;
TYPE: A 6/7
int main () {
const size_t arraySize {6};
array <int, arraySize> Num {2, 3, 4, 5};
array<int, arraySize> Den {7, 5, 2, 6};
int sum = 0;
for (int i = 0; i < arraySize; i++) {
sum = sum + Num[i]*Den[i];
} _________
cout << sum << endl;
return 0;
}
A. 67 B. 14 C. 20 D. 64 E. 87
TYPE: A 7/7