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
|
Comments
Post a Comment