Facebook

adsense

Saturday, 28 March 2015

10 Tricky Core java Question and Answers

(click for Answers).

1) Can you instantiate this class?

public class A
{
    A a = new A();
}

2) Is the below code written correctly? If yes, what will be the output?

class A
{
    static void staticMethod()
    {
        System.out.println("Static Method");
    }
}

public class MainClass
{
    public static void main(String[] args)
    {
        A a = null;

        a.staticMethod();
    }
}

3) What will be the output of the following program?

class A
{
    static int i = 1111;

    static
    {
        i = i-- - --i;
    }

    {
        i = i++ + ++i;
    }
}

class B extends A
{
    static
    {
        i = --i - i--;
    }

    {
        i = ++i + i++;
    }
}

public class MainClass
{
    public static void main(String[] args)
    {
        B b = new B();

        System.out.println(b.i);
    }
}

4) What happens when you call methodOfA() in the below class?


class A
{
    int methodOfA()
    {
        return (true ? null : 0);
    }
}

5) What will be the output of the below program?

public class MainClass
{
    public static void main(String[] args)
    {
        Integer i1 = 127;

        Integer i2 = 127;

        System.out.println(i1 == i2);

        Integer i3 = 128;

        Integer i4 = 128;

        System.out.println(i3 == i4);
    }
}

6) Can we override method(int) as method(Integer) like in the below 

class A
{
    void method(int i)
    {
        //method(int)
    }
}

class B extends A
{
    @Override
    void method(Integer i)
    {
        //method(Integer)
    }
}

7) Which statements in the below class shows compile time error (Line 5 or Line 7 or both)?

public class MainClass
{
    public static void main(String[] args)
    {
        Integer i = new Integer(null);

        String s = new String(null);
    }
}
8) What will be the output of the following program?


public class MainClass
{
    public static void main(String[] args)
    {
        String s = "ONE"+1+2+"TWO"+"THREE"+3+4+"FOUR"+"FIVE"+5; 

        System.out.println(s);
    }
}

9) What will be the output of the below program?

public class MainClass
{
    static int methodOne(int i)
    {
        return methodTwo(i *= 11);
    }

    static int methodTwo(int i)
    {
        return methodThree(i /= 11);
    }

    static int methodThree(int i)
    {
        return methodFour(i -= 11);
    }

    static int methodFour(int i)
    {
        return i += 11;
    }

    public static void main(String[] args)
    {
        System.out.println(methodOne(11));
    }
}

10) What will be the output of the following program?

public class MainClass
{
    public static void main(String[] args)
    {
        int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15;

        System.out.println(i);
    }
}

/////////////////////////////////////////////////////////////////////////////////
ANSWERS

1) Not possible. Because while instantiating, constructor will be called recursively.

2) Yes, the code is correct. You can call static methods through reference variable which is pointing to null. Output will be,
Static Method

3) 6

4) Throws NullPointerException at run time.

5)
true
false

6) No. It gives compile time error. Compiler treats int and Integer as two different types while overriding. Auto-boxing doesn’t happen here.

7) Only Line 7 shows compile time error. Because, compiler will be in an ambiguous situation of which constructor to call. Because, String class has five constructors which take one argument of derived type . Where as Integer class has only one constructor which takes one argument of derived type.

8) ONE12TWOTHREE34FOURFIVE5

9) 11

10) 75

No comments:

Post a Comment