CPSC 120 -- Test #2 Review

Topics

Some Additional Practice Exercises

  1. Self-Review questions 3.3 - 3.10 on page 197 plus exercises 3.1 - 3.9, 3.14, 3.15, 3.21 on pages 198 - 200.

  2. 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).

  3. 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 expected due to missing braces.)
     
              int answer;
              System.out.print("Enter a number: ");
              answer = Keyboard.readInt();
              if (answer < 5)
                 if (answer == 1)
                     System.out.println ("Hello");
                 if (answer = 0)
                     System.out.println("There");
                 else
                     System.out.println("You");
                 if (answer > 2)
                     System.out.println("What's");
              else
                     System.out.println("Up");
    
    
  4. Trace the execution of the following loop by showing the value of the variables at each step.
              int count1 = 0;
              int count2 = 0;
              int num = 2157;
              while ( num != 0)
              {
                 if (0 == num % 2)
                   count1++;
                 else
                   count2++;
                 num = num / 2;
              }
    
    
  5. 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).

  6. 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).

  7. Write a segment of code that asks the user to enter a month (as an integer 1 - 12). If the number entered isn't valid the code should print a message and should ask the user to try again. It should keep doing this until a valid integer (1-12) is entered.

  8. Write a segment of code to read in a sequence of rainfall data (each data item represents the number of inches of rain in a month) and find the average rainfall, the maximum rainfall, the minimum rainfall, the number of months in which the rainfall was over 4 inches and the number of months in which the rainfall was under 1 inch. Do this two different ways:

  9. Write a loop to print out the first n positive multiples of 3 and to find and print the sum of them. So, for example, if n = 7, the program would print out the numbers 3, 6, 9, 12, 15, 18, 21 and the sum 84. Is this a count-controlled loop?

  10. Modify the loop above to print out, count, and sum all multiples of 3 less than n. So, in this case, if n = 7, the program would print 3, 6 and the sum of 9 and count of 2. Is this a count-controlled loop?