ACS 168 Fall 2001 Test 2 Please circle your lecture section: Sect. 1 Califf MWF 9:00 Sect. 4 White MWF 10:00 Sect. 7 Rendon TR 11:00 Sect. 10 Fulton MWF 1:00 Sect. 13 Goodwin MWF 2:00 Sect. 16 Rendon TR 5:30 Name _________________________________________ Section _________ Part 1: __________ (50) Part 2: __________ (50) Score: __________ 1 Part 1 - Multiple Choice – 50 points ( 2 points each). For each of the questions in this section mark the one best answer on your answer sheet. 1) Which of the following could be the header for a function called Test which takes as input two integers both passed by reference? a) b) c) d) e) void Test(int & num1, int & num2) int Test (int& num1, int & num2); void Test(int num1, int num2) int Test(int num1, int num2); None of the above 2) A programmer wants to continue a loop provided the user has typed y or Y. Which of these is correct? a) while ( b) while ( c) while ( d) while ( e) none of ans ans ans ans the == ‘y’ && ans == ‘Y’) == y && ans == Y)) == ‘y’ || ans == ‘Y’) == y || ans == Y above 3) Which of the following would be necessary in order to use files in a program? a) #include <ofstream> b) #include <fstream> c) #include <ifstream> d) all of the above e) none of the above The following code prints sets of ordered pairs (i, j). int i,j; for(i=0; i<5; i++) { for(j=5; j>0; j--) { cout << "(" << i << ", " << j << ") "; } cout << endl; } 4) What is the last ordered pair to be printed in the code above? a) (5, 0) b) (4, 1) c) (5, 5) d) (4, 4) e) none of the above 2 5) Students with what characteristics would be given a basketball_scholarship of $2000? IF student_attendance = part_time THEN IF student_gender = female THEN IF student_average > 20 THEN basketball_scholarship = 2200 ELSE basketball_scholarship = 2000 ENDIF ELSE basketball_scholarship = 1900 ENDIF ELSE basketball_scholarship = 2500 ENDIF a) b) c) d) e) full time student male part time student average is less than or equal to 20 points and is a female part time student average is greater than 20 points and a part time student none of the above 6) How many times will the following loop execute? for (int i = 5; i > -2; i--) cout << i; a) b) c) d) e) 8 times 7 times 6 times 5 times None of the above 7) Consider the for loop from the question above. What is the value of i when the loop ends? a) b) c) d) e) 0 -1 -2 -3 None of the above 3 Questions 8 and 9 Use the following C++ code segment: int grade; cin >> grade; switch (grade) { case 1: cout << ("great "); case 2: cout << ("job "); case 3: cout << ("done "); break; case 4: cout << ("you need to default: cout << ("change jobs } "); "); 8) What is printed by the above switch statements if the value entered for grade is 4? a) b) c) d) great job done you need to change jobs you need to change jobs e) none of the above 9) What is printed by the given C++ statements if the value that is entered for grade is 0? a) b) c) d) e) great job done you need to get a job you need to change jobs none of the above 10) Which one of the following could be the header for a class constructor definition where the class is called Auto, the arguments passed to the constructor are: type (a character), make (a character), and year (an integer). a) Auto::Auto(char type, char make, int year) b) Auto::~Auto(char type, char make, int year) e) Auto::Constructor(char type, char make, int year) f) void Auto::Constructor(char type, char make, int year) g) None of the above 4 11) What does the following program segment do? Assume all variables are of type int and that value > 1. temp = 0; for (k = 1; k <= value; k = k + 1) { if (k % 2 == 0) temp = temp + 1; } /* end for */ a) b) c) d) e) It counts the number of the integers from 1 through value. It counts the number of the even integers from 1 through value. It counts the number of the integers from 1 through value - 1. It counts the number of the odd integers from 1 through value. None of the above 12) Which type of stream parameter should replace the ###### below so the following two function calls will work? Prod_fun(fin); // fin has been declared as an ifstream and //successfully opened in main Prod_fun(cin); void Prod_fun(###### & input_source) { int num1, num2; input_source >> num1 >> num2; cout << num1 << “ times “ << num2 << “ is equal to “; cout << num1 * num2 << endl; } a. istream b. iostream c. ifstream d. all of the above will work e. none of the above 13) A function that appears in a class definition is called a) b) c) d) e) A class function A method A member function A data member b and c 5 Questions 14-16 Consider the following structure definition: struct Person { int height; // in inches int weight; // in pounds char gender; int IQ; }; 14) Which of the following properly declares a structure variable Maurice and initializes height, weight, and gender? a) Person Maurice = {72, 180, ‘M’}; b) Person Maurice; Maurice = {72, 180, ‘M’}; c) Person Maurice = {72, 180, ‘M’, 160, 30}; d) structure Maurice = {70, 180, ‘M’}; e) None of the above 15) Which of the following properly assigns IQ the value 135 for the structure variable Maurice declared above? a) Maurice.IQ = 135; b) Person.IQ = 135; c) Maurice::IQ = 135; d) Maurice = IQ(135); e) None of the above 16) Prior to the following code, the structure variable Maurice has the following values: height 72, weight 180, gender ‘M’, IQ 135. Assuming the following code is embedded in a complete and correct program and then run. What is the output? Person Byron; Byron = Maurice; Byron.IQ = Byron.IQ + 20; cout << Maurice.IQ << “ ” << Byron.IQ << endl; a) b) c) d) e) 135 20 135 135 135 155 155 155 None of the above 6 17) Given the following call and definition for function MickeyMouse, what is the output from the following code, assuming it is embedded in a complete and correct program and then run. Call: MickeyMouse(10, 7.2); Function Definition: void MickeyMouse(int Minnie, double Donald =3.9, char Huey =‘Z’) { cout << Minnie << “ ” << Donald << “ ” << Huey << endl; } a) b) c) d) e) 10 7.2 10 7.2 Z 10 3.9 Z We don’t know the output, because the value of Huey is unknown. None of the above 18) You are writing a class named Book. You are overloading the constructor. The default constructor prototype would be written a) Book(); b) void Book(); c) myLibrary(); d) Book(“Grapes of Wrath”, “Steinbeck”); e) None of the above 19) Which of the following calls the above default constructor for the class Book? a) Book MobyDick(); b) Book MobyDick; c) Book.MyLibrary MobyDick; d) MyLibrary MobyDick(); e) None of the above 20) Which of the following is not true of a class that implements an Abstract Data Type? a) All member variables are private. b) Helper functions are private member functions c) The basic functions that programmers need to use the class must be public member functions. d) Any programmer that uses the class must understand exactly how the class is implemented. e) All of the above are true of classes that implement Abstract Data Types. 7 Questions 21 and 22 Consider the following C++ program: #include <iostream> void Calculate(int &num1, int num2); int main() { using namespace std; int num1 = 7; int num2 = 6; Calculate(num1, cout << “num1 = Calculate(num2, cout << “num1 = return 0; num2); ” << num1 << “; num2 = ” << num2 << endl; num1); ” << num1 << “; num2 = ” << num2 << endl; } void Calculate(int &num1, int num2) { num2 = num1 + num2; num1 = 2 * num2; num2 = 13; } 21) What is the first line of output from the program? a) b) c) d) e) num1 = 12; num2 = 6 num1 = 26; num2 = 6 num1 = 7; num2 = 13 num1 = 26; num2 = 13 None of the above 22) What is the second line of output from the program? a) b) c) d) e) num1 = 64; num2 = 6 num1 = 78; num2 = 13 num1 = 13; num2 = 78 num1 = 26; num2 = 64 None of the above 8 23) Consider the following type definition. What output is produced if this code is embedded in a correct program? struct ShirtType { char style; int chest_size; char length; // s)hort, r)egular, l)ong double price; }; ShirtType Shirt1 = {‘S’, 46, ‘r’}; cout << “Shirt1: “ << Shirt1.style << “ “; cout << Shirt1.chest_size << “ “ << Shirt1.length; cout << “ “ << “$” << Shirt1.price << endl; a) Shirt1: S 46 r $0.0 b) Shirt1: ‘S’ 46 ‘r’ $0.0 c) Shirt1: S 46 r $ d) Shirt1: ‘S’ 46 ‘r’ $ e) None of the above 24) What condition should be used in the following if statement to check that in_file opened? if(??????????) { cout<<”The input file inv.txt did not open.\n”; exit(1); } a) in_file == fail b) inv.txt.fail() c) in_file.fail() d) in_file.open() e) None of the above 25) Given the following declarations and call to the overloaded function average, (and assuming this code is embedded in a proper program), which function would be executed? int num1, num2; double value1, value2; average(num1, value2); a) void b) void c) void d) void e) None average(); average(int num1, int & num2) average(double & value1, double value2) average(int num1, double & value2) of the above 9 10 Name: ________________________________________ Section: ____ Score:________ Part 2 - 50 points 1. Rewrite the following code so that it uses a while loop instead of a for loop. Make sure that the code produces the same result. Note: you must rewrite the entire code segment. You may assume the code is embedded in a correct program. (5 points) int num; int product = 1; int limit; cout << “Please enter an integer less than 100: ”; cin >> limit; for (num = limit; num > 1; num--) { product = product * num; } cout << “Limit: ” << limit << endl; cout << “Product: ” << product << endl; 11 2. (5 points) A program contains a class named ATMBankAccount. The relevant parts of the definition appear below: class ATMBankAccount { public: void Withdraw(); // Handles a withdrawal from the account. // Prompts the user and reads in the amount to // withdraw. void Deposit(); // Handles a deposit to the account. // Prompts the user and reads in the amount to // deposit. void BalanceInquiry(); // Reports the account’s current balance to the user } An object named theAccount of type ATMBankAccount has been declared and given appropriate values. You are to write a switch statement to handle each task by calling the appropriate member function of theAccount or print an error message. Assume that the variable transaction has already been given a value. When transaction is W or w, perform a withdrawal; When transaction is D or d, perform a deposit; When transaction is B or b, perform a balance inquiry; Otherwise, print an appropriate error message. 12 3. Write a C++ function, MaxMin, to prompt for and read a series of nonnegative integers from the keyboard and return the maximum value and the minimum value found in the series. The end of the series of numbers should be indicated by the user entering any negative value. Give the function prototype, a statement that correctly calls the function, and the complete function definition. (10 points). Prototype: Calling statement: Complete function definition: 13 4. Write a C++ program to open the file “a:numbers.txt”, which contains nothing but integers, and write the sum of the integers in the file to the screen. Be sure that the program will end gracefully if the file cannot be opened. (10 points). 14 5. Consider the description of a class given on the final page of the exam. Then write the class definition in C++ and the code for the indicated member functions. You’ll then be asked to write code to use the class. Remember that the class should follow the rules for creating ADTs (20 points total) Write the C++ class definition for Student. You need not include comments. (6 points): 15 Write the function definition for the third constructor (2 points): Write the function definition for the function to compute the program average (2 points): Write the function definition for the function to print a student’s information (3 points): 16 Write the function definition for the helper function to compute the letter grade (3 points): Write code to declare two objects of type Student, using the default constructor for one and the third constructor for the second. Then write code to print the information for each of the objects. Assume that your code would be embedded in a complete and correct program (4 points) 17 18 Use the following class description for written question 5. You’re going to write a class Student that should keep track of the following pieces of information (all averages should be doubles): an integer student number a classwork average a program average an exam average a letter grade The class needs to perform at least the following functions: It should have a default constructor that initializes the student number and all averages to 0 and the letter grade to ‘F’. It should have a constructor that accepts a student number and initializes the averages to 0 and the letter grade to ‘F’. It should have a constructor that takes a student number, a classwork average, a program average, and an exam average, and initializes all variables. It should have three functions to compute new averages. Each would take a student’s point total and maximum points possible and update the appropriate average (classwork, program, or exam). Each function should also make sure that the letter grade is updated to be accurate. It should have a function to print a report on the student that outputs all of the data to an output stream (a file or cout) with appropriate labels. The averages should be nicely formatted with 1 digit after the decimal point. It should have a helper function that computes the letter grade. The final grade is 40% times the exam average plus 40% times the program average, plus 20% times the classwork average. Letter grades are based on the standard scale of 90 or better is an A, 80-89 is a B, etc. 19 Multiple Choice Solutions One set of POSSIBLE Written Solutions 1. num = limit; while(num>1) { product = product * num; num--; } 1 A 2 C 3 B 4 B 5 C 6 B 7 C 8 E 9 E 10 A 11 B 12 A 13 E 14 A 15 A 16 C 17 B 18 A 19 B 20 D 21 B 22 D 23 A 24 C 25 D 2. switch (toupper(transaction)) { case 'W': theAccount.Withdraw(); break; case 'D': theAccount.Deposit(); break; case 'B': theAccount.BalanceInquiry() ; break; default: cout<<"Error"; } 3. Prototype: void MaxMin(int&, int&); Calling statement: MaxMin(max, min); Complete function definition: void MaxMin(int& max, int& min) { int num; cout<<"Enter any non-negative integer "; cin>>num; max = num; min = num; while(num>0) { if(num > max) max = num; else if(num < min) min = num; cout<<"Enter next integer or any ” <<”negative number to stop "; cin>>num; } } 20 4. #include<iostream> #include<fstream> using namespace std; int main() { int num, sum =0; ifstream infile; infile.open("a:numbers.txt"); if(infile.fail()) { cout<<"numbers.txt did not open"; exit(1); } while(infile>>num) sum += num; infile.close(); cout<<"The sum is "<<sum<<endl; return 0; } 5. //Write the C++ class definition for Student. class Student { private: int stuNum; double class_avg, prog_avg, exam_avg; char letter_grade; public: Student(); Student(int); Student(int, double, double, double); void update_class_avg(int, int); void update_prog_avg(int, int); void update_exam_avg(int, int); void print_report(ostream&); private: char comp_letter(); }; 21 //Write the function definition for the third constructor Student::Student(int num,double in_class, double prog, double exam) { stuNum = num; class_avg = in_class; prog_avg = prog; exam_avg = exam; letter_grade = 'F'; } //Write the function definition for the function to compute the program average void Student::update_prog_avg(int total, int max) { prog_avg = double(total)/max; letter_grade = comp_letter(); } //Write the function definition for the function to print a student's information void Student::print_report(ostream & outs) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(1); outs<<"Student ID: "<<stuNum<<endl <<"Class Average: "<<class_avg<<endl <<"Program Average: "<<prog_avg<<endl <<"Exam Average: "<<exam_avg<<endl <<"Letter Grade "<<letter_grade<<endl; } //Write the function definition for the helper function to compute the letter grade char Student::comp_letter() { double grade; grade = exam_avg * .40 + prog_avg * .40 + class_avg * .20; if(grade >=90) letter_grade = 'A'; else if(grade >=80) letter_grade = 'B'; else if(grade >= 70) letter_grade = 'C'; else if(grade >=60) letter_grade = 'D'; else letter_grade = 'F'; } 22 /* Write code to declare two objects of type Student, using the default constructor for one and the third constructor for the second. Then write code to print the information for each of the objects. Assume that your code would be embedded in a complete and correct program */ Student StuA, StuB (5, 80, 90, 100); StuA.print_report(cout); StuB.print_report(cout); 23