COSC 237

MOCK TEST #1


1. Given the method definition

 public static int mystery(double someVal){
     if(someVal > 2.0)
         return 3 * (int)someVal;
     else
         return 0;
 }

 

 What is the value of the expression mystery(4.2)  ?

 

 a. 12
 b. 12.0
 c. 0
 d. 0.0
 e. Nothing--the method call is invalid


2.  What is the output?

public class MysteryClass {
    public static void main (String[] args){
        for(int i = 1; i <= 4; i++)
            System.out.println(mystery(i));
    }

    public static int mystery(int n) {
        int y = n;
        for( int x = 1; x <= n - 1; x++)
            y = y * (n - x);
        return y;
    }
}

3. Given the method definition

 public static int mystery(int alpha, int beta) {
     if (alpha > beta)
         return alpha + 10;
     else
         return 2 * beta;
 }

 

What is printed by the following code?

 

 System.out.println(mystery(5, mystery(9, 4)));

 

a.       15

b.      38

c.       16

d.      19

 

4.  What is the output produced by the following code fragment:

 

int[] a = new int [5];
int i;
a[0] = 1;
for(i = 1; i <= 4; i++) {
    a[i] = a[i - 1] + 3;
    System.out.println(a[i] + " ");
}

 

5. Given the declaration: int[][] a = new int[3][3];
what is the output after the following statements are executed (exercises a, and b are independent).

a. for(int i = 0; i < 3; i++){
       for(int j = 0; j < 3; j++){
           a[i][j] = 2 * i;
           System.out.printf("%3d", a[i][j]);
       }
       System.out.println();
   }

b. for(int i = 0; i < 3; i++){
       for(int j = 0; j < 3 ; j++){
           a[i][j] = i + j;
           System.out.printf("%3d", a[i][j]);
       }
       System.out.println();
   }

 

6. Consider the class declaration

public class SomeClass {
    private int x;
    private int y;

    public void method_1(){ ... }
    ...
}

 And client code:

 

SomeClass alpha = new SomeClass();
SomeClass beta  = new SomeClass();
...

 

Considering both pieces of code above, which identifiers are names of class objects?

  a. x and y
 b. alpha and beta
 c. SomeClass, x, and y
 d. alpha, beta, x, and y
 e. method_1, x, and y

7. Will the following class definition generate errors? If yes, explain.

public class SomeClass {
    private int x;

    public int SomeClass() { ... }
    public void  method_1(int n){ ... }
    public int method_2(){ ... }
}

8. Suppose that the class declaration of SomeClass includes the following method signature:

 public boolean lessThan(SomeClass otherObject)

 

Which of the following tests in the client code correctly compares two class objects alpha and beta?

  a. if(alpha < beta)
 b. if(alpha.lessThan(beta))
 c. if(lessThan(alpha, beta))
 d. if(alpha.lessThan.beta)
 e. if(lessThan(alpha).beta)

 

9. Consider the following class declaration:

public class SomeClass {
    private int x;

    public SomeClass() { ... }
    public void  method_1(int n){ ... }
    public int method_2(){ ... }
}

Will the following client code (with each line numbered in a comment) generate errors? If yes, explain.

SomeClass someObject = new SomeClass(); //1
someObject.method_1(5.2);               //2
someObject.x = 5;                       //3

 

10. Assume you have the following class definition:

public class SomeClass {
    private int x;
    private String y;

    public SomeClass(int x1, String y1) {
        x = x1;
        y = y1;
    }

    public SomeClass() {
        x = 0;
        y = " ";
    }

    public SomeClass(SomeClass other) {
        x = other.x;
        y = other.y;
    }

    public String toString() {
        return new String(x + " " + y);
    }
}

a. Mark the following statements as valid/invalid. If invalid, explain.

SomeClass object_1 = new SomeClass(100, "Review");
SomeClass object_2 = new SomeClass("Test", 80.5);

 

b. Suppose the following sequence was executed. What would be printed on the screen?

SomeClass obj_1 = new SomeClass(15, "message");
SomeClass obj_2 = new SomeClass(obj_1);
System.out.println(obj_2.toString());

 

c. Add an equals method to SomeClass to compare 2 objects of SomeClass for equality.

11.  Design a Java BankAccount class to represent a savings account and allow all necessary bank operations. Complete the following BankAccount.java program:

import java.util.*;

public class BankAccount {
    private int id;
    private String name;
    private double balance;
    private double interestRate;

    //Default constructor ...

    //Alternate constructor ...

    public void setAccount(int someID, String someName, double someBalance, double someInterest) {
        ...
    }

    //ask the user for the info needed to open account
    public void openAccount() {
        ...
    }

    //change the account name and print a message with the change done
    public void changeName(int someID, String newName) {
        name = newName;
        System.out.println("Changed the account name to " + newName);
    }

    public void addInterest() {
        double quarterRate = interestRate/400;
        balance += balance * quarterRate;
    }

    public void deposit(int someID, double someAmount) {
        ...
    }

    public void withdraw(int someID, double someAmount) {
        ...
    }

    //Print a message with the final balance.
    //followed by the message "The account has been closed!"
    //Close the account.
    public void closeAccount(int someID) {
        ...
    }

    // Accessor methods
    public int getId() {
        ...
    }

    public String getName() {
        ...
    }

    public double getBalance() {
        ...
    }

    public double getInterestRate() {
        ...
    }

    public void print() {
        ...

    }
    // Compare for equality
    public boolean equals(Object obj) {

    }

    //Copy otherAccount into this account
    public void copy(BankAccount otherAccount) {
        ...
    }

    //Make a copy of this account
    public BankAccount getCopy() {
        ...
    }
}

NEXT:  Complete the following client program to test the class BankAccount

//Program to test class BankAccount
import java.util.*;

public class BankAccountClient {
    public static final int SIZE = 3;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        BankAccount[] acc = new BankAccount[SIZE];
        int i, id, choice;
        String name;
        double balance, interestRate, amt;

        //Initialize SIZE accounts with data entered by the user
        for(i = 0; i < SIZE; i++) {
           ...
        }
        choice = menu();
        while(choice != 0) {
            switch(choice) {
                case 1: // display all accounts
                    ...
                    break;
                case 2: //change account name
                    System.out.print("Enter account number: ");
                    id = input.nextInt();
                    ...
                    break;
                case 3: //deposit
                    System.out.print("Enter account number: ");
                    id = input.nextInt();
                    ...
                    break;
                case 4: //withdrawal
                    System.out.print("Enter account number: ");
                    id = input.nextInt();
                    ...
                    break;
                case 5: // add interst to all accounts
                    ...
                    break;
                case 6: //close account
                    System.out.print("Closing the account! Enter account number: ");
                    id = input.nextInt();
                    ...
            }//close switch
            choice = menu();
        }//close while
    }//close main

    public static int menu() {
        Scanner input = new Scanner(System.in);
        int choice;
        do {
            System.out.println("\nYour options for account operations are:");
            System.out.println("-----------------------------------------");
            System.out.println("\t1) Print all accounts");
            System.out.println("\t2) Change account name");
            System.out.println("\t3) Deposit");
            System.out.println("\t4) Withdrawal");
            System.out.println("\t5) Add interest to all");
            System.out.println("\t6) Close account");
            System.out.println("\t0) EXIT");
            System.out.print("Please enter your option: ");
            choice = input.nextInt();
            System.out.println();
        } while(choice < 0 || choice > 6);
        return choice;
    } //close menu()
}//close class

12.  Given the following classes:

public class Vehicle { ... }
public class Car extends Vehicle { ... }
public class Minivan extends Car { ... }

 

Mark the following statements as valid/invalid. If invalid, explain.

 

1. Vehicle v = new Vehicle();
2. Vehicle v = new Car();
3. Vehicle v = new Minivan();
4. Car c = new Car();
5. Car c = new Vehicle();
6. Car c = new Minivan();
7. Minivan m = new Minivan();
8. Minivan m = new Vehicle();
9. Minivan m = new Car();

 

13.  Consider the following class (to represent a student):

 

public class Student {
    private int id;
    private String name;
    private int age;

    public Student() {
        id = 0;
        name = " ";
        age = 18;
    }

    public Student(int otherId, String otherName, int otherAge) {
        id = otherId;
        name = otherName;
        age = otherAge;
    }

    public void setAge(int otherAge) {
        age = otherAge;
    }

    //other public methods here...
}

and this partial implementation of a subclass UndergradStudent to represent undergraduate students:

public class UndergradStudent extends Student {
    private int year;

    //public methods here...
}

Answer the following questions / solve the following exercises:

1. Can methods in the UndergradStudent class call the setAge method defined in the Student class?
2. Can methods in the UndergradStudent class access the id, name, and age fields defined in the Student class?
3. What is the difference between the this keyword and the super keyword? Where/when are they used?
4. Change the design for the alternate constructor and setAge in class Student and use this .
5. Write a default and alternate constructor for the UndergradStudent class.
6. In the UndergradStudent class, override the setAge method (set the age and also, increment the year value by 1)