0% found this document useful (0 votes)
16 views

SE120 Midterm1 Spring2018 SampleAnswer.pdf

Uploaded by

ayham.at2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

SE120 Midterm1 Spring2018 SampleAnswer.pdf

Uploaded by

ayham.at2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Alfaisal University - College of Engineering

Software Engineering Department

Subject: SE 120 Object-Oriented Programming


Major Exam #1 (Spring 2017-2018)

Instructor Dr. Ammar Alanazi


Date March 5, 2018 Time 1:00 PM – 02:00 PM
Rooms S1.001/S2.001
Grade Percentage 15%

Student Name: Sample Answer

Student Number:

Student Signature:

Information and Instructions


Ø Please SWITHCH OFF your MOBILE NOW.
Ø This is a closed book exam and notes.
Ø The exam consists of 17 questions + 1 bonus question. Please answer all questions.
Ø This exam will be marked from 25. There is 2 marks bonus.
Ø Be sure that your exam consists of 10 pages including this cover page and the answer sheets at the end of the
exam booklet.
Ø You can write the exam in pencil or pen.
Section Mark Full
MCQ 15
Tracing 5
Class Design 5
Coding 2
Total 25

Course Learning Outcomes (CLO) Questions


Master the knowledge about the design, implementation
CLO1
of Object Oriented Programming
Clearly distinguish and describe major Object Oriented Q 1-18
CLO2 concepts such as Inheritance, polymorphism,
overloading/overriding, etc…
Multiple Choice Questions [15 marks]
For each question select the letter of the correct answer. There is only one correct answer for
each question. Write your answers in the answer sheet provided at the end of the exam.
[1 mark for each question]

1) Which of these statements is correct?


a. int arr[][] = new int[5][5];
b. int[] arr = new int[5];
c. int[][] arr = new int[5][5];
d. All of the above

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

3) Given the following:


class A {
Static String s;
}//end of class A

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

4) To allow access to a data member or a method to any class:


a. Use the private modifier.
b. Use the static modifier.
c. Use the public modifier.
d. None of the above.

5) Which of the following is incorrect statement?


a. Constructors always have the same name as the class name.
b. Constructors can be defined as static.
c. Constructors don’t have a return type.
d. Constructors can be overloaded.

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

7) Assume int c[][] = { { 3, 1 }, { 6 } };


What is the output of the following code?
for (int i = 0; i < c.length; i++)
for (int j = 0; j < c[0].length; j++)
System.out.print(c[i][j]);

a. 316
b. 36
c. 361
d. None of the above

8) What is the output of the following code:


class A {
String s;

A(String newS) {
s = newS;
}

private void print() {


System.out.println(s);
}
}//end of class A

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 class TestC {


public static void main(String[] args) {
C c1 = new C();
c1.i += 5;
C c2 = new C();
System.out.println(c1.i + “, “ + c2.i);
}
}//end of class TestC
a. 5, 5
b. 10, 5
c. 10, 10
d. 5, 10

10) What is the output of the following code?


class Test {
private int t;

public static void main(String[] args) {


Test test = new Test();
System.out.println(test.t);
}
}
a. The variable t is not initialized and therefore causes errors.
b. The variable t is private and therefore cannot be accessed in the main method.
c. 0.
d. 1.

11) What does the following code print?


class Numbers {
private int n1 = 12;
private static int n2 = 10;

public Numbers() {
n1++;
n2++;
System.out.println(n1 + “, “ + n2);
}
}//end of class Numbers

public class TestNumbers {


public static void main(String[] args) {
Numbers num1 = new Numbers();
Numbers num2 = new Numbers();
}
}//end of class TestNumbers
a. 13, 11
14, 12
b. 13, 11
13, 12
c. 13, 11
13, 11
d. None of the above.

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;

public static void main(String[] args) {


int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j); //first printing statement
}

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;
}

public int getVar1() {return var1;}


public int getVar2() {return var2;}
}//end of class E

public class TestE {


public static void main(String[] args) {
E e1 = new E();
E e2 = new E();
e2 = e1;
System.out.println(e2.getVar1() + “, “ + e2.getVar2());
}
}//end of class TestE
a. 5, 5
b. 5, 7
c. 5, 9
d. 7, 7

Tracing Question [5 marks]

16) Given the following code:

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]);

What is the output?


p.s. write your steps as this may get you partial mark if your answer is not totally correct.

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

Coding Question [2 bonus marks]

18) Answer both parts:


a. Write a method

public ________ flipAnArray(int[] arr)

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}

public void flipAnArray(int[] arr){


for(int i=0; i<arr.length/2;i++){
int temp = arr[i];
arr[i] = arr[arr.length-i-1];
arr[arr.length-i-1]=temp;
}
}

b. What is your return type of choice and why?

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.

You might also like