Uploaded by Andrew Tang

CMSC 202 - Exam 1 - Worksheet - SP23 (1)

advertisement
CMSC 202 Spring 2023
Name
Exam 1 Review Worksheet
General Concepts
Exam 1 will be based on chapters 1, 2, 3, 4, 5, the section on structures and classes in chapter 6, and the
pointers part of chapter 10. We do not provide an answer key for this, however, you are welcome to work
together.
You are responsible for any material in the book, from the labs, and available on the slides.
Fill-in-the-Blanks
1. Fill in the blank or blanks with the most appropriate answer:
a) The ___________ operator always follows the cin object, and the ___________ operator follows
the cout object.
b) _____ and _____ are C++ operators that change their operands by one.
c) The word ____________________ is used before the array declaration in a function heading to
prevent the function from modifying the array.
d) In C++, we use the _________ symbol as the dereferencing operator.
e) If nothing within a while loop ever causes the condition to become false, a(n) ________ may occur.
f) In a switch statement, if the value of the expression does not match any of the case values, the
statements following the ____________________ label execute.
g) These are the three types of loops in C++ _______________, ______________,
______________
h) When you want to process only partial data, you can use the stream function ___________ to discard
a portion of the input.
i) In C++, :: is called the ____________________.
j) A _________________
________________ is a way of changing a value of one type to a value
of another type.
k) By default, all members of a struct are ____________________.
l) If a variable is passed by ____________________, then when the formal parameter
changes, the actual parameter also changes.
m) To use setw, you need to #include the library ______________________.
n) When more than one function has the same name, they are called ___________ functions.
o) \n and \t are both example of a _________________
______________________.
p) Name the six comparison operators in C++? Include their name and their meaning:
_____________________
_____________________
_____________________
_____________________
_____________________
_____________________
True or False
2. True or False? If false, state why (or provide a counterexample).
a) _____
If the value of dollars is 5.0, the following statement will output 5.00 to the monitor:
cout << fixed << showpoint << setprecision(4) << dollars << endl;
b) _____
c) _____
d) _____
e) _____
A structure has member variables, like an object, but they are usually all public and accessed
directly with the dot operator, instead of by calling member functions.
The only difference between C-strings and string objects is how they are declared and internally
stored. They are used exactly the same way.
An expression in a C++ if statement that evaluates to 5, -5, or for that matter anything other
than 0, is considered true.
In the statement
int* p, *q;
p and q are pointer variables.
f) _____
g) _____
When you make a function call, the order of the arguments you send does not matter as long as
the number of arguments matches the number of parameters the function has.
The following two statements will assign the same value to result.
result = a + b * c;
result = b * c + a;
h) _____
In the statement cin >> x; x can be a variable or an expression.
i) _____
These three statement all do the same thing:
int *r;
int* r;
int * r;
j) _____
The C++ ________ operator represents logical OR.
k) _____
Arrays can be passed to functions, but individual array elements cannot be.
l) _____
The ________, also known as the address operator, returns the memory address of a variable.
3. Write a simple C++ program that has the following conditions:
a) Write a function named divide that takes in two doubles and returns the first divided by the second
b) Add the code for it the second double has a default value of 5.5.
4. Debugging:
Before introducing errors to the code, it worked perfectly. The code below has five errors. The errors may be
syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them.
Indicate each of the errors you find by writing the line number and correction in the space provided below.
This program is designed to print a reversed c-string.
The expected output should be:
-bash-4.1$ ./exam1_debug
Enter score #1:
100
Enter score #2:
91
Enter score #3:
80
The average score was: 90.3333
You must find and correct all five of the errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
const NUM_SCORES = 3;
double average(int scores);
int main () {
int scores[NUM_SCORES];
for(i = 0; i < NUM_SCORES;i++){
cout << "Enter score #" << i+1 << ":" << endl;
cin >> scores[i+1];
}
cout << "The average score was: " << scores << endl;
return 0;
}
double average(int scores[]){
int total = 0;
for(int i = 0;i < NUM_SCORES;i++){
total += scores[i];
}
return (total/NUM_SCORES);
}
Line Number
Correction
Fill-In-The-Blanks
5. Complete the code by filling in the blanks for each question below. (Length of blank doesn’t matter.)
a) Given the function prototype: void myFunction(int i, char j, string k); fill in the blanks
with valid arguments for the function call:
cout << myFunction(__________, __________, __________);
b) Outputs b e e p :
int main (){
_________________ myString[] = "beekeep";
for (int i = 0; i < _______________(myString) ; i+=2)
cout << myString[i] << " ";
cout << endl;
return 0;
}
c) Sets a pointer and outputs 12
int main () {
int x = 8, y = 10;
int *ptr1 = &x;
int *ptr2 = &y;
__________ = ptr2;
*ptr2 = 12;
cout << *ptr1 << endl;
return 0;
}
d) Inputs data for three students and then outputs it:
struct Student{
string name;
int stuID;
};
int main () {
const int NUM_INPUT = 3;
int counter = 0;
Student myClass[NUM_INPUT];
for(int i = 0; i < NUM_INPUT; i++){
cout << "Enter name:" << endl;
getline(cin, _________________);
cout << "Enter Student ID:" << endl;
cin >> myClass[i].stuID;
if(cin._____________ == '\n')
cin.ignore();
}
while(counter < NUM_INPUT){
cout << myClass[counter].name << " "
<< myClass[counter].stuID << endl;
_____________________
}
}
Coding Problems
6. Write the C++ code that reads an integer from the user named input. The integer is then passed to a
function named absoluteValue as a pointer and the value of input is updated. Do not pass by
reference, pass a pointer. The absolute value of a number x is defined as
|x| =
x if x ≥ 0
−x otherwise
7. Write a void function using two type integer call-by-reference parameters that outputs the following:
The output is: 6, 12, 18, 24. Be sure to include testable pre- and post- conditions.
8. Write a class definition for a table. It should have table name, material, length, and width. Include the
mutator and accessor functions for these member variables.
9. Write a simple C++ program that does the following:
a) Define a function which takes a series of equal number of positive and negative integers and populates
an array, the function should return the array.
b) Define another function which takes the populated array as a parameter. The function should create
two arrays, one for holding positive integers and the other for holding negative integers. The function
should iterate through the elements of the array and place the positive and negative integers in their
respective arrays and print the contents of the both the arrays.
Example:
Suppose the populated array looks like [-1, 2, -3, 5, -6, 7]
The program should print:
The positive integers array [2, 5, 7]
The negative integers array [-1, -3, -6]
Code Evaluation (including order of operation and precision)
10. Briefly describe the output of each small C++ program below. Assume that iostream and iomanip are
successfully imported. The output must be exact – be especially careful regarding the type of data returned.
(is in an integer? Is it a decimal based number?)
You will be expected to do questions like this on the exam.
(a)
double x = 5.5;
int y = 5.5;
cout << (x < y ? x : y) << endl;
int c = 20;
double d = 20.5;
int result = (c>=d) * d;
cout << result << endl;
(c)
(e)
(f)
(g)
(b)
(d)
double a = 3.5;
int b = 6/2;
if(a!=b)
cout << a+b << endl;
else
cout << a*b << endl;
cout << 4.0 / (2 + 6) + 5 * 2
<< endl;
Suppose sum, and num are int variables, and the input is 10 20 30 40. What is the output of the code?
cin >> num;
for (int j = 1; j <= 3; j++) {
cin >> num;
sum += num;
}
cout << sum << endl;
int list[] = {3, 4, 6, 5, 6};
for (int j = 4; j > 0; j--)
cout << list[j] << " ";
cout << endl;
double multiply1(int num1, int num2 = 1, int num3 = 2);
int main () {
int number1 = 2.5;
int number2 = 4;
int number3 = 2;
cout << multiply1(number1, number2) << endl;
return 0;
}
double multiply1(int num1, int num2, int num3) {
return num1*num2*num3;
}
(h)
int x = 10;
int *ptr = &x;
int y = 20;
ptr = &y;
y = 30;
cout << double(*ptr) + 5.5 << endl;
Code Evaluation (including order of operation and precision)
11. Briefly describe the output of each small C++ program below. The output must be exact – be especially
careful regarding the type of data returned. (is in an integer? Is it a floating point number?)
For these problems, 3 != 3.0
(a)
int num1 = 8;
int num2 = 4;
double num3 = 17.6;
double num4 = 10;
double answer = num3/num1 + num4;
cout << answer << endl;
(b)
int num1 = 8;
int num2 = 4;
double num3 = 17.6;
double num4 = 10;
int answer = num3/num1 + num4;
cout << answer << endl;
(c)
int num1 = 8;
int num2 = 4;
double num3 = 17.6;
double num4 = 10;
double answer = num3/num2 + num4;
cout << answer << endl;
(d)
int num1 = 8;
int num2 = 4;
double num3 = 17.6;
double num4 = 10;
double answer4 =
double(int(num3)/num2 + num4);
cout << answer << endl;
(e)
int main () {
double x = 3.0 / 2 + 2.0;
cout << "x = " << x << endl;
return 0;
}
(f)
int main () {
int num1 = 3;
int num2 = 6;
double z = (double)num1 / (double)num2;
cout << "z = " << z << endl;
return 0;
}
(g)
int main () {
int num1 = 4;
int num2 = 1;
double t = num1 < num2 * 9.75;
cout << t << endl;
return 0;
}
(h)
int main () {
int x = 3;
double y = 6;
cout << double(x+3/y)/2 << endl;
return 0;
}
Download