Introduction to Programming, PIC10A E. Ryu Fall 2017 Midterm Exam Friday, November 3, 2017 50 minutes, 11 questions, 100 points, 8 pages While we don t expect you will need more space than provided, you may continue on the back of the pages. This exam is closed book and closed notes. Student name: Teaching staff signature: Do not turn to the next page until the start of the exam.
1. (2 points) Consider the following program, which is poorly indented on purpose. # include < iostream > using namespace std ; int main () { for ( int i = 1; i <= 5; i ++) { for ( int j = 1; j <= i; j ++) cout << ""; cout << "\n"; What is the output of this program? A. The output is... (Repeated 25 times.) B. The output is... (Repeated 15 times.) C. The output is D. The output is 2. (2 points) Let a, b, and c be variables of type bool. Then the expression (!(a &&!c) b ) == (!a b c) A. always evaluates to false. B. always evaluates to true. C. can evalute to true or false depending on what values a, b, and c take. Page 2
3. (2 points) The type char is a fundamental type. A. True B. False 4. (2 points) Consider the following code. int a [10];... // Other code here... // Other code here for ( int b=a; b <(a +10); b +=2) cout << b << endl ; Which of the following choices most accurately describe the functionality of the for loop? A. Prints a[0],a[2],a[4],a[6],a[8]. B. Prints a[0],a[2],a[4],a[6],a[8],a[10]. C. Prints a[1],a[3],a[5],a[7],a[9]. 5. (2 points) How many statements are in string s = " UCLA "; cout << s << endl ;;; A. 0 B. 1 C. 2 D. 3 E. 4 6. (2 points) C++ variables are stored in A. primary storage. B. secondary storage. C. ternary storage. D. nowhere. Variables are not stored in storage, unless explicitly saved. 7. (2 points) Consider the following code. # include < iostream > # include < string > using namespace std ; int main () { string s; cin >> s; bool b = false ; for ( int i =0; i<s. length (); i ++) { if ( A <=s[i] && s[i]<= Z ) b = b true ; cout << b << endl ; If the input is uclastudent what is the output? A. 0 B. 1 Page 3
8. (2 points) Consider the following code. float d = new float [ 20]; Which of the following choices is the correct way to deallocate the dynamic array? A. delete d; B. delete float d; C. delete[] d; D. delete[] float d; E. delete[20] float d; F. None of the above. The array is automatically deallocated. 9. (4 points) Convert BFF2 (base 16) into base 2. Solution: 1011 1111 1111 0010 Page 4
10. (40 points) Unix time counts the number of the seconds elapsed since 00:00:00 am, January 1, 1970. (We ll not worry about timezones.) Complete the program that converts a given Unix time into a human readable time. We assume every year is exactly 365 days and every day is exactly 24 hours. (So no leap days and no leap seconds.) Your output should be given in a 24-hour clock (no need to differentiate am and pm). # include < iostream > using namespace std ; int main () { const int DAYS_IN_MONTH [12] = {31,28,31,30,31,30,31,31,30,31,30,31; int year, month, day, hour, min, sec ; // Input in unix time long long int unixtime ; cin >> unixtime ; // -------------------------------------------------------------------- FILL IN HERE // -------------------------------------------------------------------- cout << " Time is " << hour << ":" << min << ":" << sec << ", "; cout << month << "-" << day << "-" << year << ". \n"; For your information, January has 31 days, Feburary has 28 days, March has 31 days, April has 30 days, May has 31 days, June has 30 days, July has 31 days, August has 31 days, September has 30 days, October has 31 days, November has 30 days, and Decemeber has 31 days. In the blank space below, write (with your pen or pencil) the missing code. Page 5
Solution: const int SEC_ IN_ MIN = 60; const int SEC_ IN_ HOUR = SEC_ IN_ MIN 60; const int SEC_ IN_ DAY = 24 SEC_ IN_ HOUR ; const int SEC_ IN_ YEAR = 365 SEC_ IN_ DAY ; year = unixtime / SEC_ IN_ YEAR + 1970; unixtime %= SEC_ IN_ YEAR ; month = 0; while ( unixtime >= ( SEC_IN_DAY DAYS_IN_MONTH [ month ])){ unixtime -= SEC_IN_DAY DAYS_IN_MONTH [ month ]; month ++; day = unixtime / SEC_ IN_ DAY ; unixtime %= SEC_ IN_ DAY ; hour = unixtime / SEC_ IN_ HOUR ; unixtime %= SEC_ IN_ HOUR ; min = unixtime / SEC_ IN_ MIN ; unixtime %= SEC_ IN_ MIN ; sec = unixtime ; Page 6
11. (40 points) Finish writing a program that takes in two strings and checks if the two strings are equal when capitalization is ignored. The input strings can contain (English) letters, numbers, and special characters. Assume the input string does not contain a space or any whitespace characters. # include < iostream > # include < string > using namespace std ; int main (){ string s1, s2; cout << " Enter string 1: " << endl ; cin >> s1; cout << " Enter string 2: " << endl ; cin >> s2; int length1 = s1. length (); int length2 = s2. length (); // store lengths of s1 and s2 bool same ; // -------------------------------------------------------------------- FILL IN HERE // -------------------------------------------------------------------- if ( same ) cout << " The strings are the same." << endl ; else cout << " The strings are not the same." << endl ; In the blank space below, write (with your pen or pencil) the missing code. Page 7
Solution: for ( int i =0; i< length1 ; i ++) if( A <=s1[i] && s1[i]<= Z ) s1[i] = char (s1[i]- A + a ); for ( int i =0; i< length2 ; i ++) if( A <=s1[i] && s1[i]<= Z ) s2[i] = char (s2[i]- A + a ); same = (s1 == s2 ); Page 8