About  | Contact  | Site Map  |  Mirror   
Voodoo Exam  |  Question Bank  |  Databases  |  Tutorials   

In this Section
Question Bank 1
Question Bank 2
Question Bank 3
Objective 2



Previous Section
Back to Java
Voodoo Exam
Question Banks
Tutorials
Applets


Main Links
Home
Java
Tabs
Downloads
About Myself


   

Objective 2 - Flow Control & Exception Handling :

We have moved to a new website(www.irixtech.com) where you can now take mock exams online, come back & review your answers later, ask questions in our forums & SCJP questions ranging from SCJP 1.4 to SCJP 6.0, including these question banks & dumps from this site.

Click here to be taken to our new website.


Do read the disclaimer before proceeding . If you believe that any question here doesn't belong to this category bring it to my attention & I'll rectify it . Comments & suggestions will be highly appreciated . If you'd like to see your question's here mail me .


1. Will the following code compile ? If yes , what is the output & if not what is the compiler error ?

 class Test{

	static String raid(){
		try
		{
			throw new Exception();
		}
		catch (Exception e)
		{
			int i = 1 / 0 ;
			return "Error";
		}
		finally
		{
			return "Peace";
		}
	}

	public static void main(String args[]){
		System.out.println(raid());
	}
};

Options :

  1. Compiler error , return "Error" : statement not reached

  2. Compiler error ,  i = 1 / 0 : Exception must be caught or declared in the throws clause

  3. Compiles & prints Error when run

  4. Compiles & prints Peace when run


2. What is the compiler error generated by the following piece of code or what is the output if any ?

 class Test01{

	public static void main(String args[]){
		int i = 1;
		if ( i = 2 )
		{
			System.out.println("In if");
		}
		else System.out.println("In else");
	}
};

Options :

  1. Output = In If
  2. Compiles but no output is generated
  3. Output is = In else
  4. Compiler error , if ( i = 2) : can't convert int to boolean

3. Will the code given below compile ? If run with the following command line - java just - what is the output ?

 class Test02{

	public static void main(String args[]){
		String str = args[0];
		switch ( str.equals("just"))
		{
		case 1:
			System.out.println("case 1");
			break;
		case 2:
			System.out.println("case 2");
			break;
		default:
			break;		
		}
	}
};

Options :

  1. Output = case 1
  2. Output = case 2
  3. No output generated
  4. Compiler error , switch ( str.equals("just")) : Can't convert boolean to int.
  5. Compiler error , case 1 : can't convert boolean to int

4. What is the output of the following code fragment when run with the following command line - java 1 ?

 class Test03{

	public static void main(String args[]){
		int i = Integer.parseInt(args[0]);
		switch (i)
		{
		case 1:
			System.out.println("case 1");
		case 2:
			System.out.println("case 2");
		default:
			System.out.println("default");
			break;		
		}
	}
};

Options :

  1. case 1
    case 2
    default
  2. case 1
  3. Runtime exception thrown -NumberFormatException , Integer.parseInt ( args[0] ) : Can't convert String to int
  4. The code won't compile because there is no method parseInt ( ) defined in the Integer class .

5. Predict the output for the following piece of code .

 class Test04{

	public static void main(String args[]){
		for ( int i = 1 ; i <= 5 ; i++ )
		{
			if ( i < 5 )
			{
				continue;
			}
			else System.out.println( i );
		}
	}
};

Options :

  1. 1 to 5 are printed on a new line each
  2. 1 to 4 are printed on a new line each
  3. Only 5 is printed
  4. 2 to 5 are printed on a new line each
  5. 2 to 4 are printed on a new line each
  6. No output is generated because continue terminates the loop

6. Will the following code block compile ? If yes , what is the output ?

 class Test05{

	public static void main(String args[]){
		try
		{
			throw new Exception();
			System.out.println("try");
		}
		catch (Exception e)
		{
			System.out.println("catch");
		}
		finally
		{
			System.out.println("finally");
		}
	}
};

Options :

  1. try
    catch
    finally
  2. catch
    finally
  3. finally
  4. Exception must be declared in the throws clause
  5. Compiler error , System.out.println("try") : statement not reached

7. What is the output for this ?

 class Test06{

	public static void main(String args[]){
		for (int i = 0 ; i < 5 ; i++)
		{
			System.out.println(i);
			break;
		}
	}
};

Options :

  1. 0 to 5 , each on a separate line
  2. 1 to 5 , each on a new line
  3. 1 to 4 each on a new line
  4. 0
  5. 0 to 4 , each on a new line

8. Will the following code generate a compiler error ? If not , what is the output ?

class Test07{

	public static void main(String args[]){
		for (int i = 0 ; i < 5 ; i++)
		{
			break;
			System.out.println(i);
		}
	}
};

Options :

  1. 0 to 5 , each on a separate line
  2. 1 to 5 , each on a new line
  3. 1 to 4 each on a new line
  4. 0
  5. Compiler error , System.out.println ( i ) : statement not reached
  6. 0 to 4 , each on a new line

9. Choose the most appropriate option for the following piece of code -

 class Test08{

	public static void main(String args[]){
		char c = 'a';
		switch (c)
		{
		case 1:
			System.out.println( "case 1" );
			break;
		case 2:
			System.out.println( "case 2" );
			break;
		case 3:
			System.out.println( "case 3");
			break;
		default:
			System.out.println( "default" );
			break;
		}
	}
};

Options :

  1. Compiler error , switch ( c ) : Can't convert char to int
  2. Compiler error , switch ( c ) : Can't convert char to boolean
  3. Compiles & runs with the output = default
  4. Compile & runs with the output = case 2
  5. Compiler error , switch ( c ) : can't convert String to boolean

10. Does the following piece of code compile ? If yes , what is the output ?

 class Test09{

	static int tomb( char a){
		throw new NumberFormatException();
	}

	public static void main(String args[]){
		try
		{
			tomb('a');
		}
		catch (Exception e)
		{
			System.out.println("Done");
		}
	}
};

Options:

  1. NumberFormatException thrown at runtime
  2. Compiler error , throw new NumberFormatException() : Checked Exceptions must be declared in the throws clause or must be caught in the corresponding catch block
  3. Compiler error , System.out.println("Done") : statement not reached
  4. Output = Done

Answers :

  1. d
  2. d
  3. d
  4. a
  5. c
  6. e
  7. d
  8. e
  9. c
  10. d

End of Page

Disclaimer  |  Contact  |  Mirror  |  Downloads
© 2000-2002 Ashish Hareet