






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
CIS 110 — Introduction to Computer Programming. Fall 2019 — Exam 1 ANSWER KEY ... at the end of the exam if you need extra space for any graded answers. You.
Typology: Exercises
1 / 12
This page cannot be seen from the preview
Don't miss anything!







My signature below certifies that I have complied with the University of Pennsylvania’s Code of Academic Integrity in completing this examination.
Write your name on every exam page. This is in case the pages get separated during grading.
For each statement below, note whether it is true or false in the space provided. The first one has been done for you as an example. Write “True” or “False”. No credit will be given for “T” or “F” only. 2.0) main() is always the first method executed when a program is run True 2.1) An if statement must have an else if or an else block False 2.2) a[a.length] will return the last element in the array a False 2.3) (int) 13.4 + 6.6 == 20 False 2.4) (“koala”.charAt(0) - “camel”.charAt(2)) == - (^2) True 2.5) Variable names cannot start with numbers True 2.6) You can have both of the following methods in one class: public static void launch(double power) { ... } public static double launch(double accuracy){ ... } False 2.7) int[] arr = int[18]; is a valid statement False
Hogwarts has (finally) found a way to use technology inside its premises. You, a newly recruited wizard from the muggle world, have been asked to use your coding skills to help the headmaster set up a system to assign grades to the students for their O.W.L (Ordinary Wizarding Level) exams. The grade schema is as follows: Passing grades: ● O: Outstanding (85 - 100] ● E: Exceeds Expectations (70 - 85] ● A: Acceptable (60 - 70] Failing grades: ● P: Poor (50 - 60] ● D: Dreadful (33 - 50] ● T: Troll [0 - 33] “()“ means exclusive and “[ ]” means inclusive (so, (70 - 85] means if someone gets a score of 85, they get an E, but if someone gets a 70, they get an A (because 70 is not included in E). Write a function, gradeOWLs(), taking in a double array of numeric scores and returning a char array of their corresponding letter grades (in the same order). public static char[] gradeOWLs(double[] marks) { char[] grades = new char[marks.length]; for (int i = 0; i < marks.length; i++) { if (marks[i] > 85) { grades[i] = ‘O’; } else if (marks[i] > 70) { grades[i] = ‘E’; } else if (marks[i] > 60) { grades[i] = ‘A’; } else if (marks[i] > 50) { grades[i] = ‘P’; } else if (marks[i] > 33) { grades[i] = ‘D’; } else { grades[i] = ‘T’; } } return grades; }
Professor Eaton and Professor Mally are playing a game. The rules are as follows: ● The game has 9 rounds, and in each round, a random integer between 1 (inclusive) and 200 (exclusive) is generated. ○ If this integer is even, Professor Eaton gets a point. ○ If this integer is odd, Professor Mally gets a point. ○ If this integer is evenly divisible by nine, no one gets a point. ○ If the integer 110 appears, the game ends immediately. Whoever has the most points at the end of the game wins; ties will go to Professor Eaton (alphabetically). The function below simulates this game and returns a String containing the name of the winning professor. Please fill in the blanks to complete the function. public static String professorGame() { int eatonPoints = 0; int mallyPoints = 0; for (int i = 0 ; i < 9 ; i++) { int rand = (int) (Math.random() * 199 ) + 1 ; if (rand == 110) { break; } else if (rand % 9 == 0) { continue; } else if (rand % 2 == 0) { eatonPoints++; } else { mallyPoints++; } } if (eatonPoints >= mallyPoints) { return "Eaton"; } else { return "Mally"; } }
String[] holidayDrinks = {"pumpkinSpookLatte", "candyCaneCappuccino"}; int numSongs = 5, currentSong = 2; boolean isHolidaySeason = false; In inStream = new In(filename); while (!inStream.isEmpty()) { String size = inStream.readString(); String drinkName = inStream.readString(); int numFlavors = inStream.readInt(); currentSong = (currentSong * 2) % numSongs; System.out.println("On song " + currentSong + " of " + numSongs); boolean isHolidayDrink = isHolidayDrink(drinkName, holidayDrinks); if (isHolidayDrink) { if (isHolidaySeason) { System.out.println("Happy holidays!"); System.out.println("Here's your " + size + " " + drinkName
Output of java NightOnTheTowne orders.txt On song 4 out of 5 Checking coldBrew Here's your medium coldBrew with 4 pumps of syrup! Your drink is free! On song 2 out of 6 Checking pumpkinSpookLatte Happy holidays! Here's your small pumpkinSpookLatte with 4 pumps of syrup! That'll be $ On song 4 out of 6 Checking snowCone We're out; have a free coffee. Your drink is free! On song 1 out of 7 Checking candyCaneCappuccino Happy holidays! Here's your secret candyCaneCappuccino with 110 pumps of syrup! That'll be $
public static void maxwell(double x, double y, int n) { if (n > 1) { if (Math.random() < 0.5) { PennDraw.circle(x, y, 1.0 / n); } else { PennDraw.square(x, y, 1.0 / n); } maxwell(x + (1 - x) * .1, y + (1 - y) * .1, n - 1); } } public static void sierra(double x, double y, int n) { if (n > 1) { if (Math.random() < 0.5) { PennDraw.circle(x, y, 0.2); } else { PennDraw.square(x, y, 0.2); } sierra(x + (1 - x) * .1, y + (1 - y) * .1, n - 1); } } public static void michael(double x, double y, int n) { if (n > 1) { PennDraw.circle(x, y, 1.0 / (2 * n)); PennDraw.square(x, 1 - y, 1.0 / ( 3 * n)); michael(x * 2, y + 0.1, n - 1); } } public static void rachel(double x, double y, int n) { if (n > 1) { double r = n * 0.015; if (n % 2 == 1) { PennDraw.circle(x, y, r); } else { PennDraw.square(x, y, r); } rachel(x + r * 2, y, n - 1); rachel(x, y + r * 2, n - 1); } }
The TAs want to play a Halloween prank on the professors. Their plan is to reverse the order of vowels in every word on the lecture slides (e.g. compiler → cempilor, method → mothed). However, this would be tedious to do by hand, so they’re asking you to write functions to help them. For this question, we will assume all inputs are lowercase and define vowels as only ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’. We will break this problem down into smaller tasks and write a function for each one: