Unit 5 Test
Unit 5 Test
Test
Multiple Choice
public NumberPair()
{
num1 = 0;
num2 = 0;
}
Which of the following represents the correct way to construct a new NumberPair object with two random
digits, each between 1 and 10?
I.
NumberPair example = new NumberPair();
example.setFirst = first;
example.setSecond = second;
II.
NumberPair example = new NumberPair(first, second);
III.
NumberPair example = new NumberPair();
example.setFirst(first);
example.setSecond(second);
(A)
while(myPair.num1 == myPair.num2)
{
int s = (int)(Math.random() * 10) + 1;
myPair.num2 = s;
}
(B)
while(NumberPair.num1 == NumberPair.num2)
{
int s = (int)(Math.random() * 10) + 1;
NumberPair.num2 = s;
}
(C)
while(myPair.getFirst.equals(myPair.getSecond))
{
int s = (int)(Math.random() * 10) + 1;
myPair.setSecond = s;
}
(D)
while(myPair.getFirst() == myPair.getSecond())
{
int s = (int)(Math.random() * 10) + 1;
myPair.setSecond(s);
}
(E)
if(myPair.getFirst() == myPair.getSecond() && (s < 1 || s > 10))
{
int s = (int)(Math.random() * 10) + 1;
NumberPair.num2 = s;
}
___C__ 3) Consider the following class definition.
public Person(String n)
{
name = n;
num++;
}
(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
__B___ 4) Consider the following method.
}
else
{
result += "3";
}
return result;
}
(A) 1A2
(B) 1A2B
(C) 2B
(D) 13
(E) 23
__C___ 5) Consider the following class declaration.
(A) I only
(B) I and III only
(C) II and III only
(D) I and II only
(E) I, II, and III
Use the following class definition for problems 6 – 7.
public NumberPair()
{
num1 = 0;
num2 = 0;
}
(A)
myPair = NumberPair(x, y);
(B)
myPair.setFirst(x);
myPair.setSecond(y);
(C)
x = myPair.getFirst();
y = myPair.getSecond();
(D)
NumberPair myPair = new NumberPair(x, y);
(E)
myPair = new NumberPair();
myPair.setFirst(x);
myPair.setSecond(y);
__B___ 7) Consider the following implementation for the getQuadrant() method, that returns “Q1” if x
and y are both positive, “Q2” if x is negative and y is positive, “Q3” if both x and y are negative, and “Q4” if x
is positive and y is negative. If either point is a 0, the function should return “on axis”.
(A) The implementation will only work is all instances of x are replaced with myPair.num1 and all instances
of y are replaced with myPair.num2.
(B) The implementation will only work if all instances of x are replaced with myPair.getFirst() and all
instances of y are replaced with myPair.getSecond().
(C) The implementation will only work is all instances of x are replaced with myPair.x and all instances of y
are replaced with myPair.y.
(D) The implementation will only work is all instances of x are replaced with this.x and all instances of y
are replaced with this.y.
(E) The implementation works correctly as it is.
Questions 8 − 9 refer to the following class.
___E__ 8) A point will lie on the line if the equation y = mx + b (where m is the slope and b is the yInt)
evaluates to true. Which of the following correctly implements this method?
(A) I only
(B) II only
(C) II and III only
(D) III only
(E) I and III only
Which of the following methods could be added to the above methods without a compiler error?
11) You will be writing a complete BankAccount class. This class will have the following properties:
● Two instance variables, an int called accountNumber to represent an account number between 1000
and 9999, and a double called balance to represent the amount of money in the bank account.
● A parameter constructor that accepts a parameter that represents the starting amount in the bank account.
In this parameter constructor, a random accountNumber should be generated and assigned to
accountNumber.
● An accessor method for accountNumber.
● A method called deposit(double amount) that deposits the amount into your bank account
balance.
● A method called withdraw(double amount) that checks first if you have enough money in your
bank account to withdraw the amount.
o If you do, the method withdraws that amount
o If you do not, the method prints “Insufficient Funds”
● A method called printBalance() that prints the current amount you have in your bank account.
● A method called transfer(BankAccount recipient, double amount) that will transfer
amount from your bank account to the bank account of recipient. If you do not have enough money to
transfer from your account, this method will print “Insufficient Funds”.
The following table contains a sample code execution sequence and the corresponding results.
Write the complete BankAccount class, including the constructor and any required instance variables and
methods. Your implementation must meet all specifications and conform to the example.