java第14章
余年寄山水
750次浏览
2020年07月30日 16:25
最佳经验
本文由作者推荐
塑料导电吗-冲击拼音
A Java exception is an instance of __________.
a. RuntimeException
b. NumberFormatException
c. Error
d. Throwable
e. Exception
#
2. tWhat is displayed on the console when running the following program?
class Test {
public static void main(String[] args) {
try {
n("Welcome to Java");
int i = 0;
double y = 2.0 / i;
n("Welcome to HTML");
}
finally {
n("The finally clause is executed");
}
}
}
a. tWelcome to Java.
b. tNone of the above.
c. tThe program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
d. tWelcome to Java followed by The finally clause is executed in the next line.
#
3. tA method should not claim Error or RuntimeException in the method declaration, but it may throw exceptions of these types.
a. false
b. true
#
4. tWhen an Error-type exception occurs, the GUI application may continue to run.
a. true
b. false
#
5. tWhat is displayed on the console when running the following program?
class Test {
public static void main(String[] args) {
try {
n("Welcome to Java");
int i = 0;
int y = 2 / i;
n("Welcome to HTML");
}
finally {
n("The finally clause is executed");
}
}
}
a. tNone of the above.
b. tWelcome to Java followed by The finally clause is executed in the next line, then an error message.
c. tThe program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed, then an error message.
d. tWelcome to Java, then an error message.
#
6. tWhat is displayed on the console when running the following program?
class Test {
public static void main (String[] args) {
try {
n("Welcome to Java");
}
finally {
n("The finally clause is executed");
}
}
}
a. tNone of the above
b. tThe finally clause is executed
c. tWelcome to Java followed by The finally clause is executed in the next line
d. tWelcome to Java
#
7. Which method can be used to create an input object for file ?
a. new Scanner("")
b. new Scanner(File(""))
c. new Scanner(new File(""))
d. new Scanner()
#
8. tWhich of the following returns the path separator character?
a. tor
b. None of the above.
c. torChar
d. paratorChar
e. parator
#
9. tWhat is displayed on the console when running the following program?
class Test {
public static void main (String[] args) {
try {
n("Welcome to Java");
return;
}
finally {
.p
rintln("The finally clause is executed");
}
}
}
a. tNone of the above
b. tWelcome to Java followed by The finally clause is executed in the next line
c. tWelcome to Java
d. tThe finally clause is executed
#
10. The methods getMessage(), printStackTrace(), and toString() are available in ________ class.
a. Throwable
b. IOException
c. RuntimeException
d. Exception
#
11. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
int[] list = new int[5];
n(list[5]);
}
}
a. ClassCastException
b. ArithmeticException
c. No exception
d. ArrayIndexOutOfBoundsException
e. StringIndexOutOfBoundsException
#
12. tAnalyze the following code:
class Test {
public static void main(String[] args) {
try {
int zero = 0;
int y = 2/zero;
try {
String s = "5.6";
nt(s); // Cause a NumberFormatException
}
catch(Exception e) {
}
}
catch(RuntimeException e) {
n(e);
}
}
}
a. tNone of the above.
b. tA try-catch block cannot be embedded inside another try-catch block.
c. tA good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try-catch block.
d. tThe program has a compilation error because Exception appears before RuntimeException.
#
13. Which method can be used to write data?
a. print
b. exist
c. rename
d. close
#
14. If no exception occurs in a try-catch block, the code in the finally clause _________.
a. is ignored.
b. may be executed
c. is executed
d. is not executed
#
15. tWhich of the following statements creates an instance of File on Window for the file c:?
a. tnew File("c://")
b. tnew File("c:")
c. tnew File("c:")
d. tnew File("c:/")
#
16. Which method can be used to create an output object for file ?
a. new PrintWriter("")
b. new PrintWriter(new File(""))
c. new PrintWriter()
d. new PrintWriter(File(""))
#
17. Analyze the following code:
public static boolean isCorrect() {
try {
// Perform some tasks
// ...
return true;
}
finally {
return true;
}
}
a. When invoking the isCorrect method, it returns false if any exceptions occur in the method.
b. When invoking the isCorrect method, it always returns true.
c. When invoking the isCorrect method, it returns true if no exceptions occur in the method.
d. When invoking the isCorrect method, it always returns false.
#
18. tWhat is displayed on the console when running the following program?
class Test {
public static void mai
n(String[] args) {
try {
n("Welcome to Java");
int i = 0;
int y = 2/i;
n("Welcome to Java");
}
catch (RuntimeException ex) {
n("Welcome to Java");
}
finally {
n("End of the block");
}
}
}
a. tThe program displays Welcome to Java three times.
b. tThe program displays Welcome to Java two times followed by End of the block.
c. tThe program displays Welcome to Java three times followed by End of the block.
d. tThe program displays Welcome to Java two times.
#
19. tWhat are the reasons to create an instance of the File class?
a. tTo obtain the properties of the file such as whether the file can be read, written, or is hidden.
b. tTo determine whether the file exists.
c. tTo rename the file.
d. tTo delete the file.
e. To read/write data from/to a file
#
20. tAny number divided by 0 would cause a compilation error.
a. false
b. true
#
21. tYou cannot place a method call in a try-catch block unless the method claims an exception.
a. false
b. true
#
22. tA method cannot throw Exception or a subclass of Exception if it is not claimed in the method declaration.
a. true
b. false
#
23. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
n(1 / 0);
}
}
a. No exception
b. ArrayIndexOutOfBoundsException
c. ArithmeticException
d. StringIndexOutOfBoundsException
e. ClassCastException
#
24. tYou must place a method call in a try-catch block if the method claims an exception.
a. false
b. true
#
25. tA method can throw a subclass of RuntimeException.
a. true
b. false
#
26. _________ are unchecked exceptions.
a. IOException
b. Exception
c. RuntimeException
d. Throwable
#
27. Suppose you enter 34.3, the ENTER key, 57.8, the ENTER key. Analyze the following code.
Scanner input = new Scanner();
double v1 = uble();
double v2 = uble();
String line = ne();
a. tThe program has a runtime error because 34.3 is not an integer.
b. tAfter the last statement is executed, line contains characters '7 println(ex);
}
}
}
a. tAn exception is raised due to nt(s);
b. tThe program has a compilation error.
c. tAn exception is raised due to 2 / i;
d. tThe program compiles and runs without exceptions.
#
29. If an exception occurs in a try-catch block, the code in the finally clause _________.
a. may be executed
b. is not executed
c. is executed
d. is not executed if the exception is caught.
#
30. tTo create an InputStream to read from a file on a Web server, you use the method __________ in the URL class.
a. tgetInputStream();
b. tobtainInputStream();
c. tconnectStream();
d. topenStream();
#
31. tWhat is displayed on the console when running the following program?
class Test {
public static void main(String[] args) {
try {
n("Welcome to Java");
int i = 0;
int y = 2/i;
n("Welcome to Java");
}
catch (RuntimeException ex) {
n("Welcome to Java");
}
finally {
n("End of the block");
}
n("End of the block");
}
}
a. tYou cannot catch RuntimeException errors.
b. tThe program displays Welcome to Java two times followed by End of the block.
c. tThe program displays Welcome to Java two times followed by End of the block two times.
d. tThe program displays Welcome to Java three times followed by End of the block.
#
32. tWhat is displayed on the console when running the following program?
class Test {
public static void main(String[] args) {
try {
method();
n("After the method call");
}
catch (RuntimeException ex) {
n("RuntimeException");
}
catch (Exception ex) {
n("Exception");
}
}
static void method() throws Exception {
try {
String s = "5.6";
nt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
n("Welcome to Java");
}
catch (RuntimeException ex) {
n("RuntimeException");
}
catch (Exception ex) {
n("Exception");
}
}
}
a. tThe program displays Exception twice.
b. tThe program displays RuntimeException twice.
c. tThe program displays RuntimeException followed by After the method call.
d. tThe program has a compilation error.
e. tThe program displays Exception followed by RuntimeException.
#
33. Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.
Scanner input = new Scanner();
int v1 = t();
int v2 = t();
String line = ne();
a. tThe program has a runtime error because 34.3 is not an integer.
b. tAfter the last statement is executed, intValue is 34.
c. tAfter the last statement is executed, line contains characters ') does not exist, new File("c:") returns null.
d. tIf a directory (e.g., c:liang) does not exist, new File("c:liang") returns null.
e. tIf a file (e.g., c:) does not exist, new File("c:") creates a new file named c:.
#
42. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
String s = "abc";
n((3));
}
}
a. ArithmeticException
b. ClassCastException
c. No exception
d. StringIndexOutOfBoundsException
e. ArrayIndexOutOfBoundsException
#
43. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = null;
n(o);
}
}
a. ArithmeticException
b. StringIndexOutOfBoundsException
c. No exception
d. ArrayIndexOutOfBoundsException
e. NullPointerException
#
44. tWhat is displayed on the console when running the following program?
class Test {
public static void main(String[] args) {
try {
method();
n("After the method call");
}
catch (NumberFormatException ex) {
n("NumberFormatException");
}
catch (RuntimeException ex) {
n("RuntimeException");
}
}
static void method() {
String s = "5.6";
nt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
n("Welcome to Java");
}
}
a. tThe program displays RuntimeException.
b. tThe program has a compilation error.
c. tThe program displays NumberFormatException followed by RuntimeException.
d. tThe program displays NumberFormatException.
e. tThe program displays NumberFormatException followed by After the method call.
#
45. tWhat is wrong in the following program?
class Test {
public static void main (String[] args) {
try {
n("Welcome to Java");
}
}
}
a. tA method call that does not declare exceptions cannot be placed inside a try block.
b. tYou cannot have a try block without a catch block.
c. tNothing is wrong.
d. tYou cannot have a try block without a catch block or a finally block.
#
46. An exception is always an instance of ___________.
a. Throwable
b. RuntimeException
c. Exception
d. IOException
#
47. Which of the following statements are true?
a. A method may declare to throw multiple exceptions.
b. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.
c. You use the keyword throws to declare exceptions in the method heading.
d. To throw an exception, use the key word throw.
#
48. Which class contains the method for checking whether a file exists?
a. Scan
ner
b. System
c. File
d. PrintWriter
#
49. Which of the classes are in the package?
a. Throwable
b. IOException
c. RuntimeException
d. Exception
#
50. _________ are checked exceptions.
a. Exception
b. IOException
c. RuntimeException
d. Throwable
#
51. tAny number divided by 0 would generate an exception.
a. false
b. true
#
52. An instance of _________ are unchecked exceptions.
a. Exception
b. NumberFormatException
c. Error
d. Throwable
e. RuntimeException
#
53. tPlacing an exception class after its superclass in the catch block results in a compilation error.
a. true
b. false
#
54. Which method can be used to read a whole line from the file?
a. next
b. nextInt
c. nextLine
d. nextDouble
#
55. An instance of _________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
a. Exception
b. Throwable
c. NumberFormatException
d. RuntimeException
e. Error
#
56. tThe presence of the try-catch block imposes overhead when no exception occurs.
a. false
b. true
#
57. A method must declare to throw ________.
a. checked exceptions
b. unchecked exceptions
c. RuntimeException
d. Error
#
58. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = null;
n(ng());
}
}
a. ArithmeticException
b. StringIndexOutOfBoundsException
c. ArrayIndexOutOfBoundsException
d. ClassCastException
e. NullPointerException
#
59. tWhat is displayed on the console when running the following program?
class Test {
public static void main(String[] args) {
try {
method();
n("After the method call");
}
catch (RuntimeException ex) {
n("RuntimeException");
}
catch (Exception ex) {
n("Exception");
}
}
static void method() throws Exception {
try {
String s = "5.6";
nt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
n("Welcome to Java");
}
catch (NumberFormatException ex) {
n("NumberFormatException");
throw ex;
}
catch (RuntimeException ex) {
n("RuntimeException");
}
}
}
a. tThe program displays NumberFormatException twice.
b. tThe program has a compilation error.
c. tThe program displays NumberFormatException followed by After the method call.
d. tThe program displays NumberFormatException followed by RuntimeException.
#
60. An instance of _________ describes the erro
rs caused by your program and external circumstances. These errors can be caught and handled by your program.
a. Error
b. Throwable
c. NumberFormatException
d. Exception
e. RuntimeException
#
61. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = new Object();
String d = (String)o;
}
}
a. ArithmeticException
b. No exception
c. StringIndexOutOfBoundsException
d. ClassCastException
e. ArrayIndexOutOfBoundsException
#
62. tAnalyze the following code:
class Test {
public static void main(String[] args) {
try {
String s = "5.6";
nt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
}
catch (Exception ex) {
n("NumberFormatException");
}
catch (RuntimeException ex) {
n("RuntimeException");
}
}
}
a. tThe program displays RuntimeException.
b. tThe program displays NumberFormatException followed by RuntimeException.
c. tThe program displays NumberFormatException.
d. tThe program has a compilation error.
#
63. Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.
Scanner input = new Scanner();
double v1 = uble();
double v2 = uble();
String line = ne();
a. tThe program has a runtime error because 34.3 is not an integer.
b. tAfter the last statement is executed, intValue is 34.
c. tAfter the last statement is executed, line contains characters '7