SE120 Midterm1 Spring2018 SampleAnswer.pdf
SE120 Midterm1 Spring2018 SampleAnswer.pdf
Student Number:
Student Signature:
2) Given the following: int [][] x = { { 2, -4, 2}, {5}, {4, 56, 6, 8}, {-1, -5 } };
What is x[1].length?
a. 3
b. 1
c. 4
d. 2
class TestA {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
}
}//end of class TestA
Which of the following is a correct way to access s?
a. a2.s
b. A.s
c. a1.s
d. All of the above
2
6) Class attributes with a default visibility can be referenced ___________.
a. by any class
b. by any class in the same package
c. only within the class itself
d. only from a main method
a. 316
b. 36
c. 361
d. None of the above
A(String newS) {
s = newS;
}
class TestA {
public static void main(String[] args) {
A a = new A("test");
a.print();
}
}//end of class TestA
a. The program compiles fine, but has a runtime error because the print() method is private.
b. The program has a compilation error because the print() method in class A is private.
c. The program runs fine and prints Test.
d. None of the above.
3
9) What does the following code print?
class C {
public static int i = 5;
}//end of class C
public Numbers() {
n1++;
n2++;
System.out.println(n1 + “, “ + n2);
}
}//end of class Numbers
4
12) Which of the following defines a class variable?
a. static numberOfInstances;
b. int numberOfInstances;
c. final int numberOfInstances;
d. static int numberOfInstances;
13) What is the printout for the third print statement in the main method?
public class Foo {
static int i = 0;
static int j = 0;
k = i + j;
System.out.println("k is " + k); //second printing statement
System.out.println("j is " + j); //third printing statement
}
}//end of Foo class
a. j is 3
b. j is 2
c. j is 1
d. j is 0
14) Given the declaration Circle x = new Circle(); which of the following statement is most accurate.
a) x contains an object of the Circle type.
b) You can assign an int value to x.
c) x contains a reference to a Circle object.
d) x contains an int value.
5
15) What does the following code print?
class E {
private int var1 = 5;
private static int var2 = 5;
public E() {
var1 = var2;
var2 = var2 + 2;
}
int [][] arr = { { 2, 3, 5, 2}, {5, 1, 4, 6}, {4, 56, 6, 8}, {-1, -5, -11, -20} };
for(int i=0; i<arr.length; i++)
System.out.println(arr[i][i]);
Output:
2
1
6
-20
6
Class Design [5 marks]
17) A local bank approached you to create a software for them to calculate interest on loans. Each customer that
comes to the bank will have to state their monthly income and then your software will take that alongside the
customer details and prints out the following:
1. The customer details: name, ID, monthly income and age.
2. The loan monthly payment:
monthlyPay = monthlyIncome / 3
3. Total interest on the loan:
totalInterest = (monthlyPay * 60) * interestRate
4. Maximum loan that the customer can take:
maxLoan = (monthlyPay * 60) – totalInterest
The interest rate is a fixed rate that CANNOT be changed and is set to 10%.
You also need to keep a record of how many loans the bank has calculated (this basically tells the management
how many customers have inquired about their loans).
Design a class that will do the job for the bank. Make sure your class does not violate the rules of encapsulation.
You need to specify the attributes and methods of the class and use proper visibility modifiers where needed. You
do not need to write the code of the methods. Just provide the method return type and visibility type, method
name and parameters (input).
Then write a testing scenario for one customer and how your class methods will be invoked (including the
constructor).
Sample Answer:
Attributes:
private String name
private String ID
private double monthlyIncome
private int age
private static int loanCount
private static final double interestRate = 0.1
Methods:
Constructors (you will increase the loanCount in here and initialize the other variables)
Getters and setters for name, ID, monthlyIncome and age
public double calculateMonthlyPay() //returns the monthly pay based on the monthlyIncome
public double calculateTotalInterest()
public double calculateMaxLoan()
Scenario:
In the main method create an object of your class
7
Initialize name, ID, age and income
Call calculate methods
Print all values
That takes as an input a single dimension array and flips it upside down (reverses it). You are not allowed to use
another array in your method and your method should be able to work with any single dimension array no matter
the size.
Example:
Before calling your method: arr = {1, 2, 3, 4, 5}
After calling your method: arr = {5, 4, 3, 2, 1}
This can be void because arrays are passed to methods by reference and changes to them in the method will
change it in the main (or the calling context). It can also be int [] and we return arr.