Test 4
Suppose you enter 34.3, the ENTER key, 57.8, the ENTER key. Analyze the following code.1 Scanner input = new Scanner(System.in);2 double v1 = input.nextDouble();3 double v2 = input.nextDouble();4 String line = input.nextLine();
After line 2 is executed, v1 is 34.3. After line 3 is executed, v2 is 57.8. After line 4 is executed, line contains an empty string.
Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.Scanner input = new Scanner(System.in);double v1 = input.nextDouble();double v2 = input.nextDouble();String line = input.nextLine();
After the last statement is executed, line contains characters ' ', '7', '8', '9'.
Analyze the following program. class Test {public static void main(String[] args) {try {String s = "5.6";Integer.parseInt(s); // Cause a NumberFormatException int i = 0;int y = 2 / i;System.out.println("Welcome to Java");}catch (Exception ex) {System.out.println(ex);}}}
An exception is raised due to Integer.parseInt(s);
Which of the following statements are correct? I:try (PrintWriter output = new PrintWriter("input.txt")) {output.println("Welcome to CITC1311");} II:try (PrintWriter output = new PrintWriter("input.txt");) {output.println("Welcome to CITC1311");} III:PrintWriter output;try (output = new PrintWriter("input.txt");) {output.println("Welcome to CITC1311");} IV:try (PrintWriter output = new PrintWriter("input.txt");) {output.println("Welcome to CITC1311");}finally {output.close();}
I, III, IV
What is displayed on the console when running the following program? class Test {public static void main(String[] args) {try {method();System.out.println("After the method call");}catch (RuntimeException ex) {System.out.println("RuntimeException");}catch (Exception ex) {System.out.println("Exception");}} static void method() throws Exception {try {String s = "5.6";Integer.parseInt(s); // Cause a NumberFormatException int i = 0;int y = 2 / i;System.out.println("Welcome to Java");}catch (NumberFormatException ex) {System.out.println("NumberFormatException");throw ex;}catch (RuntimeException ex) {System.out.println("RuntimeException");}}}
NumberFormat ExceptionRuntimeException.
what is displayed on the console when running the following program? class Test {public static void main(String[] args) {try {method();System.out.println("After the method call");}catch (NumberFormatException ex) {System.out.println("NumberFormatException");}catch (RuntimeException ex) {System.out.println("RuntimeException");}} static void method() {String s = "5.6";Integer.parseInt(s); // Cause a NumberFormatException int i = 0;int y = 2 / i;System.out.println("Welcome to Java");}}
The program displays NumberFormatException.
What is displayed on the console when running the following program? class Test {public static void main(String[] args) {try {method();System.out.println("After the method call");}catch (RuntimeException ex) {System.out.println("RuntimeException");}catch (Exception ex) {System.out.println("Exception");}} static void method() throws Exception {try {String s = "5.6";Integer.parseInt(s); // Cause a NumberFormatException int i = 0;int y = 2 / i;System.out.println("Welcome to Java");}catch (RuntimeException ex) {System.out.println("RuntimeException");}catch (Exception ex) {System.out.println("Exception");}}}
The program displays RuntimeException followed by After the method call.
What is displayed on the console when running the following program? class Test {public static void main(String[] args) {try {System.out.println("Welcome to Java");int i = 0;int y = 2/i;System.out.println("Welcome to Java");}catch (RuntimeException ex) {System.out.println("Welcome to Java");}finally {System.out.println("End of the block");}System.out.println("End of the block");}}
The program displays Welcome to Java two times followed by End of the block two times.
What is displayed on the console when running the following program? class Test {public static void main(String[] args) {try {System.out.println("Welcome to Java");int i = 0;int y = 2/i;System.out.println("Welcome to Java");}catch (RuntimeException ex) {System.out.println("Welcome to Java");}finally {System.out.println("End of the block");}}}
The program displays Welcome to Java two times followed by End of the block.
What is displayed on the console when running the following program? class Test {public static void main(String[] args) {try {System.out.println("Welcome to Java");int i = 0;double y = 2.0 / i;System.out.println("Welcome to HTML");}finally {System.out.println("The finally clause is executed");}}}
The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
Analyze the following code: class Test {public static void main(String[] args) {try {String s = "5.6";Integer.parseInt(s); // Cause a NumberFormatException int i = 0;int y = 2 / i;}catch (Exception ex) {System.out.println("NumberFormatException");}catch (RuntimeException ex) {System.out.println("RuntimeException");}}}
The program has a compilation error.
What is displayed on the console when running the following program? class Test { public static void main (String[] args) {try {System.out.println("Welcome to Java");}finally {System.out.println("The finally clause is executed");}}}
Welcome to Java followed by The finally clause is executed in the next line
Which of the following statements creates an instance of File on Window for the file c:\CITC1311.exe?
new File("c:\\CITC1311.exe");
Which method can be used to create an output object for file CITC1311.exe?
new PrintWriter("CITC1311.exe"); new PrintWriter(new File("CITC1311.exe"));