Unit 7 Flashcards
What is the primary goal of defensive programming?
A) To ensure the program only runs in secure environments
B) To make the software robust, reusable, maintainable, and understandable
C) To maximize software performance
D) To minimize the number of features in the software
B) To make the software robust, reusable, maintainable, and understandable
Which tool or technique is commonly used in defensive programming to handle unexpected problems?
A) Code obfuscation
B) Using exceptions
C) Decreasing code readability
D) Avoiding user input
B) Using exceptions
What should a program do when an error occurs at runtime, according to defensive programming principles?
A) Stop immediately to prevent damage
B) Detect, report, and deal with the problem appropriately
C) Ignore minor errors
D) Reboot the system
B) Detect, report, and deal with the problem appropriately
What is the issue with adding validation code for every method, according to best practices in defensive programming?
A) It makes the code too robust
B) It can make the code unnecessarily long and hard to read
C) It improves the performance of the program
D) It simplifies the code
B) It can make the code unnecessarily long and hard to read
What is the Java solution for handling exceptions that disrupt the normal flow of instructions?
A) To use conditional statements
B) To handle exceptions using a specific mechanism for passing control
C) To rewrite the program without exceptions
D) To ignore the exceptions
B) To handle exceptions using a specific mechanism for passing control
What does Java do when a runtime exception occurs?
A) It pauses the execution until the error is fixed
B) The Java Virtual Machine throws an exception
C) It sends an email to the developer
D) It automatically fixes the error
B) The Java Virtual Machine throws an exception
How are exceptions categorized in Java?
A) Checked and unchecked exceptions
B) Minor and major exceptions
C) Internal and external exceptions
D) Permanent and temporary exceptions
A) Checked and unchecked exceptions
What is true about unchecked exceptions in Java?
A) They must be declared in a method’s throws clause
B) They are under programmer’s control
C) They occur due to programming errors
D) They cannot be handled or caught
C) They occur due to programming errors
What will be the output if the following code encounters an ArrayIndexOutOfBoundsException?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“Error: “ + e.getMessage());
}
A) The program will crash
B) “Error: Index 3 out of bounds for length 3”
C) No output
D) It will print “3”
B) “Error: Index 3 out of bounds for length 3”
What is a finally block used for in Java?
A) To execute code after a try-catch block, regardless of whether an exception was thrown
B) To execute code only if an exception occurs
C) To handle all types of exceptions
D) To finally end the execution of the program
A) To execute code after a try-catch block, regardless of whether an exception was thrown
What is serialization in Java?
A) Converting a byte stream to characters
B) Converting an object into a sequence of bytes
C) Increasing the efficiency of file I/O
D) Encrypting files
B) Converting an object into a sequence of bytes
What must a class implement to be serializable?
A) Serializable interface
B) Externalizable interface
C) Cloneable interface
D) Streamable interface
A) Serializable interface
What is the purpose of the FileOutputStream class in Java?
A) To read data from files
B) To serialize objects
C) To write data to files
D) To monitor file changes
C) To write data to files
What happens when you serialize an object with a transient attribute?
A) The attribute is serialized normally
B) The attribute is not serialized
C) The attribute is encrypted
D) Serialization fails
B) The attribute is not serialized
How do you handle a FileNotFoundException in Java?
A) By declaring it in the throws clause of the method
B) By using a finally block
C) By ignoring it
D) By using a for loop
A) By declaring it in the throws clause of the method