CS 116 Final
A program must be compiled every time it is run. True False
False
A program that compiles correctly will execute correctly with correct results. True False
False
A struct is a homogeneous data structure. True False
False
A struct should be defined within function main. True False
False
Actual and formal parameters must match in number and variable name (identifier). True False
False
Actual parameters are listed in the function prototype. True False
False
An array index always begin with 1 in C++ True False
False
Any variable defined within function main can be accessed by any function because it is a global variable. True False
False
In a C++ program this order of statements executes correctly hours = 40; int hours; True False
False
In the actual parameter list you must list the data type along with the identifier. True False
False
Input files must be opened before being read but output files do not have to be opened before being written to. True False
False
The computer checks for array bounds when inputting values in to the array. True False
False
The dimensioned size of an array is declared at compile time but can be modified as a program runs True False
False
The only way to return a value from a function is by using a reference parameter. True False
False
The statement A++ will increment the variable A before using it. True False
False
The two values associated with the boolean datatype are Yes and No. True False
False
While loops, for loops and do-while loops always execute at least one time True False
False
While loops, for loops and do-while loops are all pretest loops. True False
False
You must always use a priming read for an input file before the loop read. True False
False
You must include a return statement in all functions. True False
False
What is the ending value of y? int x; int y; x = 2; y = ( (x + 1) / 2 ) + (x / 2) ; Invalid expression 3.5 2.5 1.5 3 2 1
2
Correctly finds the average of integer variables a and b. Ave = A + B / 2; Ave = a + b / 2; None of these Ave = (A + B) / 2; Ave = a + b / 2.0; Ave = (a + b) / 2.0;
Ave = (a + b) / 2.0;
What is the output? #include <iostream> using namespace std; const double LB_PER_KG = 2.2; double KgsToLbs(double kilograms) { double pounds; pounds = kilograms * LB_PER_KG; return pounds; }int main() { double Pounds; Pounds = KgsToLbs(10); cout << Pounds;return 0; } -No output: variable pounds declared into two functions causes compiler error -0 -22 -2.2 -No output: LB_PER_KG causes a compiler error due to being outside any function -No output: Global variable declared as const causes compiler error -10
22
How many function calls exist in the following code? int Calc1 (int a, int b) { return a + b / 2; } int Calc2 (int a, int b) { return a * b / 100; } int main () { int x; int y; x = Calc1 (5,3); cout << x; y = Calc2 (5,3); cout << y; cout << Calc2 (5,3); } 5 2 3 4
3
How many x's will be output? i = 1; while (i <= 3) { j = 1; while (j <= i) { cout << "x"; ++j; } cout << endl; ++i; } 3 4 0 1 10 6
3
Write a complete C++ program to input two integers from Numbers.txt, Determine which number is smaller and which number is larger, and output the larger and the smaller values to Large.txt, clearly labeled. Repeat this for the entire input file contents.
#include <fstream> #include <iostream> #include <iomanip> #include <string> using namespace std; int main() { int highNum, lowNum, fileNum; ifstream fin; ofstream fout; fin.open ("Numbers.txt"); if (!fin) { cout << "Error! File not found!" << endl; exit (1); } fout.open ("Large.txt"); //access/send info to file. if (!fout) { cout << "Error! Could not create file!" << endl; exit (1); } fout << fixed << setprecision(2); while(!fin.eof()) { //there is data in the file fout << "Numbers: "; totalNum = 0; for (int i = 0; i < 2; i++) { //only use i in for loop! fin >> fileNum; fout << fileNum << " "; if (i == 0) { //checking and setting up first number on line highNum = fileNum; lowNum = fileNum; } else if (fileNum > highNum) { //highest/lowest comparison highNum = fileNum; } else if (fileNum < lowNum){ lowNum = fileNum; } } fout << endl << "Highest Number: " << highNum << endl; fout << "Lowest Number: " << lowNum << endl; fout << endl; } fin.close (); fout.close (); }
Write a complete modular C++ program to fill an array called Nums with an unknown number of values but with a maximum of 75 locations from a user, until they enter 22222 which is their sentinel value.In the calculate module assign the even values to an separate array. In the calculate module return the number of original values, the number of even values and the number of odd values to function main and output these values (total number, number of evens, number of odds) in main.Call the output module twice, once with the full array and once with the even array, outputting each separately. Clearly label each. Use 3 modules, You must pass parameters appropriately and correctly. You MUST write prototypes before main and the full function definition after main. You may only return values from each function using reference parameters.
#include <fstream> #include <iostream> #include <iomanip> #include <string> using namespace std; const int LIMIT = 75; void getData(int Nums[],int & userInput, int & totalFilled); void calc(int Nums[],int userInput, int totalFilled, int & evenFilled, int & oddFilled, int Evens[]); void outData(int Nums [], int Evens [], int evenFilled, int totalFilled, int oddFilled); int main () { int Nums[LIMIT]; int Evens [LIMIT]; int userInput, totalFilled, evenFilled, oddFilled; getData (Nums, userInput, totalFilled); if (userInput == 22222){ calc(Nums, userInput, totalFilled, evenFilled, oddFilled, Evens); } outData (Nums, totalFilled); outData (Evens, evenFilled, oddFilled); } void getData(int Nums[],int & userInput, int & totalFilled){ int i = 0; totalFilled = 0; while (userInput != 22222 && i < LIMIT){ cout << "Please enter a number or (22222) to end the program: "; cin >> userInput; Nums[i] = userInput; i++; totalFilled++; } } void calc(int Nums[],int userInput, int totalFilled, int & evenFilled, int & oddFilled, int Evens[] ){ int i, num1, total; for ( i = 0; i < LIMIT; i++) { Nums[i] = num1; total = num1 % 2; if (total == 0) { Evens[i]= total; evenFilled++; i++; } else if (total == 1){ oddFilled++; i++; } } } void outData(int Nums [], int Evens [], int EvenFilled, int totalFilled, int oddFilled){ for (int i = 0; i < totalFilled;i++ ) { cout << Nums[i] << " "; } cout << "total Numbers: " << totalFilled << endl;; for (int i = 0; i < EvenFilled;i++ ) { cout << Evens[i] << " "; } cout << "# of Even Numbers: " << EvenFilled << endl; cout << "# of Odd Numbers: " << oddFilled << endl; } (OR AT LEAST CLOSE TO IT?!)
Write a complete C++ modular program. You will need main and 3 additional modules - DataIn, CalcNums, and DataOut. From main call DataIn to input two integers from the user in module DataIn and return them to main. Call CalcNums from main to determine if each number is even or odd in the CalcNums function. Call module DataOut from main to label and output the numbers, whether each number is even or odd in DataOut. Use a prototype for each function before main and then write out each function after main.
#include <fstream> #include <iostream> #include <iomanip> #include <string> using namespace std; void DataIn (int & n1, int & n2); int CalcNums (int & n1, int & n2); void DataOut (int n1, int n2); int main () { int n1, n2; DataIn (n1, n2); CalcNums (n1, n2); DataOut (n1, n2); } void DataIn (int & n1, int & n2){ cout << "Enter 1st number: "; cin >> n1; cout << "Enter 2nd number: "; cin >> n2; } int CalcNums (int & n1, int & n2){ n1 = n1 % 2; n2 = n2 % 2; return n1; return n2; } void DataOut (int n1, int n2){ if (n1 == 1) { cout << "Your 1st number is odd!\n"; } if (n1 == 0){ cout << "Your 1st number is even!\n"; } if (n2 == 1) { cout << "Your 2nd number is odd!\n"; } if (n2 == 0){ cout << "Your 2nd number is even!\n"; } }
Write a complete C++ program with main and 2 modules. Have the user enter 2 floats and 2 ints in GetNumbers module. Call function ShowData to output the numbers in regular order and then in reverse order, clearly labeled and properly formatted.
#include <fstream> #include <iostream> #include <iomanip> #include <string> using namespace std; void GetNumbers(int & n1, int & n2, double & n3, double & n4); void ShowData(int n1, int n2, double n3, double n4); int main () { int n1, n2; double n3, n4; GetNumbers(n1, n2, n3, n4); ShowData(n1, n2, n3, n4); } void GetNumbers(int & n1, int & n2, double & n3, double & n4){ cout << "Please enter first WHOLE Number: "; cin >> n1; cout << "Please enter second WHOLE Number: "; cin >> n2; cout << "Please enter first DECIMAL Number: "; cin >> n3; cout << "Please enter first DECIMAL Number: "; cin >> n4; } void ShowData(int n1, int n2, double n3, double n4){ cout << "REGULAR ORDER: " << n1 << " " << n2 << " " << n3 << " " << n4 << endl; cout << "REVERSE ORDER: " << n4 << " " << n3 << " " << n2 << " " << n1 << endl; }
Write a complete C++ program to calculate the charge for Ricardo's book purchase. The small store where he buys books allows the user to purchase a maximum of 5 books. The user enters the total number of books purchased and the type of books (N or U): $ 9.25 for each new book (N) and $ 7.25 for each used book (U) . Handle Errors in number of books with a while loop and handle errors in type of book with a do while loop. Output all input and all calculations, clearly labeled.
#include <iostream> #include <iomanip> #include <string> using namespace std; const double NEW_PRICE = 9.25; const double USED_PRICE = 7.25; int main() { //declarations char bookType, userRepeat; int totalBooks, newCount, usedCount; float bookCost, newBookIncome = 0, usedBookIncome = 0, totalIncome = 0; string finalType; //loop do while do { //user input cout << "Please enter your Book Type (N=New, U=Used): "; cin >> bookType; userVehicle = toupper(bookType); while( (bookType != 'N' && bookType != 'U') ){ cout << "Error, please enter valid Book Type:"; cin >> bookType; bookType = toupper(bookType); } cout << "Enter number of books"; cin >> totalBooks; while ((totalBooks > 5) && (totalBooks < 0)); { cout << "Error, please enter valid number of Books:"; cin >> totalBooks; } //calculations if ( userVehicle == 'N' ){ newBookIncome = totalBooks * bookCost; finalType = "New" ; newCount++; } else if ( userVehicle == 'U'){ usedBookIncome = totalBooks * bookCost; finalType = "Used"; usedCount++; } cout << setprecision(2) << fixed; cout << left << "Book Type: " << right << setw(10) << bookType << endl; cout << "Total Income: " << "$" << right << setw(5) << bookCost << endl; cout << "Would you like to add another vehicle?(Y/N): "; cin >> userRepeat; userRepeat = toupper(userRepeat); while((userRepeat != 'Y' && userRepeat != 'N')) { cout << "Error! Would you like to add another vehicle?(Y/N): "; cin >> userRepeat; userRepeat = toupper(userRepeat); } }while((userRepeat=='Y' || userRepeat=='y')); //can end program with any letter than Y. userRepeat=='Y' || userRepeat=='y' //cout total charges of everything totalIncome = usedIncome + newIncome; cout << setprecision(2) << fixed; cout << "TOTAL CHARGES: \n"; cout << left << setw(27) << "Number of New Books: " << right << setw(8) <<newCount << endl; cout << left << setw(27) << "Number of Used Books: " << right << setw(8) << usedCount << endl; cout << left << setw(27) << "New Books Charges: " << right << "$" << setw(7) << newBookIncome << endl; cout << left << setw(27) << "Used Book Charges: " << right << "$" << setw(7) << usedBookIncome << endl; cout << left << setw(27) << "Total Charges: " << right << "$" << setw(7) << totalIncome << endl; cout << "Thank you, Good bye.\n"; cout << "Program ended."; }
Write a complete C++ program to have a user enter a temperature and then determine if water would boil at that temperature (which is 212 degrees Fahrenheit). Output the temperature (labeled) and output if the temperature entered by the user is at or above boiling or not.
#include <iostream> #include <iomanip> #include <string> using namespace std; int main() { int temp; cout << "Enter the temperature of your water: "; cin >> temp; if (temp >= 212) { cout << "Your water is above boiling temperature!" << endl; } else { cout << "Your water is below boiling temperature!" << endl; } return 0; }
How many times does the while loop execute for the given input values of -1 4 0 9? userNum = 3; while (userNum > 0) { cout << userNum; cin >> userNum; } 4 1 3 0 2
2
How many times will the loop iterate, if the input is 105 107 99 103 ? cin >> x; while (x > 100) { cout << x; cin >> x; } 2 0 3 1 4
2
A theater gives a 15% discount for children under 12. They also give the 15% discount for adults over 60. Write a complete C++ program to have the user input their name and age and determine their discount. Echo all input and label all output, name, age, and whether they receive a discount or not and amount of discount.
#include <iostream> #include <iomanip> #include <string> using namespace std; int main() { int AGE; string NAME; cout << "Please enter your name "; cin >> NAME; cout << "\nPlease enter your age "; cin >> AGE; cout << NAME << " aged " << AGE; if(AGE <= 12) { cout << NAME << " aged " << AGE << endl; cout << "Eligible for 15% Discount!"; } else if(AGE >= 12 && AGE <= 60) { cout << NAME << " aged " << AGE << endl; cout << "Not Eligible for 15% Discount..."; } else if(AGE > 60){ cout << NAME << " aged " << AGE << endl; cout << "Eligible for 15% Discount!"; } return 0; }
Problem statement: Write a complete C++ program to do the following. Have the user enter the number of yards (a whole number). Convert the yards to the equivalent number of feet. Output the number of yards entered and the amount of feet . Be sure to label all output Conversion chart: 3 feet equals 1 yard
#include <iostream> #include <iomanip> using namespace std; const int FEET = 3; int main () { int yard, feet; cout << "Please input number of yards: "; cin >> yard; feet = yard * FEET; cout << yard << " yards equal to " << feet << " feet." << endl; return 0; }
Write a complete C++ program to do the following:Have the user enter 2 numbers. One number with a decimal point and one whole number. Output the numbers entered (labeled), in the order they were entered and on a separate line in the reverse order of what was entered.
#include <iostream> using namespace std; int main() { float num1; float num2; int num3; cout << "Enter first number: " << endl; cin >> num1; cout << "Enter second number: " << endl; cin >> num2; cout << "Enter third number: " << endl; cin >> num3; cout << "Entered numbers: " << num1 << " "<< num2 << " " << num3 << endl; cout << "Reversed Result: " << num3 << " " << num2 << " " << num1 << endl; return 0; }
Which input for char c causes "Done" to be output next? c = 'y'; while (c = 'y') { // Do something cout << "Enter y to continue, n to quit: "; cin >> c; } cout << "Done"; Any value other than 'y' 'y' only 'n' only No such value (infinite loop)
'n' only
What symbol has the highest priority for evaluation ? { } / All of these ( ) % *
( )
Which expression fails to compute the area of a triangle having base b and height h (area is one-half of the quantity base time height)? (b * h) / 2.0 0.5 * b * h (1 / 2) * b * h (1.0 / 2.0 ) * b * h
(1/2) * b * h
Both must be true for a person to ride: (1) At least 5 years old, (2) Taller than 36 inches. Which expression evaluates to true if a person can ride? (age > 5) || (height <= 36) (age >= 5) && (height > 36) (age >= 5) || (height > 36) (age > 5) && (height <= 36)
(age >= 5) && (height > 36)
Which expression evaluates to false if x is 0 and y is 10? (x == 0) && (y == 20) (x == 0) || (y == 10) (x == 0) && (y == 10) (x == 0) || (y == 20) None of these
(x == 0) && (y == 20)
What is the output for the following code ? for ( i = 2; i >= 2; i ++) { cout << " * "; } * * * * * * Nothing * * * * * * (infinite loop)
* * * * * * (infinite loop)
What is the output for the following code ? for ( i = 1; i <= 5; i ++) { cout << " * "; i = i + 1; } * * * * * None of these * * * * * *
*** (1 , 3, 5)
Which input value causes "Goodbye" to be output next? int x; cin >> x; while (x >= 0) { // Do something cin >> x; } cout << "Goodbye"; None of these -1 0 1 2
-1
How many times will the loop iterate, if the input is 105 107 99 103? cin >> x; while (x > 100) { // Do something cin >> x; } 2 1 None of these 3 4
2
What is the ending value of numItems if myChar = 'X'? switch (myChar) { case 'W': numItems = 1; case 'X': numItems = 2; case 'Y': numItems = 3; case 'Z': numItems = 4; } 3 4 2 10
2
Evaluate the following and select the result: 5 + 2 - 3 * 5 / 2 2 - 0.5 7 0 10 None of these
0
What possible values can x % 10 evaluate to? (x is an integer). 1 to 11 0 to 10 1 to 10 0 to 9
0-9
How many function parameters exist in the code? double FahrenheitToCelsius(double fahrenheit) { return (fahrenheit - 32.0) * 5.0 / 9.0; } int main() { double fahrenheit; cin >> fahrenheit; int c1; int c2; int c3; int c4; c1 = FahrenheitToCelsius(fahrenheit); c2 = FahrenheitToCelsius(32); c3 = FahrenheitToCelsius(78); c4 = FahrenheitToCelsius(100); } Error: Cannot pass a variable to a function 4 3 1
1
What is the output? int a = 10; do { cout << a << " ";a = a + 1; } while (a < 15); 10 11 12 13 14 15 11 12 13 14 15 11 12 13 12 10 11 12 13 14
10 11 12 13 14
What is the ending value of x? int y = 6;x = (1 / 2) * y + 8; 7 None of these 14 8 11
11
With this declaration: int m; What is the value assigned to m after this expression evaluation ? m = 5.3 + 2 * 3 ; 21.9 11 22 21 11.3 Invalid Expression
11
What is the ending value of sum, if the input is 2 5 7 3 ? All variables are ints. cin >> x; sum = 0; for (i = 0; i < x; ++i) { cin >> currValue; sum += currValue; } 14 5 7 10 12 15
17, but its not here?
Evaluate this and select the result: 1 + 35 / 2 18.0 18 17.0 17 Invalid Expression
18
What is the result of this statement. Select the best answer below. int x = 5; int y = 4; x = x > y ? 3 : 9 ; x = 5 y = 9 x = 9 y = 3 Invalid expression x = 3
3
Which expression is evaluated first? w = y + 2 + 3 * x + z; y + 2 x + z 3 * x 2 + 3
3 * x
Evaluate this and select the result: 34.56 + 4 / 2 19.28 19 37 Invalid Expression 36.56 36
36.56
What is the ending value of w? int w; int y; y = 34; w = y % 5; 5 1 6 7 4
4
What is the output if count is 4? for (i = count; i > 0; --i) { cout << i << ' ' ; } 3 2 1 4 3 2 1 0 4 3 2 1 None of these 4
4 3 2 1
What is the index of the last element? int numList[50]; 50 0 49 Unknown, because the array has not been initialized.
49
Evaluate this and select the result: 10 % 4 + 1.0 * 3 Invalid Expression 5 9 5.0 9.0
5.0
What is the output, if the input is 3 2 1 0? cin >> x; while (x > 0) { cout << 2 * x << " "; } 6 6 6 6 6 ... (infinite loop) 6 4 2 6 4 2 0 6
6 6 6 6 6 ... (infinite loop)
Evaluate this and select the result: 5 % 7 + 5 / 2 7 Invalid Expression 5 5.0 7.5 None of these
7
Given that integer array x has elements 4, 7, 3, 0, 8, what are the values of these elements after the loop? int i; for (i = 0; i < 4; ++i) { x[i] = x[i + 1]; } 7, 3, 0, 8, 4 4, 4, 7, 3, 0 7, 3, 0, 8, 8 Error: Invalid access
7, 3, 0, 8, 8
What is output? double MyFct(double a, double b) { return (a + b) / 2.0; } int main() { double x = 3.0; double y = 5.0; double z = 8.0; double t, w; t = MyFct(x, y); w = MyFct(t, z); t = MyFct(w, z); cout << t << endl; return 0; } 4.0 14.0 8.0 7.0 6.0 16.0
7.0
The end of statement terminator is C++ is : . << >> ;
;
The C++ assignment operator is << >> <-- ; = -->
=
What symbol is used with a C++ input statement to enter a value into a variable ? <-- = --> >> <<
>>
Which is true regarding how functions work? -A return address indicates the value returned by the function -After a function returns, its local variables keep their values, which serve as their initial values the next time the function is called -If a function returns a variable, the function stores the variable's value until the function is called again -A function's local variables are discarded upon a function's return; each new call creates new local variables in memory
A function's local variables are discarded upon a function's return; each new call creates new local variables in memory
What is the name of these symbols ? ! & & | | Boolean Operators Boolean Operands Relational Operators Relational Operands Operands
Boolean Operators
Another name for branching structure in a C++ program
Branching Statements
Which is true? C++ came after C+ C++ came after C C++ came after C# but before C C++ came after B C came after C++
C++ came after C
Brains of the computer RAM IDE ROM Hard Drive CPU
CPU
Software that changes a high level language into code understood by a computer
Compiler
What is the output? void Swap (int & x, int y) { int tmp; tmp = x; x = y; y = tmp; } int main() { int p = 4, q = 3; Swap (p, q); cout << "p = " << p << ", q = " << q << endl; } Error: Argument names must match parameter names p = 4, q = 3 p = 3, q = 3 p = 3, q = 4
Error: Argument names must match parameter names
What does this code output? cout << "I ";cout << "want pie." << endl; Iwant pie "I " "want pie." I wantpie I wantpie I want pie.
I want pie.
What will a compiler do for the following three lines of code? // x = 2; // width // y = 0.5 // z = x * y; Total area Yield an error for line 1 (x = ...) Yield an error for line 2 (y = ...) Ignore all three lines Yield an error for line 3 (z = ...)
Ignore all three lines
Which line of the function has an error? int ComputeSumOfSquares(int num1, int num2) { // Line 1 int sum; // Line 2 sum = (num1 * num1) + (num2 * num2); // Line 3 return; // Line 4 } Line 1 Line 4 Line 3 Line 2 None of the lines contains an error
Line 4
The following program results in a compiler error. Why? int FindMin(int a, int b) { int min; if (a < b) { min = a; } else { min = b; } return min; } int main() { int x; int y; cin >> x; cin >> y; min = FindMin(x,y); cout << "The smaller number is: " << min << endl; return 0; } The variable min should be a parameter The variable min is declared in FindMin(), but is used in main() The variable min is not initialized FindMin() should be defined after main(), not before.
The variable min is declared in FindMin(), but is used in main()
Input devices. Select all that apply. Identifier Mouse Display Keyboard Printer
Mouse Keyboard
What is the output for x = 15? switch (x) { case 10: // Output: "First " break; case 20: // Output: "Second " break; default: // Output: "No match "break; } First Second No match First First Second Second No match
No match
Punctuation symbols such as comma and $ are allowed in ..... large integers ints representing money floats representing money None of these All of these floats
None of these
What is the output from these statements: int num = 100; if ( num > 100) cout << num << " is greater than 100. " ; Invalid Expression num is greater than 100. Nothing 100 is greater than 100 . 100
Nothing
Which XXX (expression) causes the program to output the message "Hello!"? void PrintMessage() { cout << "Hello!"; } int main() { XXX; return 0; } cout << PrintMessage( ) None of these PrintMessage("Hello!") PrintMessage( ) void PrintMessage( )
PrintMessage( )
Commands to direct a computer to solve a problem -Algorithm -Variable -Program -Constant -Pseudocode
Program
Volatile Storage on a computer. RAM None of these Solid State Hard Drive Flash Drive ROM
RAM
What is Moore's law? -Every action has an equal and opposite reaction -Any given program will become obsolete -The capacity of ICs doubles roughly every 18 months -A processor used in cold weather is more likely to fail
The capacity of ICs doubles roughly every 18 months
Write out a variable trace and output trace for the following code. You MUST use the format shown in class: int a = 2 ; int b = a * 2 + 3 ; float c = b / a; cout << "a= " << a ; cout << "b = " << b ; cout << "c= " << c; a = b + c;
Variable trace: a b c output 2 7 3.0 a=2 b=7 c=3.0 10
Which corrects the logic error in the following program? void FindPrevNext (int x, int prev, int next) { prev = x - 1; next = x + 1; } int main () { int x = 10; int y; int z; FindPrevNext (x, y, z); cout << "Previous = " << y << ", Next = " << z; return 0; } The variables y and z should be initialized to 0 The function FindPrevNext() should have a return statement for variables prev and next The variables prev and next should be passed by reference prev and next should be initialized in FindPrevNext()
The variables prev and next should be passed by reference
A do-while loop continues executing when the Boolean expression is True and exits on False True False
True
A function may call another function. True False
True
A good practice is to include a single space around operators for readability, as in num + 5, rather than num+5 True False
True
A program can read varying amounts of data in a file by using a loop that reads until the end of the file has been reached using the eof() function. True False
True
An array is a homogeneous data structure. True False
True
An array may be included as part of a struct. True False
True
Functions in C++ may send back to the calling program zero, one, or multiple values. True False
True
Parameters listed in the function heading are formal parameters. True False
True
Given only int variables a and b, which is a valid expression? a + ab a + -2 2 x 3 4a + 3
a + -2
What is the output? #include <iostream> using namespace std; struct Sum { int a; int b; }; void Display(Sum obj) { obj.a = obj.a + 5;obj.b = obj.b + 10; cout << "a = " << obj.a << " b = " << obj.b << endl; cout << "Sum = " << obj.a + obj.b << endl; } int main(){ Sum point; point.a = 10; point.b = 20; Display(point); return 0; } -a = 10 b = 20 Sum = 30 -Error: Struct declared incorrectly -a = 5 b = 10 Sum = 15 -a = 15 b = 30 Sum = 45
a = 15 b = 30 Sum = 45
Rate at which a processor executes instructions. terabytes Moore's Law clock transistor gigabytes
clock
Which statement correctly declares a constant called factor to have a value of 12 const FACTOR = int 12; None of these const int FACTOR = 12; const factor = 12 CONST FACTOR = 12; const int FACTOR = = 12;
const int FACTOR = 12;
Which XXX calculates and prints the total cost? #include <iostream> #include <string> using namespace std; struct Item { double price; string name; int quantity; double total; }; struct Total { double totalCost; }; int main( ) { Item book1; Item book2; Total cost; book1.total = book1.price*book1.quantity; book2.total = book2.price*book2.quantity; XXX } --cost.total = book1.total + book2.total; cout << "Total: " << cost.totalCost << " dollars." << endl; --cost.totalCost = book1.total + book2.total; cout << "Total: " << cost.totalCost << " dollars." << endl; --cost.totalCost = book1.totalCost + book2.totalCost;cout << "Total: " << cost.totalCost << " dollars." << endl; --totalCost = book1.total + book2.total;cout << "Total: " << totalCost << " dollars." << endl;
cost.totalCost = book1.total + book2.total; cout << "Total: " << cost.totalCost << " dollars." << endl;
Choose the statement(s) that generate(s) this output: I wish you were here -cout << "I wish" + "you were here"; -cout << "I wish"; cout << "you were here" << endl; -cout << "I wish" << endl; cout << "you were here"; -cout << "I wish" << ln << "you were here";
cout << "I wish" << endl; cout << "you were here";
Correctly outputs and labels the value in the variable num1 None of these cout >> 'Number: ' << num1; cout << "Number: " << Num1; cout << "Number: " << num1; cout >>"Number: " >> Num1; cout >> "Number: " >> num1;
cout << "Number: " << num1;
Correctly outputs variables num1 and num2 on the same line, separated by one space. cout >> Num1 >> ' ' >> Num2; cout << num1 << " " << num2; cout << num1 << num2; cout >> num1 >> " " << num2; cout << Num1 << ' ' << Num2; None of these
cout << num1 << " " << num2;
Which is a post-test loop ? for while if do while None of these if ( ) else
do while
A name for operands and operator that are combined to produce one result
expression
Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items in parentheses are evaluated first. (x == 5) || (y == 2) && (z == 5) (false OR true) AND false --> true AND false --> false false OR (true AND false) --> false OR true --> true false OR (true AND false) --> false OR false --> false (false OR true) AND false --> true AND false --> true
false OR (true AND false) --> false OR false --> false
Which best describes the meaning of a 1 (true) being output? Assume v is a large array of ints. int i; bool s; for (i = 0; i < N_SIZE; ++i) { if (v[i] < 0) { s = true; } else { s = false; } } cout << s; all values are negative first value is negative last value is negative some value other than the last is negative
first value is negative
Which valid C++ data type can be used with the / operator ? Select all that apply. decimal float bool char real int
float int
The user has entered 4 floats. Write ONLY the formatted output statement for the following: Output these values, the first 2 values with 4 decimal digits, the third number with 3 decimal digits and the last value with 1 decimal digit to a file, fileout.
float num1, num2, num3, num4; { outfile << fixed << setprecision(4) << num1 << endl; outfile << fixed << setprecision(4) << num2 << endl; outfile << << fixed << setprecision(3) << num3 << endl; outfile << << fixed << setprecision(1) << num4 << endl; } infile.close (); outfile.close ();
Which for loop will iterate 100 times? for (i = 1; i < 100; i++) for (i = 0; i <= 100; i++) for (i = 1; i < 99; i++) for (i = 0; i < 100; i++) None of these for (i = 0; i < 99; i++)
for (i = 0; i < 100; i++)
What loop must know the number of repetitions before it executes?
for loop
What must be loaded for a program to execute using files? iostream fstream instream ifstream ofstream outstream
fstream
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) if (!hasFunds && recentlyUsed) if (hasFunds && recentlyUsed) if (hasFunds && !recentlyUsed)
if (!hasFunds && !recentlyUsed)
Declare a file called inputFile for program input
ifstream inputFile;
Valid statement to input 2 integer variables called num1 and num2 from inFile. inFile >> Num1 >> Num2; cin >> num1 >> num2 >> inFile; cin >> num1 >> num2; cin<< num1 << num2; inFile >> num1, num2;
inFile >> Num1 >> Num2;
Which is NOT a rule for naming an identifier ? Select all that apply. Includes punctuation Starts with an underline Starts with a space Starts with a letter None of these Starts with a number
includes punctuation starts with a space starts with a number
Stores a whole number. Select all that apply. double bool long short int float
int
Which of the following are valid C++ data types that can be used for performing arithmetic calculations? char string bool decimal real All of these int
int
Which valid C++ data type(s) can be used with the % operator ? real float char decimal int bool
int
Write a short C++ program to do the following:A weather forecasting company needs to have a data structure that holds the following information in one group: month, date, year, the high temperature for the day, the low temperature for the day, and the top wind speed for the day.Define a data structure for this data and declare one variable of this type called DayOne.Assign the following values to DayOne: February, 18, 2017, 68, 45, 9 in main. Also in main output each of these values stored in DayOne.
int main() { struct weather { string month; int day; int year; int Htemp; int Ltemp; int windSpd; }; weather DayOne; string month = "February"; DayOne.day = 18, DayOne.year = 2017, DayOne.Htemp = 68, DayOne.Ltemp = 45, DayOne.windSpd = 9; cout << "Date: " << month << " " << DayOne.day << ", " << DayOne.year << endl; cout << "High Temp: " << DayOne.Htemp << " degrees\n"; cout << "Low Temp: " << DayOne.Ltemp << " degrees\n"; cout << "Wind speed: " << DayOne.windSpd << " mph\n"; }
Code understood by a computer. Select all that are correct. -machine language -compiler -source code -object code -binary -assembly language
machine language binary
Which assigns the array's first element with 99? int myVector[4]; myVector[1] = 99; myVector[] = 99; myVector[-1] = 99; myVector[0] = 99;
myVector[0] = 99;
Declare a file called outputFile for program output
ofstream outputFile;
Software used to control the basic functionality of a computer language compiler language translator application software IDE operating system
operating system
Which is not a valid data type in C++ ? int float real bool All are valid char
real
Name for these symbols: < <= > >= != == boolean operators airthmetic operators arithmetics mathematics relational operators
relational operators
The name of the type of a variable that signals the end of input data.
sentinel
The position of an array element is referenced by using a(n) .... Check all that apply subscript location index member name identifier
subscript location index
Correctly assigns the total of variables a and b to the variable sum. a + b = total None of these sum = a + b a + b = sum; total = a, b; sum = a + b;
sum = a + b;
Which assigns the last array element with 20. int userNum[N_SIZE]; userNum[] = 20; userNum[N_SIZE] = 20; userNum[20]; userNum[N_SIZE - 1] = 20;
userNum[N_SIZE - 1] = 20;
What general type of loop may never execute ?
while loop
Which XXX (statement) could be valid for the following code? int CalcSum(int a, int b) { return a + b; } int main() { int y; XXX cout << y; return 0; } y = CalcSum(4, 5); y = CalcSum(4 + 5); CalcSum(y, 4, 5); None of these y = CalcSum();
y = CalcSum(4, 5);