1. What exception could possibly be thrown in the following code? (Choose all that apply) import...
Question:
1. What exception could possibly be thrown in the following code?
(Choose all that apply)
import java.util.*;
public class Example {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = -1;
String name = "";
System.out.print("Enter your name: ");
name = scan.nextLine();
System.out.println("Nice to meet you, " + name + "!");
System.out.print("I see that the first initial of your name is: ");
System.out.print(name.charAt(number));
}
}
A. ArithmeticException
B. InputMismatchException
C. StringIndexOutOfBoundsException
D. No exception can be thrown by this code
2. Which of the following statements are true?
(Choose none or all that apply)
A. You must try to anticipate the possible errors a user might make.
B. If you don't solve logic errors in your program, your program will crash.
C. When using try/catch, the finally block is optional.
D. Use try/catch to deal with logic errors.
E. Use try/catch to deal with runtime errors.
F. Use try/catch to deal with syntax errors.
3. What type of error is in the following code?
public class Example {
public static void main(String args) {
String favoriteMovie = "Spirited Away";
System.out.print("My favorite movie is " + favoriteMovie);
}
}
A. Syntax error
B. Runtime error
C. Logic error
D. None of the above / There is no error
4. I want to generate a random integer number from 73-79 (in other words: 73, 74, 75, 76, 77, 78, or 79).
For the following code, what should x and y be in order to generate this randomnumber?
int randomNum = (int)(x + Math.random() * y);
A. x = 0
y = 79
B. x = 73
y = 6
C. x = 73
y = 7
D. x = 79
y = 6
E. x = 79
y = 7
F. x = 73
y = 79
G. x = 79
y = 73
H. None of the above
5. When an exception is thrown, it must be caught - otherwise the program will crash.
True
False
Introduction To Programming With Java A Problem Solving Approach
ISBN: 9781260575248
3rd International Edition
Authors: John Dean