Quiz 2 Solutions

Question 1

Having multiple methods of the same name, each with a different combination of numbers, types, and orders of the parameters is known as

Question 2

What cannot be inherited by a sub-class from its super-class?

Question 3

To define a class for cake, which of the following definitions is the most appropriate?

Question 4

Inheritance is the process by which a new class, known as a ____, is created from another class, called the ______.

Question 5

All of the following are methods of the ArrayList class except:

Question 6

All of the following are methods of the ArrayList class except:

Question 7

All of the following are wrapper classes except

Question 8

A base class is synonymous with a:

Question 9

A _____ path name gives the path to a file, starting from the folder where we run a program.

Question 10

Standard code libraries in Java are organized into

Question 11

You cannot create an object using a/an:

Question 12

An object of a derived class has the type of the derived class, plus the type of every one of its ____ classes.

Question 13

An instance variable modified by protected:

Question 14

Class A has two instance variables: x with protected access and y with package access. Indicate which of these variables can be accessed within each of the following classes:

(a) B has access to both x and y. (b) C has access to x, but not y. (c) D has no access to both x and y.

Question 15

In defining a method for a class, how do you decide whether it should be public or private? In addition, how do you decide whether it should be a static or instance method?

A method should be public if it needs to be called from another class. If a method is mostly used inside another method and contains details to be hidden from the public, it should be private. If a method access some instance variables directly, it should be declared as an instance method. If a method only access static variables or the data passed in as parameters, it should be declared as a static method.

Question 16

There are two kinds of files: text files and binary files. Identify three major differences between these two kinds of files.

  1. Textfiles only have string values, while binary files have values of mixed data types, often involving numbers.
  2. Textfiles can be created and viewed by a text editor, while binary files can only be created and used by programs.
  3. Textfiles are often processed line-by-line and tokenized into words, while binary files can be loaded and processed more efficiently.

Question 17

What do overloading and overriding have in common? What are the two major differences between them?

Question 18

Assuming that the Date class is defined elsewhere, the following code is trying to define the Employee class for which any objects must have a non-empty hireDate. Write down the line numbers for the parts that are problematic along with brief explanations about the issues.

public class Employee {
    private String name;
    private Date hireDate;

    public boolean Employee(String name, Date hireDate) {
        if (hireDate == null)
        return false;
        else {
            this.name = name;
            this.hireDate = hireDate;
            return true;
        }
    }

    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }

    public boolean equals(Employee other) {
        if (other == null)
            return false;
        return name == other.name &&
        hireDate == other.hireDate;
    }
}

Question 19

Given an ArrayList of Double numbers, write a method named “sumList” that takes the ArrayList as a parameter and return the sum as the result.

(a) Implement the “sumList” method with an Iterator object; (b) Implement the “sumList” method with a regular for-loop; (c) Implement the “sumList” method with a for-each loop.

A

public double sumList(ArrayList<Double> numbers) {
    double sum  = 0.0;
    Iterator<Double> i = numbers.iterator();
    while (i.hasNext())
         sum += i.next();
   return sum;
}

B

public double sumList(ArrayList<Double> numbers) {
     double sum = 0.0;
     for (int i = 0; i < numbers.size(); i++)
         sum += numbers.get(i);
    return sum;
}

C

public double sumList(ArrayList<Double> numbers) {
      double sum = 0.0;
      for (Double e : numbers)
          sum += e;
     return sum;
}

Question 20

Given the following code fragment:

ArrayList<String> s = new ArrayList<String>();
s.add("health");
s.add("money");
s.add("love");
Iterator<String> i = s.iterator();
while (i.hasNext())
    System.out.println(i.next());
i.remove();
  1. re-write it with a for-each loop;
  2. re-write it with a regular for-loop using an index variable of “int” type.

A

ArrayList s = new ArrayList(); s.add("health"); s.add("money"); s.add("love"); String last = null; for (String e : s) { System.out.println(e); last = e; } s.remove(last);

B

ArrayList s = new ArrayList(); s.add("health"); s.add("money"); s.add("love"); for (int i = 0; i < s.size(); i++) System.out.println(s.get(i)); s.remove(s.get(s.size() - 1));

Question 21

A Stock class has the following instance variables. For example, the ticker for Apple Inc. is “AAPL” and its current price is $121.97. Write the definition for “equals” and make it a truly overriding method.

public class Stock {
    private String ticker;
    private double price;

    public Stock(String ticker, double price) {
      ...
    }
}
public boolean equals(Object other) {
   if (other == null)
      return false;

   else if (getClass() != other.getClass())
      return false;

   else {
      Stock otherStock = (Stock)other;
      return ticker.equals(otherStock.ticker) &&
                price == otherStock.price;
   }
}

Question 22

We want to define a sub-class DividendStock with an additional instance variable for the dividend rate, which should not be negative. For example, Apple Inc. has the ticker “AAPL”, the current price at $121.97, and the dividend rate of 1.74%.

public DividendStock extends Stock {
    private double rate;

    public DividendStock(String ticker, double price, double rate) {
        ...
    }
}

public DividendStock() {
    ...
}

A

public DividendStock(String ticker, double price, double rate) {
    super(ticker, price);
    if (rate < 0.0)  {
         System.out.println("Fatal errors");
         System.exit(0);
    }
    this.rate = rate;
}

B

public DividendStock() {
  this("None", 0.0, 0.0);
}