Java quiz 5

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the main reason for using a generic data validation method?

It saves you from writing variations of the same code again and again to check multiple data entries.

Consider the code that follows. What does it do? String value = "2"; boolean tryAgain = true; while (tryAgain == true) { try { int num = Integer.parseInt(value); tryAgain = false; } System.out.println("Valid integer"); catch(NumberFormatException nfe) { System.out.println("Invalid integer"); System.out.print("Enter an integer"); value = sc.next } }

The code doesn't compile.

Output example 5-1 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:818) at java.util.Scanner.next(Scanner.java:1420) at java.util.Scanner.nextDouble(Scanner.java:2324) at FutureValueApp.main(FutureValueApp.java:17) (Refer to output example 5-1.) What caused the exception to occur?

The user didn't enter the type of data the program was expecting.

When is the code within a catch block executed?

When the exception specified in the catch block is thrown in the try block

The has methods of the Scanner class let you a. check if the user has entered data at the console b. check if the data entered at the console can be converted to a specific data type c. retrieve and discard data that isn't required by the application d. all of the above e. a and b only

a and b only

To handle an exception using the try statement, you must a. code a try block around the statement that may throw the exceptions b. code a finally block that contains the statements that will be executed at the end of the try statement c. code a catch block that contains the statements that you want to be executed when the exception occurs d. all of the above e. a and c only

a and c only

Output example 5-1 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:818) at java.util.Scanner.next(Scanner.java:1420) at java.util.Scanner.nextDouble(Scanner.java:2324) at FutureValueApp.main(FutureValueApp.java:17) (Refer to output example 5-1.) What is this output called?

a stack trace

To determine the cause of an unhandled exception, you can a. use the name of the exception class that's displayed b. use the error message that's displayed c. use the information in the stack trace d. all of the above

all of the above

Which of the following classes define exceptions that can occur in a Java application? a. ArithmeticException b. NumberFormatException c. NullPointerException d. all of the above e. none of the above

all of the above

When a statement within a try block causes an exception, the remaining statements in the try block...

...aren't executed

In Java, exceptions are...

...objects

You should validate user entries rather than catch and handle exceptions caused by invalid entries whenever possible because...

...your code will run faster

What happens in the method that follows when s is "two"? public double parseInterval(String s) { double interval = 0.0; try { interval = Double.parseDouble(s); } catch(NumberFormatException e) { } return interval; }

0.0 is returned

Output example 5-1 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:818) at java.util.Scanner.next(Scanner.java:1420) at java.util.Scanner.nextDouble(Scanner.java:2324) at FutureValueApp.main(FutureValueApp.java:17) (Refer to output example 5-1.) Which statement would you look at to find the source of the problem?

line 17 in the FutureValueApp class

Code example 5-1 import java.util.Scanner; import java.text.NumberFormat; public class WeightConverter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String prompt = "Enter weight in lbs: "; boolean isValid = false; double weightInPounds = 0.0; while (isValid == false) { weightInPounds = getDouble(sc, prompt); if (weightInPounds > 0) isValid = true; else System.out.println("Weight must be greater than 0."); } double weightInKilos = weightInPounds / 2.2; NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); String message = weightInPounds + " lbs\nequals\n" + nf.format(weightInKilos) + " kgs\n"; System.out.print(message); } public static double getDouble(Scanner sc, String prompt) { double d = 0.0; boolean isValid = false; while (isValid == false) { System.out.print(prompt); if (sc.hasNextDouble()) { d = sc.nextDouble(); isValid = true; } else { System.out.println ("Error! Invalid decimal value. Try again."); } sc.nextLine(); } return d; } } (Refer to code example 5-1.) If the user enters 118 at the console prompt, what is the third line of the resulting console display?

53.64 kgs

Which class in the following list of classes are all exceptions subclasses of? a. Exception b. Error c. RuntimeException d. Throwable

Exception

Output example 5-1 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:818) at java.util.Scanner.next(Scanner.java:1420) at java.util.Scanner.nextDouble(Scanner.java:2324) at FutureValueApp.main(FutureValueApp.java:17) (Refer to output example 5-1.) What is the order of method calls?

FutureValueApp.main calls java.util.Scanner.nextDouble calls java.util.Scanner.next calls java.util.Scanner.throwFor

Code example 5-1 import java.util.Scanner; import java.text.NumberFormat; public class WeightConverter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String prompt = "Enter weight in lbs: "; boolean isValid = false; double weightInPounds = 0.0; while (isValid == false) { weightInPounds = getDouble(sc, prompt); if (weightInPounds > 0) isValid = true; else System.out.println("Weight must be greater than 0."); } double weightInKilos = weightInPounds / 2.2; NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); String message = weightInPounds + " lbs\nequals\n" + nf.format(weightInKilos) + " kgs\n"; System.out.print(message); } public static double getDouble(Scanner sc, String prompt) { double d = 0.0; boolean isValid = false; while (isValid == false) { System.out.print(prompt); if (sc.hasNextDouble()) { d = sc.nextDouble(); isValid = true; } else { System.out.println ("Error! Invalid decimal value. Try again."); } sc.nextLine(); } return d; } } (Refer to code example 5-1.) What type of data validation does this program do?

both range and valid data type checking

Code example 5-1 import java.util.Scanner; import java.text.NumberFormat; public class WeightConverter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String prompt = "Enter weight in lbs: "; boolean isValid = false; double weightInPounds = 0.0; while (isValid == false) { weightInPounds = getDouble(sc, prompt); if (weightInPounds > 0) isValid = true; else System.out.println("Weight must be greater than 0."); } double weightInKilos = weightInPounds / 2.2; NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); String message = weightInPounds + " lbs\nequals\n" + nf.format(weightInKilos) + " kgs\n"; System.out.print(message); } public static double getDouble(Scanner sc, String prompt) { double d = 0.0; boolean isValid = false; while (isValid == false) { System.out.print(prompt); if (sc.hasNextDouble()) { d = sc.nextDouble(); isValid = true; } else { System.out.println ("Error! Invalid decimal value. Try again."); } sc.nextLine(); } return d; } } (Refer to code example 5-1.) If the user enters "two hundred" at the console prompt, what does the code do?

displays an error message from the getDouble method

Code example 5-1 import java.util.Scanner; import java.text.NumberFormat; public class WeightConverter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String prompt = "Enter weight in lbs: "; boolean isValid = false; double weightInPounds = 0.0; while (isValid == false) { weightInPounds = getDouble(sc, prompt); if (weightInPounds > 0) isValid = true; else System.out.println("Weight must be greater than 0."); } double weightInKilos = weightInPounds / 2.2; NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); String message = weightInPounds + " lbs\nequals\n" + nf.format(weightInKilos) + " kgs\n"; System.out.print(message); } public static double getDouble(Scanner sc, String prompt) { double d = 0.0; boolean isValid = false; while (isValid == false) { System.out.print(prompt); if (sc.hasNextDouble()) { d = sc.nextDouble(); isValid = true; } else { System.out.println ("Error! Invalid decimal value. Try again."); } sc.nextLine(); } return d; } } (Refer to code example 5-1.) If the user enters -1 at the first console prompt, what does the code do?

displays an error message from the main method


Kaugnay na mga set ng pag-aaral

All of the following were tennents of Calvinism EXCEPT

View Set

Personal Health Promotion- Chapter 6

View Set

Spanish: Who? What? Where? When? Why? How?

View Set

trail guide to the body workbook leg and foot

View Set

Banking - dictionary قاموس بنوك

View Set

Advanced Python Methods and Functions

View Set