Unit 7 Flashcards

1
Q

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

A

B) To make the software robust, reusable, maintainable, and understandable

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

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

A

B) Using exceptions

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

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

A

B) Detect, report, and deal with the problem appropriately

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

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

A

B) It can make the code unnecessarily long and hard to read

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

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

A

B) To handle exceptions using a specific mechanism for passing control

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

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

A

B) The Java Virtual Machine throws an exception

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

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

A) Checked and unchecked exceptions

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

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

A

C) They occur due to programming errors

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

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”

A

B) “Error: Index 3 out of bounds for length 3”

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

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

A) To execute code after a try-catch block, regardless of whether an exception was thrown

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

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

A

B) Converting an object into a sequence of bytes

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

What must a class implement to be serializable?

A) Serializable interface
B) Externalizable interface
C) Cloneable interface
D) Streamable interface

A

A) Serializable interface

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

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

A

C) To write data to files

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

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

A

B) The attribute is not serialized

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

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

A) By declaring it in the throws clause of the method

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

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

A

B) “File not found.”

17
Q

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

A) new FileOutputStream(“file.txt”, true)

18
Q

Which method from the Scanner class is used to read an entire line from a file?

A) readLine()
B) nextLine()
C) getLine()
D) read()

A

B) nextLine()

19
Q

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

A

B) Checked exceptions are for errors you can anticipate, unchecked are for programming errors

20
Q

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

A

C) It is used to write formatted representations of objects to text-output streams

21
Q

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.

A

B) Prints the string “null”

22
Q

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

A) ArrayIndexOutOfBoundsException

23
Q

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

A) InvalidClassException

24
Q

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.

A

C) By closing the resource in a finally block

25
Q

What is the primary use of the throw keyword in Java?
A) To handle an exception.
B) To define a new exception.
C) To declare an exception.
D) To manually trigger an exception.

A

C) To declare an exception.

26
Q

When a method is declared with a throws clause, what does it mean?
A) The method handles the specified exceptions.
B) The method guarantees that it will not cause any exceptions.
C) The method may pass the specified exceptions to its caller.
D) The method suppresses all exceptions.

A

C) The method may pass the specified exceptions to its caller

27
Q

What is the advantage of custom exceptions in Java?
A) They allow methods to throw any type of runtime error.
B) They provide a means to differentiate an error condition and handle it accordingly.
C) They eliminate the need for try-catch blocks.
D) They improve the performance of the application.

A

B) They provide a means to differentiate an error condition and handle it accordingly

28
Q

Which of the following is not a correct statement about the finally block?
A) It is executed after the try block, regardless of whether an exception was thrown or not.
B) It must always accompany a try block.
C) It is used to execute important code such as closing resource connections.
D) It can be used without a catch block.

A

B) It must always accompany a try block (Note: It doesn’t have to always accompany a try block; it can stand with just a try without catch.)

29
Q

What will happen if a finally block throws an exception?
A) The exception from the finally block is suppressed.
B) Any exception thrown in the try block is ignored.
C) The exception from the finally block is thrown to the next higher context.
D) The program will immediately terminate.

A

C) The exception from the finally block is thrown to the next higher context

30
Q

Which of the following best describes the concept of exception chaining in Java?
A) Preventing an exception from propagating.
B) Catching one exception and throwing a new exception that includes it.
C) Linking multiple catch blocks together.
D) Creating a list of possible exceptions to catch.

A

B) Catching one exception and throwing a new exception that includes it

31
Q

What is the purpose of using the transient keyword in Java serialization?
A) To indicate that the variable should be serialized.
B) To ensure the variable is encrypted during serialization.
C) To prevent the variable from being serialized.
D) To mark the variable as volatile.

A

C) To prevent the variable from being serialized

32
Q

What is the correct way to use try-with-resources in Java?
A) try (Resource r = new Resource()) { /* use r / }
B) try { Resource r = new Resource(); /
use r / }
C) try (Resource r) { /
use r / }
D) try { Resource r = new Resource(); /
use r */ } finally { r.close(); }

A

A) try (Resource r = new Resource()) { /* use r */ }

33
Q

Why would a developer define a custom exception in Java?
A) To handle all types of runtime exceptions.
B) To provide clearer, more specific error handling tailored to their application.
C) To increase the execution speed of the application.
D) To comply with Java standard library requirements.

A

B) To provide clearer, more specific error handling tailored to their application.

34
Q

What is a recommended practice when writing catch blocks in Java?
A) Use generic Exception classes to catch all exceptions.
B) Catch the most specific exception types first before more general types.
C) Only catch RuntimeExceptions and ignore checked exceptions.
D) Use empty catch blocks to pass all exceptions silently.

A

B) Catch the most specific exception types first before more general types.

35
Q

What does the list() method of the File class do in Java?
A) It deletes files in the specified directory.
B) It returns an array of strings naming the files and directories in the directory.
C) It checks if the directory is writable.
D) It creates a new file in the directory.

A

B) It returns an array of strings naming the files and directories in the directory.

36
Q

What should you do if an IOException occurs when reading from a file?
A) Delete the file and create a new one.
B) Retry the operation a fixed number of times before failing.
C) Log the error and inform the user, possibly retrying or aborting the operation.
D) Ignore the exception and continue to the next operation.

A

C) Log the error and inform the user, possibly retrying or aborting the operation.

37
Q

What is the advantage of using try-with-resources in Java?
A) It automatically closes the resources after use.
B) It handles all types of exceptions automatically.
C) It improves the performance of I/O operations.
D) It allows multiple resources to be opened simultaneously without closing them.

A

A) It automatically closes the resources after use.