programming languages Online Quiz - 239
Description: programming languages Online Quiz - 239 | |
Number of Questions: 20 | |
Created by: Aliensbrain Bot | |
Tags: programming languages |
From which problems is it possible for a program to recover?
-
Errors
-
Exceptions
-
Both errors and exceptions
-
Neither.
Both class Error and class Exception are children of this parent:
-
Throwable
-
Catchable
-
Runable
-
Problem
Is a program required to catch all exceptions that might happen?
-
No. You can write a program to catch just the exceptions you want.
-
No. But if a program catches one type of exception it must catch all other types as well.
-
Yes. If a program is not written to catch all exceptions it will not compile.
-
Yes. A program can not do I/O unless it catches all exceptions.
AI Explanation
To answer this question, you need to understand exception handling in programming.
Option A) No. You can write a program to catch just the exceptions you want. This option is correct. In programming, you have the flexibility to catch specific exceptions that you anticipate might occur and handle them accordingly. You can use try-catch blocks to catch specific exceptions and handle them appropriately. It is not necessary to catch all possible exceptions that might happen in a program.
Option B) No. But if a program catches one type of exception it must catch all other types as well. This option is incorrect. In programming, you are not required to catch all types of exceptions if you don't want to. You can choose to catch only specific exceptions that you want to handle. There is no requirement to catch all other types of exceptions.
Option C) Yes. If a program is not written to catch all exceptions it will not compile. This option is incorrect. An exception is a runtime error, and catching exceptions is not a requirement for the program to compile. Exceptions are generally handled at runtime, and the program will compile regardless of whether or not you catch all exceptions.
Option D) Yes. A program can not do I/O unless it catches all exceptions. This option is incorrect. While catching exceptions related to input/output (I/O) operations is good practice to handle potential errors, it is not a requirement to catch all exceptions for performing I/O operations. Exceptions related to I/O operations can be caught selectively based on the specific needs of the program.
The correct answer is A) No. You can write a program to catch just the exceptions you want. This option is correct because you have the flexibility to catch specific exceptions that you anticipate might occur and handle them accordingly. It is not necessary to catch all possible exceptions that might happen in a program.
What type of exception is thrown by parseInt() if it gets illegal data?
-
ArithmeticException
-
RunTimeException
-
NumberFormatException
-
NumberError
Which statement is FALSE about the try{} block?
-
Some of the statements in a try{} block will never throw an exception.
-
The statements in a try{} block may throw several types of exception.
-
The try{} block can not contain loops or branches.
-
The try{} block must appear before the catch{} blocks.
Which statement is FALSE about catch{} blocks?
-
There can be several catch{} blocks in a try/catch structure.
-
The catch{} block for a child exception class must PRECEED that of a parent execption class.
-
The catch{} block for a child exception class must FOLLOW that of a parent execption class.
-
If there is no catch{} block there must be a finally{} block.
Which of the following lists exception types from MOST specific to LEAST specific?
-
Error, Exception
-
Exception, RunTimeException
-
Throwable, RunTimeException
-
ArithmeticException, RunTimeException
To solve the question and determine the order of exception types from most specific to least specific, you need to understand the hierarchy of exception classes in Java.
In Java, exceptions are organized in a hierarchical structure. The topmost class in this hierarchy is Throwable
, which is the superclass of all exceptions and errors. The Throwable
class has two immediate subclasses: Error
and Exception
.
Error
represents serious issues that typically cannot be handled by your program, such as OutOfMemoryError
or StackOverflowError
. Errors are usually caused by external factors or problems with the Java Virtual Machine (JVM) itself.
Exception
is the superclass of all exceptions that can be thrown by a Java program. It further branches into various subclasses, including RuntimeException
and its subclasses.
RuntimeException
and its subclasses represent exceptions that occur during the execution of the program and are often caused by logical errors or violations of programming conventions. Examples include NullPointerException
or ArithmeticException
.
Based on this hierarchy, we can determine the order of exception types from most specific to least specific:
Option D: ArithmeticException, RunTimeException
- ArithmeticException is a specific type of exception that occurs when an arithmetic operation fails, such as dividing by zero.
- RuntimeException is a more general class that encompasses a broader range of runtime exceptions.
Option C: Throwable, RunTimeException
- Throwable is the top-level class in the exception hierarchy and is the superclass of all exceptions and errors.
- RuntimeException is a specific subclass of Exception.
Option B: Exception, RunTimeException
- Exception is a broader class that encompasses a wide range of exceptions, including runtime and non-runtime exceptions.
- RuntimeException is a more specific subclass of Exception that represents runtime exceptions.
Option A: Error, Exception
- Error is a subclass of Throwable and represents serious issues that usually cannot be recovered from.
- Exception is a superclass that encompasses both runtime and non-runtime exceptions.
Therefore, the correct order of exception types from most specific to least specific is:
D. ArithmeticException, RunTimeException
What happens in a method if an exception is thrown in a try{} block and there is NO MATCHING catch{} block?
-
This is not legal, so the program will not compile.
-
The method throws the exception to its caller, exactly if there were no try{} block.
-
The program halts immediately.
-
The program ignores the exception.
How many finally{} blocks may there be in a try/catch structure?
-
There must always be one, following the last catch{} block.
-
There can be zero or one immediately after each catch{} block.
-
There can be zero or one, following the last catch{} block.
-
There can be any number, following the last catch{} block.
AI Explanation
To answer this question, let's go through each option to understand why it is correct or incorrect:
Option A) There must always be one, following the last catch{} block. This option is incorrect. There is no requirement for a finally{} block to follow the last catch{} block. It is optional.
Option B) There can be zero or one immediately after each catch{} block. This option is incorrect. While it is true that there can be zero or one finally{} block immediately after each catch{} block, the question specifically asks about the number of finally{} blocks following the last catch{} block.
Option C) There can be zero or one, following the last catch{} block. This option is correct. The correct answer is C. After the last catch{} block, there can be zero or one finally{} block. It is not required to have a finally{} block following the last catch{} block.
Option D) There can be any number, following the last catch{} block. This option is incorrect. The number of finally{} blocks following the last catch{} block is limited to zero or one, as stated in option C.
Therefore, the correct answer is C. There can be zero or one finally{} block following the last catch{} block.
When is a finally{} block executed?
-
Always after execution has left a try{} block, no matter for what reason.
-
Only when an unhandled exception is thrown in a try{} block.
-
Only when any exception is thrown in a try{} block.
-
Always just as a method is about to finish.
AI Explanation
To answer this question, you need to understand the concept of the finally
block in exception handling.
The finally
block is a section of code that is always executed, regardless of whether an exception is thrown or not. It is typically used to perform cleanup tasks or release resources that were acquired in the try
block.
Let's go through each option to understand why it is correct or incorrect:
Option A) Always after execution has left a try
block, no matter for what reason. - This option is correct. The finally
block is always executed after the execution leaves the try
block, regardless of whether an exception was thrown or not.
Option B) Only when an unhandled exception is thrown in a try
block. - This option is incorrect. The finally
block is executed even if no exception is thrown.
Option C) Only when any exception is thrown in a try
block. - This option is incorrect. The finally
block is executed even if no exception is thrown.
Option D) Always just as a method is about to finish. - This option is incorrect. The finally
block is not specifically tied to the end of a method. It is executed after leaving the try
block, regardless of the method's completion.
The correct answer is Option A. This option is correct because the finally
block is always executed after execution has left a try
block, no matter for what reason.
The correct set of sequence for file operations for a file say 'test'
-
lseek(), open(), write(), close()
-
open(), write(), lseek(), close()
-
open(), lseek(), open(), , write() close()
-
None
Difference between fork() & vfork()
-
vforlk() allows more one child process to be created simaltaneously whereas for() allows only one
-
vfork() retains same PID for child & parent known as virtual PID
-
vfork() suspends the parent process until child process exits
-
fork() & vfork() are same
How can I get/set an environment variable from a program?
-
getenv(), setenv()
-
getenv(), putenv()
-
readenv(), writeenv()
-
You cannot environmement variables inside a program
What does alarm() do
-
Wakes up kernel from sleeping
-
Wakes up devices from sleeping
-
Invokes a kernel to send SIGALARM to the calling process
-
Invokes a kernel to send SIGALARM to the registered process
What is a shared memory
-
A memeory shared between kernel & devices
-
A memory shared between root user and ordinary user
-
A memory shared across processes
-
Harddisk
What is a zombie interval?
-
The interval between child terminating and the parent calling wait()
-
The interval between parent terminating and the child calling popen()
-
The interval between parent terminating and the child termination
-
None
Why do processes never decrease in size?
-
They never releases the memory utilized
-
They release the memory but kernel does not use them
-
Since they are running
-
It’s a false statement
How can I find out if someone else has a file open?
-
Its not possible
-
use isused() function
-
Try to open file with locking enabled
-
Try to write the file after opening
How do I `lock' a file?
-
Use filelock()
-
use lockfile()
-
Use lockfd()
-
use fcntl()
How do I find the size of a file inside a program?
-
Use ls -l
-
Use sizeof()
-
Use filesize()
-
Use fstat()