Schedule‎ > ‎

Quiz 2 Prep

Quiz 2 Prep (Quiz will be given Wed. 2/28):  Lecture Notes: 3, 4 and first half of Lecture 5

The quiz questions will be somewhat like these (I didn’t leave a lot of space for answers here.  Put your answers somewhere else)

1.      Declare a variable for a single character and assign it a value: (Lecture 3.2)

 

2.       Strings! (Lecture 3.2)

a)       Declare 3 strings,  firstName, lastName and fullName.  Initialize firstName to your first name, lastName to your last name and fullName to “” (empty string).

 

b)      Write a statement to assign firstName and lastName to fullName.

 

c)       Write a statement to print out the length of the firstName.

 

 

d)      Write a statement to print out the initials of firstName and LastName (like: SA)

 

3.      The value of x and y after the following code executes is x = ___  y = _____. (Lecture 3.3)

int x = 3;

int y = 4;

if (x != y) {

  x = y;

}

4.      Draw a flowchart of the following:  (Assume that you already have received guess from the user)     (Lecture 4.2.2)

If guess < 10 print “your guess is too low”.  If guess > 10 print “your guess is too high”.

If guess is 10 print “Correct!!!”.

 

5.       Write nested if-else-if code to do the following:  (Assume that you already have received guess from the user) (Lecture 4.2.2)

If guess < 10 print “your guess is too low”.  If guess > 10 print “your guess is too high”.

If guess is 10 print “Correct!!!”.

 

6.        What’s does this code do?  (also be able to write a while loop)  (Lecture 4.3)

 string signal = “y”; 

        while (“y” == signal)  {

                      cout << “Programming in C++ is fun!” << endl;

                      cout << “Do you want to keep going? (y/n) “;

                      cin >> signal;

                }

 

 

 

 

7.      Declare a constant that replaces a Magic Number that represents tax.  Initialize it to 9.0. (Lecture 4.1.4)

 

 

8.      Write a statement that casts a double myDouble to an integer and assigns the cast to an integer variable myInteger.  If myDouble is 9.5,  what value will end up in myInteger?  (Lecture 4.1.6)

 

 

 

9.      What is an equivalent statement for each of the following shortcut assignments? (Lecture 4.1.5)

  1. a += b;
  2. a -= b;

10.   What are 4 ways to increment the variable num by 1?  (Lecture 4.1.5)

 

11.  How many times is the following loop executed?   (Lecture 4.3)

int counter = 1;

while (counter > 20) {

   counter = counter + 1;

}

12.  What is wrong with the following code?   (Lecture 4.3.4)

char repeat = 'y';

while ('y' == repeat); {

    cout << "Do you want to run again? (y/n) ";

    // ... statements to execute

    cin >> repeat;

}

13.  You have 3 integer variables,  int numa, numb and numc.  Write an if test that checks to see if numb is between numa and numc (and not equal to either of them) (Lecture 4.2.7)

if (    you write what would go in here   )

 

14.  Write a for loop that will count down from 10 to 1 and sums the count so that at the end of the loop,  you have the sum of 10+9+8+7+6+5+4+3+2+1.  You do not have to write the whole program,  just the for loop which prints out the countdown and a cout after it which prints out the sum.  You will also need to declare any variables you need.  (Lecture 5.1)

 

15.  If you enter s (for single)  and 19 in response to the following code,  what will print out?  (Be able to write two level decision code like this.) (Lecture 4.2.4)

 

int main() {

    cout << "Enter marital status (s=single, m=married): ";

    string maritalStatus;

    cin >> maritalStatus;

    cout << "Enter your age in years: ";

    int age;

    cin >> age;

    int premium = 0;

    if (maritalStatus == "m") {

        if (age < 21) {

            premium = 1200;

        } else if (age <= 29) {

            premium = 1000;

        } else {

            premium = 800;

        }

    } else { 

        if (age < 21) {

            premium = 1500;

        } else if (age <= 29) {

            premium = 1200;

        } else {

            premium = 1000;

        }

    }

    cout << "Premium is " << premium << endl;

    return 0;

}

 

 

Comments