C++ Multiple choice
What is the ending value of y? pow(u, v) returns u raised to the power of v. x = 2.0; y = 3.0; y = pow(pow(x, y)) + 1.0;
Error: The compiler complains about calling a function within another function
Is the following statement True or False? The compiler will not compile code that has run-time errors.
False
The compiler compiles machine language into C++ code.
False
For what values of integer x will Branch 3 execute? If x < 10 : Branch 1 Else If x > 9: Branch 2 Else: Branch 3
For no values (never executes)
Write a C++ expression that computes the following formula: 2 * pi * sqrt (l*cos(theta)/g) Assume the variables l, g, and theta have been properly declared and initialized with the double data type. The value in variable theta is in radians.
2*M_PI*sqrt(l*cos(theta)/g)
Which line of code creates an Arcade object correctly? class Arcade { public: Arcade(); Arcade(string name, int r); private: string arcName; int rating; }; Arcade:: Arcade() { arcName = "New"; rating = 1; } Arcade:: Arcade(string name, int r) { arcName = name; rating = r; }
Arcade myArc("Games Ablaze", 5);
Answer the following question without executing the code: After the code below executes, what is the value of inches? int inches ( 25 ), feet ( 5 ) ; inches *= feet + sqrt( inches ) ;
250
How many objects of type Car are created? Car mustang; Car prius; int miles; Truck tundra;
2
Given that x and y are variables of type int, and the following mathematical equation: (4/5) <= y <= (6/5) Select all expressions below that are equivalent to the relation above :
!((0.8 * x > y) || (y > 1.2* x)) or (4 * x <= 5 * y) && (5 * y <= 6 * x) or (4/5 * x <= y) && ( y <= 6/5 * x)
Write the preprocessor directive line that will allow a program to use std::cout
#include <iostream> or #include<iostream> (no space)
Implement the following as a for loop where XXX is in the code below. Omit comments. ∑ i < 20 and i = 0 #include <iostream> using namespace std; int main() { XXX cout << i << endl: }
#include <iostream> using namespace std; int main() { int i; for(int j = 2; j < 20; j++){ i += j; } cout << i << endl; }
Given function Multiply(u, v) that returns u * v, and Add(u, v) that returns u + v, which expression corresponds to: Add(Multiply(x, Add(y, z)), w)
(x * (y + z)) + w
Which expression evaluates to 3.5? int x = 7; int y = 1;
(x / 2.0)
What denotes the dereference operator?
*
Given the following code: const int LIST_SIZE(5); int *list = new int[ LIST_SIZE ]; [CODE] Write a single C++ statement using pointer arithmetic that will assign the integer 22 to the second element in the array list. DO NOT use square bracket notation. I.e. do not use arr[ 1 ].
*(list+1) = 22;
What is the output? #include <iostream> #include <iomanip> using namespace std; int main() { int num = 5; for (int i = 0; i < 3; i++) { cout << setw(5) << setfill('*') << num << endl; num = num * 5; } return 0; }
****5 ***25 **125
Given the following code: double x(42.0); How many digits are displayed after the decimal place when you display variable x using default mode? Assume no other formatting options have been changed.
0
If the input is 5, what is the output? int x; int z = 0; cin >> x; if (x > 9){ z = 3; z = z + 1; } cout << z;
0
The numNegatives variable counts the number of negative values in the vector userVals. What should numNegatives be initialized to? vector<int> userVals(20); unsigned int i; int numNegatives; numNegatives = XXX; for (i = 0; i < userValues.size(); ++i) { if (userValues.at(i) < 0) { numNegatives = numNegatives + 1; } }
0
What is the ending value of y? int x; int y; x = 6; y = (1 / 2) * (x + 5);
0
What is the ending value of y? int x; int y; x = 6; y = (1 / 2) * (x + 5);
0
What is the output? int n; for (n = 0; n < 10; n = n + 3) { cout << n << " "; }
0 3 6 9
What is the output? int n; for (n = 0; n < 10; n = n + 3) { cout << n << " "; }
0 3 6 9
Which number is equivalent to the value 4.2e-3?
0.0042
What is the output? int FindSqr(int a) { int t; t = a * a; return a; } int main() { int square; square = FindSqr(10); cout << square; return 0; }
10
What is y after executing the statements? x = 5; y = x + 1; y = y * 2;
12
Which expression is evaluated first? w = y + 2 + 3 * x + z;
3*x
Given the following code: double val(35.1415366359); What is displayed to the screen when you display the value of variable val in fixed mode? Assume no other formatting options have been changed.
35.141537
What is the ending value of the element at index 1? int numbers[10]; numbers[0] = 35; numbers[1] = 37; numbers[1] = numbers[0] + 4;
39
What is the ending value of x? int x; userText = "mississippi"; x = userText.find("i", 3);
4
Which number is equivalent to the value 0.42e1?
4.2
What is the output? int x = 18; while (x > 0) { // Output x and a space x = x / 3; }
6 2 0
What is the final value of y? int x = 77; int y = 4; if (x == 77) { y = y + 1; } if (x < 100) { y = y + 1; } if (x > 77) { y = y + 1; } y = y + 1;
7
What is the final value of y? int x = 77; int y = 4; if (x == 77) { y = y + 1; } if (x < 100) { y = y + 1; } if (x > 77) { y = y + 1; } y = y + 1;
7
What is the ending value of z? x = 0; y = 3; z = pow(x + 2, y);
8
If the function's vector parameter is 0 3 9 7 7, what does the function return? int MyFct(const vector<int>& v) { int i; int x; x = v.at(0); for (i = 0; i < v.size(); ++i) { if (v.at(i) > x) { x = v.at(i); } } return x; }
9
The for statement has four parts as shown below (A - D): for ( A ; B ; C){ D; }
A
Which of the following statements is true about a class' private helper function?
A private helper function helps public functions carry out tasks.
The for statement has four parts as shown below (A - D): for ( A ; B ; C){ D } Which part will update loop variable(s)?
C
Which constructor will be called by the statement House myHouse(97373); for the given code? class House { House(); // Constructor A House(int zip, string street); // Constructor B House(int zip, int houseNumber); // Constructor C House(int zip); // Constructor D };
Constructor D
What is the output? void Area(double x, double y) { cout << x * y; } void Area(int base, int height) { cout << (base * height) / 2; } void Area(int length, int width) { cout << length * width; } int main() { int b; int h; b = 4; h = 5; Area(b, h); }
Error: Compiler cannot determine which function to call
What is the output? void Area(double x, double y) { cout << x * y; } void Area(int base, int height) { cout << (base * height) / 2; } void Area(int length, int width) { cout << length * width; } int main() { int b; int h; b = 4; h = 5; Area(b, h); }
Error: Compiler cannot determine which function to call
What is the output? void Display (int i) { cout << "Function 1: " << i << endl; } void Display (double f) { cout << "Function 2: " << f << endl; } int main() { double i; i = 10.0; Display(i); return 0; }
Function 2: 10
Which function call passes a string myStr to the given function? void StrMod(char modString[]) { int i; // Loop index for (i = 0; i < strlen(modString); ++i) { if (modString[i] == '.') { modString[i] = '!'; } } }
StrMod(myStr);
For which quantity is a double the best type?
Height of a building
What is the output? #include <iostream> using namespace std; class Greet { public: Greet(); }; Greet::Greet() { cout << "Hello"; } int main() { Greet greeting; return 0; }
Hello
Which of the following statements describes an abstraction?
Hiding the implementation and showing the important features
What does this code output? cout << "I "; cout << "want pie." << endl;
I want pie.
What is the output for the call DisplayTime(2, 24, 65)? void DisplayTime(int hours, int minutes, int seconds) { // Parameter error checking if ((hours < 1) || (hours > 12)) { cout << "Invalid hours" << endl; hours = 1; } if ((minutes < 1) || (minutes > 60)) { cout << "Invalid minutes" << endl; minutes = 1; } if ((seconds < 1) || (seconds > 60)) { cout << "Invalid seconds" << endl; seconds = 1; } cout << hours << ":" << minutes << ":" << seconds; }
Invalid seconds 2:24:1
Function CalcSum() was copied and modified to form the new function CalcProduct(). Which line of the new function contains an error? int CalcSum(int a, int b) { int s; s = a + b; return s; } int CalcProduct(int a, int b) { int p; // Line 1 p = a * b; // Line 2 return s; // Line 3 }
Line 3
If the input is 5, what is the output? int x; cin >> x; if (x < 10) { cout << "Live "; } else if (x < 20) { cout << "long "; } else if (x < 30) { cout << "and "; } cout << "prosper!";
Live prosper!
If the input is -5, what is the output? If x less than 0 Put "Low " to output Else if x greater than 0 Put "OK " to output Else if x less than -3 Put "Very low " to output
Low
What has access to a class's private data members?
Member functions but not class users
In what component does a processor store the processor's required instructions and data?
Memory
What is the output for x = 15? switch (x) { case 10: // Output: "First " break; case 20: // Output: "Second " break; default: // Output: "No match " break; }
No Match
What is the output for x = 15? switch (x) { case 10: // Output: "First " break; case 20: // Output: "Second " break; default: // Output: "No match " break; }
No match
What does strrchr() do?
Returns a pointer to the last occurrence of the character.
Given a list of syntax errors from a compiler, a programmer should focus attention on which error(s), before recompiling?
The first error only
Consider the following class definition where a circle is represented just by its radius: class Circle{ private: double radius; public: double scaled_area(double factor) const; }; What does const mean here?
The function implementation cannot change the value of radius
Which is best declared as a constant?
The number of feet in a yard
Given the following function prototypes: int get_age(); double life_expectancy(int age, double height, double weight); What is wrong with this function call? double life = life_expectancy(get_age(), 5.0);
Too few arguments (actual parameters)
In this course, you should place the function header (also called prototype) BEFORE the main function.
True
Assume you write and run a program that attempts to open a file in order to write numeric salary data (only). Which of the following situations would NOT cause a runtime error:
You write sensitive information in plain text.
Given the following class definition: class Rectangle { private: int width; int height; public: void setWidth(const int w); void setHeight(const int h); int getWidth() const; int getHeight() const; }; Write the complete definition of the member function setHeight that includes the function header and code to implement the function.
Your Answer: void Rectangle:: setHeight( const int h) { height = h; }
Given only int variables a and b, which is a valid expression?
a + -2
Consider the following code: const int SIZE = 10; int nums[SIZE]; int *intPtr = nums; for(int i = 0; i < SIZE; i++){ nums[i] = i * 2; } delete [ ] intPtr; What is the result when the very last line of the code above is executed?
a runtime error
Using correct C++ syntax, write the definition for a class called Person which ONLY contains four public class attributes: name (string), age (integer), height (integer), and salary (double). Do not define any member functions.
class Person { public: string name; int age; int height; double salary; };
Given an integer array of size NUM_ELEMENTS, which XXX, YYY, and ZZZ will count the number of times the value 4 is in the array? Choices are in the form XXX / YYY / ZZZ. int myArr[NUM_ELEMENTS]; int cntFours; XXX for (i = 0; YYY; ++i) { if (myVals[i] == 4) { ZZZ; } }
cntFours = 0; / i < NUM_ELEMENTS; / ++cntFours;
Which statement will generate 3.140e+00 as the output?
cout << setprecision(3) << scientific << 3.14;
Which statement properly frees the dynamically allocated array below? int * musketeers = new int[8];
delete[] musketeers;
Which of the following loops is/are considered a post-test loop?
do-while loop
Which of the following is an accessor declaration?
string GetName() const;
Select all the types below that should not be compared using the == operator.
double
Which XXX declares a member function called GetAmount() and returns the variable amount? class Bill { public: void SetProduct(string prodName); void SetAmount(int prodAmount); XXX private: string name; double amount; }; int main() { Bill myBill; cout << myBill.GetAmount(); }
double GetAmount();
The ____ function returns true if the previous stream operation had an error.
fail()
Given the two vectors, which code will output all the vectors' elements, in the order key, item followed by a newline? vector<int> keysList(SIZE_LIST); vector<int> itemsList(SIZE_LIST);
for (i = 0; i < keysList.size(); ++i) { cout << keyList.at(i) << ", " << itemsList.at(i) << endl; }
You have been asked to write a program that computes the sum of 7 integers entered by the user. Which type of loop would you use?
for loop
Given userStr = "Doghouse", what is the ending value of char x? x = tolower(userStr.at(2));
g
What is displayed to the screen after execution of these two lines of C++ code? char txt[] = {'g', 'o', 'o', 'd', '\0', 'b', 'y', 'e'}; cout << txt;
good
Which XXX causes every character in string inputWord to be output? for (XXX) { cout << inputWord.at(i) << endl; }
i = 0; i < inputWord.size(); ++i
Which YYY outputs the string in reverse? Ex: If the input is "Oh my", the output is "ym hO". int i; string str; getline(cin, str); for (YYY) { cout << str.at(i); }
i = str.length() - 1; i >= 0; --i)
Which line in the below code causes an error? #include <iostream> #include <vector> using namespace std; int main() { vector<int> idNums(5); idNums.at(1) = 60; idNums.at(2) = 70; idNums.at(3) = 82; idNums.at(4) = 90; idNums.at(5) = 92; cout << "Size: " << idNums.size() << endl; idNums.push_back(2); cout << "New size: " << idNums.size() << endl; return 0; }
idNums.at(5) = 92;
Which if branch executes when an account lacks funds and has not been used recently? hasFunds and recentlyUsed are booleans and have their intuitive meanings.
if (!hasFunds && !recentlyUsed)
Given an ofstream outFS, which syntax is used to check whether a file is opened successfully?
if (!outFS.is_open()) { ... }
Which is a valid definition for a function that passes two integers (a, b) as arguments, and returns an integer?
int MyFunction(int a, int b)
Which function is called? p = ProcessData(9.5);
int ProcessData (double num, int x = 2){...};
Which of the following code snippets results in a memory leak? void StudentClass() { int * ptr = new int(3); }
int main() { StudentClass(); return 0; }
Which best describes the meaning of a 1 (true) being output? Assume v is a large vector of ints. int i; bool s; for (i = 0; i < v.size(); ++i) { if (v.at(i) < 0) { s = true; } else { s = false; } } cout << s;
last value is negative
Which best describes what is output? Assume v is a large vector of ints. int i; int s; s = v.at(0); for (i = 0; i < v.size(); ++i) { if (s > v.at(i)) { s = v.at(i); } } cout << s;
min value in v
Which assigns the array's last element with 99? int myVector[15];
myVector[14] = 99;
What is the return type for constructors?
no return type
Which line of code assigns a char variable outputGames with the value the gamesPointer points to? char outputGames = 'B'; char* gamesPointer;
outputGames = *gamesPointer;
A programmer compares x == y, where x and y are doubles. Many different values are expected for x and y. For values that a programmer expects to be equal, the comparison will _____ .
sometimes evaluate to true, sometimes to false, depending on the values
Given the input "the usa has 50 states", which XXX generates the output "the USA has 50 states"? #include <iostream> #include <cstring> using namespace std; int main(void) { char userInput[100]; char* stringPosition = nullptr; cout << "Enter a line of text: "; cin.getline(userInput,100); stringPosition = strstr(userInput, "usa"); if (stringPosition != nullptr) { XXX } cout << userInput << endl; return 0; }
strncpy(stringPosition, "USA", 3);
What code for XXX, YYY correctly swaps a and b? Choices are written as XXX / YYY. XXX; a = b; YYY; b = tmp;
tmp = a / (nothing)
For the given pseudocode, which XXX and YYY will output the smallest non-negative input (stopping when a negative value is input)? Choices are in the form XXX / YYY. min = 0 val = Get next input min = val While val is not negative If XXX YYY val = Get next input Put min to output
val < min / min = val
A pointer is a(n) _____ that contains a _____.
variable, memory address
Consider the following code. Assume all preprocessor directives have been declared. ___________________ // function prototype int main( ) { int age; double salary; getData(age, salary); cout << "New salary = " << salary << endl; return 0; } Assume that the getData function was coded later in the source code file. Write the function prototype for the getData function that would set the values for both age and salary on the code fragment above.
void getData (int & age, double & salary);
What is the output? int columns; int rows; for (rows = 0; rows < 2; ++rows) { for (columns = 0; columns < 3; ++columns) { cout << "x"; } cout << endl; }
xxx xxx
Which statement is incrementing the integer x, where y is also an integer?
y = x + 1;