CSC 227 Practice Test Solutions and Unit Tests, Exams of Computer Science

The solutions for a csc 227 practice test, including the evaluation of various expressions and the writing of pass or fail assertions for junit tests. Additionally, it includes the code for a java program that tests student records and gpa calculations, as well as a unit test for a static method that returns letter grades based on percentage.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-p5s-1
koofers-user-p5s-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ANSWERS v1.1
C Sc 227 Practice Test, Spring 04 Section Leader __________ Your Name: ______________________
1. Evaluate each of the following expressions and write the final value for each such that each value clearly
indicates the type of value. Always use the initial values of the variables as shown (12pts)
int j = 5;
int k = 3;
double x = -1.5;
double y = 2.3;
String s = "Al Gore";
Remember to use ' ' for char, " " for string, a decimal point for double, true or false
a) ____2___ j / k
b) ____'r'___ s.charAt(5)
c) __2.0___ Math.sqrt(y + 1.7)
d) __2___ Math.round(y)
e) __false _ x + 5.1 > y + 2.0
f) __false_ (k > j) && (k < 3)
g) ___2_____ j % k
h) ____2____ j + k / 4
i) false_ s.equals("AL GORE") // Missing "
j) _"AL GORE"_ s.toUpperCase()
k) __false__ true && false
l) ___-1___ s.indexOf("go");
2. Write PASS or FAIL to the left of any assertion to indicate if the assertion would pass of fail if part of a
JUnit test. (8pts)
a. __ FAIL ___ assertEquals("bc", "abcde".substring(1, 2)); // returns "b"
b. __ PASS ___ assertEquals(4.0, Math.sqrt(16.0), 1e-12);
c. __ PASS ___ assertTrue(1 % 2 == 1);
d. __ PASS ___ assertEquals(2, Math.round(1.754));
e. __ FAIL ___ assertEquals(1.7, Math.round(1.789 * 10) / 10.0, 1e-12);
f. __ FAIL ___ assertEquals(4, "abcde".length());
// The long expression actually returns true
g. __ FAIL ___ assertFalse(5 % 2 == 101 % 2 && 2.0 == Math.round(1.5));
h. __ FAIL ___ assertEquals('a', "abcde".charAt(1)); // returns 'b', not 'a'
1
pf3
pf4
pf5

Partial preview of the text

Download CSC 227 Practice Test Solutions and Unit Tests and more Exams Computer Science in PDF only on Docsity!

ANSWERS v1.

C Sc 227 Practice Test, Spring 04 Section Leader __________ Your Name: ______________________

1. Evaluate each of the following expressions and write the final value for each such that each value clearly

indicates the type of value. Always use the initial values of the variables as shown (12pts)

int j = 5; int k = 3; double x = -1.5; double y = 2.3; String s = "Al Gore";

Remember to use ' ' for char, " " for string, a decimal point for double, true or false

a) ____ 2 ___ j / k b) ____ 'r' ___ s.charAt(5) c) __ 2.0 ___ Math.sqrt(y + 1.7) d) __ 2 ___ Math.round(y) e) __ false _ x + 5.1 > y + 2. f) __ false _ (k > j) && (k < 3) g) ___ 2 _____ j % k h) ____ 2 ____ j + k / 4 i) false _ s.equals("AL GORE") // Missing " j) _ "AL GORE" _ s.toUpperCase() k) __ false __ true && false l) ___ -1 ___ s.indexOf("go");

2. Write PASS or FAIL to the left of any assertion to indicate if the assertion would pass of fail if part of a

JUnit test. (8pts)

a. __ FAIL ___ assertEquals("bc", "abcde".substring(1, 2)); // returns "b"

b. __ PASS___ assertEquals(4.0, Math.sqrt(16.0), 1e-12);

c. __ PASS___ assertTrue(1 % 2 == 1);

d. __ PASS___ assertEquals(2, Math.round(1.754));

e. __ FAIL___ assertEquals(1.7, Math.round(1.789 * 10) / 10.0, 1e-12);

f. __ FAIL___ assertEquals(4, "abcde".length());

// The long expression actually returns true

g. __ FAIL ___ assertFalse(5 % 2 == 101 % 2 && 2.0 == Math.round(1.5));

h. __ FAIL___ assertEquals('a', "abcde".charAt(1)); // returns 'b', not 'a'

3. Using these two classes, write the output that is generated by the main method below. (14pts)

public class TestStudent { public static void main(String[] args) { Student aStudent = new Student("Yang Li"); Course one = new Course(3.0, 4.0); // a 3 unit A (As are 4.0) Course two = new Course(2.0, 3.0); // a 2 unit B (Bs are 3.0) System.out.println(one.getUnits()); System.out.println(one.getGrade()); aStudent.recordCourse(one); System.out.println(aStudent.getName() + "'s GPA is " + aStudent.getGPA()); aStudent.recordCourse(two); System.out.println(aStudent.getName() + "'s GPA is " + aStudent.getGPA()); } }

OUTPUT:

**3.

Yang Li's GPA is 4. Yang Li's GPA is 3. public class Course { private double my_units; private double my_grade; public Course(double units, double grade) { my_units = units; my_grade = grade; } public double getUnits() { return my_units; } public double getGrade() { return my_grade; } } public class Student { private String my_name; private double my_units; private double my_qualityPoints; public Student(String name) { my_name = name; my_units = 0.0; my_qualityPoints = 0.0; } public String getName() { return my_name; } public double getGPA() { return my_qualityPoints / my_units; } public void recordCourse(Course aCourse) { my_units = my_units + aCourse.getUnits(); my_qualityPoints = my_qualityPoints + (aCourse.getUnits() * aCourse.getGrade()); } }**

6. How many times will each of the following loops print "Hello". Zero, Unknown, and Infinite are

perfectly legitimate answers. (4pts)

int j = 1; int n = 5; while(j <= n) { System.out.print("Hello"); n++; } INFINITE int j = 1; while(j <= 11) { System.out.print("Hello"); j = j + 3; } 4

7. Write the output generated by the following code. (4pts)

int n1 = 10; int n2 = 1000; while(n1 * n1 <= n2) { System.out.println(n1 + " " + n2); n1 = n1 + 1; n2 = n2 - 200; }

OUTPUT:

10 1000 11 800 12 600 13 400 14 200 int n1 = 10; int n2 = 1000; while(n1 * n1 <= n2) { System.out.println(n1 + " " + n2); n1 = n1 + 1; n2 = n2 - 200; }

8. Write a method in a class so it returns the numbers of occurrences of the character argument in the String

argument. Once complete, all of the following assertions must pass if run as part of a JUnit test (and the code

must compile. (12pts)

assertEquals(0, CharCounter.charCount("Abba", 'C'));

assertEquals(1, CharCounter.charCount("Abba", 'a'));

assertEquals(3, CharCounter.charCount("Abbacadabra", 'b'));

ONE POSSIBLE SOLUTION:

public static int charCount(String myString, char c)

int totalNumberOfCharacters = 0;

int counter = 0;

while (counter < myString.length())

if (myString.charAt(counter) == c)

totalNumberOfCharacters = totalNumberOfCharacters + 1;

counter = counter +1;

return totalNumberOfCharacters;

9. Write a Java class named Histogram that allows for a visual peek at quiz scores where "*" means 1 out of

10 on a quiz. If a quiz score (the integer arguments in addAndShowOne ) is outside the valid range of 0 through

10, the method addAndShowOne must print the argument followed by the string "is out of range". If the

quiz score argument is in range, print the quiz score after the correct number of *'s. For example, if the quiz

score argument is 8 , print ******** followed by 8. You should be able to ask the Histogram object for the

average quiz score at any time with a method named getAverage (see the last line of output). (24pts)

Histogram quizzes = new Histogram ( ); quizzes. addAndShowOne ( 7 ); quizzes.addAndShowOne( 8 ); quizzes.addAndShowOne( -1 ); quizzes.addAndShowOne( 11 ); quizzes.addAndShowOne( 10 ); quizzes.addAndShowOne( 0 ); quizzes.addAndShowOne( 5 ); quizzes.addAndShowOne( 9 ); System.out.println( ); System.out.println( "Average: " + quizzes. getAverage ( ));

-1 is out of range 11 is out of range ********** 10 0 ***** 5 ********* 9 Average: 6.

public class Histogram

private double totalQuizScores;

private int numberOfTests;

public Histogram()

totalQuizScores = 0;

numberOfTests = 0;

public void addAndShowOne(int quizScore)

if (quizScore < 0 || quizScore > 10)

System.out.println(quizScore + " is out of range");

else

int counter = quizScore;

totalQuizScores += quizScore;

numberOfTests += 1;

while(counter > 0)

System.out.print("*");

counter -= 1;

System.out.println(" " + quizScore);

public double getAverage()

return totalQuizScores / numberOfTests;