I O T M A: AVA Rogramming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

INSTITUTE OF TECHNOLOGY & MANAGEMENT, ALIGARH

JAVA PROGRAMMING (C-401)


B.C.A. Second Year (4th Semester)
Session: 2022-23

QUESTION BANK (UNIT-5)

Maintained By – Mr. Arshil Noor


(Assistant Professor)
Department Of Computer Science
&
Engineering

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?

B. java class A. JCM

C. Applet class B. JDM

D. java.applet.Applet class C. JVM

Answer: D D. Java class

Explanation: An applet is a Java class that Answer: C


extends the java.applet.Applet class Explanation: A JVM is required to view an
applet. The JVM can be either a plug-in of the
Web browser or a separate runtime
2. Applets are designed to be embedded within environment.
an __________.
A. Javascript
4. Which method is automatically called after
B. Css the browser calls the init method?
C. HTML A. start
D. SQL B. stop
Answer: C C. destroy
Explanation: Applets are designed to be D. paint
embedded within an HTML page.
Answer: A
Explanation: start : This method is 8. paint() is an abstract method defined in
automatically called after the browser calls the AWT.
init method. It is also called whenever the user
returns to the page containing the applet after A. TRUE
having gone off to other pages. B. FALSE
C. Can be true or false
5. Which method is only called when the D. Can not say
browser shuts down normally?
Answer: A
A. start
Explanation: paint() is an abstract method
B. stop defined in AWT.
C. destroy
D. paint 9. ___________ method is defined in Graphics
Answer: C class, it is used to output a string in an applet.

Explanation: destroy : This method is only A. display()


called when the browser shuts down normally. B. Print()
Because applets are meant to live on an HTML
page, you should not normally leave resources C. drawString()
behind after a user leaves the page that D. transient()
contains the applet.
Answer: C
Explanation: drawString() method is defined in
6. Which of these operators can be used to get Graphics class, it is used to output a string in
run time information about an object? an applet.
A. getInfo
B. Info 10. What will be output for the following code?
C. instanceof
D. getinfoof import java.awt.*;
Answer: C import java.applet.*;
Explanation: instanceof can be used to get run public class myapplet extends Applet
time information about an object
{
public void paint(Graphics g)
7. Which method causes the audio clip to
replay continually? {
A. public void play() g.drawString(""A Simple Applet"", 20,
20);
B. public void loop()
}
C. public void stop()
}
D. None of the above
A. A Simple Applet
Answer: B
B. A Simple Applet 20 20
Explanation: public void loop() : Causes the
audio clip to replay continually C. Compilation Error
D. Runtime Error C. public class MyApplet extends applet
implements Runnable {
Answer: A
D. abstract class MyApplet extends
Explanation: A Simple Applet be output for java.applet.Applet {
the following code.
Answer: A

11. On invoking repaint() method for a


Component, the method invoked by AWT is : 15. Java allows programmers to develop the
following:
A. draw()
A. Applets, applications and Servlets.
B. show()
B. Applets and applications.
C. update()
C. Applets, applications, Servlets,
D. paint() JavaBeans and Distributed objects.
Answer: D D. None of the above.
Explanation: update() method is invoked by Answer: A
AWT.

12. The class at the top of the AWT hierarchy


is
A. Component
B. Window
C. Container
D. Frame 16. Which is a special program that is
embedded in the webpage to generate the
Answer: A dynamic content?
Explanation: Advertisement A) Browser
B) Package
13. Which of the following statements about C) Applet
hierarchy of the class java.awt.Applet is
incorrect? D) None of above
A. An applet is a kind of container Answer: C
B. An applet is a kind of window
C. An applet is a kind of component 17. To write applet application which class we
need to extends?
D. An applet is a kind of panel
A) Component
Answer: C
B) Applet
C) applet
14. Which one of the following is a valid
declaration of an applet? D) None of above
A. public class MyApplet extends Answer: B
java.applet.Applet {
B. public Applet MyApplet {
18. State true or false: Applet application have D) All of Above
main method.
Answer: D
A) true
B) false
23. Which of the following is not part of
Answer: B java.applet.Applet class?
A) paint()
19. Which of the following is used to execute B) start()
applet application?
C) init()
A) Browser
D) stop()
B) AppletViewer
Answer: A
C) Both A & B
D) None of above
Answer: C

24. Which of the following is not part of


20. Which is the correct order of an applet java.awt.Component class?
lifecycle?
A) init()
A) Applet is started, initialized, painted,
destroyed, stopped B) start()

B) Applet is initialized, started, painted, C) paint()


stopped, destroyed D) All of Above
C) Applet is painted, started, stopped, Answer: C
initialized, destroyed
D) None of above
25. Which method is invoked only once during
Answer: B exceution of applet?
A) paint()
21. Which of the following is used for accepting B) start()
user defined parameter in applet?
C) stop()
A) Body
D) destroy()
B) Head
Answer: D
C) Applet
D) Param
26. To write applet application which packages
Answer: D need to import?
A) import java.applet.Applet;
22. Which of the following are steps for Applet B) import java.awt.*;
life cycle?
C) Both A & B
A) init()
D) None of above
B) start()
Answer: C
C) paint()
27. Which method Invoked immediately after C) destroy(), stop()
the start() method and also any time the applet
needs to repaint itself in the browser? D) destroy(), stop(), paint()

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?

B) Width A) An applet cannot access anything on


the system except browser’s services
C) Height
B) Applets can't load libraries or define
D) All of Above native methods
Answer: D C) Applets can play sounds
D) Both A & B
29. Which attribute is required to give the Answer: D
name of the file containing your applet’s
compiled .class file?
A) Code 34. Arrange the steps involved in developing
and testing the applet in correct order.
B) codebase
(i)Creating .class file
C) alt
(ii)Preparing tag
D) name
(iii)Creating HTML file
Answer: A
(iv)Write an applet code (.java file)
(v)testing the applet code
30. Applet execution start from
A) 1-ii, 2-iii, 3-iv, 4-v, 5-i
A) init() method
B) 1-i, 2-ii, 3-iii, 4-iv, 5-v
B) main() method
C) 1-iv, 2-i, 3-ii, 4-iii, 5-v
C) paint() method
D) 1-iii, 2-iv, 3-v, 4-i, 5-ii
D) All of Above
Answer: C
Answer: A

35. PARAM tag used for


31. when an applet is terminated the following
sequence of methods calls takes place? A) To specify applet-specific arguments in
an HTML page
A) stop(), paint(), destroy()
B) To specify browser-specific arguments
B) stop(), destroy() in an HTML page
C) To specify client-specific arguments in
an HTML page
39. Which of the following statement is a
D) None of above drawback of Applet?
Answer: A A) It can be executed by browsers running
under many platforms, including Linux,
Windows, and Mac Os etc.
36. Which methods can be used to display a B) It works at client side so less response
string in an applet? time
A) display() C) Plugin is required at client browser to
B) drawString() execute 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

A) public class MyApplet extends (B) false


java.applet.Applet { Answer: B
B) public Applet MyApplet {
C) public class MyApplet extends applet 41. Which of the following is correct syntax to
implements Runnable { display 30*50 rectangle as output in applet?
D) abstract class MyApplet extends A) g.drawRect(10,20,50,30);
java.applet.Applet {
B) g.drawRect(30,50,10,30);
Answer: A
C) g.drawRect(10,20,30,50);
D) g.drawRect(30,50,10,50);
38. What will be the output of following applet
program? Answer: C
import java.awt.*;
import java.applet.*; 42. Which of the following is correct syntax to
display circle of redius 20 as output in applet?
public class myapplet extends Applet {
A) g.drawCircle(10,20,20,20);
public void paint(Graphics g) {
B) g.drawOval(10,20,20,20);
g.drawString("A Simple Applet", 20, 20);
C) g.drawCircle(20,20,40,40);
}
D) g.drawOval(10,20,40,40);
}
Answer: D
A) Compilation Error
B) A Simple Applet 20 20
43. drawLine, drawRect, drawString are
C) Runtime Error available in which of the following class?
D) A Simple Applet A) Paint
Answer: D
B) Graphics
C) Applet 48. Which method invoked by AWT when
invoking repaint() method for component?
D) Design
A) show()
Answer: B
B) draw()
C) paint()
44. To display vertical line which of the
following method is used? D) update()
A) drawStraightLine() Answer: C
B) drawVerticalLine()
C) drawLine() 49. Program which executes applet is known as
D) None of above A) JVM
Answer: C B) virtual machine
C) applet engine
45. To add component in an applet which of D) None of above
following is used?
Answer: C
A) add()
B) put()
50. Which of these functions is called to display
C) place() the output of an applet?
D) None of above A) display()
Answer: A B) paint()
C) displayApplet()
46. What will be the output of getCodeBase( ) D) PrintApplet()
method?
Answer: B
A) Returns the URL of the HTML
document that invokes the applet Explanation: Whenever the applet requires to
redraw its output, it is done by using method
B) Returns the URL associated with the paint().
invoking applet
C) Both A & B
51. Which of these methods can be used to
D) None of above output a string in an applet?
Answer: B A) display()
B) print()
47. What is fullform of AWT? C) drawString()
A) Application Window Toolkit D) transient()
B) Abstract Web Toolkit Answer: C
C) Abstract Writing Toolkit Explanation: drawString() method is defined in
Graphics class, it is used to output a string in
D) Abstract Window Toolkit an applet.
Answer: D
52. Which of these methods is a part of import java.applet.*;
Abstract Window Toolkit (AWT) ?
public class myapplet extends Applet
A) display()
{
B) paint()
public void paint(Graphics g)
C) drawString()
{
D) transient()
g.drawString("A Simple Applet", 20, 20);
Answer: B
}
Explanation: paint() is an abstract method
defined in AWT. }
A) A Simple Applet
B) A Simple Applet 20 20
C) Compilation Error

53. Which of these modifiers can be used for a D) Runtime Error


variable so that it can be accessed from any Answer: A
thread or parts of a program?
Explanation: None.
A) transient
Output:
B) volatile
A Simple Applet
C) global
(Output comes in a new java application)
D) No modifier is needed
Answer: B
56. What is the length of the application box
Explanation: The volatile modifier tells the made by the following Java program?
compiler that the variable modified by volatile
can be changed unexpectedly by other part of
the program. Specially used in situations import java.awt.*;
involving multithreading.
import java.applet.*;
public class myapplet extends Applet
54. Which of these operators can be used to get
run time information about an object? {
A) getInfo public void paint(Graphics g)
B) Info {
C) instanceof g.drawString("A Simple Applet", 20, 20);
D) getinfoof }
Answer: C }
Explanation: None. A) 20
B) 50
55. What is the Message is displayed in the C) 100
applet made by the following Java program?
D) System dependent
Answer: A
import java.awt.*;
Explanation: the code in pain() method – CharArrayReader input2 = new
g.drawString(“A Simple Applet”,20,20); draws CharArrayReader(c, 1, 4);
a applet box of length 20 and width 20.
int i;
57. What is the length of the application box
made the following Java program? int j;
try

import java.awt.*; {

import java.applet.*; while((i = input1.read()) == (j =


input2.read()))
public class myapplet extends Applet
{
{
System.out.print((char)i);
Graphic g;
}
g.drawString("A Simple Applet", 20, 20);
}
}
catch (IOException e)
A) 20
{
B) Default value
e.printStackTrace();
C) Compilation Error
}
D) Runtime Error
}
Answer: C
}
Explanation: To implement the method
drawString we need first need to define A) abc
abstract method of AWT that is paint() B) abcd
method. Without paint() method we can not
define and use drawString or any Graphic C) abcde
class methods. D) none of the mentioned
Answer: D
58. What will be the output of the following Explanation: No output is printed.
Java program? CharArrayReader object input1 contains
string “abcdefgh” whereas object input2
contains string “bcde”, when
import java.io.*; while((i=input1.read())==(j=input2.read())) is
class Chararrayinput executed the starting character of each object is
compared since they are unequal control comes
{ out of loop and nothing is printed on the
screen.
public static void main(String[] args)
Output:
{
$ javac Chararrayinput.java
String obj = "abcdefgh";
$ java Chararrayinput
int length = obj.length();
char c[] = new char[length];
59. Exception is a class/interface/abstract
obj.getChars(0, length, c, 0); class/other?
CharArrayReader input1 = new A. Class
CharArrayReader(c);
B. Interface C. catch(X x) can catch subclasses of X
where X is a subclass of Exception.
C. Abstract class
D. Any statement that can throw an
D. Other Exception must be enclosed in a try block.
Answer: A Answer: C
Explanation: Not available for this question. Explanation: Option A is correct. If the class
specified in the catch clause does have
subclasses, any exception object that subclasses
60. Exception is found in which package in java the specified class will be caught as well.
A. java Option B is wrong. The error class is a subclass
of Throwable and not Runtime Exception.
B. java.io Option C is wrong. You do not catch this class
of error. Option D is wrong. An exception can
C. java.util
be thrown to the next method higher up the
D. java.lang call stack.
Answer: D
Explanation: Not available for this question. 64. Which statement is true?
A. A try statement must have at least one
corresponding catch block.
61. When does Exceptions in Java arises in
code sequence? B. Multiple catch statements can catch the
same class of exception more than once.
A. Run Time
C. Except in case of VM shutdown, if a try
B. Compilation Time block starts to execute, a corresponding
C. Can Occur Any Time finally block will always start to execute.

D. None of the mentioned D. An Error that might be thrown in a


method must be declared as thrown by that
Answer: A method, or be handled within that method.
Explanation: Exceptions in Java are run-time Answer: C
errors.
Explanation: A is wrong. A try statement can
exist without catch, but it must have a finally
62. Which of these keywords is not a part of statement. B is wrong. A try statement executes
exception handling? a block. If a value is thrown and the try
statement has one or more catch clauses that
A. try can catch it, then control will be transferred to
the first such catch clause. If that catch block
B. finally
completes normally, then the try statement
C. thrown completes normally. C is wrong. Exceptions of
type Error and RuntimeException do not have
D. catch to be caught, only checked exceptions (java.lan
Answer: C
Explanation: Not available for this question. 65. The class at the top of exception class
hierarchy is .................

63. Which statement is true? A. Object

A. The Error class is a RuntimeException. B. Throwable

B. Any statement that can throw an Error C. Exception


must be enclosed in a try block.
D. ArithmeticException System.out.println("I am happy.");
Answer: B throw new ArithmeticException();
Explanation: Not available for this question. }
}
66. In which of the following package A) I am happy.
Exception class exist?
B) No output
A. java.io
C) Compiler error
B. java.lang
D) None of the above
C. java.util
Answer: A
D. java.file
Explanation: ArithmeticException is an
Answer: B unchecked exception. So, the compiler does not
stop with errors.
Explanation: Not available for this question
67. Exception generated in try block is caught
in ........... block. 70. What is the output of the Java code with
exceptions?
A. throw
public class ExceptionTest9
B. throws
{
C. catch
public static void main(String[] args)
D. finally
{
Answer: C
System.out.println("I am going to forest.");
Explanation: Not available for this question.
throw new ClassNotFoundException();
}
68. Which keyword is used to explicitly throw
an exception? }
A. try A) I am going to forest.
B. catch B) No output
C. throw C) Compiler error
D. throwing D) None of the above
Answer: C Answer: C
Explanation: Not available for this question. Explanation: ClassNotFoundException is a
checked exception in Java. So, the compiler
throws error for not handling it using TRY
69. What is the output of the below Java code CATCH FINALLY statements.
with exceptions? Unresolved compilation problem: Unhandled
public class ExceptionTest8 exception type ClassNotFoundException

{
public static void main(String[] args) throws 71. What is the output of the below Java code
ArithmeticException with exception handling keywords?

{ public class ExceptionTest10


{ Answer: D
public static void main(String[] args) throws
Exception
73. Can you catch an object of type Error in
{ Java using TRY-CATCH blocks?
System.out.println("Good Morning Cortana."); A) Yes
throw new NoSuchMethodException(); B) No
} C) -
} D) -
A) Good Morning Cortana. Answer: A
B) Explanation: Yes. You can catch. But do not
catch as these indicate some serious problems.
Good Morning Cortana.
Exception in thread "main"
java.lang.NoSuchMethodException 74. Which is the superclass of Error class in
Java?
at
Project1/package1.ExceptionTest1 A) RuntimeException
0.main(ExceptionTest10.java:8)
B) Exception
C) Compiler error
C) Throwable
D) None of the above
D) None of the above
Answer: B
Answer: C
Explanation: NoSuchMethodException is a
checked exception in Java. Here, you are telling Explanation: Yes. Throwable is the superclass
the compiler that the MAIN method throws of both Error and Exception classes in Java.
some exception. So, the program compiles and
outputs. As the code is throwing a newly
created exception, the execution stops here. 75. An Error is treated like a _____ type of
exception by the compiler.
A) Unchecked
72. What are the difference between Checked
and Unchecked exceptions in Java? B) Checked

A) Unchecked exceptions produce less C) -


number of compiler errors than the code D) -
with Checked exceptions.
Answer: A
B) Most of the unchecked exceptions have
RuntimeException as the superclass. Explanation: An Error is treated like an
Checked exceptions have different classes Unchecked exception in Java. So, the compiler
are superclasses. does not show any errors for not-handling the
errors properly.
C) Unchecked exceptions cause more
number of runtime exceptions at the time
of execution. As the Checked exceptions
76. Which is the exception handling block that
are handled well with TRY CATCH
closes the used resources inside of it
blocks, you will get less number of
automatically in Java?
exceptions or errors at the time of
execution. A) TRY
D) All the above B) CATCH
C) FINALLY B) Runtime
D) TRY with resource C) Programmer
Answer: D D) None
Explanation: Yes. TRY-with-resource block Answer: A
automatically closes the specified resource
automatically. Explanation: The compiler inserts the
necessary code inside the FINALLY block
(whether explicitly written or not by the
programmer) to close the resources.
77. Choose the correct syntax of a try-with-
resource in Java.
A) 79. What is the output of the below Java
program with Try With Resource block?
try(var1 initialized; var2 initialized;)
import java.io.*;
{

public class ExceptionTest12


}
{
B)
public static void main(String[] args)
try(var1; var2;)
{
{
try(BufferedWriter bw = new
BufferedWriter(new FileWriter("abc.txt"));)
} {
C) System.out.println("Inside Try With Resource
try(var1 initialized, var2 initialized;) block.");

{ }
catch(Exception e)

} {

D) }

try(var1 uninitialized, var2 uninitialized;) }

{ }
A) Inside Try With Resource block.

} B) No output

Answer: A C) Compiler error

Explanation: You should always initialize D) None of the above


variables used inside TRY-WITH-RESOURCE Answer: A
block in order to close those automatically.
Explanation: The resource variable "bw"
referring BufferedWriter will be closed
78. Who closes the resources using code in a automatically. Otherwise, the memory will be
TRY-with-resources block in Java? filled and wasted.

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

Explanation: Yes. Use the normal TRY, B)


CATCH and FINALLY blocks. Keep the code b=20
that closes resources inside FINALLY block.
a=10
C) Compiler error
81. A try-with-resource supports only the
objects of classes to close automatically that D) No output
implement ___ Interface in Java. Answer: C
A) java.lang.Closeable Explanation: As no exception is thrown, no
B) java.lang.AutoCloseable output is produced.

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?

try{} public class ExceptionTest15

catch(Exception e3){} {

finally{} void show(int a) throws MyException

} {

D) All the above System.out.println("Hello Custom Exception");

Answer: D int b = a/0;

Explanation: Nesting of exception handling }


blocks is allowed in any order. So, a try-catch- public static void main(String[] args)
finally can be kept inside another TRY or
CATCH or FINALLY block without errors. {
ExceptionTest15 obj = new ExceptionTest15();

84. The variables initialized inside a TRY-with- obj.show(5);


resource are treated like ____ variables in System.out.println("Bye Custom Exception");
Java.
}
A) static
}
B) instance
C) final
class MyException extends Exception
D) None of the above
{
Answer: C
MyException(){ super();}
Explanation: A try-with-resource statement
treats the variables like FINAL. So, MyException(String name){ super(name); }
reinitializing is not possible and the compiler
throws error. }

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[])

97. What is the output of this program? {


try

class Main {

{ int a, b;

public static void main(String args[]) b = 0;

{ 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

D. Runtime Error Answer: D

Answer: B Explanation: finally keyword is used to execute


the code before try and catch block end.
Explanation: No explanation

99. What is the output of this program?


catch(int e) {
class Main System.out.println("Got the
Exception " + e);
{
}
public static void main(String args[])
}
{
}
try
A. Got the Exception 10
{
B. Got the Exception 0
int i, sum;
C. Compiler Error
sum = 10;
D. None of the above
for (i = -1; i < 3 ;++i)
Answer: C
sum = (sum / i);
Explanation: In Java only throwable objects
} (Throwable objects are instances of any
catch(ArithmeticException e) subclass of the Throwable class) can be thrown
as exception. So basic data type can no be
{ thrown at all.
System.out.print("0");
} 101. What will be output for the following
System.out.print(sum); code?

}
} class Test extends Exception { }

A. 0
B. 5 class Main {

C. Compilation Error public static void main(String args[]) {

D. Runtime Error try {

Answer: C throw new Test();

Explanation: Value of variable sum is printed }


outside of try block, sum is declared only in try catch(Test t) {
block, outside try block it is undefined.
System.out.println("Got the Test
100. Predict the output of following Java Exception");
program?
}
finally {
class Main
System.out.println("Inside finally block ");
{
}
public static void main(String args[]) {
}
try {
}
throw 10;
A. Got the Test Exception
}
Inside finally block
B. Got the Test Exception {
C. Compiler Error throw new Derived();
D. Inside finally block }
Answer: A catch(Base b)
Explanation: In Java, the finally is always {
executed after the try-catch block. This block
can be used to do the common cleanup work. System.out.println("Caught
There is no such block in C++. base class exception");
}

102. Output of following Java program? catch(Derived d)

class Main { {

public static void main(String args[]) System.out.println("Caught


derived class exception");
{
}
int x = 0;
}
int y = 10;
}
int z = y/x;
A. Caught base class exception
}
B. Caught derived class exception
}
C. Compiler Error because derived is not
throwable
A. Compiler Error D. Compiler Error because base class
exception is caught before derived class
B. Compiles and runs fine
Answer: D
C. Compiles fine but throws
ArithmeticException exception Explanation: No explanation.
D. None of the above
Answer: C 104. What will be output for the following
code?
Explanation: ArithmeticException is an
unchecked exception, i.e., not checked by the
compiler. So the program compiles fine.
class Test
{
103. What will be output for the following
code? public static void main (String[] args)
{

class Base extends Exception {} try

class Derived extends Base {} {

public class Main { int a = 0;

public static void main(String args[]) System.out.println("a = " +


a);
{
int b = 20 / a;
try
System.out.println("b = " + {
b);
} System.out.println( "Finally" );
catch(ArithmeticException e) }
{ }
System.out.println("Divide }
by zero error");
A. Finally
}
B. Compilation fails.
finally
C. The code runs with no output.
{
D. An exception is thrown at runtime.
System.out.println("inside
Answer: A
the finally block");
Explanation: If you put a finally block after a
}
try and its associated catch blocks, then once
} execution enters the try block, the code in that
finally block will definitely be executed
}
A. Compile error
106. Which of these is a super class of all
B. Divide by zero error exceptional type classes?
C. Divide by zero error A. String
inside the finally block B. RuntimeExceptions
D. inside the finally block C. Throwable
Answer: C D. Cacheable
Explanation: On division of 20 by 0, divide by Answer: C
zero exception occurs and control goes inside
the catch block. Also, the finally block is always Explanation: All the exception types are
executed whether an exception occurs or not. subclasses of the built in class Throwable.

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

A. Default handler B. World

B. finally C. Compilation Error

C. throw handler D. First Exception then World

D. Java run time system Answer: D

Answer: A Explanation: None. Output: $ javac


exception_handling.java $ java
Explanation: None. exception_handling Exception in thread
""main"" java.lang.ArithmeticException: / by
zero World
110. What exception thrown by parseInt()
method?
A. ArithmeticException 112. What is the output of this program?

B. ClassNotFoundException class Main

C. NullPointerException {

D. NumberFormatException public static void main(String args[])

Answer: D {

Explanation: parseInt() method parses input try


into integer. The exception thrown by this {
method is NumberFormatException.
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
{
111. What is the output of this program?
sum = (sum / i);
class Main
System.out.print(i);
{
}
public static void main(String args[])
}
{
catch(ArithmeticException e)
{ specify this behaviour the behaviour so that
callers of the method can guard themselves
System.out.print("0"); against that exception. This is done by using
} throws clause in methods declaration.

}
} 115. What is the output of this program?

A. -1 class Main

B. 0 {

C. -10 public static void main(String args[])

D. -101 {

Answer: C try

Explanation: For the 1st iteration -1 is { System.out.print("A");


displayed. The 2nd exception is caught in catch throw new NullPointerException ("Hello");
block and 0 is displayed. Output: $ javac
exception_handling.java $ java }
exception_handling -10
catch(ArithmeticException e)
{
113. Which of these operator is used to
generate an instance of an exception than can System.out.print("B");
be thrown by using throw? }
A. new }
B. malloc }
C. alloc
D. thrown A. A
Answer: A B. B
Explanation: new is used to create an instance C. Hello
of an exception. All of java's built in run-time
exceptions have two constructors: one with no D. NullPointerException
parameters and one that takes a string Answer: D
parameter.
Explanation: None. Output: $ javac
exception_handling.java $ java
114. Which of these keywords is used to by the exception_handling Exception in thread
calling function to guard against the exception ""main"" java.lang.NullPointerException:
that is thrown by called function? Hello at exception_handling.main

A. try
B. throw 116. What is the output of this program?

C. throws
D. catch class Main

Answer: C {

Explanation: If a method is capable of causing public static void main(String[] args)


an exception that it does not handle. It must {
try A. ArithmeticException
{ B. MathException
return; C. IllegalAccessException
} D. IllegarException
finally Answer: A
{ Explanation: None.
System.out.println( "Finally" );
} 119. Which of these exceptions will occur if we
try to access the index of an array beyond its
} length?
} A. ArithmeticException
A. Finally B. ArrayException
B. Compilation fails C. ArrayIndexException
C. The code runs with no output D. ArrayIndexOutOfBoundsException
D. An exception is thrown at runtime Answer: D
Answer: A Explanation:
Explanation: Because finally will execute ArrayIndexOutOfBoundsException is a built
always. in exception that is caused when we try to
access an index location which is beyond the
length of an array.
117. A single try block must be followed by
which of these?
120. What is the output of this program?
A. finally
class Main
B. catch
{
C. finally & catch
public static void main(String args[])
D. none of the mentioned
{
Answer: C
try
Explanation: try block can be followed by any
of finally or catch block, try block checks for {
exceptions and work is performed by finally int a = args.length;
and catch block as per the exception. B is
wrong. A try statement executes a block. If a int b = 10 / a;
value is thrown and the try statement has one
or more catch clauses that can catch it, then System.out.print(a);
control will be transferred to the first such }
catch clause. If that catch block completes
normally, then the try statement completes catch (ArithmeticException e)
normally. C is wrong. Exceptions of type Error {
and Runt
System.out.println("1");
}
118. Which of these exceptions handles the
divide by zero error? }
}
A. 0
B. 1
C. Compilation Error
D. Runtime Error 124. Which of these keywords must be used to
handle the exception thrown by try block in
Answer: B some rational manner?
Explanation: None. Output: advertisement $ A) try
javac exception_handling.java $ java
exception_handling 1 B) finally
121. When does Exceptions in Java arises in C) throw
code sequence?
D) catch
A) Run Time
Answer: D
B) Compilation Time
Explanation: If an exception occurs within the
C) Can Occur Any Time try block, it is thrown and cached by catch
block for processing.
D) None of the mentioned
Answer: A
125. Which of these keywords is used to
Explanation: Exceptions in Java are run-time manually throw an exception?
errors.
A) try
B) finally
122. Which of these keywords is not a part of
exception handling? C) throw
A) try D) catch
B) finally Answer: C
C) thrown Explanation: None.
D) catch
Answer: C 126. What will be the output of the following
Java program?
Explanation: Exceptional handling is managed
via 5 keywords – try, catch, throws, throw and
finally.
class exception_handling
{
123. Which of these keywords must be used to
monitor for exceptions? public static void main(String args[])

A) try {

B) finally try

C) throw {

D) catch System.out.print("Hello" + " " + 1 / 0);

Answer: A }

Explanation: None. catch(ArithmeticException e)


{ }
System.out.print("World"); }
} }
} A) A
} B) B
A) Hello C) Compilation Error
B) World D) Runtime Error
C) HelloWorld Answer: B
D) Hello World Explanation: None.
Answer: B Output:
Explanation: System.ou.print() function first $ javac exception_handling.java
converts the whole parameters into a string
and then prints, before “Hello” goes to output $ java exception_handling
stream 1 / 0 error is encountered which is B
cached by catch block printing just “World”.
Output:
128. What will be the output of the following
$ javac exception_handling.java Java program?
$ java exception_handling
World class exception_handling
{
127. What will be the output of the following public static void main(String args[])
Java program?
{
try
class exception_handling
{
{
int a, b;
public static void main(String args[])
b = 0;
{
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");
}
} B) 05
} C) Compilation Error
A) A D) Runtime Error
B) B Answer: c
C) AC Explanation: Value of variable sum is printed
outside of try block, sum is declared only in try
D) BC block, outside try block it is undefined.
Answer: D Output:
Explanation: finally keyword is used to execute $ javac exception_handling.java
the code before try and catch block end.
Exception in thread "main" java.lang.Error:
Output: Unresolved compilation problem:
$ javac exception_handling.java sum cannot be resolved to a variable
$ java exception_handling
BC

129. What will be the output of the following


Java program?

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy