Multiple Choice Questions for Java interfaces and exception handling
1. abishekvk@outlook.com
1. If a statement tries to divide by zero which exception is thrown?
A. ArithemticException
B. NullPointerException
C. NumberFormatException
D. None of these
2. Which of these keywords is not a part of exception handling?
A. try
B. finally
C. thrown
D. catch
3. Which of these keywords must be used to monitor for exceptions?
A. try
B. finally
C. throw
D. catch
4. Which of these class is related to all the exceptions that can be caught by using
catch?
A. Error
B. Exception
C. RuntimeExecption
D. All of the mentioned
5. Which of these handles the exception when no catch is used?
A. Default handler
B. finally
C. throw handler
D. Java run time system
6. Which of these keywords is used to manually throw an exception?
A. try
B. finally
C. throw
D. catch
7. If an exception is generated in try block , then it is caught in ____ block
A. finally
B. throw
C. throws
D. catch
2. abishekvk@outlook.com
8. ______ is a superclass of all exception classes.
A. Exception
B. Throwable
C. RuntimeException
D. IOException
9. To create our own exception class , we have to _______
A. Extend exception class
B. Create our own try and catch block
C. use finally block
D. Use throws keyword
10. What is the output of following try catch block try
try
{
int i;
return;
}
catch(Exception e)
{
System.out.println('Hello India―);
}
finally
{
System.out.println('Hello Morbi―);
}
A. Hello India
B. Hello India Hello Morbi
C. Hello Morbi
D. the program will return without printing anything
11. What will be the output of the program?
try { int x = 0; int y = 5 / x; }
catch (Exception e)
{ System.out.println("Exception"); }
catch (ArithmeticException ae)
{ System.out.println(" Arithmetic Exception"); }
System.out.println("finished");
A. finished
B. Exception
C. Compilation fails.
3. abishekvk@outlook.com
D. Arithmetic Exception
12. What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try { badMethod();
System.out.print("A"); }
catch (Exception ex)
{ System.out.print("B"); }
finally { System.out.print("C"); }
System.out.print("D");
}
public static void badMethod()
{ throw new Error(); /* Line 22 */ }
}
A. ABCD
B. Compilation fails.
C. C is printed before exiting with an error message.
D. BC is printed before exiting with an error message.
13. Which of these access specifiers can be used for an interface?
A. Public
B. Protected
C. private
D. All of the mentioned
14. Which of these keywords is used by a class to use an interface defined
previously?
A. import
B. Import
C. implements
D. Implements
15. Which of the following is correct way of implementing an interface salary by class
manager?
4. abishekvk@outlook.com
A. class manager extends salary {}
B. class manager implements salary {}
C. class manager imports salary {}
D. None of the mentioned.
16. What is the output of this program?
interface calculate {
void cal(int item);
}
class display implements calculate
{
int x;
public void cal(int item)
{
x = item * item;
}
}
class interfaces
{
public static void main(String args[])
{
display arr = new display;
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
}
}
A. 0
B. 2
C. 4
D. None of the mentioned
17. Giventhe followingpiece of code:
publicinterface Guard
{
voiddoYourJob();
}
abstract publicclassDog implementsGuard{ }
Whichof the followingstatementsiscorrect?
A. Thiscode will notcompile,becausemethoddoYourJob()ininterfaceGuardmust
be definedabstract.
5. abishekvk@outlook.com
B. Thiscode will notcompile,because classDogmustimplementmethod
doYourJob() frominterface Guard.
C. Thiscode will notcompile,because inthe declarationof classDogwe must use
the keywordextendsinsteadof implements.
D. Thiscode will compilewithoutanyerrors.
18. Interfaces helps in which type of inheritance ?
A. Multiple inheritance
B. Multi-level inheritance
C. Hierarchical Inheritance
D. Multipath inheritance
19. Which of the following is true about interfaces in java.
1) An interface can contain following type of members.
....public, static, final fields (i.e., constants)
....default and static methods with bodies
2) An instance of interface can be created.
3) A class can implement multiple interfaces.
4) Many classes can implement the same interface.
A. 1, 3 and 4
B. 1, 2 and 4
C. 2, 3 and 4
D. 1, 2, 3 and 4
20. Which of these can be used to fully abstract a class from its implementation?
A. Objects
B. Packages
C. Interfaces
D. None of the Mentioned.