CS1 Midterm 1 Study Guide

1.  Go over all examples from class and homework.

2.  What value will z have if we execute the following assignment statement?

double z = 10 / 40;

3.  What value will z have if we execute the following assignment statement?

int z = 50 / 10.00;

4.  What will be the result of the following assignment statement?

Assume b = 6 and c = -10.
int a = b * (-c + 2) / 2;

5.  Know the differences between compile and run-time errors and be able to identify them in code.

6.  Understand when and why you have to use cin.ignore(...).

7.  Why are constants like

const double TAXRATE = .1;

important? Why do we use constants instead of variables?

8.  Consider the following code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ depending on a student’s test score. Will it work correctly? If not, for what values of score will it work correctly?

if(score >= 90)

grade = 'A';

if(score >= 80)

grade = 'B';

if(score >= 70)

grade = 'C';

if(score >= 60)

grade = 'D';

else grade = ‘F’;

9.  Assume month has been previously declared as an int and given a meaningful value. Write a switch statement that for any value of month, produces exactly the same output as the following if statement.

if (month == 6)

cout < "summer solstice";

else if (month == 12)

cout < "winter solstice";

else if (month == 3 || month == 9)

cout < "equinox";

else

cout < "nothing special";

CS1 MT1 Study Guide P. 2

10.  What is the output produced by the following program segment?

string grendel = "endl";

cout < "endl";

cout < grendel;

cout < endl;

cout < "grendel";

11.  If x is currently 0, a = 5 and b = 5, what will x become after the statement below is executed

if (a > 0)

if (b < 0)

x = x + 5;

else

if (a > 5)

x = x + 4;

else

x = x + 3;

else

x = x + 2;

12.  What is the output?

int sum = 0;

for (int k=0; k<5; k++) {

sum += k;

}

cout < sum;

13.  What is the output?

int z=2, sum=0;

while(z<9)

{

z++;

sum=sum+z;

}

cout < sum;

CS1 MT1 Study Guide P. 2

14.  What is the output if the initial value of k and p are both 0?

do {

if(k= =1)

{

p+=3;

}

k++;

p--;

} while(k<3);

cout < p ;

15.  What is the final value of p?

double p = 0;

for (int m=10; m>6; m--) {

if (m==7) {

p = p+m;

}

else {

p++;

}

}

16.  What is the value of n after the following nested loops?

int n = 0;

for (int i = 1; i <= 5; i++)

for (int j = 0; j < i; j++)

n = n + j;

17.  Consider the following code segment, what is output?

for (int i = 0; i < 5; i++) {

for (j = i; j < 5; j++)

cout < i;

cout < endl;

}

CS1 MT1 Study Guide P. 2

Free Response:

1.  You and your date are trying to get a table at a restaurant. You and your date have stylishness of your clothes, in the range 0...10, If either of you is very stylish, 8 or more, then the result is "yes". With the exception that if either of you has style of 2 or less, then the result is "no". Otherwise the result is "maybe"

2.  The squirrels in Torrance spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a bool isSummer, output "Let's Play" if the squirrels play and "I'm tired" otherwise.

3.  Combinatorics is the art of enumerating combinations and permutations. Write code (not the whole program) that will print all the triples of nonnegative integers that add up to the number 7.

When the program is executed the output appears as something similar to:

(Hint: I’d use 3 for loops arranged in a special way, it can also be done with 2)

0 0 7

0 1 6

0 2 5

0 3 4

0 4 3

.....

6 0 1

6 1 0

7 0 0

4.  Write the code below to complete the program

#include <iostream

using namespace std;

const int SECS_PER_MIN = 60;

const int SECS_PER_HOUR = 3600;

int main()

{

int seconds, minutes, hours, secs_left, mins_left;

cout < "Enter number of seconds: ";

cin > seconds;

// your code goes HERE

// do some calculations using modulus and

// the division operator

cout < seconds < " seconds is equal to "

< hours < " hours, " <mins_left

<" minutes, " < secs_left < " seconds" <endl;

return 0;

}

CS1 MT1 Study Guide P. 2

5.  Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, print a string of the form "7:00am" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00am" and on the weekend it should be "10:00am". Unless we are on vacation -- then on weekdays it should be "10:00am" and weekends it should be "off".

day: 1, vacation: false → "7:00am"

day: 5, vacation: false → "7:00am"

day: 0, vacation: false → "10:00am"

6.  You have a red lottery ticket showing ints a, b, and c, each of which is 0, 1, or 2. If they are all the value 2, print 10. Otherwise if they are all the same, the print 5. Otherwise so long as both b and c are different from a, print 1. Otherwise the result is 0.

a: 2, b: 2, c: 2 → "10"

a: 2, b: 2, c: 1 → "0"

a: 0, b: 0, c: 0 → "5"

7.  Your cell phone rings. Print "Answer it!" if you should answer it and "Don't answer it!". Normally you answer, except in the morning you only answer if it is your mom calling. In all cases, if you are asleep, you do not answer.

isMorning: false, isMom: false, isAsleep: false → "Answer it!"

isMorning: false, isMom: false, isAsleep: true → "Don't answer it!"

isMorning: true, isMom: false, isAsleep: false → "Don't answer it!"

CS1 MT1 Study Guide P. 2