q3soln - CS 302 Fall 2006 Quiz 3 (10 points) This quiz is...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 302 Fall 2006 Quiz 3 (10 points) This quiz is due no later than noon on Friday, November 3. You may turn the quiz in in—class on Thursday, November 2 or drop it off in Rebecca Hasti’s mailbox on the 5th floor of the CS building no later than noon on Friday, November 3. You may use your lecture notes, the textbook, the CS 302 website, CodeLab, and Eclipse to answer these questions. You may not get help on this quiz from any other person except a CS 302 lab instructor or lecturer. Solution Answers must be hand-written. Show all your work on the quiz. Multiple Choice [2 questions, 2 points each, 4 total points] For each question circle the one best answer after reading all of the choices. Note: no partial credit is given for multiple choice questions. 1) Given the following method: public boolean mystery(int arr, int n) boolean ans = true; for (int k = arr.length ' 1; if (arr[k] <= n) ans = ans && false; else ans = ans && true; } return ans; } k >= 0; { kr“) { Which of the following best describes what the method mystery does? Answer: B. It returns true if every value in the array is greater than n. 2) What is the output for the following lines of code? int k = O; for (int i = 1; 1 < 11; 1 += ) { int j = 2 * i; while (j >= i) { j — 3; k++, System.out.println(k); Answer: D. 12 Continues on back Code, twee, ’Vo determine. answer: “use... \< 1 a o —l l 3 g) 2 O 3 c To .J 7 H -r. a 5 7 N n 6 <5 7 5 s \‘6 q \5 at n. to a H 6 11 Written Answer [I question, 6’ points] 3) Complete the AverageInput program. This program repeatedly prompts a user to enter a positive integer value. When the user enters a zero (0), the average of all the positive values entered by the user is displayed and the program ends. If the user enters a negative integer value, an "Invalid response" message is printed and that negative value is ignored. You may assume that the user enters an integer when prompted for an integer. A sample run of the program is shown at the bottom of the page. import java.util.Scanner; public class Averagelnput { public static void main(String args) { Scanner in = new Scanner<System.in); int numValues = 0; int sum = O; boolean again = true; do { System.out.print("Enter a positive integer "); System.out.print("or O to quit: "); int value = in.nextlnt(); if (value > O) { sum += value; numValues++; } else if (value < O) System.out.println("lnvalid response"); else // value == again = false; } while (again); if (numValues > O) { double avg = (double)sum / numValues; System.out.println("Average is " + avg); } else System.out.println("No values entered"); Sample run: (what the user enters is in bold) Enter a positive integer or 0 to quit: 14 Enter a positive integer or 0 to quit: 2 Enter a positive integer or 0 to quit: -6 Invahdresponse Enter a positive integer or O to quit: 7 Enter a positive integer or 0 to quit: 3 Enter a positive integer or 0 to quit: 0 Average is 6.5 ...
View Full Document

Page1 / 2

q3soln - CS 302 Fall 2006 Quiz 3 (10 points) This quiz is...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online