Java Exam: Inheritance, Polymorphism, and Cloning, Exams of Nursing

A java exam covering topics such as inheritance, polymorphism, and cloning. It includes several coding questions that test the student's understanding of these concepts. The exam seems to be closed-book/notes, with a time limit of 60 minutes. Partial credit may be given for partially correct solutions, and the student is required to use correct java syntax. A class hierarchy on pages 15-18 that the student can reference, and there are two additional classes (complex and complextuple) on page 19 that the student must work with. The exam covers a range of topics, including method overriding, exception handling, and time complexity analysis. Overall, this document appears to be a comprehensive assessment of the student's java programming skills and understanding of object-oriented programming principles.

Typology: Exams

2024/2025

Available from 10/18/2024

maga-123
maga-123 🇺🇸

487 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COM S 2280
Fall 2024
TA Review Session - Midterm Exam 1
DO NOT OPEN THIS EXAM UNTIL INSTRUCTED TO DO SO
Name: ___________________________________________________
ISU NetID (username): ______________________________
Recitation section (please circle one):
A. T 12:40 pm (Md. Mokarram Chowdhury)
B. R 12:40 pm (Md. Arafat Hossain)
C. R 8:00 am (Jobayer Ahmmed)
D. T 8:00 am (Jobayer Ahmmed)
E. T 9:30 am (Hangjun Piao)
F. T 4:10 pm (Md. Mokarram Chowdhury)
G. R 9:30 am (Hangjun Piao)
H. R 4:10 pm (Fahmida Imrose)
J. T 2:10 pm (Fahmida Imrose)
K. R 2:10pm (Md. Arafat Hossain)
Closed book/notes, no electronic devices, no headphones. Time limit 60 minutes. Partial
credit may be given for partially correct solutions.
Use correct Java syntax for writing code.
You are not required to write comments for your code; however, brief comments
may help make your intention clear in case your code is incorrect.
If you have questions, please ask!
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Java Exam: Inheritance, Polymorphism, and Cloning and more Exams Nursing in PDF only on Docsity!

COM S 2280

Fall 2024

TA Review Session - Midterm Exam 1

DO NOT OPEN THIS EXAM UNTIL INSTRUCTED TO DO SO

Name: ___________________________________________________ ISU NetID (username): ______________________________ Recitation section (please circle one): A. T 12:40 pm (Md. Mokarram Chowdhury) B. R 12:40 pm (Md. Arafat Hossain) C. R 8:00 am (Jobayer Ahmmed) D. T 8:00 am (Jobayer Ahmmed) E. T 9:30 am (Hangjun Piao) F. T 4:10 pm (Md. Mokarram Chowdhury) G. R 9:30 am (Hangjun Piao) H. R 4:10 pm (Fahmida Imrose) J. T 2:10 pm (Fahmida Imrose) K. R 2:10pm (Md. Arafat Hossain) Closed book/notes, no electronic devices, no headphones. Time limit 60 minutes. Partial credit may be given for partially correct solutions.

  • Use correct Java syntax for writing code.
  • You are not required to write comments for your code; however, brief comments may help make your intention clear in case your code is incorrect. If you have questions, please ask!
  1. Refer to the class hierarchy on pages 15–18 to answer the questions below. (It helps to peel off pages 15 – 19 from your exam sheets for code lookup convenience and scratch purpose.) For each section of code, fill in the box stating one of the following: - the output, if any, or - that there is a compile error (briefly explain the error), or - the type of exception that occurs at runtime. MoneyMarketAccount sharpay = new MoneyMarketAccount("Sharpay Evans”, 1 ,2000000.00, 4.25); System.out.println(sharpay.getInterestRate()); Answer: 4. Account troy = new SavingsAccount("Troy Bolton", 2 ,3000.00 , 1.125); System.out.println( troy.getBalance()); Answer: 3000. Account troy = new SavingsAccount("Troy Bolton", 2 ,3000.00 , 1.125); troy.withdraw(5000); System.out.println(troy.getBalance()); Answer: IllegalArgumentException Account kelsi = new SavingsAccount("Kelsi Nielsen ", 3 , 500.00 , 1.125); System.out.println(kelsi.getAccountInfo()); Answer: Savings: Kelsi Nielsen , 3 CheckingAccount darbus = new MoneyMarketAccount(“Mrs.Darbus”,4,10000.00, 4.25); darbus.calculateInterest(365);

Answer: Exception in thread "main" java.lang.Error: Unresolved compilation

problem: The method calculateInterest(int) is undefined for the type CheckingAccount InterestBearing chad = new CheckingAccount(“Chad Danforth”, 5, 777.77); System.out.println(chad.getInterestRate());

Answer: Exception in thread "main" java.lang.Error: Unresolved compilation

problem: Type mismatch: cannot convert from CheckingAccount to InterestBearing

  1. You are given two classes Complex and ComplexTuple on page 19. They implement complex numbers and tuples of complex numbers, respectively. Hint: In both a and b, below, you are given significantly more space than you should need. a) (10 pts) Add a clone() method to the Complex class; the method must override java.lang. Object’s clone() method. The signature is shown below; you just need to fill in the try and catch blocks. @ Override public Object clone () { try { Complex c = (Complex) super .clone (); return c; } catch (CloneNotSupportedException e) { return null; }

b) Add a method to the ComplexTuple class that overrides the equals() method from java.lang.Object to perform a deep comparison between this object and an existing ComplexTuple object. Two Complexes are considered to be equal if they have the same real and imaginary parts. You can assume that Complex has a correctly implemented equals() method. @ Override public boolean equals (Object o) { // TODO

if(this == o)

return true;

if(o == null || this.getClass() != o.getClass())

return false;

ComplexTuple t = (ComplexTuple) o;

if(this.c1.equals(t.c1) && this.c2.equals(t.c2))

return true;

return false;

c) public static void main( String [] args) { int[] arr = {0,2,4,1,3}; boolean result = hasI(arr, 0); System.out.println(“Result: “ + result); } public static boolean hasI( int[] arr, int i) { if (i == arr.length) { return false; } return arr[i] == ‘i’? true : hasI(arr, i+1); } Number of recursive calls to hasI: O( 𝒏) Worst-case execution time: O( 𝒏)

  1. The following tables list the input array (first line), output array (last line), and internal array state in sequential order for each of the sorts that we have studied in class (SELECTIONSORT, INSERTIONSORT). The two O ( n^2 ) sorts print internal results as the last operation of their outer loops. Array contents occupy rows in the following tables, with the top rows containing the input and proceeding down through time to the output on the bottom. There is exactly one right answer to each problem. a–d are multiple choice. In parts e and f, you must fill in the contents of the empty row. a) Answer: INSERTIONSORT 0 6 5 7 4 1 2 3 0 6 5 7 4 1 2 3 0 5 6 7 4 1 2 3 0 5 6 7 4 1 2 3 0 4 5 6 7 1 2 3 0 1 4 5 6 7 2 3 0 1 2 4 5 6 7 3 0 1 2 3 4 5 6 7

b) Answer: SELECTIONSORT 0 6 5 7 4 1 2 3 0 6 5 7 4 1 2 3 0 1 5 7 4 6 2 3 0 1 2 7 4 6 5 3 0 1 2 3 4 6 5 7 0 1 2 3 4 6 5 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7

a) Fill in the missing row in this INSERTIONSORT output. 1 6 4 7 0 5 3 2 1 6 4 7 0 5 3 2 1 4 6 7 0 5 3 2 1 4 6 7 0 5 3 2 0 1 4 6 7 5 3 2 0 1 4 5 6 7 3 2 0 1 3 4 5 6 7 2 0 1 2 3 4 5 6 7

Sample Code for Problem 1

interface InterstBearing { double getInterestRate(); void setInterestRate( double newRate); void calculateInterest( int time); } public abstract class Account { protected String name; protected int number; protected double balance; public Account( String name, int number, double initialBalance) { this .name = name; this .number = number; balance = initialBalance; } public String getName() { return name; } public int getAccountNumber() { return number; } public double getBalance() { return balance; } public void deposit( double amount) { System.out.println(“Deposited: “ + amount); balance += amount; } public void withdraw( double amount) { if (amount > balance) { throw new IllegalArgumentException(); } System.out.println(“Withdrew: “ + amount); balance - = amount; }

public abstract String getAccountInfo(); } class SavingsAccount extends Account implements InterestBearing { protected double rate; public SavingsAccount( String name, int number, double initialBalance, double interestRate) { super (name, number, initialBalance); rate = interestRate; @Override public double getInterestRate() { return rate; } @Override public void setInterestRate( double newRate) { System.out.println(“Rate changed to: “ + newRate); rate = newRate; } @Override public void calculateInterest( int time) { System.out.println(“Calculating Interest on:” + balance + “ at “ + rate + “% over “ + time

  • “ days”); } @Override public String getAccountInfo() { return “Savings: “ + name + “, “ + number; } } class CheckingAccount extends Account { public CheckingAccount( String name, int number, double initialBalance) { super (name, number, initialBalance); } public void writeCheck( double amount) { System.out.println(“Wrote check for: ” + amount); balance – = amount; } public void checkOverDraft() { if (balance < 0) { System.out.println(“Account is overdrawn”);

Sample Code for Problem 2

public class Complex implements Cloneable { private int re; // real part private int im; // imaginary part public Complex ( int re, int im) { this. re = re; this. im = im; } @Override public boolean equals (Object o) { // Assume this is already implemented ; the // implementation details are irrelevant. } } public class Complex Tuple { private Complex c1 ; private Complex c2 ; public Complex Tuple ( Complex c1 , Complex c2 ) { this .c1 = c1 ; this .c2 = c2 ; } }