8. Given the following code, the output is __. Please select the correct option "A, B, C, D, or E"
try
{
Integer number = new Integer("1");
System.out.println("An Integer instance.");
}
catch (Exception e)
{
System.out.println("An Exception.");
}
A. An Integer instance.
B. An Exception.
C. 1
D. Error: Exception
E. None of the options
7. Given the following code, the output is __. Please select the correct option "A, B, C, D, or E"
class Sample
{
public static void main (String[] args)
{
try
{
System.out.println(5/0);
}
catch(ArithmeticException e)
{
System.out.print("Divide by zero????");
}
finally
{
System.out.println("5/0");
}
}
}
A. Divide by zero????
B. 5/0
C. Divide by zero????5/0
D. 5/0Divide by zero????
E. "5/0"
6. In Java, __ is the only type of exception that is NOT checked. Please select the correct option "A, B, C, D, or E"
A. The Exception class.
B. The RunTimeException class and its subclasses.
C. The IOException class and its subclasses.
D. The ArithmeticException class only.
E. The Throwable class only.
5. Which method of the "Exception" class prints a list of methods that were called before the exception was thrown? Please select the correct option "A, B, C, D, or E"
A. printErrors()
B. getMessage()
C. printStackTrace()
D. traceStack()
E. toString()
4. Which method of the "Exception" class returns a message string? Please select the correct option "A, B, C, D, or E"
A. getError()
B. getMessage()
C. printMessage()
D. traceMessage()
E. toString()
2. A programmer carelessly assigned a negative value as index of an array. Which type of exception will be raised during execution? Please select the correct option "A, B, C, D, or E"
A. An IndexOutOfBoundsException is thrown.
B. A NumberFormatException is thrown.
C. The first slot of the array is used.
D. This is an Error, so the program immediately terminates no matter what.
E. The last slot of the array is used
1. The following code will cause a(n) ___ exception. Please select the correct option "A, B, C, D, or E"
class Sample
{
public static void main(String[] args)
{
int x = 0;
int y = 10;
int z = y/x;
}
}
A. ArithmeticException
B. ArrayIndexOutOfBoundsException
C. StringIndexOutOfBoundsException
D. NullPointerException
E. ClassNotFoundException
Write Java codes that use the “Scanner” class to ask the user to enter a number. Then, store the given number in a variable of double type named “n”. If n is a negative number, use the “throw” keyword to throw a message, “Java does not provide mechanism to find the squared root of a negative number...”. Then, use the “Exception” class to catch the thrown exception and display the message. If n is a positive number or zero, display the result of Math.sqrt(n).
Make sure the output looks similar to the following.
C:\test>java Sample
Enter a number: -5.1
Java does not provide mechanism to find the squared root of a negative number...
C:\test>java Sample
Enter a number: 5.1
2.2583179370125386
C:\test>java Sample
Enter a number: 0
0.0
A garden watering valve can measure the volume of water coming out of a hose. A user fixed the valve to allow only 0.015 gallon of water. Create a Java exception class named “UnmatchedVolumeException” which will pass the following messages to a numerical controller.
Table:
Condition: volume > 0.015 ------- Message: Excessive volume.
Condition: volume < 0.015 ------- Message: Insufficient volume.
In the “main” method, take an input of volume. Write a proper Java exception handling code to throw an appropriate exception message. Make sure the output looks similar to the following. [Note: Be sure to use input and output dialog boxes]. Please have the input box ask "Enter the volume:" and have the output box give an output of "Excessive volume."
Write necessary class and member function definitions for a cricket player object. (Use array of objects).
The program should accept details from user (max 10) : player code, name, runs, Innings, played, number of times not out.
The program should contain following menu:-
Enter details of players.
Display average runs of a single player.
Average runs of all players