I O T M A: AVA Rogramming
I O T M A: AVA Rogramming
I O T M A: AVA Rogramming
UNIT - 5:
1. An applet is a Java class that extends the? 3. Which of the following is required to view an
A. java.Applet class applet?
A) paint() Answer: B
B) stop()
C) init() 32. State true or false: Applet executes at client
side.
D) destroy()
A) true
Answer: A
B) false
Answer: A
28. Which attribute of applet tag compulsory to
display applet?
A) Code 33. Which are commom restriction in applet?
C) print() D) Secured
D) show() Answer: C
Answer: B
40. State true or false: Applet runs inside the
browser and does not works at client side.
37. Which of the following is a valid
declaration of an applet? (A) true
import java.awt.*; {
{
public static void main(String[] args) throws 71. What is the output of the below Java code
ArithmeticException with exception handling keywords?
{ }
catch(Exception e)
} {
D) }
{ }
A) Inside Try With Resource block.
} B) No output
A) Compiler
80. Is it possible to write Java code efficiently catch(Exception e2)
without using TRY-WITH-RESOURCE
blocks? {System.out.println("a=10");}
A) Yes }
B) No }
C) - A)
D) - a=10
Answer: A b=20
C) Both A and B
D) None 83. Choose the right statement about nesting of
try-catch-finally in Java?
Answer: C
A)
Explanation: Yes. You can use the objects of
classes that implement java.lang.Closeable or try
java.lang.AutoCloseable inside TRY-WITH- {
RESOURCE.
try{}
catch(Exception e1){}
82. What is the output of the below Java
program with nested exceptions? finally{}
public class ExceptionTest14 }
{ catch(Exception e2)
public static void main(String[] args) {}
{ B)
try try
{ {
int a=10/1; try{}
try catch(Exception e1){}
{int b=20/1;} finally{}
catch(Exception e1) }
{ System.out.println("b=20"); } catch(Exception e2)
} {
try{} 85. An exception of user-generated-type is
treated like a ___ exception.
catch(Exception e3){}
A) Checked
finally{}
B) Unchecked
}
C) -
C)
D) -
try
Answer: A
{
Explanation: You can create a custom
} exception class simply by subclassing the class
catch(Exception e2) Exception. You can throw or catch this user-
generated exception like any predefined
{ exception.
}
finally 86. What is the output of the Java code with
{ custom exceptions?
catch(Exception e3){} {
} {
A)
Hello Custom Exception D. Except in case of VM shutdown, if a try
block starts to execute, a corresponding
Bye Custom Exception finally block will always start to execute.
B) Answer: D
Hello Custom Exception Explanation: A is wrong. A try statement can
C) exist without catch, but it must have a finally
statement. B is wrong. A try statement executes
Bye Custom Exception a block. If a value is thrown and the try
D) Compiler error statement has one or more catch clauses that
can catch it, then control will be transferred to
Answer: D the first such catch clause. If that catch block
completes normally, then the try statement
Explanation: You need to catch or throw the
completes normally. C is wrong. Exceptions of
custom exception as it is treated like checked.
type Error and RuntimeException do not have
So, you can not simply call the method
to be caught, only checked exceptions (java.lan
throwing a checked exception. If it is non-
checked, the compilation completes fine.
89. When does Exceptions in Java arises in
code sequence?
87. Which statement is true?
A. Run Time
A. catch(X x) can catch subclasses of X
where X is a subclass of Exception. B. Can Occur Any Time
B. Any statement that can throw an C. Compilation Time
Exception must be enclosed in a try block.
D. None of the mentioned
C. The Error class is a RuntimeException.
Answer: A
D. Any statement that can throw an Error
must be enclosed in a try block. Explanation: Exceptions in Java are run-time
errors.
Answer: A
Explanation: Option A is correct. If the class
specified in the catch clause does have
subclasses, any exception object that subclasses 90. Which of these keywords is not a part of
the specified class will be caught as well. exception handling?
Option B is wrong. The error class is a subclass
of Throwable and not Runtime Exception. A. finally
Option C is wrong. You do not catch this class B. thrown
of error. Option D is wrong. An exception can
be thrown to the next method higher up the C. catch
call stack.
D. try
Answer: B
88. Which statement is true?
Explanation: Exceptional handling is managed
A. An Error that might be thrown in a via 5 keywords try, catch, throws, throw and
method must be declared as thrown by that finally.
method, or be handled within that method.
B. Multiple catch statements can catch the
same class of exception more than once. 91. Which of these keywords must be used to
monitor for exceptions?
C. A try statement must have at least one
corresponding catch block. A. finally
B. throw
C. catch 95. In which of the following package
Exception class exist?
D. try
A. java.file
Answer: D
B. java.lang
Explanation: Try keywords must be used to
monitor for exceptions C. java.io
D. java.util
92. Which of these keywords must be used to Answer: B
handle the exception thrown by try block in
some rational manner? Explanation: No explanation.
A. finally
B. throw 96. Which exception is thrown when divide by
zero statement executes?
C. catch
A. NumberFormatException
D. try
B. NullPointerException
Answer: C
C. ArithmeticException
Explanation: If an exception occurs within the
try block, it is thrown and cached by catch D. None of these
block for processing. Answer: C
Explanation: ArithmeticException is thrown
93. Which of these keywords is used to when divide by zero statement executes.
manually throw an exception?
A. finally 97. What is the output of this program?
B. throw
C. catch class Main
D. try {
Answer: B public static void main(String args[])
Explanation: Throw keywords is used to {
manually throw an exception.
try
{
94. Which of these is a super class of all errors
and exceptions in the Java language? System.out.print("Hello" +
" " + 1 / 0);
A. Catchable
}
B. Throwable
catch(ArithmeticException e)
C. RunTimeExceptions
{
D. None of the above
System.out.print("World");
Answer: B
}
Explanation: Throwable is a super class of all
errors and exceptions in the Java language }
}
A. Hello
B. World
C. HelloWorld
D. Hello World
Answer: B
Explanation: System.out.print() function first 98. What is the output of this program?
converts the whole parameters into a string
and then prints, before ""Hello"" goes to
output stream 1 / 0 error is encountered which class Main
is cached by catch block printing just
""World"". {
public static void main(String args[])
class Main {
{ int a, b;
{ a = 5 / b;
try System.out.print("A");
{ }
int a, b; catch(ArithmeticException e)
b = 0; {
a = 5 / b; System.out.print("B");
System.out.print("A"); }
} finally
catch(ArithmeticException e) {
{ System.out.print("C");
System.out.print("B"); }
} }
} }
} A. A
A. A B. B
B. B C. AC
C. Compilation Error D. BC
}
} class Test extends Exception { }
A. 0
B. 5 class Main {
class Main { {
105. What is the output of this program? 107. Which of these class is related to all the
exceptions that can be caught by using catch?
A. Error
class Main
B. Exception
{
C. RuntimeExecption
public static void main(String[] args)
D. All of the mentioned
{
Answer: B
try
Explanation: Error class is related to java run
{ time error that can't be caught usually,
return; RuntimeExecption is subclass of Exception
class which contains all the exceptions that can
} be caught.
finally 108. Which of these class is related to all the
exceptions that cannot be caught?
A. Error try
B. Exception {
C. RuntimeExecption System.out.print("Hello" + " " + 1 / 0);
D. All of the mentioned }
Answer: A finally
Explanation: Error class is related to java run {
time error that can't be caught usually,
RuntimeExecption is subclass of Exception System.out.print("World");
class which contains all the exceptions that can }
be caught.
}
}
109. Which of these handles the exception when
no catch is used? A. Hello
C. NullPointerException {
Answer: D {
}
} 115. What is the output of this program?
A. -1 class Main
B. 0 {
D. -101 {
Answer: C try
A. try
B. throw 116. What is the output of this program?
C. throws
D. catch class Main
Answer: C {
A) try {
B) finally try
C) throw {
Answer: A }
class exception_handling
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e)
{
System.out.print("0");
}
System.out.print(sum);
}
}
A) 0