Trace the flow of execution and give the output of the following code:
int x=23;
try {
x=x+1;
throw new NumberFormatException();
}
catch (Exception e) {
x=x+2;
}
finally {
x=x+3;
}
System.out.println(x);
int x=23;
try {
x=x+1;
throw new NumberFormatException();
}
catch (Exception e) {
x=x+2;
}
finally {
x=x+3;
}
System.out.println(x);
the output will be 29 as we initialized x with 23
then we entered try block where x=x+1 is executed
after this x=24
numberformatexception is thrown
which is caught by the catch block where
x=x+2 is executed
after this x=26
finally block is always executed so we enter finally block
where x=x+3 is executed
after this x=29
then we print the value of x which is 29
so the output of this whole program will be 29
if you like the answer please provide a thumbs up.
Trace the flow of execution and give the output of the following code: int x=23; try...
9. What output is produced by the following code? (2 points) int waitTime 24 try SttrT block entered y if (waitTime> 25) throw new Exception("Time Limit Exceeded."); System.out.println("Leaving try block."); catch(Exception e) System.out.println("Exception:"+ e.getMessage0); System.out.println("After catch block")
What is the output of the following code: public static void main(String []args){ int x = 10; int y = 10; try{ System.out.println(function1(y, x)); } catch (Exception e) { System.out.println(“ERROR”); } } static int function1 (int x, int y) { x += 10; y += 7; return (x + y); }
Discuss exception handling. How does exception handling change the flow of code execution? Also discuss the use of TRY, THROW and CATCH when handling an exception.
Select the correct answer. (1) Which of the following will open a file named MyFile.txt and allow you to read data from it? (a) File file = new File("MyFile.txt"); (b) FileWriter inputFile = new FileWriter(); (c) File file = new File("MyFile.txt"); FileReader inputFile = new FileReader(file); (d) FileWriter inputFile = new FileWriter("MyFile.txt"); (2) How many times will the following do - while loop be executed? int x = 11; do { x...
Question 5 (10 points) Assume you have the following code, with blanks to be filled in below. public static void g () ( throw new () public static void f() try ) catch ( 12), e) System.out.print ("A") ; return } catch ( System.out. print ("B") throw ei e) ( 13) catch ( 14) e) ( System.out.print ("C") ; } finally System.out.print ("D") ; System.out.print ("E") ; } public static void main (String (] args) try f() } catch (...
JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...
Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...
What is the output of the following code? try { Scanner file = new Scanner(new File(“data.txt”)); int n = 0; while (file.hasNext()) { String s = file.nextLine(); if (s.equals(“A”)) n++; } System.out.println(“The value of n is “ +n); file.close(); } catch(IOException ioe) { ioe.printStackTrace(); }
PLEASE SOLVE THESE QUESTIONS
Q.3.6 What does the following code output? int x = 8; int y = 2; while(x > 8) { X- y+=2; } System.out.println(y); Q.3.7 What does the following code output? System.out.println((2 + 3 - 5) * (3/3) + (5+5)); Q.3.8 What does the following code output? if((2 == 2) && (3 != 3) && ((5 + 1)==6){ System.out.println(true); else { System.out.println(false);
Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { 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"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...