{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Final_07_spring

Final_07_spring - COMP 102 Final Exam Page 1 of 14 THE...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
May 29, 2007 COMP 102 -- Final Exam Page 1 of 14 THE HONGKONG UNIVERSITY OF SCIENCE AND TECHNOLOGY Department of Computer Science COMP 102: Computer and Programming Fundamentals I Spring 2007 Final Examination Date: May, 29, 2007 This exam contains 9 questions in 14 pages. Please count the pages. You have 3 hours to complete this exam. Problem Your point s Max point s Problem Your point s Max point s 1 5 6 16 2 13 7 20 3 6 8 10 4 8 9 10 5 12 Subtotal 44 Subtotal 56 Your total points: 100 Please identify yourself: Lecture/Lab section Name Student ID I have neither given nor received any unauthorized aid during this examination. The answers submitted are my own work. I understand that sanctions will be imposed, if I am found to have violated the University’s regulations governing academic integrity. Signature:
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
May 29, 2007 COMP 102 -- Final Exam Page 2 of 14 1 [5 marks] Consider the following enum definition and C++ statements : enum weekdays {sun,mon,tue,wed,thur,fri,sat}; weekdays day; For each of the following statement, mark it with “True” if it is correct or “False” if it is incorrect. (Please notice that compile warning is treated as correct.) a) day = fri; Answer: ___________ b) day = 3; Answer: ___________ c) day = (weekdays)1; Answer: ___________ d) int i = day; Answer: ___________ e) bool b = day; Answer: ___________
Image of page 2
May 29, 2007 COMP 102 -- Final Exam Page 3 of 14 2. [13 marks total] a) [6 marks] Consider the following function of binary search, int binary_search(char data[],// input: array int size, // input: array size char value // input: value to find ) // output: index of value if found { // -1 if value is not found int lower, middle, upper; // indexes to the array lower = 0; upper = size - 1; while (true) { for (int i = lower; i <= upper; i++) cout << data[i]<< ' '; cout << endl; middle = (lower + upper) / 2; if (data[middle] == value) return middle; else if (lower >= upper) return -1; else if (value < data[middle]) upper = middle - 1; else lower = middle + 1; } } Assuming char list[20]="12345678"; What are the outputs of the following function calls? (Please use to indicate a white space ) cout << binary_search(list, 8, '7') << endl; Answer: __________________________________________________________________ __________________________________________________________________ __________________________________________________________________ __________________________________________________________________ cout << binary_search(list, 8, '0') << endl; Answer: __________________________________________________________________ __________________________________________________________________ __________________________________________________________________ __________________________________________________________________ __________________________________________________________________
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
May 29, 2007 COMP 102 -- Final Exam Page 4 of 14 b) [7 marks] What is the output of the following program? (Please use to indicate a white space ) #include<iostream> using namespace std; int puzzle(int n){ cout << n << ' '; if (n==1) return 1; if (n%2 == 0) return 1 + puzzle(n/2 + 1); if (n%3 == 0) return 2 + puzzle(n/3); else return 10 + puzzle(n%10); } int main(){ cout << puzzle(18) << endl; return 0; } Answer: ____________________________________________________________
Image of page 4
May 29, 2007 COMP 102 -- Final Exam Page 5 of 14 3 [6 marks total] a) [2 marks] What is the output of the following program?
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern