IMPORTANT: Your Code Must Be Legible. If I Can Barely Read It, I Ll Assume the Worst!

Total Page:16

File Type:pdf, Size:1020Kb

IMPORTANT: Your Code Must Be Legible. If I Can Barely Read It, I Ll Assume the Worst!

Name: ______

IMPORTANT: Your code must be legible. If I can barely read it, I’ll assume the worst!

If you need extra space for any question, you may use the last page of the exam.

For all questions on this exam, you do NOT need to declare a class or create a main method. You only need to write the code asked in the question.

(10 points) Trace this program step by step. When you get to a commented line that says // FILL BOX NUMBER go to the corresponding box on the next page and write down the values of the variables at that point in the program. In other words, imagine that the program has been paused at that exact moment. Be sure to fill in ALL values in all the boxes. If a variable does not have a value, write the word “null”. You may want to look at a precedence chart – there will be one in any Java text. public class QuestionOne { public static void main(String[] args) { int num1 = 7, num2 = 2, num3 = 13, value; double real1 = 1.5, real2 = 6.0; boolean answer = true;

// FILL BOX NUMBER 1

value = num1 + num3 % num2;

// Now FILL BOX NUMBER 2

num1 = (int) (real1 + real2 / (num2++) );

// Now FILL BOX NUMBER 3

answer = (real1 > real2) && (num3-- == num2);

// Now FILL BOX NUMBER 4 } } 2 num1 = num1 = num2 = num2 = num3 = num3 = value = value = real1 = real1 = real2 = real2 = answer = answer =

BOX 1 BOX 2

num1 = num1 = num2 = num2 = num3 = num3 = value = value = real1 = real1 = real2 = real2 = answer = answer =

BOX 3 BOX 4

3 (6 POINTS) What is the output of this program for each of the inputs found in the box at the bottom of the page? Be precise! import java.util.Scanner; public class QuestionThree { public static void main(String[] args) { Scanner console = new Scanner(System.in); int num = 21; System.out.print("Please enter an integer <= 21: "); int bound = console.nextInt(); while (num >= bound) { System.out.println(num); num = num - 3; } System.out.println(" Done! "); } }

Input Output

5

10

22

4 For ALL of the following questions (until stated otherwise), assume that a scanner object called ‘console’ has been created for you.

(5 points) Write some code that prompts the user for their name and stores it in a variable. If their name begins A, E, I, O, U output, “Wow, your name begins with a vowel!”. (Admittedly not a particularly useful program….)

5 Where’s Waldo’s Errors? (10 points) Following is a class which contains at least 6 errors. You must find 5 of them worth 2 points each. Note, you don’t need to pay attention to what the program does – it is basically meaningless. public class Q2 { public static void main(String[] args) { // Performs numerical calculations and prints some // information int num1; char firstLetter = Q;

num1 = 3; num2 = 10;

System.out.println("The first number is " + num1 + " and the second number is " + "num2");

while (num2 >= 0) System.out.println(num2); num2 = num2 - 1;

// Performs some operation on a user entered string and // prints the results String word;

Scanner console = new Scanner(System.in); System.out.print("Please enter a word: "); console.next();

if (word.length() > 0); { System.out.println("You entered " + word); } else { System.out.println("No word was entered."); } } }

6 (6 points) Write code that will prompt the user for a yes/no answer as a single character (Y or N or y or n) and store it in a variable called ‘answer’. Your code must read in the value from the user using the next() method from the Scanner class. Recall that the next() method reads in an entire string. (Hint: There is a way to look at only the first character of a string.)

The first couple of lines are provided for you. Scanner input = new Scanner(System.in); char answer; System.out.print(“Are you feeling lucky? ”);

For all of the following questions on methods, you do NOT have to write comments.

(8 points) Write a method called isEven that accepts one argument of type int. If the number is even, the method should return true. Otherwise it should return false.

7 (8 points) Write a method called getRandomNumber that will accept one int. The method will return a random number between 1 and that number. Eg: getRandomNumber(23); will return an integer between 1 and 23. As a reminder, here is the code to generate a random number between 1 and 10: (int) (Math.random() * 10) + 1;

(8 points) Write a method called outputNums that accepts one argument of type int and outputs numbers from 0 to that number in increments of 5. If the argument is a negative number, the method should output the words: “Nothing to print here.” Sample output: outputNums(21); would output 0 5 10 15 20 outputNums(-3); would output: Nothing to print here.

8 (8 points) Write a method called isPositive that accepts one argument of type int. The method should return true if the argument is greater or equal to 0, and return false otherwise. Then, overload the method to accept one argument of type double. The method should do the same thing as the version for int.

9 Bonus (4 points): Write a method called hasAnE that accepts one argument of type String. If there is an ‘e’ present in the string (upper or lower case), the method will return true. Otherwise it should return false.

10

Recommended publications