PIC10AfinalW09_key

PIC10AfinalW09_key - Final Exam PIC 10A Winter 2009 KEY...

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

View Full Document Right Arrow Icon
Final Exam, 3/15/09 PIC 10A, Winter 2009 KEY Check your TA’s name: _____ Vlad _____ Darren You have 3 hours to complete this exam. You are not allowed to use any books, notes, calculators, or electronic devices. Write your answers carefully. Incomplete, unintelligible, or illegible answers will receive little or no credit. When you are asked to write a program, it is not necessary to comment your code but you are expected to indent appropriately to make your code easier to follow. There are a total of 100 points on this exam. PAGE SCORE POSSIBLE 1 8 2 8 3 12 4 6 5 8 6 8 7 10 8 18 9-10 22 TOTAL 100
Image of page 1

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

View Full Document Right Arrow Icon
1 1.) [2 points] In lecture we discussed that we can repeatedly read in numbers from the user using the loop while (cin >> x) where x is a number valued data type like int or double. Explain why this loop would not be appropriate for reading in a list of words when x is a string. Since every possible input counts as a string, this would create an infinite loop. 2.) [3 points] Write a for loop that prints the characters in a string s one at a time, separated by a space. For example, s="FRODO" prints: F R O D O string s; cin >> s; for (int i=0; i<s.length(); i++) cout << s[i] << " "; 3.) [3 points] Write a while loop that prints the numbers from an integer N down to zero, separated by commas. For example, N=4 prints: 4,3,2,1,0, int N; cin >> N; while ( N >= 0) { cout << N << ","; N--; }
Image of page 2
2 2.) [8 points] Correct the errors in the following blocks of code. You will get points off for changing code that was not an error. a.) string a; cin >> a; if (a[0] == "Q") cout << "Your word started with Q."; Should be single quotes 'Q' on the char. b.) string b = "Gandalf"; cout << b.substr(4,6); b.insert(0, "Hello "); cout << b; The substr parameter 6 runs off the end of the string. Can be at most substr(4,3) c.) string c = "Hello"; string* p = c; cout << *p; p is a pointer, so it needs an address not a char value string* p = &c; d.) int N; cout << "Enter array size: "; cin >> N; int d[N]; for (int i=0; i < N; i++) { cout << "Enter number: "; cin >> d[i]; } Array size must be const, cannot be input by user. Use a vector instead. vector<int> d(N);
Image of page 3

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

View Full Document Right Arrow Icon
3 3.) [12 points] Write the output of the following blocks of code.
Image of page 4
Image of page 5
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