Error handling Flashcards

1
Q

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

A

c) To run the code that might cause an exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When the parseInt method fails to convert a string to an integer, what exception is thrown?

a) NullPointerException
b) ArithmeticException
c) InputMismatchException
d) NumberFormatException

A

d) NumberFormatException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

a) To catch all possible exceptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

a) To handle different exceptions with different responses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

a) Whether the number is within the printable ASCII range

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In the if statement, what does the double ampersand && represent?

a) OR logic
b) XOR logic
c) AND logic
d) NOT logic

A

c) AND logic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

b) switch/case statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

c) To terminate the switch/case block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

b) The next case will automatically be executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

a) To handle unexpected cases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

c) do loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

a) To generate a random number between 1 and 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

b) Three

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

b) while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

b) The program will enter an infinite loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the key difference between a for loop and a while loop?

a) A for loop repeats indefinitely
b) A while loop checks the condition before the loop starts
c) A for loop runs until a condition becomes false
d) A while loop runs at least once

A

b) A while loop checks the condition before the loop starts

17
Q

What output will this loop produce if fourFound is set to true at the beginning?

do {  
    int rndNumber = random.nextInt(5) + 1;  
    switch(rndNumber) {  
        case 1: System.out.println("One"); break;  
        case 4: System.out.println("Four"); break;  
    }  
} while (!fourFound);

a) The loop runs infinitely
b) The loop prints a random number
c) The loop runs once and prints a number
d) The loop prints nothing

A

c) The loop runs once and prints a number