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
- method overridding
- method overloading
- method sharing
- method importing
Question 2
What cannot be inherited by a sub-class from its super-class?
- instance variables
- public methods
- private methods
- static variables
Question 3
To define a class for cake, which of the following definitions is the most appropriate?
- public class CAKE
- public class Cake
- public class cake
- private class Cake
Question 4
Inheritance is the process by which a new class, known as a ____, is created from another class, called the ______.
- inherited class, base class
- base class, derived class
- derived class, base class
- base class, inherited class
Question 5
All of the following are methods of the ArrayList class except:
- get()
- length()
- size()
- set()
Question 6
All of the following are methods of the ArrayList class except:
- remove()
- hasNext()
- next()
- clear()
Question 7
All of the following are wrapper classes except
- Integer
- String
- Double
- Character
Question 8
A base class is synonymous with a:
- parent class
- derived class
- abstract class
- child class
Question 9
A _____ path name gives the path to a file, starting from the folder where we run a program.
- full
- absolute
- relative
- complete
Question 10
Standard code libraries in Java are organized into
- interfaces
- objects
- classes
- packages
Question 11
You cannot create an object using a/an:
- subclass constructor
- ancestor class constructor
- abstract class constructor
- superclass constructor
Question 12
An object of a derived class has the type of the derived class, plus the type of every one of its ____ classes.
- parent
- ancestor
- descendant
- child
Question 13
An instance variable modified by protected:
- cannot be accessed by name inside its own class definition.
- cannot be accessed by name inside any class derived from it.
- cannot be accessed by name inside any class in the same package.
- cannot be accessed by name in any other class than those specified in a and c above.
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:
- Class B is in the same package as A but not a sub-class of A.
- Class C is a sub-class of A but not in the same package as A.
- Class D is neither a sub-class of A nor in the same package as A.
(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.
- Textfiles only have string values, while binary files have values of mixed data types, often involving numbers.
- Textfiles can be created and viewed by a text editor, while binary files can only be created and used by programs.
- 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?
- Both use the same name for different methods.
- Overloading uses different signatures for the methods and multiple overloading methods can be defined in one class.
- Overriding uses the same signature for the methods and each class can have only one overriding method.
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;
}
}
- lines 4, 6, and 10: a constructor shouldn’t have a return type, and thus can’t return any boolean values.
- line 14: needs to check that hireDate is not null before setting the value for the related instance variable.
- lines 19 and 20: needs to use “equals” method to compare two objects.
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();
- re-write it with a for-each loop;
- re-write it with a regular for-loop using an index variable of “int” type.
A
ArrayList
B
ArrayList
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() {
...
}
-
Write the definition for the constructor “DividendStock(String ticker, double price, double rate)” that shares the code defined in the Stock class as much as possible.
-
Write the definition for the no-argument constructor “DividendStock()” with “None” for ticker, “0.0” for price, and “0.0” for rate that shares the code in part (a) above as much as possible.
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);
}