Practice exam for CIS 263 Exam 1

You can print these problems out by using the "File -> Print..." menu item.

Problem 1:
If x is a double, what does the following code segment print out for each of the cases where x is greater than 1, x is between 0 and 1, and x is negative? (Hint: Reformat this code fragment with the proper indentation.)
          if (x >= 0)
          if (x >= 1)
          System.out.println("over here");
          else
          System.out.println("no, over there");

Problem 2:
Find the error in the following if-statement and correct it.
          if (x && y == 0)
             System.out.println("That's the origin.");

Problem 3:
Rewrite the following program segment using a for-loop. What does this loop print out?
          int n = 10, i = 0;
          while( i < 2*n+1 )
          {  i = i + 2;
             System.out.println( i );
          }

Problem 4:
What do the following two while-loops print out? What would each one of them print out if its pair of braces was removed?
          int n = 10, i = -1;                    int n = 10, i = 1;
          while( i <= 2*n-1 )                    while( i <= 2*n-1 )
          {  i = i + 2;                          {  System.out.println( i );
             System.out.println( i );               i = i + 2;
          }                                      }

Problem 5:
What is the output from the following program segment?
          for (int i = 2; i <= 5; i++)
              for (int j = 5; j > i; j = j-1)
                  System.out.println(i + " and " + j);

Problem 6:
Give the method headers for each of the following methods.

(a) Method randomArray which does not take any arguments and returns an array of doubles.

(b) Method findSomething which takes an array of integers and a single character and returns an integer.

(c) Method theyMatch which takes two strings and returns either true or false.

Problem 7:
Write a program segment that declares an array variable, constructs an array, assigns the array to the array variable, and then uses a loop to fill the array with the following integers.
           -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5

Problem 8:
Suppose that numbers is an integer array that has been initialized with random integers. Describe as best you can what the following program segment does. For example, after each loop around the for-loop, how would you describe the value of x? Also, how would you describe the output of this code?
           int x = numbers[0];
           for (i = 1; i < numbers.length; i++)
              if (numbers[i] >= x)
                 x = numbers[i];
           System.out.println(x);

Problem 9:
Suppose I have defined a class called Thing and a subclass called ThingOfBeauty. The following line of Java performs three distinct steps. What are they? How would you rewrite this single line of Java as two lines of Java? What distinct steps do each of those two lines perform?
          Thing rose = new ThingOfBeauty();

Problem 10:
Part (a) For the following pairs of classes, identify the superclass and the subclass.
          Employee, Manager
          Person, Student
          BankAccount, CheckingAccount
          Vehicle, Minivan
          Number, Integer
          Triangle, Shape
          IceCream, Food, Dessert
Part (b) Draw an inheritance diagram to show what relationships you would establish among the following classes.
          Student
          Professor
          Employee
          Secretary
          DepartmentChair
          Person
          Freshman

Problem 11:
What is the output of the following program?
           public class Foo
           {  static int i = 0;
              static int j = 0;

              public static void main(String[] args)
              {  int i = 2;
                 int k;
                 {  int j = 3;
                    k = i + j;
                    System.out.println("k is " + k);
                 }
                 k = i + j;
                 System.out.println("k is " + k);
                 System.out.println("j is " + j);
              }//main
           }//Foo

Problem 12:
Consider the following two class definitions.
          class Thing1
          {  public static int y;
             public int x;
             public void setX(int x){ this.x = x; }
             public int getX(){ return x; }
          }//Thing1

          class Thing2 extends Thing1
          {  private int x;
             public void setX(int x){ this.x = x; }
             public int getX(){ return x; }
          }//Thing2
Suppose that we have the following declarations.
          Thing1 a = new Thing1();
          Thing2 b = new Thing2();
Explain why each of the following assignment statements is either OK or not OK?

Part (a)

          a.x = 5;

Part (b)

          b.x = 5;

Part (c)

          a = b;

Part (d)

          b = a;
Part (e)
          Thing1.x = 2;
Part (f)
          Thing2.y = -2;
Part (g) Consider the following code fragment.
          Thing1 c = new Thing2();
          c.x = 5;
          c.setX(7);
          System.out.println( c.x );
          System.out.println( c.getX() );
Explain why the output of this fragment is
          5
          7


Return to the main Java page.
Return to the CIS 263 home page.


compliments and criticisms