CSCI 207

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Error: Any default parameters must always end the parameter list

A programmer defines the following: int CalcRoute(int x = -1, int y, int z) ...What is true for the following call: CalcRoute(2,3)?

maxValue = Max(15, max(35,25));

For the following function, which is a valid function call? Assume maxValue is an integer. int Max(int x, int y) { if (x > y) { return x; } else { return y; } }

2

For the given program, how many cout statements will execute? void PrintShippingCharge(double itemWeight) { if ((itemWeight > 0.0) && (itemWeight <= 10.0)) { cout << (itemWeight * 0.75) << endl; } else if ((itemWeight > 10.0) && (itemWeight <= 15.0)) { cout << (itemWeight * 0.85) << endl; } else if ((itemWeight > 15.0) && (itemWeight <= 20.0)) { cout << (itemWeight * 0.95) << endl; } } int main() { PrintShippingCharge(18); PrintShippingCharge(6); PrintShippingCharge(25); return 0; }

Line 3

Function CalcSum() was copied and modified to form the new function CalcProduct(). Which line of the new function contains and error? int CalcSum( int a, int b) { int p; //Line 1 p = a * b; //Line 2 return s; // Line 3 }

Constant, and pass by reference

Give a function with one vector parameter ages. How should the parameter be defined if ages may be very large, and the function will not modify the parameter?

(10,j,k+5)

Given integers i, j, k, which XXX correctly passes three integer arguments for the following function call? addInts XXX;

Test('x');

Given the following function definition, which function call prints x, y, 0? void Test(char 1, char b - 'y', int num = 0) { cout << a << "," << b << "," << num << endl; }

1

Given the following function. To change the function to return the product instead of the sum, how many lines of code need to be changed? int Calculate(int a, int b) { return a +b; } int main () { cout << Calculate (3,4); cout << Calculate (5,2); cout << Calculate (6,7); return 0; }

3

How many function arguments exist in the code? double FahrenheitToCelsuis(double fahrenheit) { return (fahrenheit - 32.0) * 5.0 / 9.0; } int main () { double fahrenheit; cin >> fahrenheit; int c1; int c2; int c3; c1 = FahrenheitToCelsius(fahrenheit); c2 = FahrenheitToCelsius(32); c3 = FahrenheitToCelsius(fahrenheit + 5.0); }

3

How many function calls exist in the following code? int Calc1 (int a, int b) { return 1 + b /2; } int main() { int x; int y; x = Calc1 (5,3); cout << x; y = Calc2 (5,3); cout << y; cout << Calc2 (5,3); }

the file is overwritten with the new data

If a file exists in a directory and contains data, when an ofstream is used to write data into the file ______

9

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; }

fail.()

The ______ function returns true if the previous stream operation had an error.

MyFct() cannot be called with a literal

What is the error in this code? void MyFct(int& x) { x = (x*x) + 1; } int main () { cout << MyFct(9); }

Line 3

Which line of the function has an error? int ComputeSumOfSquare(int num1, int num2) { int sum; //line 1 sum = (num1 * num1) + (num2 * num2); //line 2 return; //line 3 }

<<

Which operator is the insertion operator?

1

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); }

false when the end of the file has not been reached

The eof() function of input streams returns __________

PrintSum() has void return type, so cannot be assigned to a variable

The following program generates an error, Why? void PrintSum(int num1, int num2) { cout << num1 + num2; } int main() { int y; y = PrintSum(4,5); return 0; }

name

The inFS.open(str) function has a string parameter str that specifies the _____ of the file to open.

Reading from the file... This

What is output? #include <iostream> #include <fstream> using namespace std; int main () { ofstream outFS; ifstream inFS; string str; outFS.open("myfile.txt."); outFS << "This is a test data"; if(!outFS.is_open()) { cout << "Could not open file myoutfile.txt" << endl; return 1; } outFS.close(); inFS.open("myfile.txt"); cout << "Reading from file..." << endl; inFS >> str; cout << str; return 0; }

6.0

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; t = MyFct(x, y); t= MyFct(t, z); cout << t << endl; return 0; }

Opening the file Could not open the file.

What is the output if myContact.txt file does not exist? #include <iostream> #include <fstream> using namespace std; int main () { ifstream inFS; cout << "Opening the file." << endl; inFS.open("myContact.txt"); if(!inFS.is_open()) { cout << "Could not open the file." << endl; return 1; } inFS.close(); return 0; }

Tom

What is the output if the input is Tom - Sawyer? #include <iostream> using namespace std; int main() { string playerName; cout << "Enter name"; cin >> playerName; cout << endl << playerName; return 0; }

Sum 150

What is the output if the user enters 10 20 30 40 50 ? #include <iostream> #include <sstream> #include <string> using namespace std; int main () { istringstream inSS; int number; int sum = 0; string numList; cout << "Enters numbers separated by spaces:"; getline(cin, numList); inSS.str(numList); while (inSS >> number) { sum = sum + number; } cout << "Sum:" << sum << endl; return 0; }

Invalid seconds 2:24:1

What is the output of 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; }

22

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; }

`Checking for fever...

What is the output? double CheckForFever (double temperature) { const double NORMAL_TEMP = 98.6; const double CUTOFF_TEMP = 95; double degreesOfFever; if(temperature > NORMAL_TEMP) { degreesOfFever = temperature - NORMAL_TEMP; cout << "You have " << degreesOfFever << "degrees of fever."; } else if(temperature < CUTOFF_TEMP) { degreesOfFever = CUTOFF_TEMP - temperature; cout << "Your temperature is" << degreesOfFever << "below 95."; } return degreesOfFever; } int main () { double bodyTemperature; double degreesOfFever; bodyTemperature = 96.0; cout << "Checking for fever..." ; degreesOfFEver = CheckForFever(bodyTemperature); return 0; }

16

What is the output? int Calc(int num1, int num2) { return 1 + num1 + num2; } int main() { int x; x = Calc(4,5); cout << Calc(x,5); return 0; }

10

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; }

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); }

Function 2: 10

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; }

No output. A compiler error occurs due to unknown variable even

What is the output? void isEven(int num) { int even; if (num % 2 == 0) { even = 1; } else { even = 0 } } int main () { IsEven(7); cout << even; return 0; }

Individually testing a small part (or unit) of a program, typically a function, using a separate program called a test harness

What is unit testing?

isstringstream inSS(userInfo);

Which XXX (string stream) returns Hello John Denver as the output? #include <iostream> #include <sstream> #include <string> using namespace std; int main () { string userInfo = "John Denver"; XXX string firstName; string lastName; inSS >> firstName; inSS >> lastName; cout << "Hello" << firstName << " " << lastName << endl; return 0; }

const vector <int> scores

Which XXX best defines the function's integer vector parameter scores, if scores will have just a few elements, and the function will not change scores? void Quiz1 (XXX) { ... }

return PI_VAL * CalcSquare(r);

Which XXX calculates the area using the CalcSquare() function? The formula to calculate the area of a circle is pi * r2. double CalcSquare(double r) { const double PI_VAL = 3.14159265; XXX; } int main() { cout << CalcArea(5.0); return 0; }

GetUserScore(userName, userScore);

Which XXX calls GetUserScore() to get the user's name and score and store those values in the userName and userScore variables? void GetUserScore(string& userName, int& userScore) { cout << "Enter your name:" << endl; cin >> userScore; } int main() { string userName; int userScore; XXX; cout << userName << "," << userScore << endl; return 0; }

PrintMessage()

Which XXX causes the program to output the message "Hello"? void PrintMessage() { cout << "Hello!"; } int main() { XXX; return 0; }

double ConeVolume (double r, double h); double ComputeSquare (double r);

Which XXX complete the program to calculate the volume of a cone? XXX int main () { double coneVol; coneVol = ConeVolume(2,4); cout << coneVol; return 0; } double ConeVolume (double r, double h) { return (0.33) * 3.14 * ComputeSquare(r) * h; } double ComputeSquare (double r) { return r * r; }

cout << name << "is" << age << "years old";

Which XXX generates "Adam is 30 years old." as the output? using namespace std; int main() { string name = "Adam"; int age = 30; XXX return 0; }

playerObj << players << "is" << age << "years old.";

Which XXX generates "Tim Smith is 19 years old." as the output? #include <iostream> #include <sstream> #include <string> using namespace std; int main () { ostringstream playerObj; string players; int age; players = "Tim Smith"; age = 19 XXX cout << playerObj.str(); return 0; }

input >> numberPlayers;

Which XXX is used to read and store the data into the variable numberPlayers from the file that is opened by the stream inputFile? #include <iostream> #include <fstream> using namespace std; int main () { ifstream inputFile; int numberPlayers; inputFile.open("playerNumber.txt"); if (!inputFile.is_open()) { cout << "Could not open file numFile.txt." << endl; return 1; } XXX cout << "Players Number." << numberPlayers; return 0; }

if(!inFS.fail())

Which XXX will search the input mname in the GuestList.txt file? #include <iostream> #include <fstream> using namespace std; int main () { ifstream inFS; string userName; string listName; int flag = 0; inFS.open("GuestList.txt"); if(!inFS.is_open()) { cout << "Could not open file numFile.txt." <<endl; return 1; } cout << "Enter guest name: "; cin >> userName; while (!inFS.eof()) { inFS >> listName; XXX { if(listName == userName) { flag = 1; } } } if(flag == 1) { cout << userName << "is in the guest list" << endl; } else { cout << userName << "not in the guest list" << endl; } inFS.close(); return 0; }

The variables prev and next should be passed by reference

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; }

int ProcessData (double num, int x = 2){...};

Which function is called? p = ProcessData(9.5);

double ComputeEnergyConsumed(double power, double hours) { return (power * hours) / 1000; }

Which function is most appropriate to improve the given code? int main() { double powerConsumptionApp1; double powerConsumptionApp2; double powerConsumptionApp3; double hoursOfUse1; double hoursOfUse2; double hoursOfUse3; double EnergyPerDay1; double EnergyPerDay2; double EnergyPerDay3; double totalEnergyConsumed; powerConsumptionApp1 = 600.85; hoursOfUse1 = 12.8; energyPerDay1 = (powerConsumptionApp1 * hoursOfUse1) / 1000; powerConsumptionApp2 = 1800.45; hoursOfUse2 = 0.45; energyPerDay2 = (powerConsumptionApp2 * hoursOfUse2) / 1000; powerConsumptionApp3 = 70; hoursOfUse3 = 1.5; energyPerDay3 = (powerConsumptionApp3 * hoursOfUse3) / 1000; totalEnergyConsumed = energyPerDay1 + energyPerDay2 + energyPerDay3; cout << "The total energy consumed per day is " << totalEnergyConsumed << endl; return 0; }

#include <sstream>

Which header file should be used while working with the output string stream?

Using various calls involving assert() is a good way to test the function

Which is true about testing a function with three integer parameters and one integer return value?

A function's local cariables are discarded upon a function's return, each new call creates a new local variable in memory

Which is true regarding how function's work?


Ensembles d'études connexes

Psych Exam 1 - Ch. 27 (Anger, Aggression, & Violence)

View Set

Engelska oregelbundna verb- från choose till do

View Set

2 - Project Scope Management - Test

View Set

Social Psych Final Review (previous test answers)

View Set