|
JAVA Interview Questions 40 - 41
|
Back to the list of all Java Interview Questions
|
|
Question 40: What is a user defined exception?
|
|
User defined exceptions may be implemented by defining a new exception class by extending the Exception class.
|
|
public class MyException extends Exception
{
/* class definition of constructors goes here */
public MyException()
{
super();
}
public MyException (String errorMessage)
{
super (errorMessage);
}
}
|
Throw and/or throws statement is used to signal the occurrence of an exception. To throw an exception:
|
|
throw new MyException(“I threw my own exception.”)
|
To declare an exception:public myMethod() throws MyException {…}
|
|
Question 41: What are the flow control statements in Java?
|
|
The flow control statements allow you to conditionally execute statements, to repeatedly execute a block of
statements, or to just change the sequential flow of control.
|
|
Flow control types
|
Keyword
|
|
Looping
|
while, do-while, for
The body of the while loop is executed only if the expression is true, so it may not be executed even once:
while(i < 5){...}
The body of the do-while loop is executed at least once because the test expression is evaluated
only after executing the loop body. Also, don't forget the ending semicolon after the while
expression.
do { … } while(i < 5);
The for loop syntax is:
for(expr1; expr2; expr3)
{
// body
}
expr1 -> is for initialization, expr2 -> is the conditional test, and expr3 -> is the iteration expression.
Any of these three sections can be omitted and the syntax will still be legal:
for(  ;  ;  ) {} // an endless loop
|
|
Decision making
|
if-else, switch-case
The if-else statement is used for decision-making -- that is, it decides which course of action needs
to be taken.
if (x == 5) {…} else {..}
The switch statement is also used for decision-making, based on an integer expression. The
argument passed to the switch and case statements should be int, short, char, or byte. The
argument passed to the case statement should be a literal or a final variable. If no case matches, the
default statement (which is optional) is executed.
int i = 1;
switch(i)
{
case 0:
System.out.println("Zero");break; //if
break; is omitted case 1: also executed
case 1:
System.out.println("One");break; //if
break; is omitted default: also executed
default:
System.out.println("Default");break;
}
|
|
Branching
|
break, continue, label, return
The break statement is used to exit from a loop or switch statement, while the continue statement
is used to skip just the current iteration and continue with the next. The return is used to return from
a method based on a condition. The label statements can lead to unreadable and unmaintainable
spaghetti code hence should be avoided.
|
|
Exception handling
|
try-catch-finally, throw
Exceptions can be used to define ordinary flow control. This is a misuse of the idea of exceptions,
which are meant only for exceptional conditions and hence should be avoided.
|
|
Back to the list of all Java Interview Questions
|