Quiz 2 - Chapter 5,6,7

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following is true about method return statements?

A method can hold multiple return statements, but only one return statement executes in one method call.

Which of the following statements reflects the recommendations about closing files? Both the input and the output file should be explicitly closed in the program. Only the output file must be explicitly closed in the program. Both the input and the output file should not be explicitly closed in the program. Only the input file must be explicitly closed in the program.

Both the input and the output file should be explicitly closed in the program.

Which one of the following statements is true about passing arrays to a method? By default, arrays are passed by reference to a method. Arrays, when updated in a called method, are not reflected to the calling method. Arrays are passed only if size is specified as another argument. By default, arrays are passed by value to a method.

By default, arrays are passed by reference to a method.

Which of the following statements about a PrintWriter object is true? No data loss will occur if the program fails to close a PrintWriter before exiting. A PrintWriter will be automatically closed when the program exits. An exception will occur if the program fails to close a PrintWriter before exiting. Data loss may occur if a program fails to close a PrintWriter object before exiting.

Data loss may occur if a program fails to close a PrintWriter object before exiting.

Which statements are true about array references? I. Assigning an array reference to another creates a second copy of the array. II. An array reference specifies the location of an array. III. Two array references can reference the same array. II and III only III only II only I only

II and III only

Under which condition will the PrintWriter constructor generate a FileNotFoundException? If the output file already exists, but has data in it. If the output file does not exist. If the output file already exists, but is empty. If the output file cannot be opened or created due to a security error.

If the output file cannot be opened or created due to a security error.

What is wrong with the following code? public static String grade(int score) { if (score >= 9) { return A; } else if (score >= 8) { return B; } else if (score >= 6) { return C; } else if (score >= 4) { return D; } return F; }

Invalid argument in return statements

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? Scanner in = Scanner("hoursWorked.txt"); Scanner in = new Scanner(hoursFile); Scanner in = new Scanner("hoursWorked.txt"); Scanner in = Scanner.open(hoursFile);

Scanner in = new Scanner(hoursFile);

Which of the following is the best choice for a return type from a method that prompts users to enter their credit card number exactly as it appears on the card? String int boolean long

String

What is the purpose of this algorithm, assuming data is a non-empty array and both j and k are valid positions in the array? public void mystery(int[] data, int j, int k) { int t = data[j]; data[j] = data[k]; data[k] = t; } Removing an element in an array. Inserting an element in an array. Swapping elements of an array. Locating a value in an array.

Swapping elements of an array.

public static int mystery(int length, int n) { int[] result = new int[length]; for (int i = 0; i < result.length; i++) { result[i] = (int) (n * Math.random()); } return result; } Which statement is correct about the code? The method return type should be changed to int[]. The method has a bounds error when the array size is too large. The method works perfectly. The method returns a random number.

The method return type should be changed to int[].

What is wrong with the following code snippet? String[] data = { "abc", "def", "ghi", "jkl" }; String searchedValue = "ghi"; int pos = 0; boolean found = false; while (pos < data.length) { if (data[pos].equals(searchedValue)) { found = true; } else { found = false; } pos++; } if (found) { System.out.println("Found at position: " + pos); } else { System.out.println("Not found"); } There is a bounds error. There is nothing wrong. There is a compile-time error. There is a logic error.

There is a logic error.

File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(inputFile); while (in.hasNext()) { String input = in.next(); } Which of the following statements about this code is correct? This code will read in a word at a time from the input file. This code will read in a line at a time from the input file. This code will read in the entire input file in one operation. This code will read in a character at a time from the input file.

This code will read in a word at a time from the input file.

Scanner in = new Scanner(. . .); while (in.hasNextLine()) { String input = in.nextLine(); System.out.println(input); } Which of the following statements about this code is correct? This code will read in an entire line from the file in each iteration of the loop. This code will read in the entire contents of the file in a single iteration of the loop. This code will read in a single character from the file in each iteration of the loop. This code will read in a single word from the file in each iteration of the loop.

This code will read in an entire line from the file in each iteration of the loop.

If a method is declared to return void, then which statement below is true? When the method terminates no value will be returned. The method needs a return statement that always returns the integer value zero. The method cannot be invoked unless it is in an assignment statement. The method cannot return until reaching the end of the method body.

When the method terminates no value will be returned.

You need to write a method that calculates the shipping cost for an appliance, which depends on the item's 3D dimensions (width, height, depth) and weight. What should be the inputs and their data types for this method? double size, double weight, double shippingCost double width, double height, double depth, double weight double size, double weight double size, double weight, double price

double width, double height, double depth, double weight

Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable. String[] arr = { "abc", "def", "ghi", "jkl" }; ___________________ { System.out.print(str); } for (String str : arr) for (arr[] : str) for (str : String arr) for (str[] : arr)

for (String str : arr)

Which code snippet prints out the elements in a partially filled array of integers, assuming currentSize is the companion variable indicating the actual number of elements in the array? for (int i = 0; i < currentSize; i++) { System.out.print(myArray[i]); } for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[i]); } for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[currentSize]); } for (int i = 0; i < myArray.currentSize(); i++) { System.out.print(myArray[i]);

for (int i = 0; i < currentSize; i++) { System.out.print(myArray[i]); }

Which code snippet calculates the sum of all the even elements in an array values? int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum += values[i]; } } int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } } int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum++; } } int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum++; } }

int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } }

Which one of the following statements is a valid initialization of an array named somearray of ten elements? int somearray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int somearray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

The enhanced for loop _______________. is used to initialize all array elements to a common value is convenient for traversing elements in a partially filled array is only used for arrays of integers is convenient for traversing all elements in an array

is convenient for traversing all elements in an array

Parameter variables should not be changed within the body of a method because _______________.

it mixes the concept of a parameter with that of a variable

Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt and write to an output file named dataOut.txt. public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; String outputFileName = "dataOut.txt"; File inputFile = _________________; Scanner in = new Scanner(inputFile); . . . } new File(inputFileName) new File(System.in) new File(outputFileName) new File(inputFile)

new File(inputFileName)

public static void main(String[] args) throws IOException { PrintWriter outputFile = _______; . . . } new PrintWriter("c:/reports/dataOut.txt") new PrintWriter("c:\reports\dataOut.txt") new PrintWriter("c://reports//dataOut.txt") new PrintWriter("c:\\reports\\dataOut.txt")

new PrintWriter("c:\\reports\\dataOut.txt")

Which of the following is the correct header for a method definition named calcSum that accepts four int arguments and returns a double? public static calcSum(int a, int b, int c, int d) public static double calcSum() public static int calcSum(double a, double b, double c, double d) public static double calcSum(int a, int b, int c, int d)

public static double calcSum(int a, int b, int c, int d)

What is incorrect in the following code snippet? public static void textInRectangle(String str) { System.out.println("--------"); System.out.println("|" + str + "|"); System.out.println("--------"); } public static void main(String[] args) { System.out.println(textInRectangle("Hello there"); } textInRectangle does not return a value; therefore, it cannot be used as a parameter to the System.out.println method. textInRectangle is called with incorrect arguments. textInRectangle should be called in an assignment statement. The return value from textInRectangle is never used.

textInRectangle does not return a value; therefore, it cannot be used as a parameter to the System.out.println method.

Which String class method will remove spaces from the beginning and the end of a string? trim() truncate() strip() clean()

trim()


Ensembles d'études connexes

LearningCurve 9a. Anorexia Nervosa; Bulimia Nervosa; and Binge-Eating Disorder

View Set

Chapter 2: Motion At Plate Boundaries

View Set

CHAPTER 13 SUBSTANCE-RELATED DISORDERS

View Set

Econ 102 FINAL EXAM Review (Lessons 5-9) HW Answers

View Set

Pharm Endocrine - Thyroid (ch 31)

View Set

World History Chapter 10 Vocab Quiz

View Set

Controlling Foodservice Costs Final Exam

View Set