0

programming languages Online Quiz - 208

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

Identify the true statements.

  1. Checked exceptions must be explicitly caught or propagated(declared Thrown)

  2. Unchecked exceptions must be explicitly caught or propagated(declared Thrown)

  3. Checked exceptions in Java extend the java.lang.Exception class

  4. Unchecked exceptions extend the java.lang.RuntimeException class


Correct Option: A,C,D

By convention, all exception-class names should end with the word Exception

  1. True

  2. False


Correct Option: B

Java guarantees that a finally clause (if one is present) will execute even if a try block exits by using a return, break or continue statement.

  1. True

  2. False


Correct Option: B

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

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

Class Exception is the superclass of all exceptions

  1. True

  2. False


Correct Option: B

Java throws ------------------- when division by zero in integer arithmetic occurs

  1. an IntegerArithmeticException

  2. an ArithmeticException

  3. a DivideByZeroException

  4. a NumberFormatException


Correct Option: B

A try block must always be followed by one or more catch blocks

  1. True

  2. False


Correct Option: B

If a catch handler is written to catch exception objects of a superclass type, it can also catch all objects of that class's subclasses.

  1. True

  2. False


Correct Option: B

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 the question, the user needs to know what parseInt() method does. The parseInt() method is used to convert a string to an integer. If the string argument cannot be parsed as an integer, parseInt() throws a NumberFormatException.

Now, let's go through each option and explain why it is right or wrong:

A. NumberError: There is no exception called NumberError in Java. This option is incorrect.

B. ArithmeticException: The ArithmeticException is thrown when an exceptional arithmetic condition has occurred. Since parseInt() throws a NumberFormatException, this option is incorrect.

C. RunTimeException: RuntimeException is a superclass of all classes that represent exceptional conditions that occur during the execution of Java code. Since NumberFormatException is a subclass of RuntimeException, this option is partially correct. However, the specific exception thrown by parseInt() is NumberFormatException.

D. NumberFormatException: This is the correct answer. When parseInt() gets illegal data, it throws a NumberFormatException. This exception is thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

Therefore, the answer is: D. NumberFormatException.

Is this Legal? Class ExceptionA extends Exception {} class ExceptionB extends ExceptionA {} public class Test{ void thrower() throws ExceptionA{ throw new ExceptionA(); } public static void main(String[] args){ Test t = new Test(); try{t.thrower();} catch(ExceptionB e) {} } }

  1. True

  2. False


Correct Option: B

Which statement is FALSE about the try{} block?

  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
Explanation:

To answer this question, the user needs to know about the try-catch-finally block in exception handling.

Option A is true because not all statements in a try{} block will throw an exception, some statements may never throw an exception.

Option B is true because the statements in a try{} block may throw several types of exception.

Option C is false because the try{} block can contain loops and branches.

Option D is false because the try{} block can appear anywhere in the try-catch-finally block, as long as it appears before the catch{} and finally{} blocks.

Therefore, the answer is:

The Answer is: C. The try{} block can not contain loops or branches.

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

AI Explanation

To answer this question, we need to understand the purpose and behavior of catch{} blocks in a try/catch structure. Let's go through each option to determine which statement is false:

Option A) There can be several catch{} blocks in a try/catch structure. This statement is true. In a try/catch structure, multiple catch{} blocks can be used to handle different types of exceptions that may occur within the try block.

Option B) The catch{} block for a child exception class must PRECEDE that of a parent exception class. This statement is true. When handling exceptions, it is generally recommended to handle more specific (child) exception classes before handling more generic (parent) exception classes. This is because if a catch{} block for a parent exception class is placed before a catch{} block for a child exception class, the catch{} block for the child exception class will never be reached.

Option C) The catch{} block for a child exception class must FOLLOW that of a parent exception class. This statement is false. The catch{} block for a child exception class must PRECEDE that of a parent exception class, as explained in Option B. This ensures that the catch{} block for the child exception class is reached before the catch{} block for the parent exception class.

Option D) If there is no catch{} block, there must be a finally{} block. This statement is false. A finally{} block is not required if there is no catch{} block. A finally{} block is used to specify a block of code that will be executed whether an exception occurs or not. It is not mandatory to include a finally{} block if there are no catch{} blocks.

Therefore, the false statement is Option C) The catch{} block for a child exception class must FOLLOW that of a parent exception class.

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 if there were no matching catch{} 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

When is a finally{} block executed?

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

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

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

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


Correct Option: C

What is Java (in regard to Computer Science) ?

  1. A type of coffee

  2. An object-oriented programming language

  3. An interactive website

  4. None of the above


Correct Option: B

What is an Applet ?

  1. Type of computer

  2. A Java program that is run through a web browser

  3. An interactive website

  4. A type of fruit


Correct Option: B

Java runs on _______.

  1. Windows

  2. Unix/Linux

  3. Mac

  4. All of the Above


Correct Option: D

Why can't the whole program just consist of the one line that does the painting ?

  1. In Java, to create an applet, you can't call on functions without defining a class to call them.

  2. To appear on the web, Java must use an applet, and without first defining a class, you won't be painting onto anything.

  3. The drawString function is not defined without the "import" statements at the top.

  4. All of the above.


Correct Option: D
- Hide questions