Midterm
What operator in function declaration indicates that the function uses a pass by reference?
&
Given the following code, what are the outputs? void func1 (int &n1, int n2){ int temp = n1; n1 = n2; n2 = temp; } int main(){ int num1 = 1, num2 = 2; func1 (num1, num2); cout << num1 << " " << num2 << endl; }
2 2
cout << -3 * (2 - 3) * 7 << endl;
21 // -3*(-1)*7=21
#include <iostream> using namespace std; int f(int x, int y, int z){ // x=7, y=1, z=2 if (x > y && y < x) { // if(true) return z + x * 4; // 2+7*4 = 30 } return 3*z+6; } int a = 2; int b = 3; int main(){ int a = 1; int b = 2; int c = 7; int d = 3; d = f(c, a, b); // pass in 7 1 2 cout << d << endl; return 0; } Which number is output by this program? Select exactly one.
30
What is the output after executing the following lines of code? int num = 85934; cout << num % 100;
34
There are 8 bits in 1 byte. In C++ how many bytes are reserved in memory for an integer value
4
#include <iostream> using namespace std; int main() { int k = 3; for (int i = 3; i < 8; i++){ k = k + 2; cout << k << endl; } return 0; } Which of the following numbers are output by this program? Select at least one but you may select more than one.
5 7 9 13
In the expression x > 4 && x <= 8, which values of x make this statement true?
5, 6, 7 8
int main(){ int k = 3; for (int i = 5; i <= 9; i++) { k = k + 3; cout << k << endl; i++; } return 0; } 1 5 6 2 7 9 3 9 12 Which numbers are output by this program? Select at least one but you may select more than one.
6 9 12
#include <iostream> using namespace std; int main(){ int x, y; cout << "Enter two integers: "; cin >> x >> y; while (x < 10 && y >= 7) { x = x + 3; y = y - 2; cout << x << endl; } return 0; } If "4 8" is input in response to the prompt "Enter two integers:" which of the following numbers are output? Select at least one but you may select more than one.
7
cout << static_cast<double>(2 * 3 + 9/3.0) << endl;
9 // (6.0+3.0)=9.0
What is the output after executing the following code? int main() { int A = 3; if (A = 10) cout << "A is equal to 10" << endl; else cout << "A is not equal 10" << endl; return 0; }
A is equal to 10
Which of the following lines is NOT a correct lvalue? Assume all variables have been defined/declared and initialized.
Answer: length * width = area; area = 3.14 * pow(r, 2); mpg = miles / gal; cost = pricePerPound * numPounds;
(T/F) Variables of type char should be enclosed in double quotes.
False
If the subexpression on the left of the && logical operator is true, the right subexpression is not checked.
False
True/False - All functions must return something
False
True/False - For performance considerations, when you have nested loops it is preferable to have the index count of the outer loop be much greater than that of the inner loop.
False
True/False: The variable name "xyz" is the same as the variable name "XYZ".
False
True/False: Your program starts executing with the start function.
False
#include <iostream> using namespace std; int main(){ int a = 0; int b = 5; int c = 11; int d = 15; if ((d > b) && (c < b)) // false cout << "Ohio" << endl; else cout << "Utah" << endl; // Utah if ((a*c != d) || (b+c > d)) // true cout << "Hawaii" << endl; // Hawaii else cout << "Iowa" << endl; if (!(a+d == b+c)) // true cout << "Maine" << endl; // Maine else cout << "Alaska" << endl; return 0;
Hawaii Maine Utah
What is the output after execution of the following code? int main() { int yertle = 0; if (yertle) cout << "Yertle is"; cout << "King of the turtles." << endl; return 0; }
King of the turtles
To calculate the number of iterations of a nested loop, ________ .
Multiply the number of iterations in the outer loop by the number of iterations in the inner loop
#include <iostream> using namespace std; int main(){ int a = 0; int b = 10; int c = 5; int d = 20; if ((d > b) && (c < b)){ // true cout << "Red" << endl; Red } else{ cout << "Blue" << endl; } if (( !(a*c == d*a)) || (b+c < d)){ // true cout << "Orange" << endl; Orange } else{ cout << "Green" << endl; } if (a+d == b+c){ // false cout << "Yellow" << endl; } else{ cout << "Purple" << endl; Purple } return 0; } Which of the following words are output by this program? Select at least one but you may select more than one.
Red Orange Purple
The if statement regards an expression with a nonzero value as ________.
TRUE
In a function definition the part between the { ... } is called?
body
Write a function of return type bool called isprime(). This function accept a single integer called x as an input parameter. The function will determine if the given integer x is a prime value or not. If it is, it returns a 1. If it isn't, it will return a 0.
bool isprime(int x){ for(int i=2; i<=x/2; i++){ if (x%i == 0) return false; } return true; }
A program will "fall through" a case section if it is missing the _________ statement.
break
1. __________ cannot have their values changed once defined.
constants
A "Pass by Value" parameter passes a __________ of the value to a function.
copy
The trailing else in an if/else if statement has a similar purpose as the ___________ of a switch statement.
default
When a function is called by main(), a new set of local variables is created. A return in the function causes those local variables to be ______________ .
discarded
C++ is a case-insensitive programming language.
false
True/False - A primary reason for using functions is to enable the code to run faster.
false
j < y
false
j >= 7 && k = 9
false
k != 7
false
k == 0
false
1. A __________________ is a C++ data type to hold numbers that contain "fractions."
float
A _________________ is a C++ data type to hold numbers numbers that contain "fractions"?
float
What is an example of a "pretest" loop?
for, while
Function prototypes can be thought of as _______.
function declarations
int i; int base = 2; for ( i = 5; i > 2; i-- ) { base = base * 2; } cout << "The value of i is: " << i << endl; cout << "The value of base is: " << base << endl; What is the output of the cout statements (write answer in box below)?
i=2 base=16
How many times will the following loop iterate? int number = 0; while (number < 5) cout << "Hello\n"; number++;
infinite
A __________________ C++ data type should be used to hold "whole" numbers?
int
12. Write a function of type int called closestInt() whose parameters are two POSITIVE double numbers, and it returns the rounded integer value of the sum of those two double numbers. Do not use any math function libraries! For example, if the two inputs are 7.2 and 7.4 the function should return 15. If the two inputs are 7.2 and 7.1 the function should return 14.
int closestInt(double a, double b){ double dsum = a + b; double decsum = dsum; while(decsum>0) decsum--; decsum++; if (decsum < 0.5) return dsum - decsum; else return dsum + (1-decsum); }
Legal Identifiers/variable names can start with ___letter_________________ or __________underscore____________
letter, underscore
1. Legal variable names can start with _______ and ________ .
letters and underscores
The ___________ function is a part of all C++ programs
main
Using a return statement, a function can return how many values?
one
What is the value of product after the following code executes? int main() { int product = 0; for (int i = 0; i < 3; ++i) product *= i; cout << "product = " << product << endl; return 0; }
product = 0
What is the output after executing the following code? int main() { int sum = 0; cout << "sum = " << sum++ << endl; return 0; }
sum = 0
What is the value of sum after the following code executes? int main() { int sum = 0; for (int i = 0; i < 3; i++) sum += i; cout << "sum = " << sum << endl; return 0;}
sum = 3
How many times will the following loop iterate? for (int num = 2; num <= 5; num+=2) cout << num << endl; cout << "num is now " << num << endl;
syntax error
9 <= j || 0 > k
true
k != j
true
k && j && y
true
k <= j
true
Vectors by default are pass by ___________
value
A __________ return type indicates that the function does not return a value.
void
1. Which two types of loops are not guaranteed to run at least once?
while and for
Which loops are pretest loops (that is, test before 1st pass through loop)?
while and for loops
The output of the following code fragment is: int x; cout << "x = " << x << endl;
x = 0
Given the following code, what are the outputs? void func1 (int &n1, int & n2){ int temp = n1; n1 = n2; n2 = temp; } int main(){ int num1 = 1, num2 = 2; func1 (num1, num2); cout << num1 << " " << num2 << endl; }
2 1
What is the value of laugh after the following code is executed? int main() { int fun = 5; int laugh; if (fun == 5) laugh = 10; if (fun > 3) laugh = 8; if (fun <= 10) laugh = 2; return 0; }
2
1. What does the following expression evaluate to? !(2 || 4)||(6 && 0)
0
What is the value of answer after the following statement executes? int answer = (12/7 < 12/8);
0
cout << 5 * 3 * (3/9)
0 // 5*3*(0)
Given the following code, what are the outputs? void func1 (int n1, int & n2){ int temp = n1; n1 = n2; n2 = temp; } int main(){ int num1 = 1, num2 = 2; func1 (num1, num2); cout << num1 << " " << num2 << endl; }
1 1
What will the following program display? for (x = 1; x <= 10; x++) { cout << x << " "; x++; x++; }
1 4 7 10
What is the value of sunny after the following code is executed? int main(){ int funny = 7; int sunny; if (funny <= 7) sunny = 10; else if (funny > 3) sunny = 4; else if (funny <= 10) sunny = 2; else sunny = 0; return 0; }
10
cout << (3/5 * 3)/2 + 20/2<< endl;
10 // (0*3)/2 + 10
What is total after executing the following lines of code? int total = 4; total *= total;
16
(T/F) Variables of type string consist of only one character and are enclosed in single quotes.
True
If the subexpression on the left of the || logical operator is true, the right subexpression is not checked.
True
True/False: A function can return more than one value using a single return statement. (e.g., returns an array)
True
True/False: You have to declare each variable in C++.
True
Variables may be defined inside the body of a loop.
True
11. What is the output of the following Program? i cout << i++; 0 0 3 0 6 6 9 9 12 12 15 15 18 18 #include <iostream> using namespace std; int main(){ int i; cout<< "*"; for(i=0;i<20;i+=2){ cout << i++; } cout<<"*\n"; return 0; }
_____*0369121518*__________________________________