Ads Sponsored By Google :)

Wednesday, 14 August 2013

Core Java Written Test Questions IV

java written test questions and answers, java written test questions, java questions and answers for written test,core java written test questions and answers, java written test questions with answers,core java written test, Core Java Written Test Questions Pdf Download, Core Java Written Test Questions Pdf Download

Core Java Written Test Question IV

                        1          2         3        4         5       6

1. What will be the output of the program?
       try {
          int i = 0;
          int j = 10 / i;
      catch (Exception ex) {
       System.out.println("Exception is raised");
    catch (ArithmeticException ae)  {
    System.out.println(" Arithmetic Exception is raised");

 A.  finished B.  Exception C.  Compilation fails.  D.  Arithmetic Exception  

Answer: Option C


The Above Code Snippet raises the Compile time error because ArithmeticException is already catched by the Exception, then it not recommended to catch once again by using the ArithemticException Class, So it raises the Compile time error.
But the By Specifying the ArithmeticException then after specify the Exception catch block would recommended and file will compiled.

2. Which of the following statement is true?   

A.  A try statement must have at least one corresponding catch block.  
B.  Multiple catch statements can catch the same class of exception more than once.  
C.  An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.  
D.  Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.  

Answer: Option D

A is wrong. A try statement can exist without catch, but it must have a finally statement.
B is wrong. A try statement executes a block. If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause. If that catch block completes normally, then the try statement completes normally.
C is wrong. Exceptions of type Error and RuntimeException do not have to be caught, only checked exceptions (java.lang.Exception) have to be caught. However, speaking of Exceptions, Exceptions do not have to be handled in the same method as the throw statement. They can be passed to another method.
If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances:
An exception arising in the finally block itself.

3. class Case1  {
            public int count;
            public int Begin() { return 42; }
         class Case2  {
         public int count;
         public int Begin() { return (int)(value^5); }
which statement is true?   

 A.  class Case1 will not compile.  
 B. The Case1 Begin() method is more efficient than the Case2 Begin() method.
C. The Case1 Begin() method is less efficient than the Test2 begin() method.  
 D. class Case2 will not compile.  

Answer: Option C


From the above code snippet we can conculde that hasing algorithm is implemented by Class Case 1 is always return the value 42, which is always legal. But placing the values of hashtbale entires into the single bucket, is most in efficient way.
The Options A and D are not correct because these classes are legal. and the Option B is not correct based on the logic described above.

4. What it the excepted output of the following Code Snippet?
public class Fake {
    Fake()      {
        System.out.print("fake");    }
    class Dash{
    Dash()     {
    public void go()     {
} // Dash Ends

    public static void main (String [] args)     {
        Fake t = new Fake();
    void madeDash()     {
        (new Dash() {}).go();
}// Fake Class Ends
A. Compilation fails.   B. An error occurs at runtime.   C. It prints "fakebarhi"  
D. It prints "barhi"  

Answer: Option C

In the Above it prints fakebarhi, due to the first Fake instance is created, that means Fake Constructor runs and it prints "fake", then next to it madeDah() is excuted, When Dash creates. that means Dash Constructor runs and prints bar and go() method get Executed prints "hi".

5. class Dash { }
     class Demo  {
    Dash doDash()      {
        Dash b = new Dash(); // Line 4
        return b; // line 5
    public static void main (String args[])      {
        Demo t = new Demo(); // line 8
        Dash newBar = t.doDash();  // line 9
        newBar = new Dash(); // Line 11
        System.out.println("finishing"); // Line 12

At what point is the Bar object, created on line 6, eligible for garbage collection?   
A.  after line 5                                                                 B. after line 9
  C. after line 5, when doDash() completes        D. after line 15, when main() completes  
Answer: Option C


In the Above question Option B is correct. because all the references the the Dash object is created at the Line no 4 are destroyed when the reference to the new Dash Object is assigned. to the variable newDash at line number 11. then Dash Object is created at line no line 4 and this protects the object from garbage collection at line number 11.

Option A is wrong. Due toe it protects the object from Garbage collection.
Option C is wrong. Due to the reference in the doDash() methos return at line num 5 and it stored at line no 9. preserves the object creation at line no 4.
Option D is also Wrong because it not eligible for garbage collection at line 12.

6. class Case  {
    private Test t;
    void start()      {
        t = new Test();
        this.takeTest(t);// Line 5
    } //Line 6
    void takeTest(Test test)     {
        test = null;
        test = new Test();

When is the Test object eligible for garbage collection?   

 A.  After line 7    B. After line 8    C.  After the start() method completes
D. When the instance running this code is made eligible for garbage collection.  

Answer: Option D


Option D is correct. By a process of elimination.
Option A is wrong. The variable d is a member of the Test class and is never directly set to null.
Option B is wrong. A copy of the variable d is set to null and not the actual variable d.
Option C is wrong. The variable d exists outside the start() method (it is a class member). So, when the start() method finishes the variable d still holds a reference.

7. public class test {
    public static void main(String [] args)      {
        A a = new A();
        A a2 = m1(a); // Line 4
        A a4 = new A();
        a2 = a4;  // Line 6
    static A m1(A ma)      {
        ma = new A();
        return ma;
After Line number 6, how many objects are allocated for garbage collections ?
A.  0    B.  1 C. 2 D. 3  

Answer: Option B

In the Above Question line no 6 has run, the object without any reference is generated after the line number 4. "Pass by value in Java", in which the reference variable a is not affected by the method m1().

8.  public class BoolAssert {
    public void test()     {
        assert false;//Line 3
        assert false;// Line 4
    public void bar()    {
        while(true)        {
            assert false;//Line 8
        assert false; //Line 10

What causes compilation to fail?   

A.   Line 3 B.  Line 4    C.  Line 8  D.  Line 10  

Answer: Option D


From the Question Option D  is correct. Compile error is occured at line 10 because unreachable statement

9. What is the Excepted output of the Following Program?

public class SqrootDemo {
    public static void main(String [] args)     {
        double sqt = -9.0;
        System.out.println( Math.sqrt(sqt));
A.  -3.0      B.  3.0         C. NaN              D. Compile time Error.

Answer: Option C

In the Above Program  sqrt() method returns NaN, because the arguments are less than zero.

  10.  What is the Excepted Output of the Following Program?

  String x = "blackkite";  newspaper
  x = x.substring(5,7);
  char y = x.charAt(1);
  x = x + y;
  A.   kik      B.  kii      C.  kitk      D. kitk  

Answer: Option B


From the Above Question substring() and charAt() methods are index with zero, and the subString() returns String lenght arg2-arg1.

For More Java Written Questions and Answers

                        1          2         3        4         5       6

No comments:

Post a Comment