Ads Sponsored By Google :)

Wednesday 14 August 2013

Core Java Written Test Questions and Answers II

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 Questions and Answers II


                        1          2         3        4         5       6

1)  What is the output of the following program?

    Public class X {
      final public int FindResult(int p, int q) { return 9; }
    }
    class Y extends X {
      public int FindResult(int p, int q) {return 10; }
    }
   public class Test {
    public static void main(String args[])     {
        Y q = new Y();
        System.out.println("a = " + q.FindResult(10,9));
    }
  }

A. a = 10 B. x = 9 C. Compile Time Error.     D. An exception is thrown at runtime.

Answer:  Option C

Explanation:

From the above Question, Program is not compiled because final methods, variable not participate in Inheritance or overridden concepts.

 2  What is the output for the following program?

Public class BaseC {
    public static void main(String [] args)     {
        BaseC bc = new BaseC();
        bc.initial();
    }

    void initial()     {
        int x = 3;
        int y = 4;
        System.out.print(" " + 7 + 2 + " ");
        System.out.print(x + y);
        System.out.print(" " + x + y + " ");
        System.out.print(foo() + x + y + " ");
        System.out.println(x + y + back());
    }

    String back()     {
        return "back";
    }
}

A. 9 7 7 back 7 7back

B. 72 34 34 back34 34back

C. 9 7 7 back34 34back

D. 72 7 34 back34 7back

Answer: Option D


Explanation:

The above Program contains some exressions uses the + operator, there is no precedence to take care about of all expressions. Always + operator evalute from left to right. If String and numberic is evaluting using +  Operator. In which for String + operator will concatenate and numeric operands it adds.


3) What is Excepted Output of the Following Program?

Public class DoorWay{
    boolean [] y = new boolean[3];
    int turn = 0;
    void update(boolean [] x, int p)     {
        x[p] = true;
        ++turn;
    }
    public static void main(String [] args)     {
        DoorWay dw = new DoorWay();
        dw.update(dw.a, 0);
        dw.update(dw.a, 2);
        dw.set();
    }
    void set()     {
        if ( a[0] && a[1] | a[2] )
            turn++;
        if ( p[1] && p[(++turn - 2)] )
            turn += 7;
        System.out.println("turn = " + turn);
    }
}

A. turn = 0   B. turn = 2 C. turn = 3 D.  turn = 4

Answer: Option C

Explanation:

  The reference variables a and x both refer to the same boolean array. count is incremented for each call to the update() method, and once again when the first if test is true. Because of the && short circuit operator, count is not incremented during the second if test.

4.     Output for the 3<<2 ?

1. 12
2. 3
3. 18
4. 16

A. 1    B. 3    C. 4      D. 1&2


Answer: Option A

Explanation:

Option A is correct because the actual formual beyond the 3<<2 (x<<y is x*y*y) means 3*2*2=12.

 5.   public void test( boolean x, boolean y) {
          if( x ){
          System.out.println("X"); // line No. 5
         }
        else if(x && y)  {// Line No. 7
        System.out.println( "X && Y");
        }
    else { // Line No 11
        if ( !y ) {
            System.out.println( "notY") ;
        }
        else {
            System.out.println( "ELSE" ) ;
        }
    }
}

  A. If x is true and y is true then the output is "X && Y"
  B. If x is true and y is false then the output is "notY"
  C. If x is false and y is true then the output is "ELSE"
  D. If x is false and y is false then the output is "ELSE"

Answer: Option C

Explanation:

From the Above snippet, Option C is correct. When x is false then output lines after 11 get chance of invoking. Option A is not correct, Because Output is "A". when x is true, irrelevant of value of b, only the line no. 5 output is executed. But the condition is at line no. 7 is never executed or evaluted. (when a is true it will always trapped by line 12) then output will not at all "A&&B". Option B   is also not correc, because the Output is "A". when a is true, irrelevant to value of b, only line no 5 is executed. Option D is also not correct, because the output is "notB".

6.  What is the Excepted  output of the program?

Float y = new Float("16");
switch (y)
{
    case 16: System.out.println("Sixteen");
    case 0: System.out.println("Zero");
    default: System.out.println("Default");
}

A. Sixteen     B. Default     C. Compilation Erroe D.Zero

Answer: Option C

Explanation:

Option C Compilation error will occur. Because switch statement only supports the integers or variables or byte, cahr, chort. But Float wrapper is not allowed here. So Compilation error is Occured.

7. What is the Excepted Output of the Program?

public class Test {
    public static void aMethod() throws Exception     {
        try  {// Line No.3
            throw new Exception(); // Line No. 4
        }
        finally {// Line No 6
            System.out.print("finally "); //Line 7
        }
    }
    public static void main(String args[])     {
        try  {
            aMethod();
        }
        catch (Exception e)    {// Line 17
            System.out.print("exception ");
        }
        System.out.print("finished"); // Line No 20
    }
}

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

Answer: Option B

Explanation:

From the Above Code Snippet we Conculded that Option B is Correct, because execution in the try block at line no 3 completes quickly done because of try statement in the finally block is executed at line no 6 and prints the output finally at line 7. Finally Block completes normally, try statement completes its execution suddenly due to throw statement at line 4. exception is propagated up call stack that is caught by the catch block in the main method, then prints "exception" Finally program execution continues its execution in which exception is catch and prints the output "finished".

8. Which Of the following are true regarding Java.lang.ArrayList ?

A. Elements in the ArrayList is Ordered.
B. Collections are gaurentee to Immutable
             C. Elements in Collections are unique.
             D. Elements in the Collections are acessed using Unique value.

Answer: Option A

Explanation:

Option A is Correct, because elements in the arraylist are ordered.

9. Which of the following are true regarding method local inner class?

A. Must be make it as final.   B. Must be makr it as abstract.

C. Must be make it as public.   D. Must be make it as static.


Answer: Option B

Explanation:
From the Above query, Option B  is correct because a method-local inner classis an abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful).
Option A is not correct because the a method-local inner class not declared final.
Options C and D are not correct because a method-local inner class cannot be made as public

10. public class A implements Runnable  {
    public static void main(String args[])      {
            //Some Code is missig here
    }
    public void run() {}
}
From the below which of the following statement is suitable to replace that code missig Comment?

A. Thread x = new Thread(A);
B. Thread x = new Thread(A); x.start();
C. A run = new A(); Thread t = new Thread(run); x.start();
D. Thread x = new Thread(); x.run();

Answer: Option C

Explanation:
Option C is Correct, because the Runnable Interface in which it is extended into the Class A.
For More Details...





For More Java Interview Questions and Answers

                        1          2         3        4         5       6

No comments:

Post a Comment

LinkWithin