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
What is the result of executing the following code if the file does not exist?
try {
FileInputStream fis = new FileInputStream(“nonexistent.txt”);
} catch (FileNotFoundException e) {
System.out.println(“File not found.”);
}
A) Compilation error
B) “File not found.”
C) The program crashes
D) Nothing happens
B) “File not found.”
What is the correct way to append text to an existing file using FileOutputStream?
A) new FileOutputStream(“file.txt”, true)
B) new FileOutputStream(“file.txt”, false)
C) new FileOutputStream(“file.txt”)
D) new File(“file.txt”).append()
A) new FileOutputStream(“file.txt”, true)
Which method from the Scanner class is used to read an entire line from a file?
A) readLine()
B) nextLine()
C) getLine()
D) read()
B) nextLine()
What is the primary difference between checked and unchecked exceptions?
A) Checked exceptions are for file operations, unchecked are for memory errors
B) Checked exceptions are for errors you can anticipate, unchecked are for programming errors
C) Checked exceptions are recoverable, unchecked are not
D) There is no difference; it’s just naming
B) Checked exceptions are for errors you can anticipate, unchecked are for programming errors
Which statement is true about PrintStream?
A) It is used to read from files
B) It can only print text to the console
C) It is used to write formatted representations of objects to text-output streams
D) It is not part of the Java I/O package
C) It is used to write formatted representations of objects to text-output streams
What does PrintStream do when you pass null to one of its print methods?
A) Throws a NullPointerException.
B) Prints the string “null”.
C) Causes a compile-time error.
D) Does nothing.
B) Prints the string “null”
Which exception is thrown when trying to access an index that is out of bounds for an array?
A) ArrayIndexOutOfBoundsException
B) OutOfBoundsException
C) IndexOutOfBoundsException
D) ArrayBoundsException
A) ArrayIndexOutOfBoundsException
What will be the result of trying to deserialize an object whose serializable class has changed since it was serialized?
A) InvalidClassException
B) IOException
C) ClassNotFoundException
D) The object will be deserialized without any issues.
A) InvalidClassException
How do you ensure that a resource is closed even if an exception occurs in Java?
A) By closing the resource at the beginning of a try block.
B) By closing the resource in a catch block.
C) By closing the resource in a finally block.
D) By relying on garbage collection.
C) By closing the resource in a finally block