0% found this document useful (0 votes)
9 views7 pages

(KEY) Practice Quiz 0

This document outlines the rules and guidelines for a practice quiz in CSE 123 Spring 2025, including academic integrity policies and grading criteria. It contains specific instructions for implementing the Comparable interface in Java, testing methods using JUnit, and designing class hierarchies. Additionally, it provides coding tasks related to InstagramPost, ArrayIntList, and inheritance concepts in Java.

Uploaded by

jonnyliu4
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)
9 views7 pages

(KEY) Practice Quiz 0

This document outlines the rules and guidelines for a practice quiz in CSE 123 Spring 2025, including academic integrity policies and grading criteria. It contains specific instructions for implementing the Comparable interface in Java, testing methods using JUnit, and designing class hierarchies. Additionally, it provides coding tasks related to InstagramPost, ArrayIntList, and inheritance concepts in Java.

Uploaded by

jonnyliu4
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/ 7

CSE 123 Spring 2025 Practice Quiz 0

Name of Student: __________________________KEY_________________________________________________

Section (e.g., AA):_____________________ ​ ​ Student UW Email :[email protected]

Do not turn the page until you are instructed to do so.


Rules/Guidelines:
●​ You must not begin working before time begins, and you must stop working promptly when time is called. Any
modifications to your quiz (writing or erasing) before time begins or after time is called will be reported as
academic misconduct to the university.
●​ You are allowed one page of notes, no larger than 8.5 x 11 inches. You may not access any other resources or
use any electronic devices (including calculators, phones, or smart watches, among others) during the quiz. Using
unauthorized resources or devices will be reported as academic misconduct to the university.
●​ In general, you are limited to Java concepts or syntax covered in class. You may not use break, continue, a
return from a void method, try/catch, or Java 8 stream/functional features.
●​ You are limited to the standard Java classes and methods listed on the provided reference sheet. You do not need
to write import statements.
●​ If you abandon one answer and write another, clearly cross out the answer(s) you do not want graded and draw
a circle or box around the answer you do want graded. When in doubt, we will grade the answer that appears in
the space indicated, and the first such answer if there is more than one.
●​ If you require scratch paper, raise your hand and we will bring some to you.
●​ If you write an answer on scratch paper, please write your name and clearly label which question you are
answering on the scratch paper, and clearly indicate on the question page that your answer is on scratch paper.
Staple all scratch paper you want graded to the end of the quiz before turning in.
●​ Answers must be written as proper Java code. Pseudocode or comments will not be graded.
●​ The quiz is not graded on code quality. You are not required to include comments.
●​ You are also allowed to abbreviate System.out.print and System.out.println as S.o.p and S.o.pln
respectively. You may NOT use any other abbreviations.

Grading:
●​ There are three problems. Each problem will receive a single E/S/N grade.
●​ Minor syntax errors will be ignored as long as it is unambiguous what was intended (e.g. forgetting a semicolon,
misspelling a variable name where there is only one close option). Major syntax errors, or errors where it is
unclear what was intended, may have an impact on your grade.

Advice:
●​ Read all questions carefully. Be sure you understand the question before you begin your answer.
●​ The questions are not necessarily in order of difficulty. Be sure you at least attempt every question.
●​ Write clearly and legibly. We cannot award credit for answers we cannot read.
●​ If you have questions, raise your hand to ask. The worst that can happen is we will say "I can’t answer that."
●​ Ask questions as soon as you have them. Do not wait until you have several questions.

Initial here to indicate you have read and agreed to these rules:
1. Comparable

Part A: Implementing Comparable


Consider the following classes InstagramPost and User:
// represents a single Instagram post​
public class InstagramPost implements Comparable<InstagramPost> {​
private int likes;​
private int comments;​
private String username; // the original creator of the post

public InstagramPost(int likes, int comments, String user) { ... }​


}
Implement a compareTo() method for InstagramPost that ranks posts based on “popularity,” with more
popular posts appearing earlier in the order (i.e. more popular posts are “less than” less popular posts).
Popularity is determined by the following characteristics, in this order:
1.​ Number of likes (more likes is more popular)
2.​ Number of comments (more comments is more popular)
3.​ Username (earlier in alphabet is more popular)

public int compareTo(InstagramPost other) {


if (this.likes != other.likes) {
return other.likes - this.likes;
} else if (this.comments != other.comments) {
return other.comments - this.comments;
} else {
return this.username.compareTo(other.username);
}
}
Part B: Testing
Write tests for InstagramPost.compareTo() using JUnit assert methods as follows:
●​ Write exactly four tests (i.e. exactly three assert method calls).
●​ At least one test must cause compareTo to return a negative number.
●​ At least one test must cause compareTo to return a positive number.
●​ At least one test must cause compareTo to return 0.
●​ Your tests must, collectively, test all three of the characteristics used for comparison described above
(likes, comments, username)
○​ That is, at least one test must determine its result based on each of the characteristics.

(Note that since there are more than four requirements, some of your four tests will need to achieve more than
one requirement.)

InstagramPost post1 = new InstagramPost(200, 15, "a");


InstagramPost post2 = new InstagramPost(100, 15, "b");
InstagramPost post3 = new InstagramPost(200, 40, "a");

assertTrue(post1.compareTo(post2) < 0);


assertTrue(post1.compareTo(post3) > 0);
assertTrue(post1.compareTo(post1) == 0);
2. ArrayIntList
Consider a method called addAll to be added to the ArrayIntList class. This method takes two
parameters: an integer, index, and an array of integers, values, and adds all values in values to the list
starting with index index.

For example, suppose the variable list contains the following values:

​ [1, 2, 3, 4, 5]

and the array nums contains the following values:

​ [101, 102, 103]

Then, after the call list.addAll(2, nums), the variable list would contain:

​ [1, 2, 101, 102, 103, 3, 4, 5]

Notice how the values from nums have been inserted into list beginning at index 2, with existing values
being shifted to the right.

Below is a buggy implementation of addAll. Annotate (write on) the code to correct the method. You may add,
delete, or move code. You may not call any other methods from the ArrayIntList class in your
implementation, and you may not create any additional data structures.

public void addAll(int index, int[] values) {


if (index < 0 || index > size) {
throw new IllegalArgumentException("Invalid index");
}
if (elementData.length < size + values.length) {
// NOTE: any size at least elementData.length + values.length would be accepted
int[] temp = new int[elementData.length + values.length];
for (int i = 0; i < size; i++) {
temp[i] = elementData[i];
}
elementData = temp;
}
for (int i = size - 1; i >= index; i--) {
elementData[i + values.length] = elementData[i];
}
for (int i = 0; i < values.length; i++) {
elementData[index + i] = values[i];
}

size += values.length;

}
3. Inheritance

Part A: Tracing
Consider the following class implementations.
public class Vehicle {​ public class HondaCivic extends Car {​
public void honk() {​ public void honk() {​
System.out.println("honk");​ super.honk();​
}​ System.out.println("hoooonkkkk");​
}​ }​
​ }​
public class Car extends Vehicle {​ ​
​ public class Harley extends Motorcycle {​
}​ public void wheelie() {​
​ wheelie(5);​
public class Motorcycle extends Vehicle {​ }​
public void wheelie(int n) {​ }​
System.out.print("W");​ ​
for (int i = 0; i < n; i++) {​
System.out.print("e");​
}​
System.out.println("!");​
}​
}

(Note that all classes have a default constructor with no arguments.)

Now consider the following code. For each line, place an X next to the correct outcome. If the code runs
without error, also indicate what output is printed by filling in the blank. If it produces no output, write "none". If
the output includes multiple lines, you may indicate line breaks with a slash character "/" in the output.​
Each box should be considered independently.

Vehicle v1 = new HondaCivic(); Compile-time error


v1.honk();
Run-time error

X Runs without error and prints:


honk / hooooonkkkk

Vehicle v2 = new Motorcycle(); X Compile-time error


v2.wheelie();
Run-time error

Runs without error and prints:


_______________________
Car c = new Motorcycle(); X Compile-time error

Run-time error

Runs without error and prints:


_______________________

Motorcycle m = new Harley(); X Compile-time error


m.wheelie();
Run-time error

Runs without error and prints:


_______________________

Motorcycle m = new Harley(); Compile-time error


m.wheelie(2);
Run-time error

X Runs without error and prints:


Wee!

Part B: Designing Hierarchies


Consider the Animal class and its main method on the left and its expected output on the right.

public abstract class Animal {​ Froofer says woof!


private String name;​ Meow, says Max!
public Animal(String name) {​
this.name = name;​
}
public void sayName() {
System.out.print(this.name);
}​
public abstract void speak();​
}

public class Client {


public static void main(String[] args) {​
Animal a = new Dog("Froofer");​
a.speak();​
a = new Cat("Max");​
a.speak();​
}​
}
Fill in the following implementations of Dog.speak and Cat.speak to achieve the above behavior. You may not
make any edits outside of Dog.speak and Cat.speak.

public class Dog extends Animal {​ public class Cat extends Animal {​
public Dog(String name) {​ public Cat(String name) {​
super(name);​ super(name);​
} }
​ ​
public void speak() {​ public void speak() {​
super.sayName(); System.out.print("Meow, says ");
System.out.println(" says woof!");​ super.sayName();
​ System.out.println("!");​
​ ​
}​ }​
} }

You might also like