Ads Sponsored By Google :)

Wednesday 14 August 2013

Core Java Written Test Questions V

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


For More Java Interview Questions and Answers

                        1          2         3        4         5       6


1. which of the following declaration that follows the class level variable, and variable declarations will not compile ?  

 A. protected int a;                                                           B. transient int b = 3;
C. private synchronized int e;                                       D. volatile int d;

Answer: Option C

Explanation:
From the above question Option C is correct, it  will not compile; Sychronized modifier applies only for methods and blocks.
Option A,C,D are correct, because protected, transient variable and volatile are legal.

2. What will be the output of the program?

public class DemoTestt  {
    public static void main(String[ ] args)     {
        float x1[ ], x2[ ];
        x1 = new float[10];
        x2 = x1;
        System.out.println("x2[0] = " + x2[0]);
    }
}  
 
A. Above Code  prints f2[0] = 0.0                B. It prints f2[0] = NaN
C. An error at f2 = f1; causes compile to fail. D. It prints the garbage value.

Answer: Option A

Explanation:
From the Program Code Snippet, Option A is Correct because, When an array(x1=new float[10];) in which elements are initilises to default values for the primitive data types(float in case of 0.0). so x1 will contain 10 elements with default float values. x2=x1 copies all the elements in x1 to x2. Then while retrieving the x2 array of the zero index have value 0.0.

3. Public class X {
                   X( ) { }
                }
               class Y extends X
               { }

Which of the following Statement is true?
 A.   Class Y'S constructor is public.                   B. Class Y'S constructor has no arguments.
 C.   Class Y'S constructor includes a call to this( ). D. None of these.

Answer: Option B

Explanation:
From the Above Code Snippet. Option B is correct. Because Class Y inherits Class X's constructor which has no arguments. Option A is wrong. Class Y inherits Class X's constructor which uses default access. Option C is wrong. There is just no implicit call to this( ).

4. // Some Statements are Missing
    public class ModelTreeSet extends java.util.TreeSet  {
    public static void main(String [] args)      {
        java.util.TreeSet ts = new java.util.TreeSet();
        ts.clear();
    }
    public void clear()      {
        TreeMap tm = new TreeMap();
        tm.clear();
    }
}
which two statements, added independently at beginning of the program, allow the code to compile?

1) No statement is required    2) import java.util.*; 3) import.java.util.Tree*;
4) import java.util.TreeSet;     5) import java.util.TreeMap;  
A. 1 only      B.  2 and 5   C.  3 and 4        D.  3 and 5

Answer: Option B

Explanation: From the above Code Snippet Option B is Correct has (2) and (5). TreeMap related class is imported , but TreeSet Class related need not import, specailly because it is represented as fully qualified name. Option {1} is  not correct because TreeMap is must and should import. Option {3}  is not correct syntax because of import statement. Option{4} is incorrect because it is not import TreeMap.

5. What is the Excpeted of the Following program?
class Demo {
    byte a;
}

class test {
    public static void main(String [] args)      {
        test p = new test();
        p.init();
    }
    void start()      {
        Demo t = new Demo();
        System.out.print(t.a + " ");
        Two t2 = fix(t);
        System.out.println(t.a + " " + t2.x);
    }

    Demo update(Demo tt)     {
        tt.a = 88;
        return tt;
    }
}  
A.  null null 88 B.  0 0 88      C.  0 88 88 D.  0 0 0

Answer: Option C

Explanation:
From the above snippet has  update() method, of return type Class Demo, in which reference variable tt refers to the same object (class Demo) as the t reference variable. While Updating the tt.x in the update() method similar to it updates t.x also updates the same object.

6.  What is the Excepted Output of the Following Code Snippet?
public class test  {
    static boolean f1,f2;
    public static void main(String [] args)      {
        int i = 0;
        if ( !f1 )   { // Line 7
            if ( !f2 )  { // Line 8
                f1 = true;
                i++;
                if ( 5 > 6 ) {
                    i++;
                }
                if ( !f1 )
                    i = i + 10;
                else if ( f2 = true )// Line 15
                    i = i + 100;
                else if ( f1 | f2 ) // Line 17
                    i = i + 1000;
            }
        }
        System.out.println(i);
    }
}  
A. 0   B.  1  C. 101     D. 111  

Answer: Option C

Explanation:
From the Abov code Snippet f1, f1 are initialized to false, in which if it tests on the line no. 7 and 8 are sucess full. then f1 is set to true and also i is incremented. then test is suceed on the line number 19, in which code is not testing to see that f2 is true. Since line15 is successful but line no17 is skipped.

7. What is the Output of the Following program ?
public class Case  {
    static boolean f;
    public static void main(String [] args)      {
        short count = 42;
        if ( count < 50 & !f ) // Line 5
            count++;
        if ( count> 50 );     // Lin 7
        else if ( count> 40 )       {
            count += 7;
            count++;  
        }
        else
            --count;
        System.out.println(count);
    }
}  
 A.  41             B. 42   C.  50     D.  51

Answer: Option D

Explanation:

From the above Code Snippet boolean instance variables are default initialized to false, if the condition on the line number 5  is true then count increments, Line  is legal syntax only, do nothing statement. So else if is true count with 7 is added.

8. What is the excepted output of the following code snippet ?
int x= O;
while(1)  {
    if(x == 4)      {
        break;
    }
    ++x;
}
System.out.println("x = " + x);  
A.   x = 0     B.  x= 3     C.  x = 4 D. Compile time error

Answer: Option D

Explanation:
In the Above code snippet the while value iteration is on the basis of the int value, but is not legal. because the while condition always represents the boolean value.

9. What is the Excepted output of the Following Code Snippet ?
int x= 0, y = 5;
    tt: for (;;) {
        x++;
        for (;;)  {
            if(x > --j)  {
                break tt;
            }
        }
        System.out.println("x =" + x + ", y = " + y);  

A. x = 1, y = 0      B.  x = 1, y = 4      C.  x = 3, y = 4      D. Compilation fails.

Answer: Option D

Explanation:

From the above code snippet it raises the compile time error, because curly braces are missing at the end of the code.

10. What will be excepted output of the following Code?
int i = 0;
label:
    if (i < 2) {
    System.out.print("i is " + i);
    i++;
    continue label;
}  
 
A.  i is 0         B.  i is 0 i is 1   C. Compilation fails. D. None of the above

Answer: Option C

Explanation:
From the above code snippet, Code is not compile because the continue statement, is occur only in the looping only(while & do while). if the syntax were legal. Combination of the if statement and continue is legal then it would create the kludgey kind of loop. Compiler always recommend to write much cleaner code than this.




For More Java Interview Questions and Answers

                        1          2         3        4         5       6

No comments:

Post a Comment

LinkWithin