Test 3 CSCI 130
Fall 2022
Name ______________________________________
- Please make sure that your name is on this page.
- Please staple this sheet to your answers, this page on top.
- Please answer each question fully, carefully and thoughtfully.
- No credit will be given for unreadable answers, please write neatly.
- Answer the questions in any order, but please number each answer.
- You may assume all code given is free from syntax errors. If you find a syntax error please ask.
- Boolean
- [2 points] What values can a boolean variable assume?
- [3 points] Name three operations unique to boolean values.
- [2 points] Name or provide two operations which take other types and produce a boolean value.
- If and If-Else statements
- [3 points] Provide the syntax for the
if else
statement.
- [3 points] Provide the preferred use case for the
if else
statement.
- [3 points] Describe the semantics for the
if else
statement.
- [3 points] Provide a code segment that illustrates the preferred use of the
if else
statement. Explain your code.
- [3 points] Explain why the following code could confuse a programmer and cause a logic error. What logic error will occur when this code is run? How should this error be avoided?
if (age < 18)
cout << "You are not old enough to enter." << endl;
else
cout << "You are an adult." << endl;
cout << "You may enter." << endl;
- Else If construct
- [8 points] Use the
if, else if, else
construction described in the book and in class to write a code segment which takes a person's weight (stored in weight) along with a guess at the weight (stored in weightGuess) and prints "Too Low" if the guess is lower than the weight, "Too High" if the guess is higher than the weight or "Just Right" if the guess and the weight are equal.
- Common Errors
- [5 points] Explain the error and what will happen when the following code is executed. You should assume there are no syntax errors and
cost
and amountPaid
have been assigned a value.
if (cost = amountPaid) {
changeDue = 0;
} else {
changeDue = cost - amountPaid;
}
- Testing
- [3 points] What is white box testing?
- [3 points] What is black box testing?
- [3 points] What are "good" cases to test for in a program if you do not have access to the code?
- [3 points] What are "good" cases to test for in a program if you do have access to the code?
- [3 points] Why is it best to automate the testing process?