Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Exercises 5.14 Multiple Choice Exercises
1.
What should be the return type of the function
convertToCelsius
?
______ convertToCelsius(double fahrenheit) {
double celsius;
celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
What variable are we returning in the function, and what is the variableβs type?
The function returns celsius
, which is a double
.
What variable are we returning in the function, and what is the variableβs type?
Since we are returning something in the function, the function is not void
.
2.
What would be returned by
secretFunction
if the input was 14?
int secretFunction(int input) {
if (input % 2 == 0) {
return 3 * input - 2;
}
else {
if (input % 7 == 0) {
return input;
}
return 2 * input + 9;
}
return input + 4;
}
Although 14 is divisible by 7, take another look at the conditionals.
The flow of code would never reach the last return statement.
Check your order of operations!
Take a closer look at the conditional statements.
Since 14 is divisible by 2, the function returns two less than three times 14.
3.
If we wanted to create a boolean function called
isPrime
, which takes an
int input
as a parameter, which of the following would be the correct function header?
boolean isPrime(int input) {
In C++, use the bool
keyword for a boolean.
In a function header, the type of each variable must be specified in the parameter list.
bool isPrime(int input) {
This is the correct function header for the function.
int isPrime(bool input) {
Take a closer look at what the return type is.
4.
If we wrote the following function, which of the other functions below can we also legally write and add to the program?
int func(double x, bool y);
int func(double a, bool b);
Since this function has the same name and parameter types as the given function, it is not allowed.
int foo(double x, bool y);
This function has a different name from the given function, so it is allowed.
Although this function has the same name as the given function, it has a different number of parameters, so it is allowed.
void func(double x, bool y);
Although this function has a different return type, its parameter list is the same as the given function, so it is not allowed.
int func(bool y, double x);
Although this function has the same name as the given function, its parameter list is in a different order, so it is allowed.
5.
What is the output of the code below?
int main() {
bool x = 2 < 3;
cout << x;
cout << false;
cout << ((1 + 4) * 4 > 24);
cout << (23 == (32 + 2 - 11));
}
Since the first and last statements are true and the middle two are false, this is the correct output.
In C++, boolean values are outputted as 0 or 1.
Since the second cout
statement doesnβt have quotes around the word βfalseβ, the value of 0 is outputted.
Remember that if a boolean expression is true, it has a value of 1.
6.
What is the output of the code below?
int main() {
bool w = !(2 * 3 == 6 || 4 - 3 > 8);
bool x = true || 4 > 6;
bool y = 3 != 6 - 3 && 23 >= 23;
bool z = (4 + 9 < 15 && 3 != 4) || 2 + 5 == 7;
cout << w << x << y << z;
}
Since the expressions are false, true, false, and true, the output is 0101.
Remember that true
outputs to 1 and false
outputs to 0.
Remember the NOT operator (!) inverts the value of a boolean.
Take a closer look at the order of operations.
Take a closer look at the expressions.
7.
Are there any issues with the code below?
bool isEven(int num) {
if (num % 2 == 0) {
return true;
}
}
Yes, we have to return either 0 or 1.
Returning a 0 or 1 would be returning an int
, even though booleans evaluate to 0 or 1.
Yes, we cannot pass an
int
into a
bool
function.
The type of variables in the parameter list do not affect the return type.
Yes, there is no case for odd numbers.
Since we never established an else clause, if the input was an odd number, the function would not return anything despite not being a void function.
There are no issues with the code.
There is an issue with the code. Can you find it?
8.
Are there any issues with the code below?
double Free_time(int day) {
if (day == 1 || day == 2 || day == 3 || day == 4) {
cout << "Better study on weekday!" << endl;
return day*0.25;
}
else{
cout << "Happy weekend" << endl;
return day;
}
}
Yes, we might not return anything.
We have an else clause in which we return a value.
Yes, we cannot return an entire expression like
day*0.25
.
If the result of the expression is compatible with the return type we can return it.
Yes, we are returning an
int
(in the
else
block) where as the return type is
double
.
Implicit conversion from an int to double is ok in c++!
There are no issues with the code.
Correct! implicit conversion from int to double are ok!
9.
Are there any issues with the code below?
void moonWeight(double earth) {
double moon = 0.165 * earth;
cout << "You would weigh " << moon << " pounds on the moon." << endl;
return moon;
}
Yes, we cannot have
cout
statements in a function.
We are allowed to use cout
statements in a function.
Yes, we cannot return anything.
void
functions do not have return values, so we cannot return moon
.
Yes, we need to return the output statement.
void
functions do not have return values.
There are no issues with the code.
There is an issue with the code. Can you find it?
10.
What is the return type of main?
What keyword do we use before main()
in every program?
What keyword do we use before main()
in every program?
What keyword do we use before main()
in every program?
Yes, main
is supposed to return an integer, which is why programmers often return 0 at the end of main
.
11.
What is the base case of the
factorial
recursive function?
int factorial(int n) {
if (n == 0) {
return 1;
}
else {
int recurse = factorial(n-1);
int result = n * recurse;
return result;
}
}
When n
is 0, the function returns the value 1 without making a recursive call.
When n
is 1, the function makes a recursive call in the else statement.
n
never becomes -1.
If there was no base case, the function would recurse infinitely.
12.
void print_sequence(int n) {
if (n == 0) {
cout << 1;
return;
//we can have an empty return to a void function
}
else {
cout << n << " ";
print_sequence(n - 1);
}
}
int main() {
int val=6;
print_sequence(val);
}
Check what the base case prints.
What value do we give the recursive call?
The base case prints something!
We print a number and decrement it till we reach 0 then we print 1.
You have attempted
of
activities on this page.