Uploaded by Miguel Guerra

CS1 Final Study Guide

advertisement
CS1 Final Study Guide 1. Go over all examples from class and homework.
2. Know how to do the tracing problems with classes, aka BaskinRobbins type
problem.
3. What are some of the benefits to Object Oriented programming? What do the
concepts of object reusability and data hiding mean?
4. How do you access a data member of a structure/class?
5. How do you pass an array to a function?
6. Review functions from projects 3 and 4. Also review Pair Programming
assignments.
7. Understand this question?
if(score >= 90)
grade = 'A';
if(score >= 80)
grade = 'B';
if(score >= 70)
grade = 'C';
if(score >= 60)
grade = 'D';
else grade = ‘F’;
8. What is printed in the piece of code below? int z=3, sum=0;
while(z<7)
{
z += 2;
sum=sum+z;
}
cout << sum;
9. Write code, using a loop, that produces the following output:
12 14 16 18 20 10. What is the output of the following program?
#include <iostream>
using namespace std;
int main () {
int v1 = 10, v2 = 5;
int *p1 = &v1, *p2 = &v2;
*p2 += *p1;
p2 = p1;
*p2 = *p1 + *p2;
cout << "*p1 = " << *p1 << " *p2 = " << *p2
<< " v1 = " << v1 << " v2 = " << v2 << endl;
}
11. Consider the following expression: (A > B) && (C <= B)
Assume that A, B, and C are integer variables. Which of the expressions given below is (are) equivalent to the one given above? I. !(A < B) && !(C >= B)
II. (A > B) && (B > C)
III. !((A <= B) || (B < C))
12. Be able to use functions we've seen in class like islower(), isalpha(),
toupper() etc.
13. Write code in the space below so that the output is: I like ham! string s = "I do not like green eggs and ham!";
// Write your code here. You must use substr() to change
// the value of s. You may not use any quotation marks.
cout << s;
// should print: I like ham!
Free Response: 1. Write a function, reverseString(string &str), that reverses the characters of
the string str. string example = "Reverse";
reverseString(example),
cout << example; // prints out "esreveR"
2. Write the function repeatFront such that given a string and an int n, it returns a string made of the first n characters of the string, followed by the first n-­‐‑1 characters of the string, and so on. You may assume that n is between 0 and the size of the string, inclusive (i.e. n >= 0 and n <= str.size()). Hint: Solve this problem by using substr and C++ strings not C-­‐‑strings. Examples: repeatFront("Chocolate", 4) → "ChocChoChC"
repeatFront("Chocolate", 3) → "ChoChC"
repeatFront("Ice Cream", 2) → "IcI" repeatFront("Ice Cream", 0) → ""
3. We are having a party with amounts of tea and candy. Return the int outcome of the party encoded as 0=bad, 1=good, or 2=great. A party is good (1) if both tea and candy are at least 5. However, if either tea or candy is at least double the amount of the other one, the party is great (2). However, in all cases, if either tea or candy is less than 5, the party is always bad (0). teaParty(6, 8) → 1
teaParty(3, 8) → 0
teaParty(20, 6) → 2
Download