Saturday, 25 May 2013

Exception - 26 -

package net.dharmaraj;

class Level1Exception extends Exception
{
}

class Level2Exception extends Level1Exception
{
}

class Level3Exception extends Level2Exception
{
}

class Purple
{
    public static void main(String args[])
    {
        int a, b, c, d, f, g, x;
        a = b = c = d = f = g = 0;
        x = 2;
        try
        {
            try
            {
                switch (x) {
                case 1:
                    throw new Level1Exception();
                case 2:
                    throw new Level2Exception();
                case 3:
                    throw new Level3Exception();
                }
                a++;
            }
            catch (Level2Exception e)
            {
                b++;
            }
            finally
            {
                c++;
            }
        }
        catch (Level1Exception e)
        {
            d++;
        }
        catch (Exception e)
        {
            f++;
        }
        finally
        {
            g++;
        }
        System.out.println(a + "," + b + "," + c + "," + d + "," + f + "," + g);
    }
}

Exception - 25 -


package net.dharmaraj;

public class ThrowsDemo
{

    static void throwMethod() throws IllegalAccessException
    {
        System.out.println("Inside throwMetho.");
        throw new IllegalAccessException("demo");
    }

    public static void main(String a[])
    {
        // ThrowsDemo s=new ThrowsDemo();
        try
        {
            throwMethod();
        }
        catch (IllegalAccessException e)
        {
            System.out.println("Caught " + e);
        }
    }

}


Out Put:--
Inside throwMetho.
Caught java.lang.IllegalAccessException: demo

Exception - 24 -

package net.dharmaraj;

public class TechnoSample
{
    public static void main(String args[])
    {
        int i = 2;
        try
        {
            double x = Math.random() * 2;

            System.out.println(x);// 0.4545
            i *= (int) Math.floor(x);
            System.out.println(i);// 0
            if (i > 1)
                System.out.println("No arithmetic exception");
        }
        catch (ArithmeticException ae)
        {
            System.out.println("Arithmetic exception cautht");
        }
        finally
        {
            System.out.println(i);
        }

    }
}


Out Put:--
1.5059545776537702
2
No arithmetic exception
2

Exception - 23 -

package net.dharmaraj;

public class TechnoSample
{
    public static void main(String[] args) throws Exception
    {
        int i = 2;
        boolean b = true;
        throw new Exception("Values are:" + (b != b) + "," + (i = args.length) + "," + (b = i == 2));
    }
}

Out Put :

Exception in thread "main" java.lang.Exception: Values are:false,0,false
at net.dharmaraj.TechnoSample.main(TechnoSample.java:9)

Exception - 22 -

Imagine there are two exception classes called Exception1 and Exception2 that descend from the Exception class. Given these two class definitions: 


package net.dharmaraj;

class First
{
    void test() throws Exception1, Exception2 { . . . }
}

class Second extends First
{
    void test() { . . . }
}

Create a class called Third that extends Second and defines a test() 
method. What exceptions can Third's test() method throw? 
Select all valid answers. 
a) Exception1 
b) Exception2 
c) no checked exceptions 
d) any exceptions declared in the throws clause of the Third's test() method. 

Exception - 21 -

Given this code snippet: 
try { 
tryThis(); 
return; 
} catch (IOException x1) { 
System.out.println("exception 1"); 
return; 
} catch (Exception x2) { 
System.out.println("exception 2"); 
return; 
} finally { 
System.out.println("finally");
}


What will appear in the standard output if tryThis() throws a NumberFormatException? 
Select the one right answer. 
a) Nothing 
b) "exception 1", followed by "finally" 
c) "exception 2", followed by "finally" 
d) "exception 1" 
e) "exception 2" 



 try { 
tryThis(); 
return; 
} catch (IOException x1) { 
System.out.println("exception 1"); 
return; 
} catch (Exception x2) { 
System.out.println("exception 2"); 
return; 
} finally { 
System.out.println("finally");
}


Answers.:- c

Exception - 20 -

What all gets printed when the following gets compiled and run. Select the two correct answers.

package net.dharmaraj;

public class test
{
    public static void main(String args[])
    {
        int i = 1, j = 1;
        try
        {
            i++;
            j--;
            if (i == j)
                i++;
        }
        catch (ArithmeticException e)
        {
            System.out.println(0);
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println(1);
        }
        catch (Exception e)
        {
            System.out.println(2);
        }
        finally
        {
            System.out.println(3);
        }
        System.out.println(4);
    }
}



a) 0 
b) 1 
c) 2 
d) 3 
e) 4  


Out Put:--

3
4

Exception - 19 -

What all gets printed when the following gets compiled and run. Select the three correct answers. 

package net.dharmaraj;

public class test
{
    public static void main(String args[])
    {
        int i = 1, j = 1;
        try
        {
            i++;
            j--;
            if (i / j > 1)
                i++;
        }
        catch (ArithmeticException e)
        {
            System.out.println(0);
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println(1);
        }
        catch (Exception e)
        {
            System.out.println(2);
        }
        finally
        {
            System.out.println(3);
        }
        System.out.println(4);
    }
}


a) 0
b) 1 
c) 2 
d) 3 
e) 4 


Out Put:--
0
3
4

Exception - 18 -

Given the following code

What code would be most likely for the body of the ioCall method


package net.dharmaraj;

import java.io.*;

public class Th
{
    public static void main(String argv[])
    {
        Th t = new Th();
        t.amethod();
    }

    public void amethod()
    {
        try
        {
            ioCall();
        }
        catch (IOException ioe)
        {
        }
    }
}


1) public void ioCall ()throws IOException{
 DataInputStream din = new DataInputStream(System.in);
 din.readChar();
 }
2) public void ioCall ()throw IOException{
 DataInputStream din = new DataInputStream(System.in);
 din.readChar();
 }
3) public void ioCall (){
 DataInputStream din = new DataInputStream(System.in);
 din.readChar();
 }
4) public void ioCall throws IOException(){
 DataInputStream din = new DataInputStream(System.in);
 din.readChar();
 }



Compile Out Put:--

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method ioCall() is undefined for the type Th

at net.dharmaraj.Th.amethod(Th.java:17)

at net.dharmaraj.Th.main(Th.java:10)

Exception - 17 -

What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current directory?

package net.dharmaraj;

import java.io.*;

public class Mine
{
    public static void main(String argv[])
    {
        Mine m = new Mine();
        System.out.println(m.amethod());
    }

    public int amethod()
    {
        try
        {
            FileInputStream dis = new FileInputStream("Hello.txt");
        }
        catch (FileNotFoundException fne)
        {
            System.out.println("No such file found");
            return -1;
        }

        finally
        {
            System.out.println("Doing finally");
        }
        return 0;
    }
}


1) No such file found 
2 No such file found ,-1 
3) No such file found, Doing finally, -1 
4) 0 



Compile Out Put:--
No such file found
Doing finally
-1

Exception - 16 -

Please select the correct answer from the following? 

package net.dharmaraj;

public class ThrowsDemo
{
    static void throwMethod() throws Exception
    {
        System.out.println("Inside throwMethod.");
        throw new IllegalAccessException("demo");
    }

    public static void main(String args[])
    {
        try
        {
            throwMethod();
        }
        catch (IllegalAccessException e)
        {
            System.out.println("Caught " + e);
        }

    }
}



A) Compilation error 
B) Runtime error 
C) Compile successfully, nothing is printed. 
D) inside throwMethod. followed by caught: java.lang.IllegalAccessException: demo 

Exception - 15 -

The thread run() method has the following code, what is the result 
when the thread runs? 
try { 
     sleep( 200 ); 
     System.out.println( "Printing from thread run() method" ); 
} catch ( IOException ie) { } 



Code :--

public class dev
{
public static void main(String a[])
{
try 

      sleep( 200 ); 
      System.out.println( "Printing from thread run() method" ); 

catch ( IOException ie) { } 
}
}

A) Compile time error 
B) Prints on the console Printing from thread run() method 
C) At line 2 the thread will be stop running and resumes after 200 milliseconds and prints "Printing from thread run() method"
D) At line 2 the thread will be stop running and resumes exactly 200 milliseconds elapsed 

Exception - 14 -

What is the result from the following code when you run? 

package net.dharmaraj;

import java.io.*;

class A
{
    A()
    {
        System.out.println("Executing class A1 constructor");
    }

    A(int a) throws Exception
    {
        System.out.println("Executing class A2 constructor");
        throw new IOException("Dunu");
    }
}

public class B extends A
{
    B()
    {
        System.out.println("Executing class B constructor");
    }

    public static void main(String args[])
    {
        try
        {
            A a = new B();
            // A a1=new A(2);

        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

A) Executing class A1 constructor followed by Executing class B constructor 
B) No output 
C) Runtime error 
D) Compile time error


Compile Out pur :--

Executing class A1 constructor
Executing class B constructor

Exception - 13 -

What is the result from the following code when you run? 

package net.dharmaraj;

import java.io.*;

class A
{
    A() throws Exception
    {
        System.out.println("Executing class A constructor");
        throw new IOException("Dunu");
    }
}

public class B extends A
{
    B()
    {
        /*
         * try{ super(); System.out.println ("Executing class B constructor"); }catch(Exception e){}
         */
        System.out.println("Executing class B constructor");
    }

    public static void main(String args[])
    {
        try
        {
            A a = new B();
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}


A) Executing class A constructor 
B) Executing class B constructor 
C) Runtime error 
D) Compile time error



Answer -- d inside constructor we can not give try catch block because inside constructor super() or this() must be first statement implicitly or explicitly 


Compaile Error:--

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type Exception

at net.dharmaraj.B.<init>(B.java:16)
at net.dharmaraj.B.main(B.java:28)

Exception - 12 -

Select all  the exceptions thrown by wait() method of an Object class, which you can replace in the place of xxx legally? 

package net.dharmaraj;

class T implements Runnable
{
    public void run()
    {
        System.out.println("Executing run() method");
        myTest();
    }

    public synchronized void myTest() 
            { 
               try
               { 
                   wait(-1000);  
                   System.out.println( "Executing the myTest() method" ) ; 
               }   //XXX 
             
            }}

public class MyTest
{
    public static void main(String args[])
    {
        T t = new T();
        Thread th = new Thread(t);
        th.start();
    }
}



A) catch ( InterruptedException ie) {} 
B) catch ( IllegalArgumentException il ) {} 
C) catch ( IllegalMonitorStateException im ) {} 
D) Only catch ( InterruptedException e ) {} exception


Answer.-- a, b, c

Exception - 10 -

From the following code how many objects are eligible for garbage collection?  

package net.dharmaraj;

public class dev
{
    public static void main(String a[])
    {
        String string1 = "Test";
        String string2 = "Today";
        string1 = null;
        string1 = string2;
    }
}



String string1 = "Test";  
String string2 = "Today";  
string1 = null;  
string1 = string2;  
A) 1  
B) 2  
C) 3  
D) 0  




Exception - 9 -

What is the result when you compile the and run the following code? 


package net.dharmaraj;

public class ThrowsDemo
{
    static void throwMethod()
    {
        System.out.println("Inside throwMethod.");
        throw new IllegalAccessException("demo");
    }

    public static void main(String args[])
    {
        try
        {
            throwMethod();
        }
        catch (IllegalAccessException e)
        {
            System.out.println("Caught " + e);
        }
    }
}


A) Compilation error 
B) Runtime error  
C) Compile successfully, nothing is printed.  
D) Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo  



Compile Result :--
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unreachable catch block for IllegalAccessException. This exception is never thrown from the try statement body


at net.dharmaraj.ThrowsDemo.main(ThrowsDemo.java:17)


Changed Code :--

package net.dharmaraj;

public class ThrowsDemo
{
    static void throwMethod() throws IllegalAccessException
    {
        System.out.println("Inside throwMethod.");
        throw new IllegalAccessException("demo");
    }

    public static void main(String args[])
    {
        try
        {
            throwMethod();
        }
        catch (IllegalAccessException e)
        {
            System.out.println("Caught " + e);
        }
    }
}


Out Pur:--
Inside throwMethod.
Caught java.lang.IllegalAccessException: demo

Sunday, 19 May 2013

Exception - 8 -



package net.dharmaraj;

import java.io.*;

class MyExp
{
    void MyMethod() throws IOException, EOFException
    {
        // ............//
    }
}

class MyExp1 extends MyExp
{
    void MyMethod()
    {
        // ..........//
    }
}

public class MyExp2 extends MyExp1
{
    void MyMethod() throws IOException
    {
        // .........//
    }
}
/*
 * A) Compile time error 
 * B) No compile time error 
 * C) Run-Time error 
 * D) MyMethod() cannot declare IOException in MyExp2 class
 */



What is the result of compiling the following code?  


A) Compile time error  
B) No compile time error  
C) Run-Time error 
D) MyMethod() cannot declare IOException in MyExp2 class  

Exception - 7 -



package net.dharmaraj;

public class A
{
    private void method1() throws Exception
    {
        throw new RuntimeException();
    }

    public void method2()
    {
        try
        {
            method1();
        }
        catch (RuntimeException e)
        {
            System.out.println("Caught Runtime Exception");
        }
        catch (Exception e)
        {
            System.out.println("Caught Exception");
        }
    }

    public static void main(String args[])
    {
        A a = new A();
        a.method2();
    }
}



The above lines of code - 
1)will not compile. 
2)will compile and show - "Caught Runtime Exception". 
3)will compile and show - "Caught Exception". 
4)will compile and show both the messages one after another in the order they appear. 

Exception - 6 -



package net.dharmaraj;

public class AQuestion
{
    public static void main(String args[])
    {
        System.out.println("Before Try");
        try
        {
        }
        catch (java.io.IOException t)
        {
            System.out.println("Inside Catch");
        }
        System.out.println("At the End");
    }
}



1) Compiler error complaining about the catch block where no IOException object
can ever be   thrown. 
2) Compiler error - IOException not found. It must be imported in the first line 
of the code.
3)  No compiler error. The lines "Before Try" and "At the end" are printed on the screen. 

Exception - 5 -




package net.dharmaraj;

public class AQuestion
{
    public static void main(String args[])
    {
        System.out.println("Before Try");
        try
        {
        }
        catch (Throwable t)
        {
            System.out.println("Inside Catch");
        }
        System.out.println("At the End");
    }
}



Compiler error complaining about the catch block,  where no Throwable object can ever be thrown.

1) Compiler error - Throwable Object can not be caught, only Exceptions must be caught. 
2) No compiler error. The lines "Before Try" and "At the end" are printed on the screen. 

Exception - 4 -



package net.dharmaraj;

public interface AQuestion 
    { 
        public abstract void someMethod() throws Exception; 
    } 



Read the following code below. 

A Class implementing this interface 
1) Should Necessarily be an abstract class.
2) Should have the method public abstract void someMethod(); 
3) Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception.
4) Should have the method public void someMethod() which need not throw an Exception.  

Exception - 3 -


package net.dharmaraj;

import java.io.*;

class Base
{
    public static void amethod() throws FileNotFoundException
    {
        System.out.println("amethod BASE");
    }
}

class ExcepDemo extends Base
{
    public static void main(String argv[])
    {
        ExcepDemo e = new ExcepDemo();
    }

    public static void amethod()
    {
        System.out.println("amethod Child");
    }

    protected ExcepDemo()
    {
        try
        {
            DataInputStream din = new DataInputStream(System.in);
            System.out.println("Pausing");
            din.readChar();
            System.out.println("Continuing");
            this.amethod();
            super.amethod();
        }
        catch (IOException ioe)
        {
        }
    }
}



What will happen when you attempt to compile and run the following code


1)Compile time error caused by protected constructor
2) Compile time error caused by amethod not declaring Exception
3) Runtime error caused by amethod not declaring Exception
4) Compile and run with output of "Pausing" and "Continuing" after a key is hit



Console Out Put:


Error: Could not find or load main class net.dharmaraj.Ppvg


Exception - 2 -


package net.dharmaraj;

import java.io.*;

public class Ppvg
{
    public static void main(String argv[])
    {
        Ppvg p = new Ppvg();
        p.fliton();
    }

    public int fliton()
    {

        try
        {
            DataInputStream din = new DataInputStream(System.in);
            din.readChar();
        }
        catch (IOException ioe)
        {
            System.out.println("flytwick");
        }
        return 99;
    }
}

Exception - 1 -


What will happen when you attempt to compile and run the following code? 



package net.dharmaraj;
import java.io.*;

abstract class ExBase
{
    abstract public void martley();

}

public class MyEx extends ExBase
{
    public void martley()
    {
    }

    public static void main(String argv[])
    {
        DataInputStream fi = new DataInputStream(System.in);
        try
        {
            fi.readChar();
        }

        catch (IOException e)
        {
            System.exit(0);
        }

        finally
        {
            System.out.println("Doing finally");
        }
    }
}








1) Compile time error
2) It will run, wait for a key press and then exit
3) It will run, wait for a keypress, print "Doing finally" then exit
4) At run and immediately exit