[PDF] [PDF] CSE 142 Final Exam - Washington

____-2_______________________ column what values would be stored in the array after the method arrayMystery executes if the array in the left-hand column is passed as its parameter int[] a = {40}; // an array with just one element



Previous PDF Next PDF





[PDF] FACULTY OF MANAGEMENT DECEMBER - CORE

The ____ method can find the array position of a requested value in an array that is To use the Sort() method, you pass the array name to ____ a List Sort() c



[PDF] Test 1

____ 10 As parameters to functions, class objects can be passed only by to store the length of the list, and a variable to store the size of the array In the function ____, the growth rate is quadrupled when the problem size is doubled a



[PDF] http://17216220/printqpphp?heading=III BCA - MG University

______ is passed to a method by use of call-by-reference A variables B objects ______ statement is valid for array declaration A int number (); One interface can inherit another by use of the keyword _____ A public B extends



[PDF] parameter

20 jan 2016 · Additional information can be passed to some methods within the int[ ] nums = new int[20]; //an array of ____ ______ boolean[ ] truths = new 



[PDF] CSE 142 Final Exam - Washington

____-2_______________________ column what values would be stored in the array after the method arrayMystery executes if the array in the left-hand column is passed as its parameter int[] a = {40}; // an array with just one element



[PDF] Chapter 4: Methods

When a method is invoked, you pass a value to the parameter This value is the string[ ] type String [ ] indicates that the parameter is an array of String



[PDF] Two-Dimensional Arrays

dimensional arrays, a single line of elements When you write a method that has a 2D array as a parameter, how do row++) { sum += rating[_____][____]; }



[PDF] ExamView - JavaExamSpring2018tst - Mr Whites Class Pages

____ 2 The elements of the Grade array may store grades of what type? is a correct way to pass a double array called dd to a method called maxVert? a



[PDF] ____ 3 What is actually printed with the following code? Systemout

D String s; B String s[]; E None of these C Both A and B ____ 4 Which of the following is a correct way to pass a double array called dd to a method called



[PDF] object oriented programming with c++

Variables that are declared inside a function are called as ______ A local B global c) arrays d) variables 20 The ______ access specifies allows functions or data to be accessible to other parts of the program C++ begins its execution with ____ a) Base Class b) Derived Class c) Message Passing d) Inheritance 2

[PDF] arrays are quizlet

[PDF] arrays can store references to the objects of a class.

[PDF] arrays in c

[PDF] arrays in data structure notes

[PDF] arrays in math

[PDF] arrays in python

[PDF] arrays in sas

[PDF] arrays in sas listen data

[PDF] arrays java

[PDF] arrays.aslist java

[PDF] arrays.sort

[PDF] arrays.sort java

[PDF] arrc sofr

[PDF] arrêt cour de cassation 4 décembre 2013

[PDF] arret cour de cassation 4 janvier 2005

1 of 9

1. Expressions (5 points)

For each expression at left, indicate its value in the right column. List a value of appropriate type and capitalization.

e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean.

Expression Value

3 * 4 + 5 * 6

23 % 5 + - 17 % (16 % 10)

"1" + 2 + 3 * 4 + (5 + 6)

1.5 * 2 + 20 / 3 / 4.0 + 6 / 4

345 / 10 / 3 + 10 / (5 / 2.0)

____42_______________________ ____-2_______________________ ____"121211"_________________ ____5.5______________________ ____15.0_____________________

2 of 9

2. Array Mystery (10 points)

Consider the following method:

public static void arrayMystery(int[] a) { for (int i = 1; i < a.length - 1; i++) { a[i] = a[i - 1] - a[i] + a[i + 1];

Indicate in the right-hand column what values would be stored in the array after the method arrayMystery executes

if the array in the left-hand column is passed as its parameter. Original Contents of Array Final Contents of Array int[] a1 = {42, 42}; arrayMystery(a1); int[] a2 = {6, 2, 4}; arrayMystery(a2); int[] a3 = {7, 7, 3, 8, 2}; arrayMystery(a3); int[] a4 = {4, 2, 3, 1, 2, 5}; arrayMystery(a4); int[] a5 = {6, 0, -1, 3, 5, 0, -3}; arrayMystery(a5); ____{42, 42}_________________________ ____{6, 8, 4}________________________ ____{7, 3, 8, 2, 2}__________________ ____{4, 5, 3, 4, 7, 5}_______________ ____{6, 5, 9, 11, 6, 3, -3}__________

3 of 9

3. Reference Semantics Mystery (9 points)

The following program produces 4 lines of output. Write the output below, as it would appear on the console.

import java.util.*; // for Arrays class public class Rectangle { int w; int h; public Rectangle(int width, int height) { w = width; h = height; public String toString() { return "w: " + w + ", h: " + h; public class ReferenceMystery { public static void main(String[] args) { int n = 20; int[] a = {40}; // an array with just one element

Rectangle r = new Rectangle(50, 10);

mystery(n, a, r); System.out.println(n + " " + Arrays.toString(a) + " " + r); a[0]++; r.w++; mystery(n, a, r); System.out.println(n + " " + Arrays.toString(a) + " " + r); public static int mystery(int n, int[] a, Rectangle r) { n++; a[0]++; r.h++; System.out.println(n + " " + Arrays.toString(a) + " " + r); return n;

Solution:

21 [41] w: 50, h: 11

20 [41] w: 50, h: 11

21 [43] w: 51, h: 12

20 [43] w: 51, h: 12

4 of 9

4. Inheritance Mystery (12 points)

Assume that the following four classes have been defined: public class Gala extends Apple { public void method1() {

System.out.print("gala 1 ");

public String toString() { return "gala " + super.toString(); public class Fruit { public void method1() {

System.out.print("fruit 1 ");

public void method2() {

System.out.print("fruit 2 ");

public String toString() { return "fruit"; public class Fuji extends Apple { public void method1() {

System.out.print("fuji 1 ");

public class Apple extends Fruit { public void method2() { method1();

System.out.print("apple 2 ");

public String toString() { return "apple"; Given the classes above, what output is produced by the following code? Fruit[] elements = {new Gala(), new Fruit(), new Fuji(), new Apple()}; for (int i = 0; i < elements.length; i++) { elements[i].method1();

System.out.println();

elements[i].method2();

System.out.println();

System.out.println(elements[i]);

System.out.println();

Solution:

gala 1 gala 1 apple 2 gala apple fruit 1 fruit 2 fruit fuji 1 fuji 1 apple 2 apple fruit 1 fruit 1 apple 2 apple

5 of 9

5. File Processing (12 points)

Write a static method named computeGrade that accepts as its parameter a Scanner for an input file whose data

represents a student's grades for tests and assignments. Your method should compute the student's overall grade

percentage from the total points earned for all tests and assignments versus the total points possible for all tests and

assignments. The input consists a series of one or more score records. Each score record consists of three tokens,

where the first is the name of the assignment or test, the second is the number of points the student earned and the

third is the number of points possible. For example, if the input file contains the following text: homework1 40 57 test1 78 100 test2 67 80

Your method would produce the following output. Notice that the grade percentage is truncated to an integer

(truncated not rounded). homework1: 40/57 test1: 78/100 test2: 67/80 grade: 78%

The 78% comes from adding (40 + 78 + 67) and dividing by (57 + 100 + 80). Unlike in your assignment 4, you

should not cap the percentage.

Here is a second example. Suppose the input file contains the following text. Notice the capitalization and spacing:

HomeWork1 10 20 HOMEWORK2 30 30

HoMeWoRk19

10 10 exam 100 100

Then your method would produce the following output. All text output should be in lowercase. homework1: 10/20 homework2: 30/30 homework19: 10/10 exam: 100/100 grade: 93% honor roll

"honor roll" should be output if the student has earned 90% or higher. You may assume that the file contains at

least one score record (a set of 3 tokens). You may also assume that the input is valid; that the input has sets of 3

tokens and that the second two are always integers.

One possible solution:

public static void computeGrade(Scanner input) { int totalPoints = 0; int totalEarned = 0; while(input.hasNext()) {

String name = input.next();

int score = input.nextInt(); int total = input.nextInt(); System.out.println(name.toLowerCase() + ": " + score + "/" + total); totalEarned += score; totalPoints += total;

System.out.println();

double grade = totalEarned * 100.0 / totalPoints;

System.out.println("grade: " + (int)grade + "%");

if(grade >= 90) {

System.out.println("honor roll");

6 of 9

6. File Processing (13 points)

Write a static method named goodBooks that accepts as its parameters a String containing an input file name and a

real number d. Your method should output information about books that have a rating of d or higher.

The input file is comprised of a series of lines each containing information about a different book. The lines are in the

format . For example, suppose the input file contains the following text: <h4>1900 6.4 The Wizard of Oz</h4> <h4>1969 7.2 The Hungry Caterpillar</h4> <h4>1981 8.0 The Hitchhiker's Guide to the Galaxy</h4> <h4>1947 4.1 Goodnight Moon</h4> <h4>1595 6.1 The Comedy of Errors</h4> <h4>2013 9.9 Building Java Programs</h4> <h4>2012 6.2 Collected Poems</h4> <p>When passed the above file and a d of 6.2, your method would produce the following output. Notice that your</p> method must place just one space between each word in the title. The Wizard of Oz, 113 years old The Hungry Caterpillar, 44 years old The Hitchhiker's Guide to the Galaxy, 32 years old Building Java Programs, 0 years old Collected Poems, 1 years old <p>Notice that the output contains the age of the book, not its publication date. You should calculate the date by</p> determining the difference between the publication date and the current year, 2013. <p>If your method is passed a d higher than the ratings of all books in the passed in file it should output the following:</p> <h3>No books found.</h3> <h3>One possible solution:</h3> public static void goodBooks(String fileName, double rating) throws <h3>FileNotFoundException {</h3> <h3>Scanner input = new Scanner(new File(fileName));</h3> boolean results = false; while(input.hasNextLine()) { <h3>String line = input.nextLine();</h3> <h3>Scanner lineScan = new Scanner(line);</h3> int year = lineScan.nextInt(); double currentRating = lineScan.nextDouble(); <h3>String name = "";</h3> while (lineScan.hasNext()) { name += " " + lineScan.next(); if(currentRating >= rating) { results = true; System.out.println(name.toUpperCase() + ", " + (2013 - year) + " years old"); if (!results) { <h3>System.out.println("No books found");</h3> <h4>7 of 9</h4> <h4>7. Array Programming (10 points)</h4> <p>Write a static method named isConsecutive that accepts an array of ints as a parameter and returns true if the list</p> <p>of integers contains a sequence of increasing consecutive integers and returns false otherwise. Consecutive integers</p> are integers that come one after the other, as in 5, 6, 7, 8, 9, etc. For example, if a variable called list1 stores the following values: [16, 17, 18, 19] A call on isConsecutive (list1) should return true. If instead list1 stored the following sequence of values: [16, 17, 18, 19, 20, 19] A call on isConsecutive (list1) should return false. An array of fewer than two elements is considered to be consecutive. <h3>One possible solution:</h3> public static boolean isConsecutive(int[] a) { for(int i = 0; i < a.length - 1; i++) { if(a[i] != a[i + 1] - 1) { return false; return true; <h4>8 of 9</h4> <h4>8. Critters (14 points)</h4> <p>Write a critter class HoneyBadger along with its fight, movement and eating behavior. All unspecified aspects of</p> <p>HoneyBadger use the default behavior. Write the complete class with any fields, constructors, etc. necessary to</p> implement the behavior. <p>HoneyBadgers just don't care. They are always hungry and so eat whenever they come across food. In a fight they</p> <p>are vicious and attack by choosing randomly with equal probability between pouncing and scratching on each turn.</p> <p>When a HoneyBadger is created it gets passed an initial amount of steps. It always starts moving by going North for</p> <h4>1 step and then moves (steps 1) times West. A HoneyBadger restarts its movement pattern every time it eats.</h4> <h3>HoneyBadger(4) would move in this pattern:</h3> N, W, W, W, N, W, (eats food), N, W, W, W, N, W, W, W, N, W, ... <p>As in assignment 8, all fields must be declared private and fields that need to be initialized to a non-default value must</p> be set in a constructor. <h3>One possible solution:</h3> public class HoneyBadger extends Critter { private int moves; private int steps; public HoneyBadger(int steps) { moves = -1; this.steps = steps; public boolean eat() { moves = -1; return true; public Attack fight(String opponent) { <h3>Random r = new Random();</h3> if(r.nextInt(2) == 0) { return Attack.Pounce; } else { return Attack.Scratch; public Direction getMove() { moves++; if (moves % steps == 0) { return Direction.North; } else { return Direction.West; <h4>9 of 9</h4> <h4>9. Array Programming (15 points)</h4> <p>Write a static method named distributeCount that accepts an array of integers and an integer n as parameters.</p> <p>Each array element stores a number of tokens for a game. The tokens at index n are to be distributed to the other array</p> positions one at a time starting with the index n + 1. For example, if a variable called list stores the following values: [1, 2, 3, 4, 5] Then after the call distributeCount(list, 1), list should store: [1, 0, 4, 5, 5] Notice that the 2 tokens that were at index 1 have been distributed to indexes 2 and 3. <p>In distributing tokens, you may reach the end of the array. In this case, you should move to the beginning of the array</p> <p>(index 0) and continue distributing tokens. For example, if list instead stored this sequence of values:</p> [1, 7, 3, 4, 5] Then after the call distributeCount(list, 1), list should store: [2, 1, 5, 6, 6] The 7 tokens that were at index 1 have been distributed to indices 2, 3, 4, 0, 1, 2, 3. <p>You may assume that n is a valid index of the array. Do not make any assumptions about the length of the array or the</p> size of values stored in it. <p>You may not use any temporary arrays to help you solve this problem. (But you may declare as many simple</p> <p>variables as you like, such as ints.) You also may not use any other data structures or complex types such as</p> <p>Strings, or other data structures that were not taught in CSE 142 such as the ArrayList class from Chapter 10.</p> <h3>One possible solution:</h3> public static void distributeCount(int[] a, int n) { int count = a[n]; a[n] = 0; for(int i = 0; i < count; i++) { a[(n + 1 + i) % a.length]++;quotesdbs_dbs9.pdfusesText_15 </div> </div> <div class="left"> <br/> <div style="width:100%;min-height:300px !important;padding:2px;"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1570187411384203" crossorigin="anonymous"></script> <!-- Responsive1 --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-1570187411384203" data-ad-slot="2836242943" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div> <!-- The Modal --> <div> <div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <span class="closeA">×</span> if you Get <b>No preview available</b> Click on (Next PDF) <a href="https://pdfprof.com/PDFV2/Documents3/39797/28/6" class="bblinkss"> Next PDF </a> <iframe frameBorder="0" width="100%" height="50" frameBorder="0" src="https://pdfprof.com/PDFV2/ShowLink.php?links=https://courses.cs.washington.edu/courses/cse142/13sp/exams/final/final_13sp_key.pdf" ></iframe> <iframe id="ppif" frameBorder="0" width="100%" src="https://pdfprof.com/PDFV2/getFrameTestV3.php?links=https://courses.cs.washington.edu/courses/cse142/13sp/exams/final/final_13sp_key.pdf&idt=39797&t=28&view=5&url=https://pdfprof.com/PDFV2/Documents3/39797/28/5" style="width:100%;height:90vh;" ></iframe> </div> </div> </div> <style> body {font-family: Arial, Helvetica, sans-serif;} /* The Modal (background) */ .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ padding-top: 10px; /* Location of the box */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ } /* Modal Content */ .modal-content { background-color: #fefefe; margin: auto; padding: 5px; border: 1px solid #888; width: 85%; } /* The Close Button */ .closeA { color: #aaaaaa; float: right; font-size: 28px; font-weight: bold; background:red; padding:1px; } .closeA:hover, .closeA:focus { color: #000; text-decoration: none; cursor: pointer; } </style> <script> // Get the modal var modal = document.getElementById("myModal"); // Get the button that opens the modal var btn = document.getElementById("myBtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("closeA")[0]; // When the user clicks the button, open the modal btn.onclick = function() { modal.style.display = "block"; } // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } </script> </body>