CSCE 2004 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour closed book final exam. Students are allowed one 8.5 by 11 page of notes. No calculators or cell phones are allowed. Please raise your hand if you have questions. Reminder: This class is governed by the University of Arkansas code of ethics. Any instances of cheating will result in referral to the All University Academic Integrity Board. -1-
Conditionals - 15 points [5 points] Which statement below best matches there are fewer than 10 numbers and the total is more than 100? a) ((Num <= 10) && (Total >= 100)) b) ((Num < 10) (Total > 100)) c) ((Num < 10) && (Total > 100)) d) ((Num <= 10) (Total >= 100)) [5 points] What will the following code output when executed? int Age = 18; string Name = "David"; if (Age >= 18) if (Name[0] == 'D') cout << "Hello. "; else cout << "Goodbye. "; cout << "Congratulations!"; a) Hello. Congratulations! b) Goodbye. Congratulations! c) Congratulations! d) Hello. Goodbye. [5 points] Which statement below best matches the person is 18 years old or older and they are more than 70 inches tall? a) (!(Age <= 18) &&!(Height < 70) ) b) ( (Age >= 18) && (Height > 70) ) c) (!(Age <= 18) && (Height >= 70)) d) ( (Age >= 18) && (Height < 70)) -2-
Iteration - 20 points [5 points] What will the following code output when executed? int Num = 0; int Sum = 1; while (Sum < 10) Sum = Sum + Num; Num = Num + 1; cout << "Sum = " << Sum << ", Num = " << Num << endl; a) Sum = 11, Num = 5 b) Sum = 10, Num = 5 c) Sum = 14, Num = 5 d) Sum = 11, Num = 4 [5 points] How many times will "Hello" be printed by the following code? int num = 0; while (num < 4) for (int cnt = 0; cnt < 6; cnt = cnt + 3) cout << "Hello\n"; num = num + 1; a) 2 times. b) 4 times. c) 6 times. d) 8 times. -3-
[5 points] What will the following code output when executed? int Outer, Inner; for (Outer = 0; Outer < 4; Outer++) Inner = Outer + 1; while (Inner <= 5) cout << Outer; Inner++; cout << " "; cout << endl; a) 0000 111 22 3 b) 1234 234 34 4 c) 00000 1111 222 33 d) 12345 2345 345 45 [5 points] What will the following code output when executed? int n = 0; while ((n <= 5) (n > 0)) n++; cout << n << ' '; cout << endl; a) 0 1 2 3... and so on forever b) 1 2 3 4 and so on forever c) 0 1 2 3 4 d) 1 2 3 4 5 e) 2 3 4 5 6-4-
Arrays 10 points [5 points] Assume that the one-dimensional array data contains 10 integers with values between [-100..100]. Which of the following pieces of code will find and print the maximum value in this array? a) b) c) d) int val = data[0]; for (int i = 0; i < 10; i++) if (data[i] < val) val = data[i]; cout << val << endl; int val = -100; for (int i = 1; i <= 10; i++) if (data[i] < val) val = data[i]; cout << val << endl; int val = 100; for (int i = 0; i < 10; i++) if (val < data[i]) val = data[i]; cout << val << endl; int val = data[0]; for (int i = 1; i < 10; i++) if (val < data[i]) val = data[i]; cout << val << endl; [5 points] Which one of the following C++ declarations will create a twodimensional array of integers called number with 4 rows and 6 columns? a) int[4][6] number; b) int[6][4] number; c) int number[4][6]; d) int number[6][4]; e) None of the above -5-
Functions 20 points [10 points] Assume we are given the following variables and function prototypes. Which of the function calls below are syntactically correct and use the return values from functions properly? // Variable declarations int A, B; float X, Y, Z; char C, D; // Function prototypes float ReadNum(); int GetMax(int Value1, int Value2); void Difference(float Num1, float Num2, float &Diff); GetMax(A, B, C); A = GetMax(2+2, 4); cout << GetMax(5, 10); B = GetMax(GetMax(1,2), 3); Difference(X, Y, 6.0); Z = Difference(X, Y); Difference(X, Y, Z); Y = ReadNum(); cin >> ReadNum(); cout << ReadNum(); -6-
Consider the following C++ code: int work(int num) num = num * 2; return num; int play(int & val) val = val / 3; if (val > 10) return val; else return work(val); int main() int A, B; A = 12; B = work(a); cout << A << " " << B << endl; B = 21; A = play(b); cout << A << " " << B << endl; return 0; // ONE // TWO [5 points] What values will be printed at position ONE in the code? a) 21 42 b) 42 21 c) 12 24 d) 24 12 e) None of the above [5 points] What values will be printed at position TWO in the code? a) 4 8 b) 14 7 c) 8 4 d) 7 14 e) None of the above -7-
Files 10 points Assume that the following information is stored in a text file called "data.txt". 1 Jane 3.5 2 Joseph 0.6 3 Jennifer 4.0 4 John 2.2 5 Jessie 2.3 Consider the following C++ code: int main() int num; string name; float score; ifstream din; din.open( data.txt ); // CHECK OPEN // READ DATA din.close(); return 0; [5 points] What code should be used in place of CHECK OPEN above to check if the input file has been opened correctly, and end the program if an error is detected? a) if (din.fail() == false) return -1; b) if (!din.eof()) cout << Error detected end program\n ; c) if (din.fail()) return -1; d) if (din.open() == false) din.close(); e) None of the above -8-
[5 points] What code should be used in place of READ DATA above to read and print all of the information in the data file to the screen? a) b) c) d) din >> name >> num >> score; while (!din.eof()) cout << num << << name << << score << endl; din >> name >> num >> score; while (din >> num >> name >> score) cout << name << << num << << score << endl; while (!din.eof()) cout << din.getline(); while (din >> num, name, score) cout << num << << name << << score << endl; e) None of the above -9-
Classes 25 points [10 points] Circle true or false to answer the following questions. The constructor method in a class is automatically called when we declare an object. One advantage of private variables is that they take less memory space than public variables. The copy constructor is automatically called when we pass an object as a value parameter. It is possible to declare public variables in a class, but this violates the information hiding principle of classes. When we have a method in a class with more than three parameters, this is called parameter overloading. The destructor method is automatically called inside the constructor method. All methods in a class must have at least one parameter, or return a data value of some type. The private variables in a class can be accessed and modified by any method in the class. Private methods can be called in the main program just like public methods; there is no difference between them. Classes in C++ are intended to make it easier for groups of programmers to work together and call each others code. -10-
Consider the following C++ class definition: class Sphere public: Sphere(); Sphere(const float R); void SetRadius(const float R); float GetRadius(); float Modify(); void Change(float &S); void Print(); private: float radius; bool IsNotZero() const; ; [10 points] Assume the following C++ variables are declared in the main function and the method calls below are made after the declarations. Which method calls are syntactically correct and use the return values properly? Sphere UnitSphere(1); Sphere Ball; Sphere Several[5]; float Radius = 0; float Number = 0.5; if (Ball.IsNotZero()) Ball.Print(); Change(UnitSphere, 2.5); Ball.GetRadius(Radius); cout << Ball.Modify(); UnitSphere.Modify(Number); Radius = Several[3].GetRadius(); Several[2].SetRadius(3); UnitSphere.Change(Number); Ball = Several[2]; Ball.radius = UnitSphere.radius; -11-
[5 points] Consider the following C++ program with one class called Math. We have provided inline implementations of the class methods to reduce the size of the program. What will this program output when it is executed? class Math public: Math() num = 5; Math(int val) num = val; void Work() num = num * num; int Play(int val) num = num + val; return num; void Print(string name) cout << name << " = " << num << ", "; private: int num; ; int main() Math x; Math y(6); x. Work(); y. Work(); x.print("x"); y.print("y"); int a = x.play(2); int b = y.play(4); x.print("x"); y.print("y"); return 0; a) x = 5, y = 6, x = 4, y = 2, b) x = 25, y = 36, x = 21, y = 34, c) x = 5, y = 6, x = 25, y = 36, d) x = 25, y = 36, x = 27, y = 40, e) None of the above -12-