CMPSC 101
Exam Information
Exam #1 - Exam
#2
This exam will cover variables, statements, data types, if statements, & loops. The first portion of the exam will consist of True/False questions. You must use a #2 pencil to answer this portion of the exam. The second part of the exam will require you to write C++ programs and/or code segments. You will not be able to use a computer during the exam.
To review for the exam, please look over online notes, worksheets, and demo programs. Review the textbook material and assigned questions, projects, and exercises as well. Be sure to practice writing code segments and even complete working programs. You may want to write a program out on paper and then to informally grade yourself, type it into Visual C++ to see if it would successfully compile. While you can receive partial credit on the exam, you must still use basic syntax correctly to do well on the exam.
Here is an up-to-date practice exam.
Part I - True/False
1. The +
operator comes before the / operator in the
order of operations.
2. Keywords are words that you cannot use as identifiers.
3. A bug is a logic error or other problem in a computer program.
4. "ant eater" is a legal identifier.
5. The statement i++;
increments the variable i.
6. The statement cin >> cost;
gets a number from the keyboard and stores it in a variable named cost.
7. An if statement must have an else part.
8. A do while loop should be used when you
can determine the exact number of iterations that will be necessary.
9. A break statement causes a loop to end.
10. A while loop is a top-checking, determinate
loop.
11. The loop set up with the statement for (j = 1; j <= 5; j++) will iterate exactly 5 times (assuming j is not affected within the loop and that there are no break's).
12. The loop set up with the statement for (j = 0;
j < 10; j++) will iterate exactly 10 times (assuming j is not affected within the loop and that there are no break's).
13. The loop set up with the statement for (j = 10;
j < 100; j = j + 3)will iterate exactly 5 times (assuming j is not affected within the loop and that there are no break's).
14. At one point the value of j is 11 when
the loop set up with the statement for (j = -4; j
<=20; j = j + 5) iterates.
Part II - Write valid, efficient C++ code to perform the following tasks. It is NOT necessary to include documentation. However, you MUST use the same exact variable names that are specified. In each separate exercise, you may assume that the necessary variables have been declared unless you are being asked to write an actual declaration statement or a whole program. Use good white space and indentation, so that your answers are neat and easy to read.
1. Write a statement that declares a variable
sum of type double.
2. Write a statement that assigns the value 209 to the double variable sum.
3. Write a statement that displays the value of the integer variable sum
to the screen.
4. Write a statement that increments the variable sum
using the incrementing operator.
5. Write a single statement that declares a constant named DOZEN
with the value of 12. Use a logical & reasonable data type.
6. Write an if statement that displays the
message "Won" if the value of the double variable runsScored is greater than 8.
7. Write an if else statement that displays
the message "You earned an A." if the value of the variable named studentGrade is greater than or equal to 90. Otherwise, the if else statement causes the message "You could have done better!" to
display.
8. Write a for loop that displays the values
of 1 to 10 on the screen.
9. Write a for loop that counts by twos
from 10 to 20, displaying each value as it counts.
10. Write a top-checking indeterminate loop that allows the user to input a
sequence of integers until the sum of the numbers that he has entered is greater
than 100.
11. Write a whole program that prompts the user to enter his age. The program then displays the number of years the user has until retirement assuming that the user works until he is 65.
12. Write a full C++ program that obtains three floating-point values from the user via the keyboard, providing suitable prompt statements indicating that each input is a weight in pounds known to the hundredths place. The program must display the average of the three values rounded to the first decimal place (tenths place) along with a suitable message indicating that this result is the average.
13. Draw a complete truth table with the rows and columns labeled exactly as I have done in the lecture notes.
A student supplied these answers to this practice exam. Ask another student or ask a question in class you disagree with any of the answers.
This exam will focus on functions & files but it will also require you to use syntax and concepts learned in earlier chapters.
The first portion of the exam will consist of True/False questions. You must use a #2 pencil to answer this portion of the exam. The second part of the exam will require you to write C++ programs and/or code segments. You will not be able to use a computer during the exam.
To review for the exam, please look over online notes, worksheets, and demo programs. Review the textbook material and assigned questions, projects, and exercises as well. Be sure to practice writing code segments and even complete working programs. You may want to write a program out on paper and then to informally grade yourself, type it into Visual C++ to see if it would successfully compile. While you can receive partial credit on the exam, you must still use basic syntax correctly to do well on the exam.
Here is an up-to-date practice exam.
Part I - True/False
1. You should try to avoid using global variables in C++ programs.
2. All C++ programs have a main function.
3. Function prototypes should always appear below the main
function.
4. It is dangerous to leave a file open when it is not
being used in a program.
5. The statement
infile().close();
would close a file named infile.
6. The statement
file
infile; would declare a file pointer.
7. Random access files are used to store databases.
8. Adding data to the end of an existing file is called appending.
9. The compiler directive #include <cmath> must
appear at the top of a program that uses files.
10. The name infile must be used
for a file pointer that is used to read data from a file.
Part II - Write valid, efficient C++ code to perform the following tasks. It is NOT necessary to include documentation. However, you MUST use the same exact variable names that are specified. In each separate exercise, you may assume that the necessary variables have been declared unless you are being asked to write an actual declaration statement or a whole program. Use good white space and indentation, so that your answers are neat and easy to read.
1. Write a statement that declares an output file pointer
named outfile that would point to the file
named TEXT.TXT.
2.
Use the open function
to open a file named TEXT.TXT for output using a file pointer named outfile.
You are to assume that outfile has already
been declared as an ofstream file pointer.
3.
Close a file named TEXT.TXT that is pointed to by a
file pointer named outfile.
4.
Open a file named TEXT.TXT for appending using a file
pointer named outfile.
5.
Write a function named max2 that returns the maximum value of the two integer parameters that are passed
to it.
6.
Write a function named ave3 that returns the exact average of three integer parameters that are passed
to it.
7.
Write a complete program that allows the user to input
two integers and then displays the larger value. The program
should use a function named max2 from a
previous exercise to determine which of the two inputed values is the largest.
Two int parameters should
be passed to the function max2 and the
function should return the larger of the two. The main function should then display the largest value.
8.
Write a complete program that allows the user to input
two integer values. The program must pass those two values to a function named
computeAve which returns the average of the
two parameters. The main function should
then display the final average rounded to the nearest whole number.
A student supplied these answers to this practice exam. Feel free to email the class via Angel or me if you disagree with any of the answers.