Ads Sponsored By Google :)

Wednesday, 14 August 2013

Core Java Written Questions VI

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 Questions VI


                        1          2         3        4         5       6


1) What is the Excepted Output of the Following Program?
    public class Demo {
    public int F1()    {
        static int i = 0;
        i++;
        return i;
    }
    public static void main(String args[])   {
        Demo demo = new Demo();
        demo.F1();
        int j = demo.F1();
        System.out.println(j);
    }
}
 
A. 2 B. 0 C. 1 D. Compile Time Error

Answer: Option D

Explanation:
Compile Time Error will occur because, static is an illegal start of Expression.
Compile time error is occur due to static an illegal start of the expression in which method variables have modifier.(but they always consider as local).

2. What is the Expected Output Of the Following Program?
class VText  {
    public static void main(String [] args)      {
        VText t = new VText();
        t.test();
    }
    void test()      {
        long [] x1 = {3,4,5};
        long [] x2 = bug(x1);
        System.out.print(x1[0] + x1[1] + x1[2] + " ");
        System.out.println(x2[0] + x2[1] + x2[2]);
    }

    long [] bug(long [] x3)      {
        x3[1] = 7;
        return x3;
    }
}
 
A. 15 15    B. 12 15      C. 3 7 5 3 4 5     D. 3 4 5 3 4 5


Answer: Option A

Explanation:
Option A is Correct because the reference variables x1,x3 refers to the long array object. In this above class VText  having method bug() have an index array of [1] is updated the value as 7. then the reference variable x2 is also refers to same array object.
From the Above Program Ouput is : 3+7+5+" "3+7+5
Output will be 15 15
3. What is the Excepted Output of the Folliwng Program?
class Updates  {
    public static void main(String [] args)     {
        int i = 100;
        double j = 100.1;
        boolean j = (i = j); // Line No 7
        System.out.println(j);
    }
}
 
A. true B. false  C. Compile Time Error     D. Run Time Error or Exception.

Answer: Option C

Explanation:
From the Above Program we can understand that at the line number 7, the line will works when (x==y).  While Taking Operators into considers (= =) and (=) in which == do operation and return boolean value. But = is used to assigns the value to variable.
Of Course, output of the above program of the optiosn A,B and D are in correct.
Learn more problems on : Operators and Assignments 
Discuss about this problem : Discuss in Forum 

4. What will be the output of the program? 
class BitwiseOperators {
    public static void main(String [] args)     {
        int i = 11 & 9;
        int j = i ^ 3;
        System.out.println( j | 12 );
    }
}
 
A. 0    B. 14   C. 8   D. 7

Answer: Option B

Explanation:
Option B is correct. From the Above Questions Operations between operands using operators. The & Operator generates one bit whent both bits are one. Then from Above Result of & Operation is 9. Even ^ Operator also genrates one bit, the result of the Operation is 10. The Operator |  produces the at least one bit is 1, then Result of the Operation is 14.

5. What is the Excepted Output Of the Following Program ?
class BoolTest {
    public static void main(String [] args)     {
        boolean a1 = true;
        boolean a2 = false;
        boolean a3 = true;
        if ( a1 & a2 | a2 & a3 | a2 )// Linn 8
            System.out.print("Hello ");
        if ( a1 & a2 | a2 & a3 | a2 | a1 ) /*Line 10*/
            System.out.println("JavaJavax");
    }
}
 
A. Hello  B. JavaJavax C. Hello JavaJavax           D. No output is produced

Answer: Option B

Explanation:
Operator & has hiher precedence than | Operator, at the line number 8 a1 and a2 evalutes together produces as are a2 & a3. at the line number 10 a1 if the test is true then it shows JavaJavax.

6. What is Expected Output Of the Following Program
Class A{
   public static void main(String args[]){
    int i = l, j = 6; 
    while (j--) {
       i++; 
      } 
     System.out.println("x = " + x +" y = " + y);
    }
}
 
A. i = 6 j = 0    B. i = 7 j = 0     C. i = 6 j = -1    D. Compile Error

Answer: Option D

Explanation:
 Option D Compile Time Error is raises, because While loop always considers Boolean value.

7. What is Expected Output Of the Following Program
Class Demo{
public static void main(String args[]){
for (int x = 0; x < 4; x+= 2) { 
    System.out.print(x + " "); 
  } 
   System.out.println(x); // Lin No 5
   }
}  
A. 0 2 4   B. 0 2 4 5    C. 0 1 2 3 4   D. Compile Time Error.

Answer: Option D

Explanation:
Compile time Error will raises because the variable x at line No 5 is accessed, because variable x is defined inside the for loop.

8. What is the Excepted output of the Following Program
int i = 3; 
int j = 1; 
if (i = j) // Line No 3{
    System.out.println("i =" + i); 
}
 
A. i = 1   B. i = 3    C. Compile Time Error
D. No Output.


Answer: Option C

Explanation:
In the Above program at the Line No 3 uses the = operator instead  ==, so if statement  receives the integer instead of boolean.

9. What is the Expected Output Of the Following Program?
public class A{  
    public static void main(String [] args)     {
        try         {
            ErroeMethod();  
            System.out.print("X");  
        } 
        catch (RuntimeException ex) // Line No 7        { 
            System.out.print("Y"); 
        } 
        catch (Exception ex1)         { 
            System.out.print("Z"); 
        } 
        finally {
            System.out.print("W"); 
        } 
        System.out.print("U"); 
    } 
    public static void ErrorMethod()     { 
        throw new RuntimeException(); 
    } 
}
 
A. XY   B. XYZ   C. XYW    D. XYZWU

Answer: Option C

Explanation:
In the Above Program Runtime Exception is thrown and is caught at the line number  7, then all the code after the finally block is run exception is catched.


For More Java Interview Questions and Answers

                        1          2         3        4         5       6

No comments:

Post a Comment

LinkWithin