CSC 1301 Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Consider the following method: public static int arrayMystery4(int[] list) { int x = 0; for (int i = 1; i < list.length; i++) { int y = list[0] - list[i]; if (y < x) { x = y; } } return x; } For the array {4, 6, 12}, indicate what value would be returned if the method mystery were called and passed that array as its parameter. Enter a positive or negative integer.

-8

Consider a file called readme.txt that has the following 6 lines: This "89.3" file has input lines in it. 67.3 46.8 are bad grades. Aren't they? And the code that outputs those 6 lines: 1 Scanner input = new Scanner(new File("readme.txt")); 2 int count = 0; 3 while (input.hasNextDouble()) 4 { 5 System.out.println("input: " + input.nextDouble()); 6 count++; 7 } 8 System.out.println(count + " total"); What would be the value of count? Enter an integer. (Be careful)

0

Consider the following code. What range of values can the variable have? Specify ranges with a dash, i.e. "1-10", and separate multiple numbers with commas, i.e. "1,2,3,4,5,etc..." (no spaces between values) Random rand = new Random(); int second = rand.nextInt(5) * 3;

0, 3, 6, 9, 12

Given the following program: public class MysteryReturn { public static void main(String[] args) { int x = 1; int y = 2; int z = 3; z = mystery(x, z, y); // Statement 1 System.out.println(x + " " + y + " " + z); // Statement 2 } public static int mystery(int z, int x, int y) { z--; x = 2 * y + z; y = x - 1; System.out.println(y + " " + z); return x; } } What is the output of Statement 2? 4 2 1 2 1 4 1 2 3 1 2 4

1 2 4

What output is produced by the following program? public class Odds { public static void main(String[] args) { printOdds(3); printOdds(17 / 2); int x = 25; printOdds(37 - x + 1); } public static void printOdds(int n) { for (int i = 1; i <= n; i++) { int odd = 2 * i - 1; System.out.print(odd + " "); } System.out.println(); } } Answers: 1 3 1 3 5 7 9 11 13 1 3 5 7 9 11 13 15 17 19 21 23 1 3 5 1 3 5 7 9 11 13 1 3 5 7 9 11 13 15 17 19 21 23 1 3 5 1 3 5 7 9 11 1 3 5 7 9 11 13 15 17 19 1 3 5 1 3 5 7 9 11 13 15 1 3 5 7 9 11 13 15 17 19 21 23 25

1 3 5 1 3 5 7 9 11 13 15 1 3 5 7 9 11 13 15 17 19 21 23 25

Match the values that would be stored in the array after the following code executes: int[] data = new int[6]; data[0] = 3; data[5] = -8; data[2] = 14; data[4] = 5; data[1] = data[5]; ​ int x = data[4]; data[4] = 6; data[x] = data[0] * data[1]; -24 6 14 3 -8 0

1. data[0] : = 3 2. data[1] = -8 3. data[2] = 14 4. data[3] = 0 5. data[4] = 6 6. data[5] = -24

Given the following program, match each statement with its output. public class MysteryReturn { public static void main(String[] args) { int x = 1; int y = 2; int z = 3; z = mystery(x, y, z); // Statement System.out.println(x + " " + y + " " + z); // Statement 2 x = mystery(y, y, x); // Statement 3 System.out.println(x + " " + y + " " + z); // Statement 4 y = mystery(z, y, y); // Statement 5 System.out.println(x + " " + y + " " + z); // Statement 6 } public static int mystery(int z, int x, int y) { z++; x = 2 * y + z; y = x - 1; System.out.println(y + " " + z); return x; } }

1. 7 2 2. 1 2 8 3. 4 3 4. 5 2 8 5. 12 9 6. 5 13 8

Assuming that the following variables have been declared, String str1 = "Q.E.D."; String str2 = "Arcturan Megadonkey"; String str3 = "Sirius Cybernetics Corporation"; Evaluate the following expressions. Make sure to give a value of the appropriate type (such as including quotes around a String). 1. str2.length() 2. str1.indexOf("D") 3. str2.indexOf("donkey") 4. str3.substring(9, str3.indexOf("e")) 4 "b" 13 19

1. str2.length() : 19 2. str1.indexOf("D") : 4 3. str2.indexOf("donkey") : 13 4. str3.substring(9, str3.indexOf("e")) : "b"

Consider the following method. public static void ifElseMystery1(int x, int y) { int z = 4; if (x >= z) { z = y + 3; } else { z = y - 2; } if (y <= z) { y++; } System.out.println(z + " " + y); } For the call below, indicate what output is produced (watch spacing). ifElseMystery1(13, 8);

11 9

Evaluate the following expression. Make sure to give a value of the appropriate type (such as including a decimal point in a double). Math.sqrt(76 + 45)

11.0

Given the following method: public static void mystery(int x) { int y = 3; while (x % 2 == 1) { y--; x = x / 2; } System.out.println(x + " " + y); } Write the output of the following method calls: mystery(51)

12 1

Consider the following method. public static void ifElseMystery1(int x, int y) { int z = 4; if (z <= x) { z = x + 1; } else { z = z + 9; } if (z <= y) { y++; } System.out.println(z + " " + y); } For the call below, indicate what output is produced (watch spacing). ifElseMystery1(3, 20);

13 21

Consider a file called readme.txt that has the following 6 lines: This "89.3" file has input lines in it. 67.3 46.8 are bad grades. Aren't they? And the code that outputs those 6 lines: 1 Scanner input = new Scanner(new File("readme.txt")); 2 int count = 0; 3 while (input.hasNext()) 4 { 5 System.out.println("input: " + input.next()); 6 count++; 7 } 8 System.out.println(count + " total"); What would be the value of count? Enter an integer.

15

Assuming that the following variables have been declared, // index 012345678901234567890123 String str1 = "Hush and Dottie"; String str2 = "Skyhigh Corporation"; String str3 = "Kelly Temporary Services"; Evaluate the following expressions. Make sure to give a value of the appropriate type (such as including quotes around a String). str1.length() str2.substring(12, 18) str3.indexOf("p") str3.substring(14, str3.indexOf("v")) str1.toLowerCase().substring(0, 4) + " " + str3.toLowerCase().substring(0, 5) Answer for blank # 1: Answer for blank # 2: Answer for blank # 3: Answer for blank # 4: Answer for blank # 5:

15 "oratio" 9 "y Ser" "hush kelly"

What output is produced by the following program? public class MysteryNums { public static void main(String[] args) { int x = 15; sentence(x, 42); int y = x - 5; sentence(y, x + y); } public static void sentence(int num1, int num2) { System.out.println(num1 + " " + num2); } }

15 42 10 25

What will be returned from the following method? public static double addNum() { double a = 8.5 + 9.5; return a; } A) 18 .5 B) 18.0 C) 18 D) 8.0

18.0

Assume values is an int array that is currently filled to capacity, with the following values: 9 4 12 2 6 8 18 What is returned by values[3]?

2

Evaluate the following expressions. Make sure to give a value of the appropriate type (such as including a decimal point in a double). Math.abs(2 + -4)

2

Given the following method: public static int mystery(int x, int y) { while (x != 0 && y != 0) { if (x < y) { y = y - x; } else { x = x - y; } } return x + y; } Write the value returned by the following method call: mystery(6, 10)

2

What output is produced by the following program? public class Evens { public static void main(String[] args) { int x = 12; printOdds(19 - x + 1); } public static void printOdds(int n) { for (int i = 1; i <= n; i++) { int even = 2 * i; System.out.print(even + " "); } System.out.println(); } } Answers: 2 4 6 8 10 12 14 16 18 4 6 8 10 12 14 16 2 4 6 8 10 12 14 2 4 6 8 10 12 14 16

2 4 6 8 10 12 14 16

Match the definition to the name of a useful Method of the Arrays Class: 1. copyOf() 2. equals() 3. fill() 4. sort() 5. toString()

2 : Returns true if the arrays contain the same elements 1: Returns an array with the given size and same elements 3: Sets every element of the array to be the given value 5: Returns a representation of the array as in [37] 4: Rearranges the elements so that they appear in order

Given the following code, match the purpose of each object to it's function. You may assume that files exist and there are no Exceptions. Scanner console = new Scanner(System.in); Scanner data = new Scanner(new File("example.txt")); String line = data.nextLine(); Scanner lineScan = new Scanner(line); PrintStream output = new PrintStream("test.txt"); 1. Reads input from the keyboard 2. Reads input from a file 3. Stores a line of a file as text 4. Reads input from a line of text 5. Writes data to a file

2 : data 5 : output 4 : lineScan 3 : line 1 : console

Given the following: double grade = 2.7; Math.round(grade); grade = Math.round(grade); double min = Math.min(grade, Math.floor(2.9)); What is the value of min after the last statement?

2.0

What is the value of numbers[5] after the following code is executed? int[] numbers = {14, 32, 28, 5, 8, 23}; for (int i = 0; i < numbers.length-1; i++) { numbers[i] = numbers[i + 1]; }

23

Given the following method: public static void mystery(int x) { int y = 3; int z = -4; while (2 * y <= x) { y = y * 2; z = z + 2; } System.out.println(y + " " + z); } Write the output of the following method call: mystery(24)

24 2

How many times will the while loop execute? Type the answer as an integer, "infinity" or "unknown". int x = 3; while (x < 370) { System.out.print(x + " "); x *= x; }

3

Consider the following code. What range of values can the variable have? Specify ranges with a dash, i.e. "1-10", and separate multiple numbers with commas, i.e. "1,2,3,4,5,etc..." Random rand = new Random(); int first = rand.nextInt(10) + 30;

30-39

Evaluate the following expressions. Make sure to give a value of the appropriate type (such as including a decimal point in a double). 27 + Math.abs(-14) - Math.pow(3, 2) + 7

39.0

Match the following declared objects to their correct initialization. 1. PrintStream output 2. Scanner console 3. Scanner input 4. Scanner lineScan

4 = new Scanner(line); 3 = new Scanner(new File("message.txt"); 2 = new Scanner(System.in); 1 = new PrintStream(new File("message.txt");

What is Math.ceil(3.6)? A) 3.0 B) 3 C) 4.0 D) 5.0

4.0

For the following do/while loop, how many times will the loop execute its body? Remember that "zero," "infinity," and "unknown" are legal answers. int x = 50; do { System.out.println(x / 10); x = x / 2; } while (x > 0);

6

The following program contains a number of errors. public class Oops6 { public static void main(String[] args) { Scanner in = new Scanner("example.txt"); countWords(in); } // Counts total lines and words in the input scanner. public static void countWords(Scanner input) { Scanner input = new Scanner("example.txt"); int lineCount = 0; int wordCount = 0; while (input.nextLine()) { String line = input.line(); // read one line lineCount++; while (line.next()) { // tokens in line String word = line.hasNext; wordCount++; } } System.out.println("Lines: " + lineCount); System.out.println("Words: " + wordCount); } } The corrected version of the program should produce the following output: Lines: 5 Words: 21 When it is run on the following input file, example.txt: hello how are you 1 2 3 4 I am fine This line has a large number of words on it How many errors are in the program? Please enter a whole number between 0 and 10.

6

Consider the following method. public static void ifElseMystery1(int x, int y) { int z = 4; if (z <= x) { z = x + 1; } else { z = z + 9; } if (z <= y) { y++; } System.out.println(z + " " + y); } For the call below, indicate what output is produced (watch spacing). ifElseMystery1(5, 5);

6 5

Assume values is an int array that is currently filled to capacity, with the following values: 9 4 12 2 6 8 18 What is the value of values.length?

7

Consider the following method. public static void ifElseMystery1(int x, int y) { int z = 15; if (z > x) { z = x + 1; } else { z = z + 9; } if (z < y) { y++; } System.out.println(z + " " + y); } What output is produced by the following method call? (watch spacing). ifElseMystery1(7, 9);

8 10

What output is produced by the following program? public class MysteryNums2 { public static void main(String[] args) { int x = 12; int y = x - 3; sentence(y, x + y); } public static void sentence(int num1, int num2) { System.out.println(num1 + " " + num2); } }

9 21

An array of 1000 integers has been created. What is the largest integer that can be used as an index to the array?

999

What best describes and array traversal? A sequential processing of each of an array's elements Changing the elements into the opposite order. Constructing a new empty array. All of the above

A sequential processing of each of an array's elements

Consider a file, words.txt, that contains one line, 152 Hello There how is your 1st day? What would be stored in rest after the following code is run on words.txt? Scanner input = new Scanner(new File("words.txt")); String line = input.nextLine(); Scanner lineScan = new Scanner(line); int number = lineScan.nextInt(); String first = lineScan.next(); String second = lineScan.next(); String rest = lineScan.nextLine(); A) how is your 1st day? B) how C) nothing will be stored in rest because there isn't another line in the file D) 152 Hello There how is your 1st day?

A) how is your 1st day?

To print out the values in an array called data, use: A) Arrays.toString(data) B) data.toString() C) Arrays.println(data) D) System.out.println(data)

A) Arrays.toString(data)

Given the following code, complete the /*FINISH ME*/ to find the first initial of the given name. public static String lastFirst(String name) { int space = name.indexOf(" "); String lastName = name.substring(space + 1); /*FINISH ME*/ .... A) String firstInitial = name.substring(0, 1); B) String firstInitial = name.substring(1); C) String firstInitial = name.substring(1, 2); D) String firstInitial = name.substring(0, 2);

A) String firstInitial = name.substring(0, 1);

Which of the following is a legal way to declare and instantiate an array of 10 Strings? A) String[ ] s = new String[10]; B) String s = new String(10); C) String[ ] s = new String; D) String s = new String[10]; E) String[10] s = new String;

A) String[ ] s = new String[10];

What is wrong with the following code? Correct the bugs to produce the following expected output: first = [3, 7] second = [3, 7] int[] first = new int[2]; first[0] = 3; first[1] = 7; int[] second = new int[2]; second[0] = 3; second[1] = 7; System.out.println("first = " + first); System.out.println("second = " + second); A) System.out.println("first = " + Arrays.toString(first)); System.out.println("second = " + Arrays.toString(second)); B) System.out.println("first = " , Arrays.toString(first)); System.out.println("second = " , Arrays.toString(second)); C) System.out.println("first = " + Arrays.first.toString); System.out.println("second = " + Arrays.second.toString); D) System.out.println("first = " + first.toString()); System.out.println("second = " + second.toString());

A) System.out.println("first = " + Arrays.toString(first)); System.out.println("second = " + Arrays.toString(second));

What is wrong with the following code given the two array declarations? int[] first = new int[2]; int[] second = new int[2]; //code here if (first == second) { //code here } else { //code here } A) You cannot compare arrays using the == operator B) The [] braces should be after first, second C) The values in the arrays are not equal D) The initialization of the arrays is incorrect

A) You cannot compare arrays using the == operator

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines of text and an int variable named count, write the code necessary to count the lines of text in the input source and place that value into count. A) count = 0; while (input.hasNext()) { String s = input.nextLine(); count++; } B) count = 0; while (input.hasNext()) { String s = input.next(); count++; } C) count = 0; while (input.hasNextInt()) { String s = input.nextLine(); count++; } D) count = 0; if (input.hasNext()) { String s = input.nextLine(); count++; }

A) count = 0; while (input.hasNext()) { String s = input.nextLine(); count++; }

Which of the following choices is the correct syntax for declaring/initializing an array of eight doubles? A) double[] array = new double[8]; B) double[8] array; C) double array[8] = new double[]; D) double[8] array = new double[];

A) double[] array = new double[8];

The values stored in an array are called ________. A) elements B) nodes C) indexes D) units

A) elements

Given the following code: Scanner console = new Scanner(System.in); System.out.print("How many numbers? "); int count = console.nextInt(); int product = 1; Which for loop would be correct to produce the following output? How many numbers? 4 Next number --> 7 Next number --> 2 Next number --> 3 Next number --> 15 Product = 630 A) for (int i = 1; i <= count; i++) { System.out.print("Next number --> "); int num = console.nextInt(); product *= num; } System.out.println("Product = " + product); B) for (int i = 0; i <= count; i++) { System.out.print("Next number --> "); int num = console.nextInt(); product *= num; } System.out.println("Product = " + product); C) for (int i = 1; i < count; i++) { System.out.print("Next number --> "); int num = console.nextInt(); product *= num; } System.out.println("Product = " + product);

A) for (int i = 1; i <= count; i++) { System.out.print("Next number --> "); int num = console.nextInt(); product *= num; } System.out.println("Product = " + product);

Given input for a number from a user, how can we determine if the number is even? A) if (number % 2 == 0) B) if (number / 2 == 0) C) if (number == "even") D) if (number.equals(0))

A) if (number % 2 == 0)

Write an if/else statement that compares the variable age with 65, adds 1 to the variable seniorCitizens if age is greater than or equal to 65, and adds 1 to the variable nonSeniors otherwise. A) if(age >= 65) { seniorCitizens = seniorCitizens + 1; } else { nonSeniors = nonSeniors + 1; } B) if(age > 65) { seniorCitizens = seniorCitizens + 1; } else { nonSeniors = nonSeniors + 1; } C) if(age <= 65) { seniorCitizens = seniorCitizens + 1; } else { nonSeniors = nonSeniors + 1; } D) if(age >= 65) { nonSeniors = nonSeniors + 1; } else { seniorCitizens = seniorCitizens + 1; }

A) if(age >= 65) { seniorCitizens = seniorCitizens + 1; } else { nonSeniors = nonSeniors + 1; }

Write code that creates an array of integers named data of size 5 with the following contents: [13, -2, 14, 27, 51] A) int[] data = {13, -2, 14, 27, 51} B) int data[] = {13, -2, 14, 27, 51} C) String[] data = {13, -2, 14, 27, 51} D) data[] = {13, -2, 14, 27, 51}

A) int[] data = {13, -2, 14, 27, 51}

In Java, arrays are passed as parameters by: A) reference B) value C) Strings D) static

A) reference

Assume that an int variable age has been declared and already given a value. Assume further that the user has just been presented with the following menu: S: steak, red potatoes, broccoli T: ahi tuna, wild rice, asparagus B: hamburger, fries, cole slaw Write some code that reads the String (S or T or B) that the user types in into a String variable choice that has already been declared and prints out a recommended accompanying drink as follows: if the value of age is 21 or lower, the recommendation is "sparkling water" for steak, "tea" for tuna, and "soda" for the burger. Otherwise, the recommendations are "merlot", "chardonnay", and "beer" for steak, tuna, and burger respectively. Regardless of the value of age, your code should print "invalid menu selection" if the character read into choice was not S or T or B. A) choice = console.next(); if (choice.equals("S")) { if (age <= 21) { System.out.println ("sparkling water"); } else { System.out.println ("merlot"); } } else if (choice.equals("T")){ if (age <= 21) { System.out.println ("tea"); } else { System.out.println ("chardonnay"); } } else if (choice.equals("B")){ if (age <= 21) { System.out.println ("soda"); } else { System.out.println ("beer"); } } else { System.out.println ("invalid menu selection"); } B) choice = console.next(); if (choice == "S") { if (age <= 21) { System.out.println ("sparkling water"); } else { System.out.println ("merlot"); } } else if (choice == "T"){ if (age <= 21) { System.out.println ("tea"); } else { System.out.println ("chardonnay"); } } else if (choice == "B"){ if (age <= 21) { System.out.println ("soda"); } else { System.out.println ("beer"); } } else { System.out.println ("invalid menu selection"); } C) choice = console.next(); if (choice.equals("S")) { if (age <= 21) { System.out.println ("sparkling water"); } else { System.out.println ("merlot"); } } else (choice.equals("T")){ if (age <= 21) { System.out.println ("tea"); } else { System.out.println ("chardonnay"); } } else (choice.equals("B")){ if (age <= 21) { System.out.println ("soda"); } else { System.out.println ("beer"); } } else { System.out.println ("invalid menu selection"); } D) choice = console.next(); if (choice.equals("S")) { if (age < 21) { System.out.println ("sparkling water"); } else { System.out.println ("merlot"); } } else if (choice.equals("T")){ if (age < 21) { System.out.println ("tea"); } else { System.out.println ("chardonnay"); } } else if (choice.equals("B")){ if (age < 21) { System.out.println ("soda"); } else { System.out.println ("beer"); } } else { System.out.println ("invalid menu selection"); }

A) choice = console.next(); if (choice.equals("S")) { if (age <= 21) { System.out.println ("sparkling water"); } else { System.out.println ("merlot"); } } else if (choice.equals("T")){ if (age <= 21) { System.out.println ("tea"); } else { System.out.println ("chardonnay"); } } else if (choice.equals("B")){ if (age <= 21) { System.out.println ("soda"); } else { System.out.println ("beer"); } } else { System.out.println ("invalid menu selection"); }

Consider a file called readme.txt that has the following contents: 15 Hello there it is a pleasure to meet you. -3 18 24 57 word What would be the output from the following code when it is run on the readme.txt file? Scanner input = new Scanner(new File("readme.txt")); int count = 0; while (input.hasNextInt()) { int next = input.nextInt(); System.out.println("input: " + next); count++; } System.out.println(count + "total"); A) input: 15 1 total B) input: 15 5 total C) 0 total D) input: -3 1 total

A) input: 15 1 total

Given this line of input, what tokens does a Scanner break the line apart into? welcome...to the matrix. A) "welcome...to", "the", "matrix." B) "welcome", "...", "to", "the", "matrix." C) "welcome", "...to", "the", "matrix." D) "welcome...", "to", "the", "matrix."

A) "welcome...to", "the", "matrix."

What will be printed when the following code is executed? double value = 45678.259; System.out.printf("%.2f", value); A) 45,678.26 B) 45,678.3 C) 45678.259

A) 45,678.26

The line of input below has how many tokens? hello howare "you" today? I-am-well. A) 5 B) 7 C) 8 D) 4

A) 5

A value that appears in parentheses in a method call is a(n) A) Actual Parameter B) Method Variable C) Formal Parameter D) Call Variable

A) Actual Parameter

If we try to read a file that doesn't exist: A) An exception is thrown and the file is not read B) It returns 0 C) A new file is created D) It reads from the console instead

A) An exception is thrown and the file is not read

Which of the following is the correct syntax for creating a File object? A) File file = new File("input.txt") B) File file = File.open("input.txt") C) File file = File("input.txt") D) File file = new File.open("input.txt")

A) File file = new File("input.txt")

A condition that must be true before a method executes is called a: A) Exception B) Precondition C) Tag D) Postcondition

A) Precondition

What does the following code do? importjava.io.*; importjava.util.*; public class Mystery { public static void main(String[]args)throwsFileNotFoundException { Scanner input = new Scanner(new File("Mystery.java")); while(input.hasNextLine()) { System.out.println(input.nextLine()); } } } A) Prints each line of the file to the console B) Prints lines to a file called Mystery.java C) Returns each line of the file to main D) Prints each token on a new line to the console

A) Prints each line of the file to the console

When using the logical operators && and ||, the property that prevents the second operand from being evaluated if the result is obvious from the first operand is called: A) Short-Circuited Evaluation B) Circuit Propery Evaluation C) Logical-Circuited Evaluation D) First-Selected Evaluation

A) Short-Circuited Evaluation

If we output to a file that already exists: A) The file is overwritten B) An error is thrown and the file is not written C) A duplicate file is created D) The output goes to the console instead

A) The file is overwritten

The ________ loop is ideal in situations where you always want the loop to run at least once. A) do-while B) while C) pretest D) for

A) do-while

Given the following declarations, the variable combined will contain: String s1 = "hello"; String s2 = "world"; String combined = s1 + " " + s2 A) hello world B) empty C) helloworld D) hello

A) hello world

The while loop and the for loop are very similar. However, one difference is the __________ of the variable used in the loop test condition(s). A) scope B) name C) value D) length

A) scope

A ________ is a value that signals when the end of a list of values has been reached. A) sentinel B) delimiter C) token D) terminal

A) sentinel

If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal? A) str1.equals(str2) B) str1 == str2 C) str1 && str2 D) str1 + str2

A) str1.equals(str2)

In Java, Cohesion is a desirable quality in which: A) the responsibilities of a method are closely related to each other B) a method is coherent C) each method is related to every other method D) the method of a class are coupled

A) the responsibilities of a method are closely related to each other

A Boolean expression is one that is either: A) true or false B) positive or negative C) x or y D) None of the above

A) true or false

This class provides many methods that make it easier to work with arrays, for example a quick method to print contents of an Array. Utility Interface Helper Arrays

Arrays

Given the following array declaration: double[] temperature = new double[3]; The array is constructed as: This known in Java as: Auto-Initialization Traversal-Initialization Double-Initialization Zero-Initialization

Auto-Initialization

Write a sentinel loop that repeatedly prompts the user to enter a number and, once the number -1 is typed, displays the maximum number that the user entered. Here is a sample dialogue: Type a number (or -1 to stop): 5 Type a number (or -1 to stop): 2 Type a number (or -1 to stop): 17 Type a number (or -1 to stop): 8 Type a number (or -1 to stop): -1 Maximum was 17 Some starter code has been provided for you: Scanner console = new Scanner(System.in); System.out.print("Type a number (or -1 to stop): "); int number = console.nextInt(); int max = number; while (//FINISH ME) { //FINISH ME } System.out.print("Maximum was " + max); A) Scanner console = new Scanner(System.in); System.out.print("Type a number (or -1 to stop): "); int number = console.nextInt(); int max = number; while (number > 0) { if (number > max) { max = number; } System.out.print("Type a number (or -1 to stop): "); number = console.nextInt(); } System.out.print("Maximum was " + max); B) Scanner console = new Scanner(System.in); System.out.print("Type a number (or -1 to stop): "); int number = console.nextInt(); int max = number; while (number != -1) { if (number > max) { max = number; } System.out.print("Type a number (or -1 to stop): "); number = console.nextInt(); } System.out.print("Maximum was " + max); C) Scanner console = new Scanner(System.in); System.out.print("Type a number (or -1 to stop): "); int number = console.nextInt(); int max = number; while (number > -1) { if (number > max) { max = number; } System.out.print("Type a number (or -1 to stop): "); number = console.nextInt(); } System.out.print("Maximum was " + max); D) Scanner console = new Scanner(System.in); System.out.print("Type a number (or -1 to stop): "); int number = console.nextInt(); int max = number; while (number.hasNextInt()) { if (number > max) { max = number; } System.out.print("Type a number (or -1 to stop): "); number = console.nextInt(); } System.out.print("Maximum was " + max);

B) Scanner console = new Scanner(System.in); System.out.print("Type a number (or -1 to stop): "); int number = console.nextInt(); int max = number; while (number != -1) { if (number > max) { max = number; } System.out.print("Type a number (or -1 to stop): "); number = console.nextInt(); } System.out.print("Maximum was " + max);

When you write a method that throws a checked exception, you must: A) have an if statement that throws an exception B) have a throws clause in the method header. C) ensure that the error will occur at least once each time the program is executed. D) have a throws clause in the class header.

B) have a throws clause in the method header.

Write a method called min that accepts an array of integers as a parameter and returns the minimum value in the array. For example, if the array passed stores {12, 7, -1, 25, 3, 9}, your method should return -1. You may assume that the array contains at least one element. Your method should not modify the elements of the array. A) public static int min(int[] array) { int min = array[array.length]; for (int value: array) { if (value < min) { min = value; } } return min; } B) public static int min(int[] array) { int min = array[0]; for (int value: array) { if (value < min) { min = value; } } return min; } C) public static int min(int[] array) { int min = 0; for (int value: array) { if (value > min) { min = value; } } return min; } D) public static int min(int[] array) { int min = array[0]; for (int value: array) { if (value > min) { min = value; } } return min; }

B) public static int min(int[] array) { int min = array[0]; for (int value: array) { if (value < min) { min = value; } } return min; }

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines, write the code necessary to read in every line and print them all out on a single line, separated by a space. There should NOT be a trailing space at the end of the line. A) while (input.hasNextLine()) { System.out.print(" " + input.nextLine()); } System.out.println(); B) while (input.hasNextLine()) { System.out.print(input.nextLine()); if (input.hasNextLine()) { System.out.print(" "); } } System.out.println(); C) while (input.hasNextLine()) { System.out.print(input.nextLine()); if (input.hasNextInt()) { System.out.print(" "); } } System.out.println(); D) while (input.hasNextLine()) { System.out.print(input.nextLine() + " "); } System.out.println();

B) while (input.hasNextLine()) { System.out.print(input.nextLine()); if (input.hasNextLine()) { System.out.print(" "); } } System.out.println();

Given s2 = "how are you?", what is the return value of s2.substring(4, 7)? A) "are " B) "are" C) " are" D) "re "

B) "are"

A method that creates and initializes an object is called: A) A Console B) A Constructor C) A Token D) A method Creater

B) A Constructor

What will be printed when the following code is executed? String name = "Alexandra"; System.out.printf("%12s", name); A) Alexandra right-aligned B) Alexandra right-aligned with 3 spaces in front C) Alexandra left-aligned D) Alexandra left-aligned with 3 spaces in back

B) Alexandra right-aligned with 3 spaces in front

What type of object is used to write output to a file in Java? A) File B) PrintStream C) Scanner D) String

B) PrintStream

This type of operator determines whether a relation holds between the value on the left of an expression and the value on the right of the expression. A) Unary B) Relational C) Logical D) Mathematical

B) Relational

Consider the following code snippet. File hoursFile = new File("hoursWorked.txt"); Your program must read the contents of this file using a Scanner object. Which of the following is the correct syntax for doing this? A) Scanner in = new Scanner("hoursWorked.txt"); B) Scanner in = new Scanner(hoursFile); C) Scanner in = Scanner.open(hoursFile); D) Scanner in = Scanner("hoursWorked.txt");

B) Scanner in = new Scanner(hoursFile);

If you are on a Windows machine and have a file stored in the C:\projects directory, which is the correct way to create a Scanner to read the file numbers.txt located in this directory? A) Scanner input = new Scanner("C:/projects/numbers.txt"); B) Scanner input = new Scanner(new File("C:/projects/numbers.txt"); C) Scanner input = new Scanner(new File("C://projects//numbers.txt"); D) Scanner input = new Scanner(new File("C:\projects\numbers.txt");

B) Scanner input = new Scanner(new File("C:/projects/numbers.txt");

Write a statement that constructs a Scanner object, stored in a variable named input, to read the file input.txt, which exists in the same folder as your program. A) Scanner input = new Scanner(new File(input.txt)); B) Scanner input = new Scanner(new File("input.txt")); C) Scanner input = Scanner(new File("input.txt")); D) Scanner input = new Scanner("input.txt");

B) Scanner input = new Scanner(new File("input.txt"));

A calculator shows that 11/1 divided by 4 = 2.775. But Java reports the result as 2.77500000000000004. This is called: A) a rounding error B) a roundoff error C) a cummulative error D) a algebraic error

B) a roundoff error

What is one of the methods that can be used to test if the file a user types at the console can be read and used in a program? A) isValid() B) exists() C) new file() D) hasFile()

B) exists()

Given the following variable declarations: int x = 25; int y = -3 boolean b = false; The value of !((x > 0) && (y < 0)) is: A) true B) false C) negative D) positive

B) false

Which of the following statements determines whether temp is within the range of 0 through 100, inclusive? A) if (temp > 0 && temp < 100) B) if (temp >= 0 && temp <= 100) C) if (temp > 0 || temp < 100) D) if (temp >= 0 || temp <= 100)

B) if (temp >= 0 && temp <= 100)

When working with the File class, which of the following import statements should you have near the top of your program? A) import javax.swing.*; B) import java.io.*; C) import java.file.*; D) import javac.io.*;

B) import java.io.*;

To specify the location of a value in a sequence of values, such as a String, we use an: A) indicator B) index C) input value D) order

B) index

Given the following declaration: Printstream output = new PrintStream(new File(hello.txt)); How do we print a blank line to the output file? A) PrintStream.println(); B) output.println(); C) System.out.println(); D) output.print();

B) output.println();

In order to handle illegal data that is entered by a user at a prompt, we can use the Scanner class's "hasNext "methods. This will make our code: A) weak B) robust C) strong D) error-proof

B) robust

The following code attempts to examine a String and return whether it contains a given letter. A flag named found is used. However, the Boolean logic is not implemented correctly, so the method does not always return the correct answer. What practice is this code not using? public static boolean contains(String str, char ch) { boolean found = false; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ch) { found = true; } else { found = false; } } return found; } String logic Boolean Loop logic Boolean Zen if/else Zen

Boolean Zen

The code below has one bug. How can we fix the code so it will run correctly? Scanner console = new Scanner(System.in); System.out.print("What is your favorite color? "); String name = console.next(); if (name == "blue") { System.out.println("Mine, too!"); } A) There is no bug in the code B) Change to name = "blue" C) Change to name.equals("blue") D) Add and else statement to catch other colors entered

C) Change to name.equals("blue")

Write a do/while loop that repeatedly prints random numbers between 0 and 500 until a number equal to or above 600 is printed. At least one line of output should always be printed, even if the first random number is above 600. Here is a sample execution: Random number: 235 Random number: 15 Random number: 510 Random number: 147 Random number: 615 A) Scanner console = new Scanner((System.in)); Random rand = new Random(); int num; do { num = rand.nextInt(600); System.out.println("Random number: " + num); } while (num < 500); B) Scanner console = new Scanner((System.in)); Random rand = new Random(); int num; do { num = rand.nextInt(500); System.out.println("Random number: " + num); } while (num <= 600); C) Scanner console = new Scanner((System.in)); Random rand = new Random(); int num; do { num = rand.nextInt(500); System.out.println("Random number: " + num); } while (num < 600); D) Scanner console = new Scanner((System.in)); Random rand = new Random(); int num; do { num = rand.nextInt(500); System.out.println("Random number: " + num); } while (num > 600);

C) Scanner console = new Scanner((System.in)); Random rand = new Random(); int num; do { num = rand.nextInt(500); System.out.println("Random number: " + num); } while (num < 600);

Write a piece of code that reads a shorthand text description of a color and prints the longer equivalent. Acceptable color names are B for Blue, G for Green, and R for Red. If the user types something other than B, G, or R, the program should print an error message. Make your program case-insensitive so that the user can type an uppercase or lowercase letter. Here are two example executions: What color do you want? R You have chosen Red. What color do you want? Bork Unknown color: Bork AB Scanner console = new Scanner(System.in); System.out.print("What color do you want? "); String color = console.next(); if (color.equals("R")) { System.out.print("You have chosen Red."); } else if (color.equals("G")) { System.out.print("You have chosen Green."); } else if (color.equals("B")) { System.out.print("You have chosen Blue."); } else { System.out.print("Unknown color: " + color); } B) Scanner console = new Scanner(System.in); System.out.print("What color do you want? "); String color = console.next(); if (color.equalsIgnoreCase("R")) { System.out.print("You have chosen Red."); } else if (color.equalsIgnoreCase("G")) { System.out.print("You have chosen Green."); } else if (color.equalsIgnoreCase("B")) { System.out.print("You have chosen Blue."); } else if { System.out.print("Unknown color: " + color); } C) Scanner console = new Scanner(System.in); System.out.print("What color do you want? "); String color = console.next(); if (color.equalsIgnoreCase("R")) { System.out.print("You have chosen Red."); } else if (color.equalsIgnoreCase("G")) { System.out.print("You have chosen Green."); } else if (color.equalsIgnoreCase("B")) { System.out.print("You have chosen Blue."); } else { System.out.print("Unknown color: " + color); }

C) Scanner console = new Scanner(System.in); System.out.print("What color do you want? "); String color = console.next(); if (color.equalsIgnoreCase("R")) { System.out.print("You have chosen Red."); } else if (color.equalsIgnoreCase("G")) { System.out.print("You have chosen Green."); } else if (color.equalsIgnoreCase("B")) { System.out.print("You have chosen Blue."); } else { System.out.print("Unknown color: " + color); }

Given the following file, example.txt: hello how are you 1 2 3 4 I am fine What is the output of the following program? public static void printEntireFile() throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Type a file name: "); String fileName = console.nextLine(); Scanner input = new Scanner(new File(fileName)); while (input.hasNextLine()) { System.out.println(input.nextLine()); } } A) hello how are you 1 2 3 4 I am fine B) hello how are you 1 2 3 4 I am fine C) hello how are you 1 2 3 4 I am fine D) no output due to error

C) hello how are you 1 2 3 4 I am fine

Assume that choice is a variable of type String. Write a statement that throws an Exception with a message "null choice" if command is null and that throws an Exception with a message "empty choice" if choice equals the empty string. A) if (choice == null) { throw Exception("null choice"); } else if (choice.equals("")) { throw Exception("empty choice"); } B) if (choice == null) { new Exception("null choice"); } else if (choice.equals("")) { new Exception("empty choice"); } C) if (choice == null) { throw new Exception("null choice"); } else if (choice.equals("")) { throw new Exception("empty choice"); } D) if (choice = null) { throw new Exception("null choice"); } else if (choice.equals("")) { throw new Exception("empty choice"); }

C) if (choice == null) { throw new Exception("null choice"); } else if (choice.equals("")) { throw new Exception("empty choice"); }

Which of the following if statement headers uses the correct syntax? A) if (x => y) { B) if x = 10 then { C) if (x == y) { D) if [x == 10] { E) if (x equals 42) {

C) if (x == y) {

Consider a file called readme.txt that has the following contents: 5 Hello World there are 10 things to do today What would be the output from the following code when it is run on the readme.txt file? Scanner input = new Scanner(new File("readme.txt")); int count = 0; while (input.hasNextLine()) { System.out.println("input: " + input.nextLine()); count++; } System.out.println(count + " total"); A) input: 5 Hello world input: there are input: input: 10 things to do input: input: today 4 total B) input: 5 Hello world input: there are input: input: 10 things to do input: input: today 6 total C) input: 5 Hello world input: there are input: input: 10 things to do input: input: today 6 total D) input: 5 Hello world input: there are input: 10 things to do input: today 4 total

C) input: 5 Hello world input: there are input: input: 10 things to do input: input: today 6 total

Write a complete Java program that prints itself to the console as output. Assume that the program is stored in a file named print.txt, and make your code open the file print.txt and print its contents to the console. A) public class PrintMyself { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("print.txt")); while (input.hasNextLine()) { String line = input.next(); System.out.println(line); } } } B) public class PrintMyself { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("print.txt")); System.out.println(input.nextLine()); } } C) public class PrintMyself { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("print.txt")); while (input.hasNextLine()) { System.out.println(input.nextLine()); } } } D) public class PrintMyself { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("print.txt")); while (input.hasNext()) { System.out.println(input.next()); } } }

C) public class PrintMyself { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("print.txt")); while (input.hasNextLine()) { System.out.println(input.nextLine()); } } }

Write a method named getFileName that repeatedly prompts the user for a file name until the user types the name of a file that exists on the system. Once you get a good file name, return that file name as a String. Here is a sample dialogue from one call to your method, assuming that the file good.txt does exist and the others do not: Type a file name: bad.txt Type a file name: not_here.txt Type a file name: good.txt A) public static String getFileName() { Scanner console = new Scanner(System.in); System.out.print("Type a file name: "); String fileName = console.nextLine(); while (!fileName.exists()) { System.out.print("Type a file name: "); fileName = console.nextLine(); file = new File(fileName); } return fileName; } B) public static String getFileName() { Scanner console = new Scanner(System.in); System.out.print("Type a file name: "); String fileName = console.nextLine(); File file = new File(fileName); while (!file.exists()) { System.out.print("Type a file name: "); fileName = console.nextLine(); } return fileName; } C) public static String getFileName() { Scanner console = new Scanner(System.in); System.out.print("Type a file name: "); String fileName = console.nextLine(); File file = new File(fileName); while (!file.exists()) { System.out.print("Type a file name: "); fileName = console.nextLine(); file = new File(fileName); } return fileName; } D) public static String getFileName() { Scanner console = new Scanner(System.in); System.out.print("Type a file name: "); File fileName = console.nextLine(); while (!file.exists()) { System.out.print("Type a file name: "); fileName = console.nextLine(); file = new File(fileName); } return fileName; }

C) public static String getFileName() { Scanner console = new Scanner(System.in); System.out.print("Type a file name: "); String fileName = console.nextLine(); File file = new File(fileName); while (!file.exists()) { System.out.print("Type a file name: "); fileName = console.nextLine(); file = new File(fileName); } return fileName; }

Write a method named consecutive that accepts three integers as parameters and returns true if they are three consecutive numbers; that is, if the numbers can be arranged into consecutive order. Your method should return false if the integers are not consecutive. Note that order is not significant; your method should return the same result for the same three integers passed in any order. For example, the calls consecutive(1, 2, 3), consecutive(3, 2, 4), and consecutive(-10, -8, -9) would return true. The calls consecutive(3, 5, 7), consecutive(1, 2, 2), and consecutive(7, 7, 9) would return false. A) public static boolean consecutive(int num1, int num2, int num3) { int min = Math.min(num1, num2, num3); int max = Math.max(num1, num2, num3); int mid = (num1 + num2 + num3) / 3; return (min + 1 == mid && mid + 1 == max); } B) public static boolean consecutive(int num1, int num2, int num3) { int min = Math.min(num1, Math.min(num2, num3)); int max = Math.max(num1, Math.max(num2, num3)); int mid = (num1 + num2 + num3) / 3; return (min < mid || mid < max); } C) public static boolean consecutive(int num1, int num2, int num3) { int min = Math.min(num1, Math.min(num2, num3)); int max = Math.max(num1, Math.max(num2, num3)); int mid = (num1 + num2 + num3) / 3; return (min + 1 == mid && mid + 1 == max); } D) public static int consecutive(int num1, int num2, int num3) { int min = Math.min(num1, Math.min(num2, num3)); int max = Math.max(num1, Math.max(num2, num3)); int mid = (num1 + num2 + num3) / 3; return (min - 1 == mid && mid - 1 == max); }

C) public static boolean consecutive(int num1, int num2, int num3) { int min = Math.min(num1, Math.min(num2, num3)); int max = Math.max(num1, Math.max(num2, num3)); int mid = (num1 + num2 + num3) / 3; return (min + 1 == mid && mid + 1 == max); }

The following method, which returns whether the given String starts and ends with the same character, is too verbose. What is the recommended and best way to write this method? public static boolean startEndSame(String str) { if (str.charAt(0) == str.charAt(str.length() - 1)) { return true; } else { return false; } } A) public static boolean startEndSame(String str) { String first = str.charAt(0); String last = str.charAt(str.length() - 1); if (first == last) { return true; } else { return false; } } B) public static boolean startEndSame(String str) { if (str.charAt(0) == str.charAt(str.length() - 1)) { return true; } return false; } C) public static boolean startEndSame(String str) { return (str.charAt(0) == str.charAt(str.length() - 1)); } D) public static boolean startEndSame(String str) { if (str.charAt(0) == str.charAt(str.length() - 1)) { return true; } }

C) public static boolean startEndSame(String str) { return (str.charAt(0) == str.charAt(str.length() - 1)); }

Which one of the following is the not equal operator? A) NOT B) <> C) != D) *&

C) !=

The following input file contains 17 total tokens that could be read by a Scanner. Hello there,how are you? I am "very well", thank you. 12 34 5.67 (8 + 9) "10" What is token 7? A) well", B) am C) "very D) very

C) "very

Which of the following is the correct Boolean expression to test for: int x being a value between, but not including, 500 and 650, OR int y not equal to 1000? A) ((x > 500 AND x < 650) OR !(y.equal(1000))) B) ((x >= 500 && x <= 650) && (y != 1000)) C) ((x > 500 && x < 650) || (y != 1000)) D) ((x < 500 && x > 650) || !(y == 1000))

C) ((x > 500 && x < 650) || (y != 1000))

The following input file contains 17 total tokens that could be read by a Scanner. Hello there,how are you? I am "very well", thank you. 12 34 5.67 (8 + 9) "10" What is token 15? A) (8 B) 5.67 C) + D) 9)

C) +

Consider the following code. What range of values can the variable, b, have? Specify ranges with a dash i.e. "1 - 10" and separate multiple numbers with commas i.e. "1,2,3,4,5,etc..." Random rand = new Random() int b = rand.nextInt(5) * 3; A) 0 - 15 B) 0, 3, 6, 9, 12, 15 C) 0, 3, 6, 9, 12 D) 0 - 12

C) 0, 3, 6, 9, 12

What will be the value of x after the following code is executed? int x = 75; int y = 60; if (x > y) x = x - y; A) 135 B) 60 C) 15 D) 75

C) 15

What is Math.round(3.6)? A) 3.0 B) 3 C) 4 D) 4.0

C) 4

Given that String s1 = "hello"; The method call s1.length() will return: A) 6 B) error C) 5 D) 0

C) 5

An operation in which an overall value is computed incrementally, often using a loop is called a: A) Sequence structure B) a Min/Max algorithm C) Commulative algorithm D) Decision structure

C) Commulative algorithm

Given the following words.txt file: four score and seven years What does the following code do? Scanner data = new Scanner(new File(words.txt)); PrintStream output = new PrintStream(new File(words2.txt)); if (data.hasNext()) { output.print(data.next()); while (data.hasNext()) { output.print(" " + data.next()); } } A) Fixes the spacing by writing each word of the file on their own line B) Writes the words of the file exactly as they are C) Fixes the spacing by writing the words of the file separated by one space. D) Right-aligning the words of the file in the output file

C) Fixes the spacing by writing the words of the file separated by one space.

The practice of processing input line by line is called A) Scanner line processing B) println processing C) Line-based processing D) Linear processing

C) Line-based processing

Assuming that str is declared as follows: String str = "RSTUVWXYZ"; What value will be returned from str.charAt(5)? A) U B) V C) W D) X

C) W

Why does a method to swap two array elements work correctly when a method to swap two integer values does not? A) arrays use more memory than primitives B) integers are objects and are not able to be swapped C) arrays are objects and use reference semantics D) arrays are slower and more inefficient than primitives

C) arrays are objects and use reference semantics

Given the following, int x = 1; the result of the following test is: if(x == 1 || 2 || 3) A) false B) true C) compiler error D) positive

C) compiler error

When a program cannot continue its normal execution, we call this an: A) incompatible type B) omission C) exception D) error

C) exception

The ________ statement is used to make simple decisions in Java. A) for B) do/while C) if D) branch

C) if

Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use to read a real number? A) input.Double(); B) input.nextdouble(); C) input.nextDouble(); D) input.double();

C) input.nextDouble();

Write the definition of a method named sumArray that has one parameter, an array of ints. The method returns the sum of the elements of the array as an int. A) public void sumArray (int[] list) B) public int sumArray (int list) C) public int sumArray (int[] list) D) public int[] sumArray (int[] list)

C) public int sumArray (int[] list)

Given that s3 = "Java", which of the following is the correct statement to return "JAVA"? A) String.toUpperCase("Java") B) s3.toUpperCase("Java") C) s3.toUpperCase() D) toUpperCase("Java")

C) s3.toUpperCase()

Literal values of type char are expressed by placing the character within: A) parentheses B) double quotes C) single quotes D) commas

C) single quotes

Which of the following expressions could be used to perform a case-insensitive comparison of two String objects named str1 and str2? A) str1 || str2 B) str1.equalsInsensitive(str2) C) str1.equalsIgnoreCase(str2) D) str1 != str2

C) str1.equalsIgnoreCase(str2

You can use this method to determine whether a file exists. A) the File class's canOpen() method B) the Scanner class's exists() method C) the File class's canRead() method D) the PrintWriter class's fileExists() method

C) the File class's canRead() method

One of the most common programming errors with while loops is: A) the ending loop B) the diminishing loop C) the infinite loop D) the error loop

C) the infinite loop

The while loop performs its test at the _______ of the loop. A) end B) bottom C) top D) middle

C) top

Assume that the array arr has been declared. Write a statement that assigns the next to last element of the array to the variable x, which has already been declared. A) x = arr[arr.length]-2; B) x = arr[arr.length-1]; C) x = arr[arr.length-2]; D) x = arr[arr.length];

C) x = arr[arr.length-2];

Write an expression that evaluates to true if the value of the integer variable total is evenly divisible (with no remainder) by the integer variable count. Assume that count is not zero. A. total % count B. total / count = 0 C. total % count == 0 D. total / count == 0

C. total % count == 0

The following code is not robust against invalid user input. Change the code so that it will not proceed until the user has entered a valid age (any integer) and grade point average (GPA, any real number). Make use of the appropriate Scanner class methods to check input. You may assume that the user enters a single token of input each time when prompted. Here is a sample dialogue of how the code should behave: Type your age: hello Type your age: ? Type your age: 3.14 Type your age: 25 Type your GPA: a Type your GPA: bcd Type your GPA: 2.5 age = 25, GPA = 2.5 Scanner console = new Scanner(System.in); System.out.print("Type your age: "); int age = console.nextInt(); System.out.print("Type your GPA: "); double gpa = console.nextDouble(); System.out.println("age = " + age + ", GPA = " + gpa); A) Scanner console = new Scanner(System.in); System.out.print("Type your age: "); while (console.hasNextInt()) { console.next(); System.out.print("Type your age: "); } int age = console.nextInt(); System.out.print("Type your GPA: "); while (console.hasNextDouble()) { console.next(); System.out.print("Type your GPA: "); } double gpa = console.nextDouble(); System.out.println("age = " + age + ", GPA = " + gpa); B) Scanner console = new Scanner(System.in); System.out.print("Type your age: "); while (!console.hasNextInt()) { console.next(); System.out.print("Type your age: "); int age = console.nextInt(); } System.out.print("Type your GPA: "); while (!console.hasNextDouble()) { console.next(); System.out.print("Type your GPA: "); double gpa = console.nextDouble(); } System.out.println("age = " + age + ", GPA = " + gpa); C) Scanner console = new Scanner(System.in); while (!console.hasNextInt()) { console.next(); System.out.print("Type your age: "); } int age = console.nextInt(); while (!console.hasNextDouble()) { console.next(); System.out.print("Type your GPA: "); } double gpa = console.nextDouble(); System.out.println("age = " + age + ", GPA = " + gpa); D) Scanner console = new Scanner(System.in); System.out.print("Type your age: "); while (!console.hasNextInt()) { console.next(); System.out.print("Type your age: "); } int age = console.nextInt(); System.out.print("Type your GPA: "); while (!console.hasNextDouble()) { console.next(); System.out.print("Type your GPA: "); } double gpa = console.nextDouble(); System.out.println("age = " + age + ", GPA = " + gpa);

D) Scanner console = new Scanner(System.in); System.out.print("Type your age: "); while (!console.hasNextInt()) { console.next(); System.out.print("Type your age: "); } int age = console.nextInt(); System.out.print("Type your GPA: "); while (!console.hasNextDouble()) { console.next(); System.out.print("Type your GPA: "); } double gpa = console.nextDouble(); System.out.println("age = " + age + ", GPA = " + gpa);

Suppose there is a method called average that computes the average (arithmetic mean) of all elements in an array of integers and returns the answer as a double. For example, if the array passed contains the values [1, -2, 4, -4, 9, -6, 16, -8, 25, -10], the calculated average should be 2.5. The method accepts an array of integers named list as its parameter and returns the average. What is the correct way to use the enhanced for loop (or for-each loop) to sum the values in the array? A) for (int list : n) { sum += n; } B) for (n : int[] list) { sum += n; } C) for (int n : int list[]) { sum += n; } D) for (int n : list) { sum += n; }

D) for (int n : list) { sum += n; }

Given int variables i and total that have already been declared, use a do/while loop to compute the sum of the squares of the first 10 counting numbers, and store this value in total. Use no variables other than i and total. A) i = 0; total = 0; do { total += i * i; i++; } while (i <= 10); B) i = 1; total = 0; do { total += i * i; i++; } while (i < 10); C) i = 1; total = 0; do { total += i * i; } i++; while (i <= 10); D) i = 1; total = 0; do { total += i * i; i++; } while (i <= 10);

D) i = 1; total = 0; do { total += i * i; i++; } while (i <= 10);

Consider a file called readme.txt that has the following contents: 6 Hello world. I have nothing What would be the output from the following code when it is run on the readme.txt file? Scanner input = new Scanner(new File("readme.txt")); int count = 0; while (input.hasNext()) { System.out.println("input: " + input.next()); count++; } System.out.println(count + " total"); A) input: 6 input: Hello world. input: I have nothing 3 total B) input: 6 input: Hello input: world. input: input: I input: have input: nothing 7 total C) input: 6 input: Hello input: world input. input: I input: have input: nothing 7 total D) input: 6 input: Hello input: world. input: I input: have input: nothing 6 total

D) input: 6 input: Hello input: world. input: I input: have input: nothing 6 total

Something is wrong with the following code, which attempts to count the number occurrences of the letter 'e' in a string, case-insensitively. Which code is the corrected version that works properly? int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i).toLowerCase() == 'e') { count++; } } A) int count = 0; for (int i = 0; i < s.length(); i++) { if (Character(s.charAt(i).toLowerCase) == 'e') { count++; } } B) int count = 0; for (int i = 0; i < s.length(); i++) { if (s.toLowerCase(s.charAt(i)) == 'e') { count++; } } C) int count = 0; for (int i = 0; i < s.length(); i++) { if ((Character.toLowerCase(s.charAt(i))).equals('e')) { count++; } } D) int count = 0; for (int i = 0; i < s.length(); i++) { if (Character.toLowerCase(s.charAt(i)) == 'e') { count++; } }

D) int count = 0; for (int i = 0; i < s.length(); i++) { if (Character.toLowerCase(s.charAt(i)) == 'e') { count++; } }

Convert the following for loop into an equivalent while loop. int total = 25; for (int number = 1; number <= (total / 2); number++) { total = total - number; System.out.println(total + " " + number); } System.out.println(); A) int total = 25; while (number <= (total / 2)) { int number = 1; total = total - number; System.out.println(total + " " + number); number++ } System.out.println(); B) int total = 25; int number = 1; while (number >= (total / 2)) { total = total - number; System.out.println(total + " " + number); total--; } System.out.println(); C) int total = 25; int number = 0; while (number <= (total / 2)) { total = total - number; System.out.println(total + " " + number); number++ } System.out.println(); D) int total = 25; int number = 1; while (number <= (total / 2)) { total = total - number; System.out.println(total + " " + number); number++ } System.out.println();

D) int total = 25; int number = 1; while (number <= (total / 2)) { total = total - number; System.out.println(total + " " + number); number++ } System.out.println();

Given an int variable n that has already been declared, use a while loop to print a single line consisting of 50 asterisks. Use no variables other than n. A) n = 1; while(n <= 50) { System.out.println('*'); n++; } B) n = 1; while(n <= 50) { System.out.print('*'); } n++; C) n = 1; while(n < 50) { System.out.print('*'); n++; } D) n = 1; while(n <= 50) { System.out.print('*'); n++; }

D) n = 1; while(n <= 50) { System.out.print('*'); n++; }

Write a method named readEntireFile that accepts a Scanner representing an input file as its parameter, then reads that file and returns the entire text contents of that file as a String. A) public static String readEntireFile(Scanner input) { String text = ""; while (input.hasNextLine()) { text = input.nextLine() + "\n"; } return text; } B) public static void readEntireFile(Scanner input) { String text = ""; while (input.hasNextLine()) { text += input.nextLine() + "\n"; } return text; } C) public static String readEntireFile(Scanner input) { Scanner input = new Scanner(System.in); String text = ""; while (input.hasNextLine()) { text += input.nextLine() + "\n"; } return text; } D) public static String readEntireFile(Scanner input) { String text = ""; while (input.hasNextLine()) { text += input.nextLine() + "\n"; } return text; }

D) public static String readEntireFile(Scanner input) { String text = ""; while (input.hasNextLine()) { text += input.nextLine() + "\n"; } return text; }

Given the following file contents for brownfox.txt, the quick brown fox jumps over the lazy dog what will be the output from the following code fragment? Scanner input = new Scanner(new File("brownfox.txt")); while (input.hasNext()) { String token = input.next(); System.out.println(token); } A) the quick brown fox jumps over the lazy dog B) the quick brown fox jumps over the lazy dog C) the quick brown fox jumps over the lazy dog D) the quick brown fox jumps over the lazy dog

D) the quick brown fox jumps over the lazy dog

An Array collects a sequence of values of __________ data type: A) int B) String C) double D) the same

D) the same

The following input file contains 17 total tokens that could be read by a Scanner. Hello there,how are you? I am "very well", thank you. 12 34 5.67 (8 + 9) "10" How many tokens can be read as an integer? A) none B) 5 C) 3 D) 2

D) 2

Consider the following code. What range of values can the variable, a, have? Random rand = new Random() int a = rand.nextInt(52) + 2; A) 2 - 51 B) 0 - 51 C) 0 - 53 D) 2 - 53

D) 2 - 53

Given two String arrays, arr1 and arr2, what is the correct way to compare if they are equal? A) arr1 == arr2 B) arr1 = arr2 C) arr1.equals(arr2) D) Arrays.equals(arr1, arr2)

D) Arrays.equals(arr1, arr2)

What will be printed after the following code is executed? String str = "abc"; System.out.print(Character.toUpperCase(str.charAt(1))); A) b B) A C) a D) B

D) B

The undesireable design of methods calling each other without returning control to the main method is called: A) Coupling B) Owning C) Controlling D) Chaining

D) Chaining

Under which condition will the Scanner constructor generate a FileNotFoundException? A) If the input file already exists, but has data in it. B) If the input file already exists, but is empty. C) If the input file cannot be opened due to a security error. D) If the input file does not exist.

D) If the input file does not exist.

What elements does the array numbers contain after the following code is executed? (Write the elements in the format: {0, 1, 2, ...} ) int[] numbers = new int[11]; numbers[1] = 2; numbers[4] = 23; numbers[5] = 10; int x = numbers[1]; numbers[x] = 37; numbers[numbers[5]] = 77; A) [0, 2, 23, 0, 37, 10, 0, 0, 0, 0, 77] B) [0, 2, 37, 0, 23, 10, 0, 0, 0, 0, 0] C) [0, 2, 37, 0, 23, 77, 0, 0, 0, 0, 10] D) [0, 2, 37, 0, 23, 10, 0, 0, 0, 0, 77]

D) [0, 2, 37, 0, 23, 10, 0, 0, 0, 0, 77]

A single element of input, such as one word or one number, is called A) input B) a whitespace C) a variable D) a token

D) a token

The type String is capitalized unlike primitive types int and double, because it is: A) a variable B) proper English C) more important D) an object

D) an object

Assume that an array of integers named arr has been declared and initialized. Write a single statement that assigns a new value to the first element of the array. The new value should be equal to twice the value stored in the last element of the array. A) arr[0] = arr[2 * arr.length-1]; B) arr[0] = 2 * arr[arr.length]; C) arr[1] = 2 * arr[arr.length-1]; D) arr[0] = 2 * arr[arr.length-1];

D) arr[0] = 2 * arr[arr.length-1];

Which of the following would be a valid method call for the following method? public static void drawBox (int height, int width) A) drawBox(5.5, 4.0); B) drawBox(33.0, 55.0); C) drawBox(10.0, 4); D) drawBox(5, 10);

D) drawBox(5, 10);

Suppose we want to use a while loop to obtain the output: 1, 2, 3, 4, 5 but instead the output we get is: 1, 2, 3, 4, 5, This particular looping problem is know as the: A) looping algorithm B) output algorithm C) sentinel algorithm D) fencepost algorithm

D) fencepost algorithm

When a return statement is executed inside a method, Java A) exits the program B) contiues processing the method C) returns an error D) immediately exits the method

D) immediately exits the method

The while loop is known as a(n): A) post-test loop B) decision structure C) definite loop D) indefinite loop

D) indefinite loop

What expression can be used to access its last element, regardless of its length? A) numbers[length - 1] B) numbers[numbers.length] C) numbers[numbers.last] D) numbers[numbers.length - 1]

D) numbers[numbers.length - 1]

Consider the following code snippet: Scanner console = new Scanner(System.in); System.out.print("What is the file name? "); String name = console.nextLine(); Scanner input = new Scanner(new File(name)); System.out.println(); Why are there two Scanner objects? A) they each read different input from a file B) both Scanner objects are not needed C) they each read different input from the console D) one reads input from the console and one reads data from a file

D) one reads input from the console and one reads data from a file

java.util is an example of a Java A) library B) constant C) method call D) package

D) package

Insert the missing code in the following code fragment. This fragment is intended to read an input file. public static void main(String[] args) __________________ { String inputFileName = "dataIn.txt"; File inputFile = new File(inputFileName); Scanner input = new Scanner(inputFile); . . . } A) catches FileNotFoundException B) inherits FileNotFoundException C) extends FileNotFoundException D) throws FileNotFoundException

D) throws FileNotFoundException

A parameter variable in a method can exist outside of the method. A) False B) True

False

In Java, the following declaration represents an empty array: int[] list = null; False True

False

More than one value can be returned from a method in Java. A) False B) True

False

A variable that appears in a method header and holds a value being passed into a method is called what? A) Modifier B) Actual Parameter C) Formal Parameter D) Argument

Formal Parameter

Given the following code, select all of the output that would be produced. String first = "Harry"; String last = "Jones"; String middle = "W."; System.out.println(first + last); System.out.println(first + " " + last); System.out.println(last + ", " + first + " " + middle); System.out.println(first + middle + last); Correct Response HarryJones Jones,HarryW. Harry Jones Jones, Harry W. Harry W. Jones HarryW.Jones

HarryJones Harry Jones Jones, Harry W. HarryW.Jones

One of the classes in the Java class libraries (predefined code) is the Math class. To call its sqrt method, we use: A) Math(sqrt) B) sqrt C) Math.sqrt D) Math:sqrt

Math.sqrt

The area of a square is stored in a double variable named area. Using the Math class, write an expression whose value is length of one side of the square. You do not need a semicolon ;

Math.sqrt(area

Consider a method printTriangleType that accepts three integer arguments representing the lengths of the sides of a triangle and prints the type of triangle that these sides form. public static void printTriangleType(int a, int b, int c) { ... } The three types of triangles are equilateral, isosceles, and scalene. However, certain integer values (or combinations of values) would be illegal and could not represent the sides of an actual triangle. How would you describe the precondition(s) of the printTriangleType method? Check all that apply. No side's length can be greater than 180. No side's length may exceed the sum of any two other side lengths. The sum of the sides' lengths must be an even number. The three side lengths must be positive integers. All sides must have the same length. No two sides' lengths can be the same.

No side's length may exceed the sum of any two other side lengths. The three side lengths must be positive integers.

Manipulating values in any order to allow access to each value is know as: Random array Standard access Sequential access Random access

Random access

Manipulating values in a manner of first to last is known as: Array access Sequential access Binary access Random access

Sequential access

Write the declaration of a String variable named background and initialize it to "purple".

String background = "purple";

Suppose we want to write code that prompts the user for a phrase and a number of times to repeat it, then prints the entire phrase that many times. Which statement below would be the correct way to read in this phrase? Here is an example dialogue with the user: What is your phrase? His name is Robert Paulson How many times should I repeat it? 3 His name is Robert Paulson His name is Robert Paulson His name is Robert Paulson String phrase = console.nextLine(); String phrase = nextLine(); String phrase = console.next(); int phrase = console.next();

String phrase = console.nextLine();

Declare a String variable named separator, and initialize it to a String consisting of two spaces.

String separator = " ";

Assume that temperature is a String variable. Write a statement to display the message "Today's Temperature is: " followed by the value of temperature. The message and the value of temperature should appear together, on a single line on standard output. System.out.println("Today's Temperature is: + temperature"); System.out.println("Today's Temperature is:" + temperature); System.out.println("Today's Temperature is: temperature"); System.out.println("Today's Temperature is: " + temperature);

System.out.println("Today's Temperature is: " + temperature);

Assume that message is a String variable. Write a println statement to display its value in the console.

System.out.println(message);

What is wrong with the following method? public static int min(int a, int b, int c) { int lowest = Math.min(a, b, c); return lowest; } Answer: The Math.min method does not accept three parameters. The lowest variable should be type double. The return type in the method header is incorrect. Nothing. The method will return the minimum of the 3 integers.

The Math.min method does not accept three parameters.

Consider a method getGrade that accepts an integer representing a student's grade percentage in a course and returns that student's numerical course grade: public static double getGrade(int percentage) { ... } The grade can be between 0.0 (failing) and 4.0 (perfect). Which of the following are precondition(s) that are appropriate for this method? Check all that apply. The parameter's value must be greater than or equal to 0. The parameter's value must be between 0.0 and 4.0. The parameter can be an integer or a real number. The parameter must be a letter grade of 'A', 'B', 'C', or 'D'. The parameter's value must be less than or equal to 100.

The parameter's value must be greater than or equal to 0. The parameter's value must be less than or equal to 100.

Given the following variable declarations: int x = 4; int y = -3; int z = 4; What is the result of the following relational expression? x * (y + 2) > y - (y + z) * 2 True False

True

Given the following variable declarations: int x = 4; int y = -3; int z = 4; What is the result of the following relational expression? x + y > 0 True False

True

In Java, only one formal parameter can be declared in a method header. A) False B) True

True

Consider the following code snippet: Scanner in = new Scanner(. . .); while (in.hasNextInt()) { . . . } Under which condition will the body of the while loop be processed? When any numeric value is encountered in the input. When a non-integer value is encountered in the input. When any type of value is encountered in the input. When an integer value is encountered in the input.

When an integer value is encountered in the input.

Write the definition (method header) of a method printOutput, which has two int parameters, x and y, and returns nothing. You do not need {

Write the definition (method header) of a method printOutput, which has two int parameters, x and y, and returns nothing. You do not need {

In Java, Arrays use: Zero-based indexing Length indexing Normal indexing String indexing

Zero-based indexing

Given the following code fragment, select all user responses that will cause an exception. Scanner console = new Scanner(System.in); System.out.print("How much money do you have? "); double money = console.nextDouble(); million 6.0 645 100*5 34.50 $25.00

all of the above

Assume values is an int array that is currently filled to capacity, with the following values: 9 4 12 2 6 8 18 The statement System.out.println(values[7]); will cause an ArrayOutOfBoundsException to be thrown output 7 cause a syntax error output 18 output nothing

cause an ArrayOutOfBoundsException to be thrown

Write the statement to declare a character variable named letter.

char letter;

Given the following declaration: Scanner console = new Scanner(System.in); What simple statement can we use to test if a user enters an integer at the console prompt?

console.hasNextInt();, console.hasNextInt()

Assume that day is a variable of type String that has been assigned a value. Write an expression whose value is the first character of the value of name. You do not need a semicolon ;

day.charAt(0)

The following input file contains tokens that could be read by a Scanner. What is token #5? Happy day. How is,your day? Mine is "very well", I hope youknow. My lucky numbers are: 2 11 20 "25"

day?

The _______ loop is ideal in situations where you always want the loop to run at least once.

do-while

What output is produced by the following program? public class CharMystery { public static void main(String[] args) { printRange('e', 'g'); } public static void printRange(char startLetter, char endLetter) { for (char letter = startLetter; letter <= endLetter; letter++) { System.out.print(letter); } System.out.println(); } }

efg

When Java encounters a return statement, it A) evaluates the statement and continues to the next method B) terminates the method and stores the value it obtained C) evaluates the statement and executes the next statement in the method D) evaluates the statement and immediately terminates the method, returning the value it obtained

evaluates the statement and immediately terminates the method, returning the value it obtained

Given the following variable declarations: int x = -12; int y = 15; int z = 7; What is the result of the following relational expression? (write your solution in lowercase) y / y < x

false

What is the best way to traverse an array?. for loop if/else do/while loop while loop

for loop

getTotalSum is a method that accepts no arguments and returns no value. Write a statement that calls getTotalSum. Assume that getTotalSum is defined in the same class that calls it.

getTotalSum();

Insert the missing code of the while loop condition in the following code fragment. This fragment is intended to read decimal numbers from a text file. Assume that in has been declared correctly. Scanner in = new Scanner(. . .); while (____________) { double hoursWorked = in.nextDouble(); System.out.println(hoursWorked); }

in.hasNextDouble()

A common programming error when passing a variable to a parameter is A) changing the value of the variable B) not including the data type of the variable C) naming the variable the same as the parameter D) including the data type of the variable

including the data type of the variable

Write a sequence of statements that finds the first space in the String phrase, and assigns to the variable segment the portion of line up to, but not including the space. You may assume that an int variable index as well as the variables phrase and segment, have already been declared. index = phrase.indexOf(" "); segment = phrase.substring(1, index); index = phrase.indexOf(" "); segment = phrase.substring(0, index); index = phrase.indexOf(" "); segment = phrase.substring(0, index -1); index = phrase.indexOf(""); segment = phrase.substring(0, index);

index = phrase.indexOf(" "); segment = phrase.substring(0, index);

Given the following program, what is the output? public class MysteryWho { public static void main(String[] args) { String whom = "her"; String who = "him"; String it = "who"; String he = "it"; String she = "whom"; sentence(she, he, who); } public static void sentence(String she, String who, String whom) { System.out.println(who + " and " + whom + " like " + she); } } she and he like who whom and it like him it and him like whom who and who like she

it and him like whom

When an argument (or actual parameter) is passed to a method: A) the actual variable name is passed to the method B) its value is reset to zero C) values may not be passed to methods D) its value is copied into the method's formal parameter variable

its value is copied into the method's formal parameter variable

Given a String variable named line and given a Scanner reference variable input that has been assigned a reference to a Scanner object, write the statement to read the next line from input and save it in line. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

line = input.nextLine();

Given a String variable named line that has been initialized, write an expression whose value is the the very last character in line.

line.charAt(line.length()-1)

In the following method header, the return type void means: public static void sum() A) no value is returned from the method B) the method returns 0 C) this will be an error D) the method has no parameter

no value is returned from the method

Write a statement that reads an integer value from standard input into number. Assume that number has already been declared as an int variable. Assume also that console is a variable that references a Scanner object associated with standard input.

number = console.nextInt();

What expression can be used to access the last element in array numbers, regardless of its length? numbers[numbers.length - 1] numbers[length - 1] numbers[numbers.length] numbers[numbers.length - i]

numbers[numbers.length - 1]

We want to write code to print the following four lines into a file named message.txt, in the same directory of your program. Testing, 1, 2, 3. This is my output file. Given the following declaration of a PrintStream object, PrintStream output = new PrintStream(new File("message.txt")); Write one complete statement to output the literal String "Testing," to the message.txt file. Write one complete statement to output the blank line to the message.txt file.

output.println("Testing,"); output.println()

Write the code for invoking a method named passNumber . There is one int argument for this method. Send the number 5 as an argument to this method. passNumber(int 5); passNumber(5); passNumber(x = 5); int passNumber(5);

passNumber(5);

Assume the String variables suffix and prefix have already been declared. Write an expression that concatenates suffix onto the end of the prefix with one space in between. You do not need a semicolon ;

prefix + " " + suffix

What methods does the PrintStream object have available for you to use? System.in print println output printf

print println printf

printCustomer is a method that accepts an array of Strings as a parameter. The printCustomer method prints the contents of the array; it does not return a value. customerList is an array of Strings that has been already declared and filled with names of customers. Write a statement that prints the contents of the customerList array by calling the printCustomer method.

printCustomer(customerList);, printCustomer(customerList)

printSmaller is a method that accepts two int arguments and returns no value. Two int variables, cost1 and cost2, have already been declared and initialized. Write a statement that calls printSmaller, passing it cost1 and cost2. Answers: printSmaller(int sales1, int sales2); printSmaller(sales1, sales2); printSmaller(sales 1, sales 2); int printSmaller(sales1, sales2);

printSmaller(sales1, sales2);

Write a method named area that accepts the radius of a circle as a parameter and returns the area of a circle with that radius. For example, the call area(2.0) should return 12.566370614359172. You may assume that the radius is non-negative. public static double area(radius) { double answer = Math.PI * Math.pow(radius, 2); return answer; } public static void area(double radius) { double answer = Math.PI * Math.pow(radius, 2); return answer; } public static double area(int radius) { double answer = Math.PI * Math.pow(radius, 2); return answer; } public static double area(double radius) { double answer = Math.PI * Math.pow(radius, 2); return answer; }

public static double area(double radius) { double answer = Math.PI * Math.pow(radius, 2); return answer; }

Which of the following is the correct syntax for a method header with parameters? public static (int x, int y) example() public static void example(x: int, y: int) public static void example(int x,y) public static void example(x, y) public static void example(int x, int y)

public static void example(int x, int y)

Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call: printStrings("abc", 5); will print the following output: abcabcabcabcabc Answers: public static void printStrings(String letters, int number) { for (int i = 0; i < number; i++) { System.out.print(letters); } } public static void printStrings(String letters, int number) { for (int i = 0; i < number; i++) { System.out.println(letters); } } public static void printStrings(String letters, int number) { for (int i = 0; i <= number; i++) { System.out.print(letters); } } public static void printStrings(String letters, int number) { for (int i = 1; i < number; i++) { System.out.print(letters); } }

public static void printStrings(String letters, int number) { for (int i = 0; i < number; i++) { System.out.print(letters); } }

In the following method header, double is called the ____________ of the method. public static double sqrt(int n) A) constant B) return type C) variable D) parameter

return type

Write the code for invoking a method named sendValue. There is one int argument for this method. Assume that an int variable, called x, has already been declared and initialized to some value. Use this variable's value as an argument in your method invocation.

sendValue(x);

Assume that student and grade have been declared suitably for storing a student name and a grade on a 4.0 scale, respectively. Assume also that console is a variable that references a Scanner object associated with standard input. Write some code that reads in a student and a grade and then prints the message "The student is STUDENT and the grade is GRADE" on a line by itself, where STUDENT and GRADE are replaced by the values read in for the variables student and grade. student = console.next(); grade = console.nextInt(); System.out.println("The student is " + student + " and the grade is " + grade); student = console.next(); grade = console.nextDouble(); System.out.println("The student is " + student + " and the grade is " + grade); student = console.next(); grade = console.nextDouble(); System.out.println("The student is" + student + "and the grade is" + grade); student = console.nextString(); grade = console.nextDouble(); System.out.println("The student is " + student + " and the grade is " + grade);

student = console.next(); grade = console.nextDouble(); System.out.println("The student is " + student + " and the grade is " + grade);

Given the following String declarations, write a simple statement to compare if the Strings contain the same value. You do not need a semicolon, ; or any type of decision structure. String student1 = "Amanda"; String student2 = "John";

student1.equals(student2), student1.equalsIgnoreCase(student2), student2.equalsIgnoreCase(student1), student2.equals(student1)

Write an entire statement that throws an IllegalArgumentException with the customized message "negative value".

throw new IllegalArgumentException("negative value");

Given the following variable declarations: int x = 3; int y = -7; int z = 1; What is the result of the following relational expression? (write your solution in lowercase) x * (y + 2) < y - (y + z) * 2

true

Given the following method, public static String repl(String s, int n) { String result = ""; for (int i = 0; i < n; i++) { result = result + s; } return result; } What is returned from the following method call? repl("zoom", 4)

zoomzoomzoomzoom


Set pelajaran terkait

Financial Accounting Ch. 4 Notes

View Set

II Lecture Chapter 13 True-False Exercise 13.05

View Set

Quickbooks Section 1 - Attempt 4/11/2021

View Set

PSYCH 210 - MALE SEXUAL ANATOMY (CHAPTER 5)

View Set