Name:_____________________
SID (Last
4):_____________________
Login:
_____________________
CS 10 - Fall
2004
Final
Be sure to read each problem carefully and follow the
directions. Points may be taken off if
you do not follow the directions. For
example, if the problem asks you to write code or a statement, do not write an
entire program. Please feel free to ask
if you have any questions. Illegible
answers will not be graded.
You will not be graded on commenting. You do not need to write any comments. If, however, you do something strange that
you think may be hard for the grader to figure out what you are doing, you may
want to comment that part. Partial
credit will be given. You will also not
be graded on error checking unless specifically asked to do so.
Please work wisely.
Do not spend too much time on any one problem.
You will be required to show your student ID when you hand
in your exam.
|
Problem 1 - 50
|
65
|
|
|
Problem 51
|
5
|
|
|
Problem 52
|
15
|
|
|
Problem 53
|
15
|
|
|
TOTAL
|
100
|
|
TOTAL POINTS = 100
Mark a for TRUE and mark b for FALSE. (1.3 pts
each)
- The
body of a for loop will always be executed at least once.
FALSE
- Primary
storage is volatile, will be lost when the power is turned off.
TRUE
- The
word cout is a reserved word.
FALSE
- The
identifier X-ray is a valid identifier.
FALSE
- If
the user is asked to enter an integer and 2 integers are entered, the fail
bit will be set when the input is read.
FALSE
- If
the user is asked to enter an integer and a double is entered, the fail
bit will be set when the input is read.
FALSE
- If
the user is asked to enter an integer and a string is entered, the fail
bit will be set when the input is read.
TRUE
- A
single invocation (call) of a function may return multiple values by
including multiple return statements as long as all values returned are
the same type.
FALSE
- A
vector cannot be the return type of a function.
FALSE
- A
function cannot be defined within another function.
TRUE
- If
you try to change the value of a parameter passed-by-value within a function,
you will get a syntax error.
FALSE
- A
void function cannot have a return statement.
FALSE
- Any
function can be invoked (called) within another function.
TRUE
Multiple Choice.
(1.3 pts each)
- Storing
an integer into a double is an example of a
- syntax
error
- semantic
error
- logic
error
- no error
- The
____________ manages the resources, such as time and memory, of the
computer.
a.
ALU
b.
Input Unit
c.
Memory Unit
d.
Operating
System
e.
Secondary Storage Unit
- C++
is an example of
a.
High-level
language
b.
Assembly language
c.
Machine language
- The
following code has what kind of error?
Point (10, 0);
move(-2.5, -3);
- syntax
- logic
- no
error
- The
following code has what kind of error?
Point p1(14, 22);
Point p2 = p1;
Point
p3(p1.get_x(), p2.get_y());
- syntax
- logic
- no error
- The
following code has what kind of error?
Point dot(rand() %
21 – 10, 0);
- syntax
- logic
- no error
- The
following code has what kind of error?
const int A = 5;
A = 10;
- syntax
- logic
- no
error
- The
following code has what kind of error?
int b;
cout << b;
- syntax
- logic
- no
error
- The
following code has what kind of error?
int a, b;
cout << “Enter 2 numbers:”;
cin >> a, b;
- syntax
- logic
- no
error
- The
following code has what kind of error?
string s =
Hello;
s = s + s;
- syntax
- logic
- no
error
- Given
the following variables, what is the value of the given condition?
int i = 3;
int j = 13;
int k = 30;
(i != j) && (((i < 5)
|| (j < 10)) && (k >= 30))
a.
true
b.
false
c.
invalid condition
- Given
the following variables, what is the value of the given condition?
int a = 1;
int b = 0;
bool test1 = true;
bool test2 = false;
!test1 || !(a > b &&
test2)
a.
true
b.
false
c.
invalid condition
- Which
of the following expressions is equivalent to the expression?
!(a == b || c <= d)
- a
!= b || c >= d
- a
!= b && c >= d
- a
!= b || c > d
- a != b && c > d
- What
is the output of the following code?
int x = 1;
int a = 3;
if (x < 0)
a++;
else if (x == 1)
a++;
else if (x > 0)
a++;
cout << a;
a.
3
b.
4
c.
5
d.
6
e.
None of the above
- What
is the output of the following code?
int x = 1;
int a = 3;
if (x < 0)
a++;
if (x == 1)
a++;
if (x > 0)
a++;
cout << a;
a.
3
b.
4
c.
5
d.
6
e.
None of the above
- How
would the following equation be translated into C++ code?
- 2/3*a-1+b-1/4
- (2/3)*(a-1)+(b-1/4)
- (2/3*(a-1))+(b-1)/4
- None
of the above
- What
is the result of the following expression, given that a is a double with
the value of 4.2.
a % 2
- 0
- 0.2
- 2.1
- 4.2
- None of the above
- What
is the result of the following expression, given that all variables are
integers and a is 2, b is 4, c is 5, and d is 1.
a + b * c – d
- 21
- 24
- 29
- 39
- None
of the above
- What
is the result of the following expression, given that a is an integer with
value of 13, b is an integer with value 3, and c is a double with value
2.1.
a % b * c
- 2.1
- 3.1
- 6.33
- 8.4
- None
of the above
- What
does the following code output?
int x = 0;
for (int i = 0; i < 10; i = i +
2)
{
x = x + i;
cout << x << “ “;
}
- 0 2 4 6 8
- 2
4 6 8 10 12
- 0 2 6 12 20
- 2
4 6
- 0
2 4 6
- What
does the following code output?
int x = 1;
while ( x < 5 )
{
x++;
}
cout << x;
- 1
2 3 4 5
- 2
3 4 5 6
- 4
- 5
- 6
- What
does the following code output?
double x = 10.0;
int count = 0;
do
{
x = x / 2.0;
count++;
} while (x > 2.0);
cout << count;
- 1.25
- 2.5
- 3
- 2
- 1
- Given
the function f declared below and the int variables w, x, y, and z that
have been declared and initialized, which of the following invocations
(calls) of f is legal?
void f(int &a, int b, int c);
- f(x,
y, z);
- w
= f(x, y, z);
- f(10,
20, 30);
- f(x,
20, 30);
- A and D
- All
of the above
Use the following code for the next 2 questions. Read both
questions before tracing the code.
for (int i = 0; i < 3; i++ )
{
int x = 0;
for (int j = 0; j < 3; j++)
{
if ( (i * j) % 2 == 0 )
{
x = x + (i * j);
cout << x;
}
}
}
- What
is the output?
- 0
0 0 0 2 0 2 6 12
- 0 0 0 0 2 0 2 6
- 0
0 0 0 2 2 4 8 14
- 22
- 0
3 6
- 0
2 6
- How
many total times will the inner for loop iterate. In other words, how many
times will the program check the if structure’s condition.
- 0
- 1
- 3
- 8
- 9
- 16
- Which
is the BEST choice for the following function’s header.
_____________????____________
}
- void
do_something(int& x, int& y)
- int do_something(double x,
double y)
- void
do_something(int x, int y)
- int
do_something(double& x, double & y)
- double
do_something(int x, int y)
- double
do_something(double& x, double& y)
- What
does the following code output?
void f(int a,
int& b)
{
a
= a + b;
b
= b + a;
}
int
main()
{
int
first_num = 2;
int
second_num = 3;
f(first_num,
second_num);
cout
<< first_num << “ “ << second_num << endl;
f(second_num,
first_num);
cout
<< first_num << “ “ << second_num << endl;
return
0;
}
- 2 8
12 8
- 2
3
3 2
- 5
3
5 8
- 5
8
18 13
- 2
8
7 3
- None
of the above
- Which
is the BEST choice for the following function’s header.
_____________????____________
}
- void
add_to_a()
- int
add_to_a(int a, int b)
- void
add_to_a(int& a, int& b)
- int
add_to_a(int a, int& b)
- void add_to_a(int& a,
int b)
- void
add_to_a(int a, int b)
- What
does the following code output?
bool x(int&
a, int& b)
{
if
(a < b)
{
int
temp = a;
a
= b;
b = temp;
}
if
(a % b != 0)
return
true;
else
return
false;
}
int
y(int n, int m)
{
while
( x(n, m) )
{
n
= n – m;
}
return m;
}
int
main()
{
int
num1 = 5;
int
num2 = 3;
cout
<< y(num1, num2);
return
0;
}
- true
- false
- 0
- 1
- 2
- 3
- Which
is the BEST choice for the following function’s header.
_____________????____________
}
- print_mult_table()
- void
print_mult_table()
- int
print_mult_table(int& rows, int& cols)
- void print_mult_table(int
rows, int cols, int col_width)
- int
print_mult_table(int rows, int cols, int col_width)
- void
print_mult_table(int& rows, int& cols, int col_width)
- The
variable r in the previous problem is classified as a
________________________?
- loop
variable
- global
variable
- global
constant
- local variable
- parameter
variable
- The
scope of variable c in the previous problem extends from the point it is
declared until
_____________________________________?
- the
end of the file it was declared in.
- the
end of the function it was declared in.
- the end of the block it
was declared in.
- the
end of the program it was declared in.
- none
of the above
- Which
code fragment removes the first element from a vector v without losing the
order of the rest of the elements of v?
- for
(int i = 0; i < v.size() – 1; i++) {v[i] = v[i + 1];} v.pop_back();
- for
(int i = 0; i < v.size(); i++) {v[i] = v[i + 1];} v.pop_back();
- v.pop_back();
for (int i = 0; i < v.size() – 1; i++) {v[i] = v[i + 1];}
- for
(int i = 1; i < v.size(); i++) {v[i - 1] = v[i];} v.pop_back();
- None
of the above
- A and D
Use the following code fragment to
answer the next 4 questions.
vector<int> v;
for (int i = 1; i < 20; i = i +
3)
{
v.push_back(i);
}
- How
many elements does v have after the for loop executes?
- v.size()
- v.size()
- 1
- 7
- 8
- 19
- A and C
- Which
of the following for loops will print the entire vector v?
- for
(int i = 1; i < 20; i = i + 3) cout << v[i];
- for
(int i = 0; i < 20; i = i + 3) cout << v;
- for
(int i = 1; i < v.size(); i = i + 3) cout << v[i];
- for
(int i = 1; i <= v.size() - 1; i++) cout << v[i];
- for (int i = 0; i <
v.size(); i++) cout << v[i];
- D
and E
- Which
of the following statements will print just the last element of v?
- cout
<< v[last];
- cout
<< v[v.size()];
- cout << v[v.size() –
1];
- cout
<< v[19];
- cout
<< v[7];
- B
and E
- What
does the following code output?
cout << v[3];
- 0
1 2 3
- 1
2 3
- 1
4 7 10
- 3
- 7
- 10
- (5
points) Write out a good definition for a computer.
- (15
points) Write a function that calculates the amount a bank charges for
using an ATM. This bank charges a base fee of some amount for all
transactions and then if the transaction is a withdrawal adds to the base
fee a percentage of the amount withdrawn. However, the bank caps the total
ATM fee at $5, so it will never charge more than that. Your function
should take the amount of the base fee, the percentage amount, and the
amount to be withdrawn. It should calculate and return the total ATM fee.
You may assume that if the transaction is not a withdrawal, the amount to
be withdrawn will be 0. For example, if the base fee is $1, the percentage
amount is 10% and the amount to be withdrawn is $20, the ATM fee will be
$3.
- (15
points) Write a function that takes two vectors of integers as parameters
and returns true if the number of integers larger than 0 in the first
vector is greater than the number of integers larger than 0 in the second
vector. The vectors can be any size and will not necessarily be the same
size.