CMPT 111 Mock Midterm 1

advertisement
CMPT 111
Mock
Midterm 1
2010-2011 Term 1
This practice exam was
prepared for you by your SSS
coach (not your professor!).
It is designed to help you test
yourself on the topics covered
in class and should not be
considered as a preview of the
actual midterm.
CMPT 111 Mock Midterm 1
2010-2011
University Learning Centre
Section 1: Multiple Choice
Circle the correct answer to the following questions.
1. C++ is the only programming language.
a. True
b. False
2. “Keep studying until you are too tired” is what type of task?
a. Simple
b. Conditional
c. Repetitive
3. Which one of these is NOT a simple data type?
a. Text
b. List
c. Array
d. Record
4. Abstraction is the process of making things more detailed than they are.
a. True
b. False
5. TRUE AND NOT (FALSE OR NOT TRUE)
a. True
b. False
Section 2: Variable Naming
Determine whether a variable is valid or invalid. If it’s invalid, explain why.

B
- Valid

Canada-is-number-one
- Invalid, due to the dash

_myVar
- Valid

4myVar
- Invalid, variables can’t start with numbers

Int
- Invalid, int is a reserved work (and so are if, float, etc.)

success!
- Invalid, due to the exclamation point

_____________peekaboo
- Valid

This_variable_is_invalid
- Valid
2
CMPT 111 Mock Midterm 1
2010-2011
University Learning Centre
Section 3: Program Analysis
Circle all the statements that get executed in the following code fragments.
1. Pseudocode
int a  9
int b  10
int c  a + b
if (b > a)
statement1
if (c > a)
statement2
else
statement3
2. C++
if (6 > 4 && 3 < 4) {
statement1;
}
else if (9 == 9) {
statement2;
}
if (true || 4 % 2 != 1) {
statement3;
}
statement4;
3. Pseudocode
int myFavouriteNumber  7
print “What is your favourite number?”
myFavouriteNumber  getInput
if (false)
statement1
if (myFavouriteNumber == 7)
statement2
myFavouriteNumber = 9
else if (myFavouriteNumber == 9 OR true)
statement3
print “Your favourite number is” myFavouriteNumber
else
statement4
3
CMPT 111 Mock Midterm 1
2010-2011
University Learning Centre
Section 4: Program Output
Write down what would be output to the screen if you were to run these programs.
1. C++
#include <iostream>
using namespace std;
int main() {
int g = 6;
char _6 = ‘g’;
if (g == 6) {
cout << “Just gonna stand there and hear me cry” << endl;
}
if (_6 == ‘G’) {
cout << “But that’s alright ”;
}
cout << “because ”;
if (g == _6) {
cout << “I love the way you lie.” << endl;
}
else {
cout << “I love computer science. << endl;
}
cout << g << _6 << endl;
}
Output (Write in the space below)
> g++ my_program.cpp
> ./a.out
Just gonna stand there and hear me cry
because I love computer science.
6g
(see my_program.cpp to try it for yourself)
4
CMPT 111 Mock Midterm 1
2010-2011
University Learning Centre
2. Pseudocode
int j  100
ink k  5
while (k < 8)
print “@”
for (int i  0; i < 2; i++)
j  j / 2
print “$”
print j
print newline
k  k + 1
Output (Write in the space below)
@$50
$25
@$12
$6
@$3
$1
(see s4q2.cpp to try it out for yourself)
3. Pseudocode
print “Programming can be beautiful”
for (int i  0; i < 5; i++)
for (int j  0; j < i; j++)
print “*”
print newline
Output (Write in the space below)
Programming can be beautiful
*
**
***
****
(see s4q3.cpp to try it out for yourself)
5
CMPT 111 Mock Midterm 1
2010-2011
University Learning Centre
Section 5: C++ Errors
Cross out the errors in this C++ program and correct them.
// (see s5q1.cpp to try it out for yourself)
#include <icecreamiostream>
int MAINmain() {
int theNumber = 88;
cout << “This is the number: ” << theNumber << endl;
int input = 0;
cout >><< “Multiply the number by three: ” >><< endl;
cin <<>> input;
if (input == theNumber * 3) {
cout << “Congratulations!” << endl;
}
else {
cout << “Here’s failure 5 times:” << endl;
print “Here’s failure 5 times:” newline
int i = 0;
while (i < 5) {
cout << “FAILURE!” << endl;
i = i + 1;
}
}
Int theNumber = 88;return 0;
}
6
CMPT 111 Mock Midterm 1
2010-2011
University Learning Centre
Section 6: Algorithm Creation
Create an algorithm to accomplish the following problems.
1. Pseudocode
Write a program to get a user to type in the number ‘42’. Prompt the user to enter a number
repeatedly then give clues to try and get to the number ‘42’.
If the guess is between 40 and 49, output “You’ve got the first digit!”
If the guess is between 30 and 39, output “Hot!”
If the guess is between 50 and 59, output “Warm!”
If the guess is negative or three digits, output “Ice Cold!”
Otherwise, output “Higher” or “Lower” depending on the number.
Keep doing this until the user gets ‘42’. When they do, output “That’s the answer” and then exit
the program.
BONUS: If the user enters a number ending with ‘2’, output “You’ve got the last digit!”
(see s6q1.txt for a possible solution)
7
CMPT 111 Mock Midterm 1
2010-2011
University Learning Centre
2. C++
Alice, Bob, and Carol are planning a Secret Santa this Christmas.
(A Secret Santa is where each person secretly gives gifts to another person in the group)
Therefore, write a computer program that will act as a 3rd-party to determine who gets whose
name for gifts. The program should come up with a configuration first. Then, come up with a
creative way to show each of the three friends their selection while still maintaining the secrecy.
Hint: there are only 2 possible configurations, so to pick randomly in C++, use
if (rand() % 2 < 1) { ... } else { ... }
(see s6q2.cpp for a possible solution)
NOTE: You don’t need to understand how the rand() part works yet.
8
Download