CPSC 120 -- Test #2 Review


Some Additional Practice Exercises

  1. Self-Review questions 4.1, 4.2, 4.4 - 4.9 on pages 194-5 (answers are provided on pages 197-199) plus exercise 4.1; Self-Review questions 5.1, 5.3, 5.6 - 5.8 on page 274 plus exercises 5.1 - 5.4, 5.26, 5.28, 5.30.

  2. Write the Card class and associated program described in Programming Project 4.3 in the text (page 196). The Card class should include a toString method to print Card objects, and accessor methods for the suit and the face in addition to a constructor.

  3. Write a segment of code that takes an integer between 0 and 15 and assigns the character variable hexDigit the corresponding hex digit (as a character -- so if the integer is 1 hexDigit will be '1'; if the integer is 11 hexDigit will be 'B'). You should use a cascading if ... else... statement (you could also try a switch).

  4. Trace the execution of the following segment of code and determine what is printed for each of the following values of answer: (a) 3; (b) 0; (c) 8; (d) 1 (NOTE: This requires a very careful trace because it doesn't quite do what is expected due to missing braces.)
              int answer;
              System.out.print("Enter a number: ");
              answer = scan.nextInt();
              if (answer < 5)
                 if (answer == 1)
                     System.out.println ("Hello");
                 if (answer = 0)
                 if (answer > 2)
  5. Is the following if equivalent to the if in the MinOfThree.java program on page 219 of the text? (NOTE: Exercise 5.1 also asks about that program.)
              if (num1 < num2 && num1 < num3)
                 min = num1;
              else if (num2 < num1 && num2 < num3)
                 min = num2;
                 min = num3;

  6. Assume the variable seconds has been declared to be of type int and a value has been assigned to it. Write a declaration of a variable named validSeconds of type boolean and assign to this variable a boolean expression that determines whether or not seconds contains a valid number of seconds (a number between 0 and 59 inclusive).

  7. Assume the variable yearsExperience (type int) contains the number of years computer experience of a person and the variable passed120 (type boolean) is true if the person passed CPSC 120 at Roanoke College. Assume a person is eligible for a computer related job if they have at least 5 years computer experience or they passed CPSC 120. (a) Write a declaration of a boolean variable eligible and assign it the boolean expression that is true if the person is eligible for the job. (DON'T use IFs!!) (b) Write a boolean expression that would be true if the person is NOT eligible for the job -- write this two different ways (one way with ANDS and one with ORS -- and of course some NOTS).