printable version
Quiz 3 Review A
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
[12]
[13]
Problem Q3rA.1.
Sometimes in writing the constructor for a subclass,
the first line in the constructor must start with the word
super. Explain what this line does, and explain when it
is required.
This line indicates the parameters to be used in constructing the
portion of the new object representing the parent class. The
parameters will be passed into the appropriate constructor from
the parent class. If the parent class
contains a constructor with no parameters, this line may be
omitted, and Java will use that no-argument constructor. But
when all constructors for the parent class require at least one
argument, the line is required.
Problem Q3rA.2.
Suppose we have an Account class with the following
constructor and method.
Account(double initial)
Constructs an account with an initial balance of
initial.
double deposit(double amount)
Adds amount into this account's current
balance, returning the resulting balance.
Write a subclass of Account, named
SavingsAccount.
It should have an additional method endMonth, which
should increase the balance by the monthly interest rate of 0.1%.
This class will inherit deposit from Account;
it should not define this method again.
SavingsAccount(double init)
(Constructor method)
Constructs a savings account whose current balance is
init.
void endMonth()
Increases this account's balance by 0.1%.
class SavingsAccount extends Account {
public SavingsAccount(double init) {
super(init);
}
public void endMonth() {
double curBalance = deposit(0.0);
deposit(curBalance * 0.001);
}
}
Problem Q3rA.3.
Consider the following two class definitions.
class A {
public double g(double x) {
return 3.0 * f(x);
}
public double f(double x) {
return x;
}
} | | class B extends A {
public double f(double x) {
return 5.0 * x;
}
} |
What will the following sequence of statements display?
B y = new B();
println(y.f(2.0));
A x = y;
println(x.f(2.0));
println(x.g(2.0));
Problem Q3rA.4.
Suppose we have the following two class definitions.
class A {
public int f(int x) {
return 2 * x;
}
public int g(int x) {
return f(x * 3);
}
} | | class B extends A {
public int f(int x) {
return 5 * x;
}
public int h(int x) {
return f(7 * x);
}
} |
What would the following sequence of statements display?
B y = new B();
println(y.f(1) + " " + y.g(1) + " " + y.h(1));
A x = y;
println(x.f(1) + " " + x.g(1));
Problem Q3rA.5.
Suppose we have the following two class definitions.
class A {
public int f(int x) {
return 2 * h(x);
}
public int h(int x) {
return x * 3;
}
} | | class B extends A {
public int g(int x) {
return f(5 * x);
}
public int h(int x) {
return 7 * x;
}
} |
What would the following sequence of statements display?
B y = new B();
println(y.f(1) + " " + y.g(1) + " " + y.h(1));
A x = y;
println(x.f(1) + " " + x.h(1));
Problem Q3rA.6.
What does the below fragment display if
next is an array of ints
containing:
<3, 4, 5, −1, 2, 6, −2>
int loc = 1;
while(loc >= 0) {
println(loc);
loc = next[loc];
}
Problem Q3rA.7.
Suppose nums were an array of integers containing
the following:
1, 2, 2, 3, 5, 5, 5, 8
What would be in nums after executing the
below fragment?
int j = 0;
for(int i = 1; i < nums.length; i++) {
if(nums[i] != nums[i - 1]) {
nums[j] = nums[i];
j++;
}
}
<2, 3, 5, 8, 5, 5, 5, 8>
Problem Q3rA.8.
Suppose we have an array of integers referenced by the variable
vals containing <8,7,6,5,4>.
What will be in this array after executing the following
fragment?
for(int i = 1; i < vals.length; i++) {
vals[i] = vals[i - 1] + 1;
}
Problem Q3rA.9.
Complete the below method to return the maximum
value within the parameter array.
public double max(double[] nums) {
double ret = nums[0];
for(int i = 1; i < nums.length; i++) {
if(nums[i] > ret) ret = nums[i];
}
return ret;
}
Problem Q3rA.10.
Complete the following method so that it returns an array of the
specified length, with each element filled by the parameter
value.
public int[] repeat(int len, int val) {
int[] ret = new int[len];
for(int i = 0; i < len; i++) {
ret[i] = val;
}
return ret;
}
Problem Q3rA.11.
Complete the following method so that it returns the longest
string in the parameter array. For example, if names
refers to the array <Martin,
Galloway,
Couch,
Raney>, then findLongest(names) should return
Galloway.
public String findLongest(String[] words) {
String ret = words[0];
for(int i = 1; i < words.length; i++) {
if(words[i].length() > ret.length()) {
ret = words[i];
}
}
return ret;
}
Problem Q3rA.12.
Suppose we have two arrays, an array of Strings called
candidates which enumerates the names of candidates
running in an election, and an array of ints (of the same
length) called votes which has the number of votes tallied
for each candidate. For example, votes[0] contains the
number of votes garnered by the candidate whose name is in
candidates[0].
Write a program fragment that prints the name of the candidate
who received the most votes in the election.
int maxPos = 0;
for(int i = 1; i < candidates.length; i++) {
if(votes[i] > votes[maxPos]) maxPos = i;
}
println(candidates[maxPos]);
Problem Q3rA.13.
What will the below program display when run?
public class Problem extends Program {
public void run() {
int[] a = new int[2];
a[0] = 10;
a[1] = 15;
test(a);
println(a[0] + " " + a[1]);
}
public void test(int[] b) {
int[] a = new int[2];
b[0]++;
a[1] = 13;
b = a;
}
}
11 15