Name:____________________          Alpha ____________   Instructor: ________

 

 

SI204

Sample Written 6-Week Exam

 

 

 

This is a multi section exam.  You may NOT communicate about this exam with anyone using any medium until your instructor tells you that you can.

 

This exam is closed book and no electronic device can be used.

 

 

 

 

Problem

Points

Avail

 

Score

 

1.

 

15

 

 

2.

 

12

 

 

3.

 

6

 

 

4.

 

13

 

 

5.

 

12

 

 

6.

 

8

 

 

7.

 

4

 

 

8.

 

10

 

 

9.

 

10

 

 

10.

 

10

 

 

Total

 

100

 


1. (15 Points) True or False: the following variable names are legal, and if False, why?

 

            (a) T / F           thisIsARidiculouslyLongVariableName

 

(b) T / F           2nd_company

 

            (c) T / F           Navy#1

 

            (d) T / F           USNA

 

            (e) T / F           double

 

2. (12 Points) The following variable declarations apply to parts (a) thru (c).

 

int i, j, k;

double x, y, z;

char a, b, c;

string s, t;

 

For each of (a) thru (c) below, write a single cin statement that would read what is underlined and store the required values in the prescribed variables.

 

(a)  Read discount = $22.25 such that x contains 22.25 and s contains “discount”.

 

           

 

 

 

(b) Read m081234@usna.edu such that j contains 81234 and t contains usna.edu.

 

           

 

 

 

(c) Read 36.9% such that x contains 36.9, y contains 18.4 and z contains 83.6.

               18.4%

               83.6%

 

              

 

3. (6 Points) The 8-bit sequence (a byte) 10101011 has what interpretation as an

 

            (a) int (Show work or risk losing all credit)

 

     

 

 

            (b) as a bool

         

 


 

4. (13 Points) Given the following declarations:

 

                int i = 6, j = 4, k = 7;

      char c = ‘H’;

      double x = 3.6, y = 1.3;

      string s = “app”, t = “ly”, u = “rove”;

 

            Fill in the following table by providing the type and value of each expression.

Expression

Type

Value


i
/ j

 

 


c – ‘B’

 

 


s + u

 

 


i
% k

 

 

 

cout >> y

 

 

 

Leave Blank

 

1.0/3 + 1/3.0 + 1.0/3.0 + 1/3

 

 


k = j * y

 

 

 


 

5. (12 Points)

(a)  Assume that variable x is of type double with a value of -17.325, write the statement(s) that would print exactly the following:

 

                        The integer portion of the variable x is -17.

 

      

 

 

 

(b) Assume the variable c is of type char.  Write the statement(s) that would print “Upper Case” if the value stored in c is a capital letter and “Not Upper Case” otherwise.  A copy of the ASCII Character Table can be found on the last page of the exam.

 

      

 

 

 

 

 

 

 

 

(c) What is the output from the following code fragment?

 

char c1, g;

c1 = ‘*’;

c1 = g;

cout << “The value of c1 is “ << c1;

 

a.   The value of c1 is *

b.   The value of c1 is g

c.   The value of c1 is c1

d.   none of the above

 


6. (8 Points) Rewrite the following code fragment so that it prints exactly the same answer but uses a for loop rather than a while loop.

 

    double answer = 1.0;

    int trials = 3;

    while (trials < 15)
    {

         answer = answer * 2.0;

         trials = trials + 3;

    }

    cout << “The answer is “ << answer << ‘.’ << endl;

 

 

   

 

 

 

 

 

7. (4 Points) Which of the following is/are infinite loop(s)?

 

a.   for (int k = 0; k!=10; k++)

      cout << “Beat Army” << endl;

 

b. int numb = 101;

   while (numb <= 100)

   {

      cout << “Beat Army” << endl;

      numb --;

   }

 

c.    for (int numb = 25; numb <= 100; numb--)

      cout << “Beat Army” << endl;

 

d. none of the above

 

 

8. (10 Points) I am writing a program which computes PRT grades.  The portion of the program that calculates the grades is given below.

 

double push_score, curl_score, run_score, avg;

 

cin >> push_score;

cin >> curl_score;

cin >> run_score;

 

avg = push_score + curl_score + run_score / 3.0;

 

(a) Why do I get the incorrect result for avg?

 

 

 

(b) How would you correct the problem?

 

 

9. (10 Points) Explain why the following program does not compile, and explain what needs to be done to fix it.

 

#include <iostream>

using namespace std;

 

int main()

{

     int k;

     cin >> k;

 

     if (k < 0)

     {

       int n = -1*k;

     }

     else

     {

      int n = k;

     }

     cout << "The absolute value of k is " << n << endl;

 

     return 0;

}

 

(a) Why won’t this program compile?

 

 

(b) How can the problem from part (a) be fixed?

 


10. (10 Points) According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, a leap year is a year that is:

 

§         divisible by 400, or

§         divisible by 4, but not divisible by 100

 

For example, 2000 was a leap year (divisible by 400), as was 2004 (divisible by 4, but not by 100).  However, 2100 will not be a leap year because it is divisible by 4 and also divisible by 100 (centurial year).

 

Provide the three missing expressions and one missing logical operator in the C++ program below so that the program informs the user whether or not the year they entered is a leap year.  You can assume the user enters a valid integer input.

 

#include <iostream>

using namespace std;

 

int main()

{

 

    int year;

    cout << "Enter a 4 digit year: ";

    cin >> year;

 

    if (                )

        cout << "Leap Year" << endl;

    else if ((            )    (                ))

        cout << "Leap Year" << endl;

    else

        cout << "Not Leap Year" << endl;

 

    return 0;

}