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.
To answer this question, the user needs to know the difference between errors and exceptions in programming.
Errors are problems that prevent the program from running entirely. They can occur due to syntax errors, logical errors, or runtime errors. When an error occurs, the program usually stops executing, and the user must fix the error before running the program again.
Exceptions, on the other hand, are problems that occur during the execution of a program. They are caused by unexpected situations or events that the program may not have been designed to handle. When an exception occurs, the program can often recover from the problem and continue executing, or it may terminate.
Now, let's examine each option:
A. Errors: Programs cannot recover from errors. Errors are fatal problems that prevent the program from running, so there is no opportunity for recovery.
B. Exceptions: Programs can recover from exceptions. Exceptions are unexpected problems that can be caught and handled by the program, allowing it to continue executing.
C. Both errors and exceptions: This option is incorrect. Programs cannot recover from errors, but they can recover from exceptions.
D. Neither: This option is incorrect. Programs can recover from exceptions, even if they cannot recover from errors.
Therefore, the answer is: B. Exceptions
-
Throwable
-
Catchable
-
Runable
-
Problem
To solve this question, the user needs to know the hierarchy of Java exceptions and errors.
Both class Error and class Exception are children of the parent class Throwable. This means that any class that extends Throwable can be used to represent an error or an exception that occurs during the execution of a Java program.
Option A is correct because Throwable is the parent class of both Error and Exception.
Option B is incorrect because there is no class called Catchable in Java.
Option C is incorrect because Runnable is an interface in Java, not a class that represents an error or an exception.
Option D is incorrect because there is no class called Problem in Java that represents an error or an exception.
Therefore, the answer is: A
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.
-
ArithmeticException
-
RunTimeException
-
NumberFormatException
-
NumberError
To solve this question, the user needs to be familiar with the parseInt() method and the types of exceptions it can throw.
The parseInt() method is used to convert a string to an integer. If the string contains illegal data (i.e. not a valid integer), the method will throw an exception.
Now, let's go through each option and explain why it is right or wrong:
A. ArithmeticException: This exception is thrown when an arithmetic operation produces an error, such as dividing by zero. It is not related to the parseInt() method, so this option is incorrect.
B. RunTimeException: This is a general exception that can be thrown at runtime for a variety of reasons. It is not specific to the parseInt() method, so this option is incorrect.
C. NumberFormatException: This is the correct answer. This exception is thrown by parseInt() when the string being parsed contains illegal data. For example, if the string "abc" is passed to parseInt(), a NumberFormatException will be thrown because "abc" is not a valid integer.
D. NumberError: This is not a standard Java exception, so this option is incorrect.
Therefore, the answer is: C. NumberFormatException
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.
AI Explanation
To answer this question, we need to understand the purpose and structure of a try{} block in programming.
A try{} block is used to enclose a block of code that may potentially throw an exception. The purpose of the try{} block is to catch and handle any exceptions that are thrown within it.
Let's go through each statement to determine which one is false:
A. Some of the statements in a try{} block will never throw an exception. This statement is true. Not all statements within a try{} block will necessarily throw an exception. Only specific statements or operations that have the potential to throw an exception, such as accessing an array out of bounds or dividing by zero, will result in an exception being thrown.
B. The statements in a try{} block may throw several types of exception. This statement is true. The statements within a try{} block can potentially throw multiple types of exceptions, depending on the operations being performed.
C. The try{} block cannot contain loops or branches. This statement is false. The try{} block can contain loops, branches, and any other valid statements or constructs that are allowed within the programming language.
D. The try{} block must appear before the catch{} blocks. This statement is true. In most programming languages, the try{} block must appear before any catch{} blocks that are used to handle the thrown exceptions. This allows the catch{} blocks to catch the exceptions thrown within the try{} block.
Based on the explanations above, the false statement about the try{} block is C) The try{} block cannot contain loops or branches.
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.
To answer this question, the user needs to have knowledge of the try-catch-finally error handling mechanism in programming.
Option A: This statement is true. We can have several catch{} blocks in a try/catch structure in order to handle different types of exceptions.
Option B: This statement is true. The catch{} block for a child exception class must precede that of a parent exception class because if a child exception is caught by a parent catch{} block, the catch{} block for the child exception will never be executed.
Option C: This statement is false. The catch{} block for a child exception class must follow that of a parent exception class. This is because if the catch{} block for a child exception class precedes that of a parent exception class, the parent catch{} block will catch the exception first and the child catch{} block will never be executed.
Option D: This statement is false. If there is no catch{} block, there does not necessarily have to be a finally{} block. A finally{} block is optional and is used to execute code regardless of whether an exception was thrown or not.
Therefore, the answer is:
The Answer is: C
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.
-
lseek(), open(), write(), close()
-
open(), write(), lseek(), close()
-
open(), lseek(), open(), , write() close()
-
None
-
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()