Ads Sponsored By Google :)

Wednesday 14 August 2013

Core Java Written Test Questions III

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 III

                        1          2         3        4         5       6

1. What will be the output of the program? 

   class UserThread extends Thread {
    public static void main(String [] args)      {
        UserThread  x= new UserThread();
        x.start();// line no 4
        System.out.print("Hai ");
        x.start();//line no 6
        System.out.print("Hello ");
      }
      public void run()      {
        System.out.print("RunThread ");
     }
   }
 
 A.    Compilation fails                                          B. Runtime Error Or Exception
 C.    It prints "ThreadHai ThreadHello."       D. The output cannot be determined.


Answer: Option B

Explanation:

When the start() method is trying to calling the Thread Object, then method will throws an Exception Called IlegalThreadStateException( at line No 4, and Line No 6 start() is trying to invoke the single variable x. then it throws an Exception.

2. What will be the output of the program? 

  class DemoThread extends Thread { 
     DemoThread() {} 
     DemoThread(Runnable x) {
     super(x); } 
    public void run()      { 
        System.out.print("Run() Method");
    } 

class DemoRunnable implements Runnable  { 
    public void run()      { 
        System.out.print("Runnable Interface"); 
    } 

class Test  {  
    public static void main(String[] args)      { 
        new DemoThread().start(); 
        new DemoThread(new DemoRunnable()).start(); 
    } 
}
 
A.  Prints "Run() Method Run() Method"       B.   Prints "Run() Method  Runnable Interface"
C. Does not compile                            D. Throws exception at runtime


Answer: Option A

Explanation:

From the Above Code Snippet Option A is Correct, because Runnable Object is passes as an Constructor argument, in which run() method of Thread Class is invoke or execute the run() method of Runnabel Object. In case run() method in the Thread Class is overriden by the run() method in the UserThread Class. Then run() method in DemoRunnable is never executed. Both the time run() method of DemoThread is executed.

3. What is the Excepted output for the Following Code Snippet ?

  Class X implements Runnable{ 
    int a,b; 
    public void run() { 
        for(int i = 0; i < 1000; i++) 
            synchronized(this) { 
                a = 18; 
                b = 18; 
            } 
        System.out.print(a + " " + b + " "); 
    } 
    public static void main(String args[])  { 
        X test = new X(); 
        Thread x1 = new Thread(test); 
        Thread x2 = new Thread(test); 
        x1.start(); 
        x2.start(); 
    } 
}
 
A. DeadLock B.  It prints 18 18 18 18   C.Compilation Error D. Cannot determine 


Answer: Option B

Explanation:

Output of the above program code snippet is 18 18 18 18

4. public class Demo { 
            public void test()     {
            assert false; // Line 3
           assert false; // Line 4
          } 
         public void foo()    {
            while(true)        {
            assert false; // Line 8
          } 
          assert false;  // Line 10
      } 
   }
Which line causes the Compile time error?
 
A. Line 3    B. Line 4    C. Line 18    D. Line 10


Answer: Option D

Explanation:
From the Above Program snippet Option D is correct because in which at line 10 Causes the Compile time error, in which the statement at the line 10 is unreachable. 

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

public class Demo {
    public static int b;
    public static void test(int a)     {
        System.out.print("test ");
        b = a;
    }
    public static int bar(int c)     {
        System.out.print("Case ");
        return b = c;
    }
    public static void main(String [] args )     {
        int p = 0;
        assert p > 0 : bar(7);
        assert p > 1 : test(8); /* Line 18 */
        System.out.println("created ");
    }
}
 
A.  Cas    B.  Case created  C.  test created      D. Compilation fails

Answer: Option D

Explanation:

From the Code test() Method returns void. it absoultely returns the void, but it not possible to return the assert statement.
The test() method returns void. It is a perfectly acceptable method, but because it returns void it cannot be used in an assert statement, so line 18 will not compile.

6. public class Return {
    public static int p;
    public static int test(int q)      {
        return q * 2;
    }
    public static void main(String [] args)      {
        int r = 5;
        assert r > 0; /* Line 11 */
        assert r > 2: test(r); /* Line 12 */
        if ( r < 7 )
            assert r > 4; /* Line 14 */
        switch (r)  {
            case 4: System.out.println("4 ");
            case 5: System.out.println("5 ");
            default: assert r < 10;
        }
        if ( r < 10 )
            assert r > 4: r++;//Line No.  22
        System.out.println(r);// Line no. 23
    }
}
which line is an example of an inappropriate use of assertions?

 A. Line 12    B. Line 11     C. Line 14     D. Line 22


Answer: Option D

Explanation:

Assert Statemnt doesnot casue any side effects. at the Line number 22, value of r changes, if the assert statement is false. Option A is OK, because the acceptable to call second expression of assert statement. Options A is OK, in second expression in this assert statement is not required. option C is also OK, Call of an assert statement conditionally.

7. What will be the output of the program? 
   public class ErrorClassOrNot {
    public static void main(String [] args)    {
    String x = "68";
        try  {
            x = x.concat(".5"); // Line No 8
            double d = Double.parseDouble(x);
            x = Double.toString(d);
            int y = (int) Math.ceil(Double.valueOf(x).doubleValue());// Line No 11
            System.out.println(y);
        }
        catch (NumberFormatException excep) {
            System.out.println("Number is not correct");
        }
    }
}
 
A. 68 B.  68.5     C. 69   D. Number is not correct 
Answer: Option C

Explanation:
From the above code, line number 8 Creates the value of x is  "68.5. Line 9 and 10 converts the String to double. At the line number 11 first of all Math.ceil() is evaluted first, then we execute the valueOf() method that returns the Double Object has value 68.5. then double Value() method is called, return the double primitive with value of 68.5. then ceil method is invokesconverts it to 69.0, then type cast to int.

8. What will be Excepted output of the following program?

public class Demo{ 
    public static void stringRe (String txt) {
        txt = txt.replace ('j' , 'c'); // Line No 5
    } 
    public static void bufferRe(StringBuffer txt)  { 
        txt = txt.append ("c");  // Line No 9
    } 
    public static void main (String args[]) { 
        String ts = new String ("java"); 
        StringBuffer tb = new StringBuffer ("java"); /* Line 14 */
        stringRe(ts); 
        bufferRe(tb); 
        System.out.println (ts + tb); 
    } 
}
 
A. java      B. javajavac      C. javac    D. Compile error

Answer: Option B

Explanation:
 String is always immutable(Changes/Replaces in the String is not allowed). But StringBuffer is muttable(String is altered). Because of that reason String remains same as "java". But StringBuffer Object tb can be altered that we can find the changes in StringBuffer Object, then it becomes javac. Then finally output is javajavac, that is produced at line statement of the main method. '+' Operator is used to concate the two Strings.

8. What is the Excepted output for this Program?

    public class TestCase {
     public static void main(String [] args)      {
        Boolean a1 = new Boolean("false");
        boolean a2;
        a2 = a1.booleanValue();
        if (!a2) { // Line 6
            a2 = true;
            System.out.print("p ");
        }
        if (a1 & a2)   { //Line 13
            System.out.print("q ");
        }
        System.out.println("r");
       }
    }
 
A.   r  B.   pr   C.  qr     D. Compilation fails.


Answer: Option B




For More Java Written Test Questions :
                        1          2         3        4         5       6

No comments:

Post a Comment

LinkWithin