Set 35

Multiple Choice Question

QID: 
543

Identify the exceptions that will be received when the following code snippets are executed.

1. int factorial(int n){
       if(n==1) return 1;
      else return n*factorial(n-1);
   }
Assume that it is called with a very large integer.

2. void printMe(Object[] oa){
       for(int i=0; i<=oa.length; i++)
       System.out.println(oa[i]);
    }
Assume that it is called as such: printMe(null);

3. Object m1(){
       return new Object(); 
    }
    void m2(){
       String s = (String) m1();
    }

Select 1 option

A. Class Cast Exception
Array Index out of Bounds Exception
Stack Overflow Error

B. Class Cast Exception
Array Index Out Of Bounds Exception
Security Exception

C. No Exception Will Be Thrown
Security Exception
Will Not Compile
 
D. Stack Over flow Error
NullPointerException
No Exception Will Be Thrown 

E. Stack Over flow Error
Array Index Out Of Bounds Exception
Class Cast Exception

F. Stack Over flow Error
NullPointerException
NullPointerException
 
G. Security Exception
Null Pointer Exception
No Exception Will Be Thrown
 
H. Stack Overflow Error
Null Pointer Exception
Class Cast Exception

Multiple Choice Question

QID: 
542

You have a method that currently does not handle any exception thrown from the code contained in its method body. You are now changing this method to call another method that throws IO Exception.

What changes, independent of each other, can you make to your method so that it will compile?

Select 2 options

A. Set the exception to null and don't rethrow it.
B. Declare IO Exception in the throws clause of your method.
C. Wrap the call to another method within a try-catch block that catches RuntimeException.
D. Wrap the call to another method within a try-catch block that catches Exception.

Multiple Choice Question

QID: 
541

Consider the following code:

public class TestClass {
    public static void doStuff() throws Exception{
        System.out.println("Doing stuff...");
        if(Math.random()&gt;0.4){
            throw new Exception("Too high!");
        }
        System.out.println("Done stuff.");
    }
    public static void main(String[] args) throws Exception {
        doStuff();
        System.out.println("Over"); 
    }
}

Which of the following are possible outputs when the above program is compiled and run? (Assume that Math. Random () returns a double between 0.0 and 1.0 not including 1.0. Further, assume that there is no mistake in the line numbers printed in the output.)

Select 2 options

A. Doing stuff...
Exception in thread "main" java.lang.Exception: Too high!
 at TestClass.doStuff(TestClass.java:29)
 at TestClass.main(TestClass.java:41)
B.Doing stuff...
Exception in thread "main" java.lang.Exception: Too high!
 at TestClass.doStuff(TestClass.java:29)
 at TestClass.main(TestClass.java:41)
Over 
C.Doing stuff...
Done stuff.
Over
D.Doing stuff...
Exception in thread "main" java.lang.Exception: Too high!
 at TestClass.doStuff(TestClass.java:29)
 at TestClass.main(TestClass.java:41)
Done stuff.

Multiple Choice Question

QID: 
540

Java Exceptions is a mechanism...

Select 2 options

A. for dealing with unexpected user inputs.
B. that you can use to determine what to do when something unexpected happens.
C. for logging unexpected behavior.
D. to ensure that the program runs even if something unexpected happens.
E. that the VM uses to exit the program when something unexpected happens.

Multiple Choice Question

QID: 
539

Consider the following

public class TestClass {
public static void main(String[] args) { 
TestClass tc = new TestClass(); 
tc.myMethod();
}
public void myMethod() { 
yourMethod();
}
public void yourMethod() { 
throw new Exception();
}
}

What changes can be done to make the above code compile?

Select 1 option

A. Change declaration of main to 
          public static void main(String[] args) throws Exception
B. Change declaration of myMethod to
          public void myMethod throws Exception
C. Change declaration of yourMethod to
          public void yourMethod throws Exception
D. Change declaration of main and yourMethod to 
          public static void main(String[] args) throws Exception and
          public void yourMethod throws Exception
E. Change declaration of all the three method to include throws Exception.

Pages

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)