Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Exam 1 Practice

  1. Write a method named total that has two int parameters named start and finish. total should add all integers between start and finish (inclusive) and return this value when it is invoked. You may assume that start is less than finish.

    For example, the following invocation of total should return 15:

        total(4, 6);
           
    

    In the second example, calling total returns 42:

        total(9, 12);
           
    
  2. Write a method named arithmetic that has three parameters:

    The method uses the first parameter to determine the arithmetic operation to be performed on the second and third parameters. The result is printed on the monitor when the method is invoked. The only legal values that can be used as parameters when the method is invoked are: '+', '-', '*', '/', '%'. Any other character value will cause "illegal operation" to be printed instead.

    For example, if arithmetic('+', 2, 3) is called, then 5 is printed.

    On the other hand, if arithmetic('*', 3, 2) is called, then 6 is printed.

  3. Write a method named sequence, which prints the first few numbers in a special sequence that is formed by starting with 1, 1. The third number in the sequence is found by adding the first two together; the fourth number in the sequence is found by adding the second and third numbers together; all other numbers in the sequence are also found by adding the prior two numbers together. sequence takes one integer parameter (called number): the number of numbers that will be printed from the sequence, which starts with 1, 1. If sequence is called with a non-positive integer, then the string, "Only positives may be used.", is printed.

    sequence(4) prints

    1
    1
    2
    3
           
    

    sequence(8) prints

    1
    1
    2
    3
    5
    8
    13
    21
           
    
  4. Write a class called DPUprinting that has an int field called totalPages. totalPages is a DPU student's current total number of pages of paper that have been printed. Write the entire source code for the class, including the fields, the constructor and two methods. One of the methods, getPages, will simply return the current value of the one field. The other, addOnePage, will add one to the totalPages field every time addOnePage is called/invoked.

  5. What is the output of the following code fragment?

    x = 0;
    y = 1;
    while (y < 20) {
      if (x == 0) {
        System.out.println("Hello!");
        y = y + 2;
      }
      else if (x == 1) {
        System.out.println(y);
      }
      else if (x == 4) {
        if (y > 10) {
          System.out.println("y is getting big!");
          y = y * 2;
          x = 0;
        }
        else {
          x = 3;
          y = y + 5;
        }               
      }
      x = x + 1;
      System.out.println("x = " + x);
    } 
    System.out.println("y = " + y);         
           
    
  6. Evaluate 3 * 4 % 6
  7. Evaluate (3 == 4) && (7 > 2)
  8. What is the scope of a formal parameter?
  9. What is the lifetime of a field?
Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University, Computer Science Department, Spring 2005
Maintained by Brian Howard (bhoward@depauw.edu). Last updated