Southern Illinois University Carbondale CS 202 by xje10735

VIEWS: 14 PAGES: 12

									          Southern Illinois University Carbondale
         CS 202: Introduction to Computer Science
                          Fall 2005
                          Section 6

                    MIDTERM EXAMINATION 1

                       Instructor: Kenny Fong

                          September 29, 2005


Name: __________________________________________
ID: _____________________________________________
Signature: _______________________________________


DIRECTIONS:
 1. Write your name and student ID above.
 2. The maximum score of this exam is 100.
 3. Attempt all questions.
 4. Complete all answers in the spaces provided.
 5. No aids are permitted. All notes and books have to be placed on the floor.
 6. Blank sheets are available for your scratch work.
 7. You are not required to document any code you write.
 8. You are not allowed to leave the exam room during the first hour of the exam.
 9. The duration of the exam is 120 minutes.
 10. Cheating is an academic offense. Your signature above indicates that you
     understand and agree to the University’s policies regarding cheating on exams.



             Module                 Marks              Maximum
               1                                          26
               2                                          18
               3                                          19
               4                                          22
               5                                          15
              Total                                      100
MODULE 1: COMPUTER SYSTEMS & JAVA FUNDAMENTALS (26 points)

1.   (4) There are four main hardware components in a computer system. What are they? Briefly describe the
     basic role of each of them.




2.   (1) How many unique items can be represented with 9 bits? Give the exact answer.




3.   (3) There are three kinds of program errors. What are they? Give an example for each of them.




                                                                                                        2
4.   (1) Which of the following would not be considered an algorithm?
         A. A recipe
         B. A computer program
         C. Pseudocode
         D. A shopping list
         E. Travel directions

5.   (1) What kind of software tool is required to run Java bytecode?




6.   (1) Which of the following is an illegal Java identifier?
         A. _i
         B. println
         C. Public
         D. 1value
         E. cs202

7.   (1) Following Java naming convention, which of the following would be the best name for a class about
     store customers?
          A. StoreCustomer
          B. storecustomer
          C. storeCustomer
          D. STORE_CUSTOMER
          E. store_customer

8.   (1) Which of the following is not a reserved word in Java?
         A. else
         B. main
         C. new
         D. public
         E. void

9.   (4) Write a single System.out.print statement that prints the following:

                                                  ″Ke′nn′y″\n
                                                  is/*great*/




                                                                                                        3
10. (4) What output is produced by each of the following statements? Label any space with the ∆ symbol.

    a)   System.out.println(123 + ″456″ + (789 + 1));




    b) System.out.println(123 + 456 + (″789″ + 1));




    c)   System.out.printf(″%5s\n″, ″Java″);




    d) System.out.printf(″%10.3f\n″, 528.94276);




11. (2) What is the size of the primitive data type char in Java? What is its largest value?




12. (1) What is the data type of the literal constant 1.23?




13. (2) Consider the following segment of code taken from a Java source file that imports java.util.Random:

                         Random generator = new Random();
                         double value = generator.nextFloat() + 3;
                         value *= 5;
                         int num = (int)value – 3;

    What are the smallest possible value and the largest possible value of the variable num?




                                                                                                              4
MODULE 2: STRINGS (18 points)

1.   (11) Consider the following declarations:

                             String s1 = ″CS202 is INTERESTING!″;
                             String s2 = ″CS202 is boring!″;

     What output is produced by each of the following statements?

     a)   System.out.println(s2.length());



     b) System.out.println(s1.toUpperCase());



     c)   System.out.println(s1.charAt(13));



     d) System.out.println(s2.substring(0));



     e)   System.out.println(s2.substring(4, 11));



     f)   System.out.println(s1.replace(′T′, ′t′));



     g) System.out.println(s1.indexOf(″interesting″));



     h) System.out.println(s1.indexOf(″T″, 12));



     i)   System.out.println(s1.lastIndexOf(″IN″));



     j)   System.out.println(s1.substring(5).indexOf(″IN″));



     k) System.out.println(s1.compareTo(s2) < 0);




                                                                    5
2.   (3) Let sentence be a String reference variable. Suppose that it references a string that contains
     multiple occurrences of the character ‘a’. Write a single statement that prints the location (i.e., index) of the
     second occurrence of ‘a’ in this string.




3.   (4) Consider the following segment of code:

                        String str1 = new String(″Kenny is great!″);
                        String str2 = str1;

     Draw a state-of-memory diagram depicting all reference variables and objects resulted after the sequence of
     statements above executes. Label the stack and the heap in your diagram.




                                                                                                                    6
MODULE 3: EXPRESSIONS (19 points)

1.   (8) Consider the following declarations:

                                         int x = 45, y = 6, z;
                                         double f = 5.867, g;

     What output is produced by each of the following segments of code? If there is an error, state it and explain
     why.

     a)   z = x / y;
          System.out.println(z);




     b) z = x % y;
        System.out.println(z);




     c)   z = x + f;
          System.out.println(z);




     d) z = x / (int)f;
        System.out.println(z);




     e)   g = x / y;
          System.out.println(g);




     f)   g = x / (double)y;
          System.out.println(g);




     g) System.out.println(x++ <= 45);
        System.out.println(x);




                                                                                                                7
2.   (8) Consider the following declarations:

                                int a = 5, b = 4, c = 6, d = 40, e = 30;

     For each of the following expressions, indicate the order in which the operators will be evaluated by
     writing a number beneath each operator. Then, evaluate each expression using the declarations above.

     a)   a   +   b   *     c    %    (d   –    e)




     b) – a       *   - b   *     (b   +   c)     /   e




     c)   a   <   b   ||    c     ==   6   &&     d   <=      e




     d) a     >   b   +     c    &&    b   <=    c    +   d   %   e   ||    ! (c     >=    e)




3.   (3) Let a, b and c be three boolean variables. Construct a truth table for the following boolean expression.
     Your truth table should have 6 columns.

                                                a || (b && !c)




                                                                                                               8
MODULE 4: SELECTION STATEMENTS (22 points)

1.   (8) Let a, b and c be three boolean variables. Consider the following code segment:

                                    if (!a || c) {
                                      if (a == c)
                                        System.out.println(1);
                                      else if (b && c)
                                        System.out.println(2);
                                      else if (a == b)
                                        System.out.println(3);
                                    }
                                    else
                                      System.out.println(4);

     For each of the following parts, give values for a, b and c that cause the code segment above to print the
     specified number.

     a)   1




     b) 2




     c)   3




     d) 4




2.   (2) Let x, y, z be variables of type int. Rewrite the following if-else statement using a conditional
     operator.

                                               if (x > y)
                                                 z = x - 5;
                                               else
                                                 z = y + 5;




                                                                                                             9
3.   (6) Let answer be a variable of type char. Convert the following multibranch if-else statement into
     an equivalent switch statement. Make your switch statement as clean and simple as possible.

                         if (answer == ′T′ || answer == ′t′)
                           System.out.println(″TRUE″);
                         else if (answer == ′F′ || answer == ′f′)
                           System.out.println(″FALSE″);
                         else
                           System.out.println(″ERROR″);




4.   (6) Write a segment of code that prompts the user to enter an integer and outputs an appropriate message
     specifying whether this integer is the Unicode number of one of the ten digit characters. Use
     JOptionPane for I/O.




                                                                                                          10
MODULE 5: PROGRAMMING (15 points)

Kenny’s office supply company reimburses its salespersons for travel expenses as follows. Each mile driven is
reimbursed at a specific rate of $0.135/mile. Additionally, a salesperson is reimbursed for her/his meals. There
is a separate rate for each type of meal (breakfast, lunch, and supper). The rate for breakfast is $7.50/breakfast.
The rate for lunch is $13.75/lunch. The rate for supper is $20.00/supper.

Write a complete Java program that prompts the user to enter the number of miles driven, the number of
breakfasts, the number of lunches, and the number of suppers, and computes how much an employee is
reimbursed. The company has a policy regarding the number of meals. The number of breakfasts must not
exceed the number of lunches and the number of lunches must not exceed the number of suppers. If these
conditions are not met, then the claim for the meal portion of the reimbursement is rejected. Your program
should print the total reimbursement amount on the screen, rounded off to 2 decimal places. In the event that the
meal portion of the claim is rejected, your program should also print an appropriate warning message.

You may assume that the user always enters non-negative integers. Make the four rates named constants in
your program. Use the Scanner class for console input.




                                                                                                               11
12

								
To top