CSCI 240 - Midterm Test Sample Question Solutions
There may be other possible solutions to some of the questions.
10.
string getLet() { string ans; cout << "Enter a letter (a..d)"; cin >> ans; while (ans != "a") && ans != "b") && ans != "c") && ans != "d")) { cou << "\nError: reenter a..d"; cin >> ans; } return (ans); }
11. cout << getAns());
12.
2nd output 0 2nd output 2 2nd output 4 1st output 6 1st output 8
13.
#include <iostream> using namespace std; int main() { int sum = 0; int n, i; cout << "Enter n: "; cin >> n; for (i = 1; i <= n; i++) sum += i; cout << sum; }
14.
A.
int i;
for (i = 1; i <= 5; i++)
cout << i;
B.
int i = 1;
while (i <= 5)
{
cout << i;
i++;
}
C.
int i = 1;
do
{
cout << i;
i++;
}
while (i <= 5);