10 Tricky Core Java Interview Coding Questions :

(Answers are provided at the end)

1) Can you instantiate this class?
?
1
2
3
4
public class A
{
    A a = new A();
}
2) Is the below code written correctly? If yes, what will be the output?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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?
?
1
2
3
4
5
6
7
class A
{
    int methodOfA()
    {
        return (true ? null : 0);
    }
}
5) What will be the output of the below program?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 example?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)?
?
1
2
3
4
5
6
7
8
9
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?
?
1
2
3
4
5
6
7
8
9
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?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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?
?
1
2
3
4
5
6
7
8
9
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

Comments

Popular posts from this blog

C Language Class 1 : Introduction to C Programming Language

8 Common Programming Mistakes

Basic Data Types In Java Examples And Programs