Wednesday, October 26, 2011

Java Interview Questions part -3(Exception Handling)



Q1.       Consider the following two classes:

            public class Parent{

                        protected void demo() throws Exception{}

            }



            public class Child extends Parent{

                        private void demo(){}

            }          



            What will be result if you try to compile above two classes?



            A.  Compiler object for the method of a Child class, "Cannot reduce the visibility of the inherited method."

            B.  Compiler object for demo() method of a Child class, "Inherited method is not compatible with void                            Parent.demo() throws Exception."

            C.  Compile successfully.

            D.  None of the above



Q2.       class Level1Exception extends Exception {}

            class Level2Exception extends Level1Exception {}

            class Level3Exception extends Level2Exception {}

            class Brown {

                        public static void main(String args[]) {

                                    int a, b, c, d, f; a = b = c = d = f = 0;

                                    int x = 1;

                                    try {

                                                switch (x) {

                                                            case 1: throw new Level1Exception();

                                                            case 2: throw new Level2Exception();

                                                            case 3: throw new Level3Exception();

                                                }

                                                a++;

                                    }

                                    catch (Level3Exception e) {b++;}

                                    catch (Level2Exception e) {c++;}

                                    catch (Level1Exception e) {d++;}

                                    finally {f++;}



                                    System.out.print(a+", "+b+", "+c+", "+d+", "+f);

                        }

            }          



            What is the result of attempting to compile and run the program?



            A.  Prints: 0, 0, 0, 1, 1 B.  Prints: 0, 0, 1, 1, 1                C.  Prints: 0, 1, 1, 1, 1

            D.  Prints: 1, 1, 1, 1, 1                E.  Prints: 0, 0, 1, 0, 1                F.  Prints: 0, 1, 0, 0, 1

            G.  Prints: 1, 0, 0, 0, 1                H.  Compile-time error                I.  Run-time error

            J.  None of the above



Q3.       What will be the output of the program?



            public class Foo { 

                        public static void main(String[] args) {

                                    try {

                                                return;

                                    }

                                    finally {

                                                System.out.println( "Finally" );

                                    }

                        }

            }



            A. Finally                                 B. Compilation fails.                    C. The code runs with no output.

            D. An exception is thrown at runtime.



Q4.       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.                D. Arithmetic Exception



Q5.       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();

                        }

            }



            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.



Q6.       What will be the output of the program?



            public class E3 { 

                        public static void main(String [] args) {

                                    try {

                                                badMethod(); 

                                                System.out.print("A"); 

                                    }

                                    catch (RuntimeException ex) {

                                                System.out.print("B");

                                    }

                                    catch (Exception ex1) {

                                                System.out.print("C");

                                    }

                                    finally {

                                                System.out.print("D");

                                    }

                                    System.out.print("E");

                        }

                        public static void badMethod() {

                                    throw new RuntimeException();

                        }

            }



            A. BD                            B. BCD                          C. BDE                         D. BCDE



Q7.       What will be the output of the program?



            public class E4 {

                        public static void throwit () {

                                    System.out.print("throwit ");

                                    throw new RuntimeException();

                        }

                        public static void main(String [] args) {

                                    try {

                                                System.out.print("hello ");

                                                throwit();

                                    }

                                    catch (Exception re ) {

                                                System.out.print("caught ");

                                    }

                                    finally {

                                                System.out.print("finally ");

                                    }

                                    System.out.println("after ");

                        }

            }



            A. hello throwit caught                                                    B. Compilation fails

            C. hello throwit RuntimeException caught after                  D. hello throwit caught finally after



Q8.       What will be the output of the program?



            public class E5 { 

                        public static void aMethod() throws Exception {

                                    try {

                                                throw new Exception();

                                    }

                                    finally {

                                                System.out.println("finally");

                                    }

                        }

                        public static void main(String args[]) {

                                    try {

                                                aMethod(); 

                                    }

                                    catch (Exception e) {

                                                System.out.println("exception");

                                    }

                                    System.out.println("finished");

                        }

            }



            A. finally              B. exception finished                C. finally exception finished          D. Compilation fails



Q9.       What will be the output of the program?



            public class E6 {

                        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() {}

            }



            A. AC                            B. BC                            C. ACD              D. ABCD



Q10.     What will be the output of the program?



            public class E7 { 

                        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 RuntimeException();

                        }

            }



            A. AB                            B. BC                            C. ABC                          D. BCD



Q11.     What will be the output of the program?



            public class MyProgram {

                        public static void main(String args[]) {

                                    try {

                                                System.out.print("Hello world ");

                                    }

                                    finally {

                                                System.out.println("Finally executing ");

                                    }

                        }

            }



            A.   Nothing. The program will not compile because no exceptions are specified.

            B.   Nothing. The program will not compile because no catch clauses are specified.

            C.   Hello world.

            D.   Hello world Finally executing



Q12.     What will be the output of the program?



            class Exc0 extends Exception { }

            class Exc1 extends Exc0 { }

            public class E8 { 

                        public static void main(String args[]) {

                                    try { 

                                                throw new Exc1();

                                    }

                                    catch (Exc0 e0)  {

                                                System.out.println("Ex0 caught");

                                    }

                                    catch (Exception e) {

                                                System.out.println("exception caught"); 

                                    }

                        }

            }



            A.   Ex0 caught                                                            B.   exception caught

            C.   Compilation fails because of an error at line 2.            D.   Compilation fails because of an error at line 9.



Q13.     class E9 {

                        public static void parse(String str) {

                                    try {

                                                float f = Float.parseFloat(str);

                                    }

                                    catch (NumberFormatException nfe) {

                                                f = 0;

                                    }

                                    finally {

                                                System.out.println(f);

                                    }

                        }

                       

                        public static void main(String[] args) {

                                    parse("invalid");

                        }

            }



            What is the result?



            A.   0.0                                                 

            B.   Compilation fails.

            C.   A ParseException is thrown by the parse method at runtime.

            D.   A NumberFormatException is thrown by the parse method at runtime.

Q14.     Given:

            class E10{

                        static void test() throws Error {

                                    if (true)

                                         throw new AssertionError();



                                    System.out.print("test ");

                        }



                        public static void main(String[] args) {

                                    try {

                                                test();

                                    }

                                    catch (Exception ex) {

                                                System.out.print("exception ");

                                    }

                                    System.out.print("end ");

                        }

            }



            What is the result?



            A. end                           B. Compilation fails.                                C. exception end

            D. exception test end      E. A Throwable is thrown by main.    F. An Exception is thrown by main.



Q15.     Given:

            public static void main(String[] args) {



                        Float pi = new Float(3.14f);



                        if (pi > 3) {

                                    System.out.print("pi is bigger than 3. ");

                        }

                        else {

                                    System.out.print("pi is not bigger than 3. ");

                        }

                        finally {

                                    System.out.println("Have a nice day.");

                        }

            }



            What is the result?



            A. Compilation fails.                                       B. pi is bigger than 3.

            C. An exception occurs at runtime.                       D. pi is bigger than 3. Have a nice day.

            E. pi is not bigger than 3. Have a nice day.



Q16.     Given:

            public static void main(String[] args) {

                        try {

                                    args = null;

                                    args[0] = "test";

                                    System.out.println(args[0]);

                        }

                        catch(Exception ex) {

                                    System.out.println("Exception");

                        }

                        catch(NullPointerException npe) {

                                    System.out.println("NullPointerException");

                        }

            }



            What is the result?

                       

            A. test               B. Exception                   C. Compilation fails.               D. NullPointerException



Q17      Click the Exhibit button.

            Given:

            25.        try {

            26.                    A a = new A();

            27.                    a.method1();

            28.        } catch (Exception e) {

            29.                    System.out.print("an error occurred");

            30.        }



            Which two statements are true if a NullPointerException is thrown on line 3 of class C? (Choose two.)










































            A. The application will crash.                                           B. The code on line 29 will be executed.

            C. The code on line 5 of class A will execute.                    D. The code on line 5 of class B will execute.

            E. The exception will be propagated back to line 27.



Q18.     Given:

            84.    try {

            85.        ResourceConnection con = resourceFactory.getConnection();

            86.        Results r = con.query("GET INFO FROM CUSTOMER");

            87.        info = r.getData();

            88.        con.close();

            89.    } catch (ResourceException re) {

            90.        errorLog.write(re.getMessage());

            91.    }

            92.    return info;



            Which statements is true if a ResourceException is thrown on line 86? (Choose two.)

                       

            A.   Line 92 will not execute.

            B.   The connection will not be retrieved in line 85.

            C.   The resource connection will not be closed on line 88.

            D.   The enclosing method will throw an exception to its caller.

Q19.     Given:

            31.        // some code here

            32.        try {

            33.                    // some code here

            34.        } catch (SomeException se) {

            35.                    // some code here

            36.        } finally {

            37.                    // some code here

            38.        }



            Under which three circumstances will the code on line 37 be executed? (Choose three.)



            A.   The instance gets garbage collected.

            B.   The code on line 33 throws an exception.

            C.   The code on line 35 throws an exception.

            D.   The code on line 31 throws an exception.

            E.   The code on line 33 executes successfully.



Q20.     Given:

            11.        class A {

            12.                    public void process() { System.out.print("A,"); }}

            13.        class B extends A {

            14.                    public void process() throws IOException {

            15.                    super.process();

            16.                    System.out.print("B,");

            17.                    throw new IOException();

            18.        }

            19.        public static void main(String[] args) {

            20.                    try { new B().process(); }

            21.                    catch (IOException e) { System.out.println("Exception"); }}



            What is the result?



            A.    Exception                           

            B.    A, B, Exception

            C.    Compilation fails because of an error in line 20.

            D.    Compilation fails because of an error in line 14.

            E.    A NullPointerException is thrown at runtime.



Q21.     Given:

            static void test() throws RuntimeException {

                        try {

                                    System.out.print("test ");

                                    throw new RuntimeException();

                        }

                        catch (Exception ex) {

                                    System.out.print("exception ");

                        }

            }

            public static void main(String[] args) {

                        try {

                                    test();

                        }

                        catch(RuntimeException ex) {

                                    System.out.print("runtime ");

                        }

                        System.out.print("end ");

            }

            What is the result?



            A. test end                                 B. Compilation fails.                                            C. test runtime end

            D. test exception end             E. A Throwable is thrown by main at runtime.



Q22      Given:

            33.        try {

            34.                    // some code here

            35.        }

            36.        catch (NullPointerException e1) {

            37.                    System.out.print("a");

            38.        }

            39.        catch (RuntimeException e2) {

            40.                    System.out.print("b");

            41.        }

            42.        finally {

            43.                    System.out.print("c");

            44.        }



            What is the result if a NullPointerException occurs on line 34?



            A. c                   B. a                  C. ab                 D. ac                E. bc                 F. abc



Q23.     Given:

            public class Foo {

                        static int[] a;

                        static{ a[0]=2; }

                        public static void main( String[] args ) {}

            }



            Which exception or error will be thrown when a programmer attempts to run this code?



            A. java.lang.StackOverflowError

            B. java.lang.IllegalStateException

            C. java.lang.ExceptionInInitializerError

            D. java.lang.ArrayIndexOutOfBoundsException



Q24.     Given:

            static void test() {

                        try {

                                    String x = null;

                                    System.out.print(x.toString() + " ");

                        }

                        finally {

                                    System.out.print("finally ");

                        }

            }

            public static void main(String[] args) {

                        try {

                                    test();

                        }

                        catch (Exception ex) {

                                    System.out.print("exception ");

                        }

            }           What is the result?



            A. null               B. finally            C. null finally                  D. Compilation fails.       E. finally exception



Q25.     Click the Exhibit button.

            Given:

            31.        public void method() {

            32.                    A a = new A();

            33.                    a.method1();

            34.        }



            Which statement is true if a TestException is thrown on line 3 of class B?






            A.   Line 33 must be called within a try block.

            B.   The exception thrown by method1 in class A is not required to be caught.

            C.   The method declared on line 31 must be declared to throw a RuntimeException.

            D.   On line 5 of class A, the call to method2 of class B does not need to be placed in a try/catch block.



Q26.     Click the Exhibit button.



            Which statement is true about the two classes?






            A.   Compilation of both classes will fail.

            B.   Compilation of both classes will succeed.

            C.   Compilation of class A will fail. Compilation of class B will succeed.

            D.   Compilation of class B will fail. Compilation of class A will succeed.



Q27.     Click the Exhibit button.



            Class TestException:

            public class TestException extends Exception {}

            Class A:

            public class A {

                        public String sayHello(String name) throws TestException {

                                    if(name == null) {

                                                throw new TestException();

                                    }

                                    return "Hello "+ name;

                        }

            }



            A programmer wants to use this code in an application:



            45.        A a=new A();

            46.        System.out.println(a.sayHello("John"));



            Which two are true? (Choose two.)

                       

            A.   Class A will not compile.

            B.   Line 46 can throw the unchecked exception TestException.

            C.   Line 45 can throw the unchecked exception TestException.

            D.   Line 46 will compile if the enclosing method throws a TestException.

            E.   Line 46 will compile if enclosed in a try block, where TestException is caught.



Q28.     Given:

            11.        class A {

            12.                    public void process() { System.out.print("A "); } }

            13.        class B extends A {

            14.                    public void process() throws RuntimeException {

            15.                    super.process();

            16.                    if (true) throw new RuntimeException();

            17.                                System.out.print("B"); }}

            18.                    public static void main(String[] args) {

            19.                    try { ((A)new B()).process(); }

            20.                    catch (Exception e) { System.out.print("Exception "); }

            21.        }



            What is the result?



            A. Exception                        B. A Exception                    C. A Exception B             D. A B Exception

            E. Compilation fails because of an error in line 14. F. Compilation fails because of an error in line 19.



Q29.     Given:

            import java.io.*;



            class Master {

                        String doFileStuff() throws FileNotFoundException {

                                    return "a";

                        }

            }

            class Slave extends Master {

                        public static void main(String[] args) {

                                    String s = null;

                                    try {

                                                s = new Slave().doFileStuff();

                                    }

                                    catch ( Exception x) {

                                                s = "b";

                                    }

                                    System.out.println(s);

                        }

                        // insert code here

            }



Which, inserted independently at // insert code here, will compile, and produce the output b? (Choose all that apply.)



            A.   String doFileStuff() { return "b"; }

            B.   String doFileStuff() throws IOException { return "b"; }

            C.   String doFileStuff(int x) throws IOException { return "b"; }

            D.   String doFileStuff() throws FileNotFoundException { return "b"; }

            E.   String doFileStuff() throws NumberFormatException { return "b"; }

            F.   String doFileStuff() throws NumberFormatException,  FileNotFoundException { return "b"; }



Q30.     try { int x = Integer.parseInt("two"); }



            Which could be used to create an appropriate catch block? (Choose all that apply.)



            A. ClassCastException                             B. IllegalStateException               C. NumberFormatException

            D. IllegalArgumentException E. ExceptionInInitializerError 

            F. ArrayIndexOutOfBoundsException






1 comments:

Naviya Nair said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Vamshi krishnam raju | Bloggerized by Vamshi krishnam raju - Vamshi krishnam raju | Vamshi krishnam raju