C++ Final Review
Given the following code fragment, what output is generated? double tax; int total = 9; if ( total > 7) { tax = 0.5; cout << total + (total * tax) << endl; } else{ cout << total << endl; }
13.5
In for loop after the first time through the loop the Boolean expression (second section) is evaluated before the update action (third section) is executed. Hint: rewrite as a while loop
False
In our recommended development environment, you can use make to compile your source code and generate executables with no g++ installed.
False
The default method of parameter passing in C++ is pass-by-ref.
False
The only reason to use pass-by-reference parameters is that it is faster (no need to duplicate the data).
False
To use the istringstream and ostringstream classes, you. must include the <stringstream> header.
False
When considering boolean expressions that test ranges of numbers, the opposite of greater than is less than.
False
int a == 5; is a valid C++ statement
False
What is the error in the following code? ifstream inFS; int numBananas; int numMonkeys = 40; inFS >> numBananas; cout << "Bananas per monkey: "; cout << numBananas / numMonkeys;
The file stream has not been properly opened
A C++ program may have both syntax errors and logic errors at the same time.
True
It is possible to have a function that has no parameters and no return value.
True
Loop (iteration) control structures are used when we need our program to repeat something over and over again.
True
The compiler always pairs an else with the nearest previous if not already paired with an else.
True
The setprecision function behaves differently when worked with or without the fixed keyword.
True
cin and cout are special streams bound to standard input (keyboard) and standard output (console screen)
True
What is the command to change directory in Linux?
cd
The operator == is a(n)
comparison operator
Given the following, double cost; cost = 35.94; // a floating-point manipulator cout << cost; Which manipulator replacing the comment above would cause the output to appear as "35.9"?
cout << setprecision(3); // without fixed
Which of the following statements carries syntax error?
x + 6 = 15;
What prints when the following code fragment is run runs? double x = 3; double y = 5.0; x += 4.0 * y; x -= 2.0; cout << "x = " << x << endl;
x = 21
Given the following code fragment, what are the final values of x and of y? int x = -1; int y = 3; while(x <= 6){ y = y + 3;x = x + 2; } cout << " x = " << x << " y = " << y << endl;
x = 7 y = 15
What is the correct way to re-write the following condition which is not correctly understood by the compiler: 32 > a >= 12
((32 > a) && (a >= 12))
What is the output of the following code? int func1(int, int); int func2(int); int func1(int i, int j) { if (i < 0) return 0; else return j + func2(i - 1); } int func2(int i) { if (i < 5) return 1; else return 0; } int main() { int a = 5; int b = 1; cout << func1(a, b) << endl; return 0; }
2
What is the output of this code snippet? cout << myFunc(2.9) << endl; Given the function definition: int myFunc(int input) { return input + 3; } Hint: Parameter passing is just an assignment. Typecasting will happen.
5
Which of the following statement about >> operator is wrong?
>> can only be used with cin
In the function call matching process, the _______ is passed/assigned to the _________.
Actual parameter, Formal parameter
A compiler translates source code to machine code while the program runs.
False
A do while loop is the best one to use if you do not know how many times the loop will iterates.
False
A for loop is the best one to use if you do not know how many times the loop will iterate.
False
A function can be called only when its declaration appeared before where the function is called.
False
A high-level programming language like C++ is the only way to design an algorithm.
False
A return statement is mandatory in any function definitions.
False
C++ compiler does not allow more than one functions using the same name in a program.
False
If x = 6 and y = 16, what is the truth value of this statement: x > 3 && x < 5 && y != 8
False
Predict the output of this code snippet: istringstream inSS; inSS.str("Hello my friend!"); string word; while (inSS >> word) cout << word << endl;
Hello my friend!
What is wrong with the following loop? Guess what this snippet want to achieve. How can it be fixed without changing its goal? for (int x = 0; x != 5; x += 2) { cout << "x = " << x << endl; }
Increment of 2 will cause x to jump from 4 to 6, skipping the termination condition of when x == 5. This will cause an infinite loop Instead of x != 5, the condition should use x < 5 or x <= 5.
What is NOT true about the following function with pass by reference parameters? int myFunc1(const string &name, int &score);
You may call the function like string name = "abc"; cout << myFunc1(name, 13);
What is the output of the following code fragment if the variable ans = 'Y'. char ans; cout <<"Type y for yes on n for no\n"; cin >> ans; switch (ans){ case 'y': case 'Y': cout << "You said yes\n"; case 'n': case 'N': cout << "You said no\n"; default: cout <<"invalid answer\n"; }
You said yes You said no invalid answer //no break
Predict the output of the program (pay attention to the pass-by-ref): #include <iostream> using namespace std; int mystery(int &a, int b) { if (a < b) a += b; else a -= b; b = a + b; return b; } int main() { int a = 10; int b = 8; int c; c = mystery(a, b); cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; return 0; }
a = 2 b = 8 c = 10
Which of the following tool is not used to generate executable from source code in this course?
gcc
Implement full functions to meet the expectation as follows: The call of division(5, 2); will give 2 and the call of division(5.0, 2.0); will give 2.5. Provide the definition(s) only!
int division(int a, int b) { return a / b; } double division(double a, double b) { return a / b; }
Find the valid function overloading of the given function: int math(int value1, double value2);
int math(int val1, int val2);
Assuming we have a function call as follows: double result = myFunc("abc", 3.0); What is the matching function declaration?
int myFunc(string txt, double size);
The most-specific reason (a reason that cannot be achieved using alternative methods) to write a declaration like const int INTERESTRATE = 10; is to:
make sure INTERESTRATE is not changed
What is wrong with the following code? int a = 23; double b = 53.7; a = a * b;
might lose precision
The following statement: cout << number;
outputs to screen
What is the value of the expression? (8.0 % 2) + 5 / 2
syntax error
What is the value of x after the following statements? int x; x += 50;
undefined
Which of the following functions take pass-by-reference parameters?
void swap(int &a, int &b);