0

programming languages Online Quiz - 239

Description: programming languages Online Quiz - 239
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

From which problems is it possible for a program to recover?

  1. Errors

  2. Exceptions

  3. Both errors and exceptions

  4. Neither.


Correct Option: B
Explanation:

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

  1. Throwable

  2. Catchable

  3. Runable

  4. Problem


Correct Option: A
Explanation:

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?

  1. No. You can write a program to catch just the exceptions you want.

  2. No. But if a program catches one type of exception it must catch all other types as well.

  3. Yes. If a program is not written to catch all exceptions it will not compile.

  4. Yes. A program can not do I/O unless it catches all exceptions.


Correct Option: A

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?

  1. ArithmeticException

  2. RunTimeException

  3. NumberFormatException

  4. NumberError


Correct Option: C
Explanation:

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

  1. Some of the statements in a try{} block will never throw an exception.

  2. The statements in a try{} block may throw several types of exception.

  3. The try{} block can not contain loops or branches.

  4. The try{} block must appear before the catch{} blocks.


Correct Option: C

Which statement is FALSE about catch{} blocks?

  1. There can be several catch{} blocks in a try/catch structure.

  2. The catch{} block for a child exception class must PRECEED that of a parent execption class.

  3. The catch{} block for a child exception class must FOLLOW that of a parent execption class.

  4. If there is no catch{} block there must be a finally{} block.


Correct Option: C
Explanation:

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

  1. Error, Exception

  2. Exception, RunTimeException

  3. Throwable, RunTimeException

  4. ArithmeticException, RunTimeException


Correct Option: D
Explanation:

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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?

  1. This is not legal, so the program will not compile.

  2. The method throws the exception to its caller, exactly if there were no try{} block.

  3. The program halts immediately.

  4. The program ignores the exception.


Correct Option: B

How many finally{} blocks may there be in a try/catch structure?

  1. There must always be one, following the last catch{} block.

  2. There can be zero or one immediately after each catch{} block.

  3. There can be zero or one, following the last catch{} block.

  4. There can be any number, following the last catch{} block.


Correct Option: C

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?

  1. Always after execution has left a try{} block, no matter for what reason.

  2. Only when an unhandled exception is thrown in a try{} block.

  3. Only when any exception is thrown in a try{} block.

  4. Always just as a method is about to finish.


Correct Option: A

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'

  1. lseek(), open(), write(), close()

  2. open(), write(), lseek(), close()

  3. open(), lseek(), open(), , write() close()

  4. None


Correct Option: B

Difference between fork() & vfork()

  1. vforlk() allows more one child process to be created simaltaneously whereas for() allows only one

  2. vfork() retains same PID for child & parent known as virtual PID

  3. vfork() suspends the parent process until child process exits

  4. fork() & vfork() are same


Correct Option: C

How can I get/set an environment variable from a program?

  1. getenv(), setenv()

  2. getenv(), putenv()

  3. readenv(), writeenv()

  4. You cannot environmement variables inside a program


Correct Option: B

What does alarm() do

  1. Wakes up kernel from sleeping

  2. Wakes up devices from sleeping

  3. Invokes a kernel to send SIGALARM to the calling process

  4. Invokes a kernel to send SIGALARM to the registered process


Correct Option: D

What is a shared memory

  1. A memeory shared between kernel & devices

  2. A memory shared between root user and ordinary user

  3. A memory shared across processes

  4. Harddisk


Correct Option: C

What is a zombie interval?

  1. The interval between child terminating and the parent calling wait()

  2. The interval between parent terminating and the child calling popen()

  3. The interval between parent terminating and the child termination

  4. None


Correct Option: B

Why do processes never decrease in size?

  1. They never releases the memory utilized

  2. They release the memory but kernel does not use them

  3. Since they are running

  4. It’s a false statement


Correct Option: B

How can I find out if someone else has a file open?

  1. Its not possible

  2. use isused() function

  3. Try to open file with locking enabled

  4. Try to write the file after opening


Correct Option: C

How do I `lock' a file?

  1. Use filelock()

  2. use lockfile()

  3. Use lockfd()

  4. use fcntl()


Correct Option: D

How do I find the size of a file inside a program?

  1. Use ls -l

  2. Use sizeof()

  3. Use filesize()

  4. Use fstat()


Correct Option: D
- Hide questions