Error handling Flashcards
What is the role of the try
block in error handling?
a) To catch errors and prevent program execution
b) To handle exceptions only if they occur
c) To run the code that might cause an exception
d) To define a safe area in the code
c) To run the code that might cause an exception
When the parseInt
method fails to convert a string to an integer, what exception is thrown?
a) NullPointerException
b) ArithmeticException
c) InputMismatchException
d) NumberFormatException
d) NumberFormatException
In the code catch (Exception ex)
, what is the purpose of catching the base Exception
?
a) To catch all possible exceptions
b) To ignore all exceptions
c) To handle only runtime errors
d) To handle specific exceptions
a) To catch all possible exceptions
What is the purpose of adding multiple catch clauses in a try-catch block?
a) To handle different exceptions with different responses
b) To handle the same exception multiple times
c) To improve performance
d) To avoid handling exceptions
a) To handle different exceptions with different responses
What does the condition if (number > 31 && number < 256)
check?
a) Whether the number is within the printable ASCII range
b) Whether the number is a valid integer
c) Whether the number equals 31 or 256
d) Whether the number is less than 31 or greater than 256
a) Whether the number is within the printable ASCII range
In the if
statement, what does the double ampersand &&
represent?
a) OR logic
b) XOR logic
c) AND logic
d) NOT logic
c) AND logic
In Java, what is the correct name for a structure that checks multiple cases based on one variable?
a) if/else structure
b) switch/case statement
c) for loop
d) try/catch block
b) switch/case statement
What is the purpose of the break
statement inside a switch/case structure?
a) To exit the program
b) To exit the loop
c) To terminate the switch/case block
d) To skip a case
c) To terminate the switch/case block
In the switch/case structure, what happens if the break
statement is missing after a case?
a) The program crashes
b) The next case will automatically be executed
c) The switch statement will stop
d) The code execution will stop
b) The next case will automatically be executed
Why do we add a default
case to a switch/case statement?
a) To handle unexpected cases
b) To increase performance
c) To skip certain cases
d) To avoid the use of break
statements
a) To handle unexpected cases
Which loop would you use to ensure that the code runs at least once?
a) for loop
b) while loop
c) do loop
d) switch loop
c) do loop
What is the purpose of using random.nextInt(5) + 1
in the RandomCase program?
a) To generate a random number between 1 and 5
b) To generate a random number between 0 and 5
c) To generate only even numbers
d) To generate a random number between 0 and 1
a) To generate a random number between 1 and 5
What will the following code output if rndNumber
is 3?
switch(rndNumber) { case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; case 3: System.out.println("Three"); break; }
a) Two
b) Three
c) One
d) Nothing
b) Three
What type of loop is used to repeat code until a specified condition is met?
a) do loop
b) while loop
c) for loop
d) switch loop
b) while loop
What is the potential risk of forgetting to set a condition to true
inside a while
loop?
a) The program will not compile
b) The program will enter an infinite loop
c) The program will skip the loop
d) The program will break after the first iteration
b) The program will enter an infinite loop