PIC10AfinalW09_key

PIC10AfinalW09_key - Final Exam, 3/15/09 PIC 10A, Winter...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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--; }
Background 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);
Background image of page 3

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

View Full DocumentRight Arrow Icon
3 3.) [12 points] Write the output of the following blocks of code. a.)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

PIC10AfinalW09_key - Final Exam, 3/15/09 PIC 10A, Winter...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online