CS 165

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Find the errors using namespace std; int main() { double number1, number2, sum; Cout << "Enter a number: "; Cin << number1; Cout << "Enter another number: "; Cin << number2;number1 + number2 = sum; Cout "The sum of the two numbers is " << sum return 0; }

#include <iostream> is missing. Each cin and cout statement starts with capital C. The << operator is mistakenly used with cin. The assignment statement should read:sum = number1 + number2; The last cout statement should have << after cout. The last cout statement is missing a semicolon. The body of the main function should be indented within the braces.

What will the program display? for (int row = 1; row <= 3; row++) { cout << "\n$"; for (int digit = 1; digit <= 4; digit++) cout << '9'; }

$9999 $9999 $9999

How many lines will each of the following while loops display? B) int count = 10; while (count < 5) { cout << "My favorite day is Sunday \n"; count = count + 1; }

0

What will be the output? #include <iostream> using namespace std; int main() { int freeze = 32, boil = 212; freeze = 0; boil = 100; cout << freeze << endl << boil << endl; return 0; }

0 100

What will the following program segment display? int a = 0, b = 2, x = 4, y = 0; cout << (a == b) << " " << (a != y) << " " << (b <= x) << " " << (y > a) << endl;

0 0 1 0

The following program asks the user to enter two numbers. What is the output of the program if the user enters 12 and 14? #include <iostream> using namespace std; void func1(int &, int &); void func2(int &, int &, int &); void func3(int, int, int); int main() { int x = 0, y = 0, z = 0; cout << x << " " << y << z << endl; func1(x, y); cout << x << " " << y << z << endl; func2(x, y, z); cout << x << " " << y << z << endl; func3(x, y, z); cout << x << " " << y << z << endl; return 0; } void func1(int &a, int &b) { cout << "Enter two numbers: "; cin >> a >> b; } void func2(int &a, int &b, int &c) { b++;c--;a = b + c; } void func3(int a, int b, int c) { a = b - c; }

0 00 Enter two numbers: 12 14 12 140 14 15-1 14 15-1

What is the output of the following program? #include <iostream> using namespace std; void func1(double, int); // Function prototype int main() { int x = 0; double y = 1.5; cout << x << " " << y << endl; func1(y, x); cout << x << " " << y << endl; return 0; } void func1(double a, int b) { cout << a << " " << b << endl; a = 0.0; b = 10; cout << a << " " << b << endl; }

0 1.5 1.5 0 0 10 0 1.5

What will the program display? for (int count = 0; count < 6; count++) cout << (count + count) << " ";

0 2 4 6 8 10

How many lines will each of the following while loops display? int count = 1; while (count < 5); { cout << "My favorite day is Sunday \n"; count = count + 1; }

0. Notice the semicolon after the while test expression. This causes an infinite loop that prints nothing.

What will each of the following program segments display? int x = 1; while (x < 10); x++; cout << x;

1 2 3 4 5 6 7 8 9

What will the following program segment display? int addOn = 0, subTotal = 0; while (addOn < 5) { addOn++; if (addOn == 3) continue; subTotal += addOn; cout << subTotal << " "; }

1 3 7 12

Find the errors - 4 #include <iostream> using namespace std; int main() { int number1, number2; double quotient; cout << "Enter two numbers and I will divide\n"; cout << "the first by the second for you.\n"; cin >> number1, number2; quotient = double<static_cast>(number1)/number2; cout << quotient }

1) The cin statement should read:cin >> number1 >> number2; 2) The assignment statement should read:quotient = static_cast<double>(number1) / number2; 3) The last cout statement is missing a semicolon. 4)There is no return 0;

What is the output of the following program? #include <iostream> #include <cstdlib> using namespace std; void showVals(double, double); int main() { double x = 1.2, y = 4.5; showVals(x, y); return 0; } void showVals(double p1, double p2) { cout << p1 << endl; exit(0); cout << p2 << endl; }

1.2

What is the output of the following program? #include <iostream> using namespace std; void showVar(); // Function prototype int main() { for (int count = 0; count < 10; count++) showVar() ;return 0; } // Definition of function showVarvoid showVar() { static int var = 10; cout << var << endl; var++; }

10 11 12 13 14 15 16 17 18 19

What is the output of the following program? #include <iostream> using namespace std; void myFunc(); // Function prototype int main() { int var = 100; cout << var << endl; myFunc(); cout << var << endl; return 0; } // Definition of function myFuncvoid myFunc() { int var = 50; cout << var << endl; }

100 50 100

What will it output? x = 2; y = 4; cout << x++ << " " << --y;

2 3

Predict the output for (int count = 1; count <= 10; count++) { cout << ++count << " "; // This is a bad thing to do!}

2 4 6 8 10

What will it output? x = 2; y = x++; cout << x << " " << y;

3 2

What will it output? x = 2; y = ++x; cout << x << " " << y;

3 3

What will it output? x = 2; y = 2 * x++; cout << x << " " << y;

3 4

What will the program display? int x for (x = 3; x <= 10; x += 3) cout << x << " "; cout << x << " ";

3 6 9 12

What is the output of the following program? #include <iostream> using namespace std; int manip(int); int manip(int, int); int manip(int, double); int main() { int x = 2, y= 4, z; double a = 3.1; z = manip(x) + manip(x, y) + manip(y, a); cout << z << endl; return 0; } int manip(int val) { return val + val * 2; } int manip(int val1, int val2) {return (val1 + val2) * 2; } int manip(int val1, double val2) { return val1 * static_cast<int>(val2); }

30

How many lines will each of the following while loops display? A) int count = 1; while (count < 5) { cout << "My favorite day is Sunday \n"; count = count + 1; }

4

What will the following program display on the screen?#include <iostream> using namespace std; class Package { private:int value; public:Package() { value = 7; cout << value << endl; } Package(int v) { value = v; cout << value << endl; } ~Package() { cout << "goodbye" << endl; }; int main() { Package obj1(4); Package obj2; return 0; }

47 goodbye goodbye

What is the output of the following program? #include <iostream> using namespace std; void test(int = 2, int = 4, int = 6); int main() { test(); test(6); test(3, 9); test(1, 5, 7); return 0; } void test (int first, int second, int third) { first += 3; second += 6; third += 9; cout << first << " " << second << " " << third << endl; }

5 10 15 9 10 15 6 15 15 4 11 16

What will the following program segment display? int funny = 1, serious; if (funny != 1) { funny = serious = 1;} else if (funny == 2) { funny = serious = 3;} else { funny = serious = 5;} cout << funny << " " << serious << endl;

5 5

What will the program display? int val = 5; do cout << val << " "; while (val >= 5);

5 5 5 5 5 5 5 5 ... (Infinite loop)

What will the following program display on the screen?#include <iostream> using namespace std; class Tank { private:int gallons; public:Tank() { gallons = 50; } Tank(int gal) { gallons = gal; } int getGallons() { return gallons; }; int main() { Tank storage1, storage2, storage3(20); cout << storage1.getGallons() << endl; cout << storage2.getGallons() << endl; cout << storage3.getGallons() << endl; return 0;}

50 50 20

Determine the output of this program #include <iostream> using namespace std; int main() { long x, y, z; x = y = z = 4; x += 2;y -= 1; z *= 3; cout << x << " " << y << " " << z << endl; return 0; }

6 3 12

Assume a program has the following variable definitions int a, b = 2; double c = 4.3; and the following statement: a = b * c; What value will be stored in a?

8

What will be the output? #include <iostream> using namespace std; int main() {int x = 0, y = 2; x = y * 4; cout << x << endl << y << endl; return 0;}

8 2

What will the program display? int count = 0, number = 0, limit = 4; do{ number += 2; count++;} while (count < limit); cout << number << " " << count << endl;

8 4

A constructor that requires no arguments is called A) a default constructor B) an inline constructor C) a null constructor D) none of the above

A

An object's private member variables can be accessed from outside the object by A) public member functions B) any function C) the dot operator D) the scope resolution operator

A

Constructor functions have the same name as the A) class B) class instance C) program D) none of the above

A

When an object is passed to a function, a copy of it is made if the object is A) passed by value B) passed by reference C) passed by constant reference D) any of the above

A

What is the difference between a static local variable and a global variable?

A static local variable's scope is limited to the function in which it is defined. A global variable's scope is the portion of the program from its definition to the end of the program.

Find the errors double testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; switch (testScore) { case (testScore < 60.0): cout << "Your grade is F.\n"; break; case (testScore < 70.0): cout << "Your grade is D.\n"; break; case (testScore < 80.0): cout << "Your grade is C.\n"; break; case (testScore < 90.0): cout << "Your grade is B.\n"; break; case (testScore <= 100.0): cout << "Your grade is A.\n"; break; default: cout << "That score isn't valid\n"; }

A switch case construct cannot be used to test relational expressions. An if/else if statement should be used instead.

Assume x = 4, y = 7, and z = 2. What value will be stored in integer variable result by each of the following statements?A) result = x + y; B) result = y * 2; C)result = y / z;

A) 11 B) 14 C) 3 (An integer divide takes place.)

Assume the following preprocessor directive appears in a program: #define SIZE 12 How will the preprocessor rewrite the following lines?A)price = SIZE * unitCost; B)cout << setw(SIZE) << 98.7; C)cout << SIZE;

A) price = 12 * unitCost; B) cout << setw(12) << 98.7; C) cout << 12;

A destructor function name always starts with A) a number B) the tilde character (~) C) a data type name D) the name of the class

B

If a function receives an object as an argument and needs to change the object's member data, the object should be A) passed by value B) passed by reference C) passed by constant reference D) none of the above

B

Which of the following shows the correct use of the scope resolution operator in a member function definition? A)InvItem::void setOnHand(int units) B)void InvItem::setOnHand(int units)

B

Which of the following are not valid cout statements? A) cout << "Hello" << endl; B) cout << "Hello" << \n; C) cout << Hello;

B (C is valid, but prints the contents of variable Hello, rather than the string"Hello".)

Which of the following are not valid assignment statements? A) total = 9; B) 72 = amount; C) yourAge = myAge;

B - should be amount = 7;

Assuming that soap is an instance of the Inventory class, which of the following is a valid call to the setOnHand member function?A)setOnHand(20); B)soap::setOnHand(20); C)soap.setOnHand(20); D)Inventory.setOnHand(20);

C

Assuming dataFile is an ofstream object associated with a disk file namedpayroll.dat, which of the following statements would write the value of thesalary variable to the file? A)cout << salary; B)ofstream << salary; C) dataFile << salary; D) payroll.dat << salary;

C.dataFile << salary;

Find the errors - three class Change { private: int pennies; int nickels; int dimes; int quarters; Change() { pennies = nickels = dimes = quarters = 0; } Change(int p = 100, int n = 50, d = 50, q = 25); }; void Change::Change(int p, int n, d, q) { pennies = p; nickels = n; dimes = d; quarters = q; }

Constructors must be public, not private. Both constructors are considered the default constructor. This is illegal since there can be only one default constructor. All the parameters in the Change function header should have a data type.

A private class member function can be called by A) any other function B) only public functions in the same class C) only private functions in the same class D) any function in the same class

D

Assume a Map class has a member variable named position that is an instance of the Location class. The Location class has a private member variable named latitude and a public member function called getLatitude. Which of the following lines of code would correctly get and return the value stored in latitude? A)return Location.latitude; B)return Location.getLatitude(); C)return position.latitude; D)return position.getLatitude();

D

Locate the error and determine the output interestRate = .05; if (interestRate > .07) cout << "This account earns a $10 bonus.\n"; balance += 10.0;

Error: The 2 statements that are supposed to be included in the body of the if statement are not surrounded by curly braces. Result: Only the cout statement is in the if body. The $10 addition to balance will always be done, even when interest Rate is not greater than .07. Output:None

Locate the error and determine the output interestRate = .05; if (interestRate = .07) cout << "This account is earning the maximum rate.\n";

Error: The if test condition uses an assignment operator (=) rather than an equality test (==). Result: interestRate will be assigned the value .07, and the cout statement will execute even though it shouldn't. Output:This account is earning the maximum rate.

Locate the error and determine the output hours = 12; if (hours > 40); cout << hours << " hours qualifies for over-time.\n";

Error: There is a semicolon after the if test condition. Result: The cout statement will execute even though hours is not greater than 40. Output:12 hours qualifies for over-time.

If a = 2, b = 4, and c = 6, indicate whether each of the following conditions is true or false: (6 <= c) && (a > 3)

False

Indicate whether each of the following statements about relational expressions is correct or incorrect. A)x <= y is the same as y > x.

False

Indicate whether each of the following statements about relational expressions is correct or incorrect. x != y is the same as y >= x.

False

True or false: A class may have a constructor with no parameter list, and an overloaded constructor whose parameters all take default arguments.

False

True or false: Just as a class can have multiple constructors, it can also have multiple destructors.

False

Will the following string literal fit in the space allocated for name? T/F char name[4] = "John";

False- Space is needed for a fifth character, to hold the null terminator.

True or false: When an object is passed to a function, but the function is not supposed to change it, it is best to pass it by value.

False. Passing it by value will ensure it is not changed, but it is best to pass it as a constant reference.

True or false: Objects can be passed to functions, but they cannot be returned by functions.

False. They can be both passed to functions and returned by functions.

Indicate which of the following is the function prototype, the function header, andthe function call: void showNum(double num) void showNum(double); showNum(45.67);

Header Prototype Function call

What will the program display? int count = 3; do cout << "Hello World\n"; count--;

Hello World

What will be the output? #include <iostream> using namespace std; int main() {cout << "I am the incredible"; cout << "computing\nmachine"; cout << "\nand I will\namaze\n" ;cout << "you.\n"; return 0;}

I am the incrediblecomputing machine and I will amaze you.

What will the output of the following program be if the user enters 10? #include <iostream> using namespace std; void func1() { cout << "Able was I\n"; } void func2() { cout << "I saw Elba\n"; } int main() { int input; cout << "Enter a number: "; cin >> input; if (input < 10) { func1(); func2(); } else { func2() ;func1(); } return 0; }

I saw Elba Able was I

Determine the output of this program #include <iostream> using namespace std; #define WHO "Columbus" #define DID "sailed" #define WHAT "the ocean blue." int main() { const int WHEN = 1492; cout << "In " << WHEN << " " << WHO << " " << DID << " " << WHAT << endl; return 0;}

In 1492 Columbus sailed the ocean blue.

What will it output? x = 0; if (++x) cout << "It is true!\n"; else cout << "It is false!\n";

It is true!

What will the output be? x = 99; if (x++ < 100) cout "It is true!\n"; else cout << "It is false!\n";

It is true!

Will the if/else statement shown on the right below function exactly the same as the two separate if statements shown on the left? if (x < y) if (x < y) cout << 1; cout << 1; if(x > y) else cout << 2; cout < <2;

No. When x equals y the two separate if statements don't display anything, but the if/else statement causes a 2 to display.

Predict the output int x = 1; while (x < 10); x++; cout << x;

Nothing will print. The erroneous semicolon after the while condition causes the while loop to end there. Because x will continue to remain 1, x<10 will remain true and the infinite loop can never be exited.

How many lines will each of the following while loops display? A) int count = 1; while (count < 5) cout << "My favorite day is Sunday \n"; count = count + 1;

Notice the missing braces. This means the line that increments count is not in the loop, so count always remains less than 5, causing an infinite loop. The cout statement executes over and over again until the user stops the program.

What will the following program segment display? int funny = 7, serious = 15; funny = serious * 2; switch (funny) { case 0 : cout << "That is funny.\n"; break; case 30: cout << "That is serious.\n"; break; case 32: cout << "That is seriously funny.\n"; break; default: cout << funny << endl; }

That is serious.

The following program contains syntax errors. Locate as many as you can. */ What's wrong with this program? /* #include iostream using namespace std; int main(); } int a, b, c \\ Three integers a = 3 b = 4 c = a + b Cout < "The value of c is %d" < C; return 0; {

The C-style comments symbols are backwards. iostream should be enclosed in angle brackets. There shouldn't be a semicolon after int main(). The opening and closing braces of function main are reversed. There should be a semicolon after int a, b, c. The comment \\ Three integers should read // Three integers. There should be a semicolon at the end of each of the following lines: a = 3 b = 4 c = a + b cout begins with a capital letter. The stream insertion operator (that appears twice in the cout statement) should read << instead of <. The cout statement uses the variable C instead of c.

Find the errors - two double average(int value1, int value2, int value3) { double average; average = value1 + value2 + value3 / 3; }

The assignment statement should read: average = (value1 + value2 + value3) / 3.0; The function is declared as a double but returns no value.

What is wrong with the following switch statement? switch (temp) { case temp < 0 : cout << "Temp is negative.\n"; break; case temp == 0: cout << "Temp is zero.\n"; break; case temp > 0 : cout << "Temp is positive.\n"; break;

The case statements must be followed by an integer constant, not a relational expression.

Find the errors - two of them void total(int value1, value2, value3) { return value1 + value2 + value3; }

The data type of value2 and value3 must be declared. The function is declared void but returns a value.

Find the error do { cout << "Enter a number: "; cin >> num1; cout << "Enter another number: "; cin >> num2; cout << "Their sum is " << (num1 + num2) << endl; cout << "Do you want to do this again?\n"; cout << "1 = yes, 0 = no\n"; cin >> choice; } while (choice = 1)

The expression tested by the do-while loop should be choice == 1 instead of choice = 1

Each of the following program segments has errors. Find as many as you can. A) cout << "Enter your 3 test scores and I will "; << "average them:"; int score1, score2, score3, cin >> score1 >> score2 >> score3; double average; average = (score1 + score2 + score3) / 3.0; if (average = 100); perfectScore = true;// Set the flag variable cout << "Your average is " << average << endl; bool perfectScore; if (perfectScore); { cout << "Congratulations!\n"; cout << "That's a perfect score.\n"; cout << "You deserve a pat on the back!\n";

The first cout statement is terminated by a semicolon too early. The definition of score1, score2, and score3 should end with a semicolon. The following statement: if (average = 100) should read: if (average == 100) perfectScore is used before it is declared. The following if statement should not be terminated with a semicolon :if (perfectScore); The conditionally executed block in the if statement shown above should end with a closing brace.

Find the error - one // Overloaded functions int getValue() { int inputValue; cout << "Enter an integer: "; cin >> inputValue; return inputValue; } double getValue() { double inputValue; cout << "Enter a floating-point number: "; cin >> inputValue; return inputValue; }

The functions must have different parameter lists.

The following code has an error. Can you correct it? ofstream outputFile; string filename = "numbers.txt"; outputFile.open(filename);

The open function needs an argument that is a C-string. Change the third line to outputFile.open(filename.c_str());

Find the errors - two void getValue(int value&) { cout << "Enter a value: "; cin >> value&; }

The parameter should be declared as:int &value The cin statement should read:cin >> value;

Find the errors #include <iostream> using namespace std; class DumbBell; { int weight; public:void setWeight(int); }; void setWeight(int w) { weight = w; } int main() { DumBell bar; DumbBell.setWeight(200); cout << "The weight is " << bar.weight << endl; return 0; }

The semicolon should not appear after the word DumbBell in the class declaration. Even though the weight member variable is private by default, it should be preceded with the private access specifier. Because the setWeight member function is defined outside the class declaration, its function header must appear as: void DumbBell::setWeight(int w) The line that reads: DumbBell.setWeight(200); should read: bar.setWeight(200); Because the weight member variable is private, it cannot be accessed outside the class, so the cout statement cannot legally output bar.weight. There needs to be a public getWeight() function that the main program can call.

Find the error int testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; if (testScore < 60) cout << "Your grade is F.\n"; else if (testScore < 70) cout << "Your grade is D.\n"; else if (testScore < 80) cout << "Your grade is C.\n"; else if (testScore < 90) cout << "Your grade is B.\n"; else cout << "That is not a valid score.\n"; else if (testScore <= 100) cout << "Your grade is A.\n";

The trailing else statement should come at the end of the if/else construct

Find the errors // This code should display the sum of the numbers 1 - 100. int count = 1, total; while (count <= 100) total += count; cout << "The sum of the numbers 1 - 100 is "; cout << total << endl;

The variable total is not initialized to 0. The while loop does not change the value of count, so it iterates an infinite number of times.

Find the errors #include <iostream>; using namespace std; main { double number, half; cout << "Enter a number and I will divide it\n" cout << "in half for you.\n" cin >> number1; half =/ 2; }

There shouldn't be a semicolon after the #include directive. The function header for main should read:int main() The variable number is defined, but it is called number1 in the cin statement. The combined assignment operator is improperly used. The statement should read: half /= 2; There is a logical error. The value divided by 2 should be number, not half. The results are never output. There is no return 0;

Find the errors #include <iostream>; using namespace std; int main(){char name, go; cout << "Enter your name: "; cin.width(20); cin.getline >> name; cout << "Hi " << name << endl; cout "Press the ENTER key to end this program."; cin >> go; return 0; }

There shouldn't be a semicolon after the #include directive. name should be declared as a string or a char array. If declared as string, a #include <string> directive is needed. The statement cin.getline >> name; should read cin >> name; The statement cin >> go; should read cin.get(go);

If a = 2, b = 4, and c = 6, indicate whether each of the following conditions is true or false: !(a > 2)

True

If a = 2, b = 4, and c = 6, indicate whether each of the following conditions is true or false: (1 != b) && (c != 3)

True

If a = 2, b = 4, and c = 6, indicate whether each of the following conditions is true or false: (a == 4) || (b > 2)

True

If a = 2, b = 4, and c = 6, indicate whether each of the following conditions is true or false: (a >= -1) || (a <= b)

True

Indicate whether each of the following statements about relational expressions iscorrect or incorrect. x >= y is the same as y <= x.

True

True or false: The following if/else statements cause the same output to display. A)if (x > y) cout << "x is greater than y.\n"; else cout << "x is not greater than y.\n"; B)if (x <= y) cout << "x is not greater than y.\n"; else cout << "x is greater than y\n";

True

True or false: Like any C++ function, a constructor may be overloaded, providing each constructor has a unique parameter list.

True

If a = 2, b = 4, and c = 6, is the following expression true or false? (b > a) || (b > c) && (c == 5)

True (&& is done before ||)

Determine the output of this program (Assume the user enters 38711. Use a calculator.) #include <iostream> using namespace std; int main() { double salary, monthly; cout << "What is your annual salary? "; cin >> salary; monthly = static_cast<int>(salary) / 12; cout << "Your monthly wages are " << monthly << endl; return 0; }

Your monthly wages are 3225

Rewrite the following variable definition so the variable is a named constant with the value 12. int rate;

const int RATE = 12

TRUE or FALSE: Both of the following if statements perform the same operation. if (calls == 20) rate *= 0.5; if (calls = 20) rate *= 0.5;

false

Is the following a function header or a function call?calcTotal();

function call

Is the following a function header or a function call? void showResults()

function header

"/* Purpose of these is.. */"

multi line comment

"//" purpose of this is

single line comment

Find the errors - two void area(int length = 30, int width) { return length * width; }

width should have a default argument value. The function is declared void but returns a value.

What will the program display? for (int value = -5; value < 5; value++) cout << value << " ";

−5 −4 −3 −2 −1 0 1 2 3 4


Kaugnay na mga set ng pag-aaral

prep u chap 22: management of pts w arrhythmias and conduction problems

View Set

AUD507 Auditing & Monitoring Networks, Perimeters & Systems

View Set

International Business Environment UIowa

View Set

animal husbandry and restraint review

View Set

VENERACION, AGRARIAN REFORM, PH CONS

View Set

Emotion, love, romance, affection, relationship

View Set