4.17. Multiple Choice ExercisesΒΆ
Q1
What should be the return type of the function convert_to_celsius?
______ convert_to_celsius (double fahrenheit) {
double celsius;
celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
Q2
What would be returned by secret_function if the input was 14?
int secret_function (int input) {
if (input % 2 == 0) {
return 3 * input - 2;
}
else {
if (input % 7 == 0) {
return input;
}
return 2 * input + 9;
}
return input + 4;
}
Q3
If we wanted to create a boolean function called is_prime, which takes an int input
as a parameter, which of the following would be the correct function header?
Q4
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);
Q5
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));
}
Q6
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;
}
Q7
Are there any issues with the code below?
bool is_even (int num) {
if (num % 2 == 0) {
return true;
}
}
Q8
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!"<<'\n';
return day*0.25;
}
else{
cout<<"Happy weekend"<<'\n';
return day;
}
}
Q9
Are there any issues with the code below?
void moon_weight (double earth) {
double moon = 0.165 * earth;
cout << "You would weigh " << moon << " pounds on the moon." << '\n';
return moon;
}
Q10
What is the return type of main?
Q11
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;
}
}
Q12
What is printed?
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);
}