Java Test
Java Test
------------------------------------------------------
Question 1
-----------------------------------------------------
What will happen if you compile/run this code?
------------------------------------------------------
Question 2
------------------------------------------------------
What will happen if you compile/run the following code?
1: class Test
2: {
3: static void show()
4: {
5: System.out.println("Show method in Test class");
6: }
7: }
8:
9: public class Q2 extends Test
10: {
11: static void show()
12: {
13: System.out.println("Show method in Q2 class");
14: }
15: public static void main(String[] args)
16: {
17: Test t = new Test();
18: t.show();
19: Q2 q = new Q2();
20: q.show();
21:
22: t = q;
23: t.show();
[email protected]
-2-
24:
25: q = t; incompitable types
26: q.show();
27: }
28: }
D) Compilation error.
------------------------------------------------------
Question 3
------------------------------------------------------
The following code will give
1: class Test
2: {
3: void show()
4: {
5: System.out.println("non-static method in Test");
6: }
7: }
8: public class Q3 extends Test
9: {
10: static void show()
11: {
12: System.out.println("Overridden non-static method in Q3");
13: }
14:
15: public static void main(String[] args)
16: {
17: Q3 a = new Q3();
18: }
19: }
show() in Q3 cannot override show() in Test
for overriding methods function signature must be same
------------------------------------------------------
Question 4
------------------------------------------------------
The following code will give
1: class Test
2: {
3: static void show()
4: {
5: System.out.println("Static method in Test");
6: }
7: }
8: public class Q4 extends Test
9: {
10: static void show()
11: {
12: System.out.println("Overridden static method in Q4");
13: }
14: public static void main(String[] args)
15: {
16: }
17: }
------------------------------------------------------
Question 5
------------------------------------------------------
The following code will print
1: int i = 1;
2: i <<= 31;
3: i >>= 31;
4: i >>= 1;
5:
6: int j = 1;
7: j >>= 31;
8: j >>= 31;
9:
10: System.out.println("i = " +i );
11: System.out.println("j = " +j);
A) i = 1
j=1
[email protected]
-4-
B) i = -1
j=1
C) i = 1
j = -1
D) i = -1
j=0
------------------------------------------------------
Question 6
------------------------------------------------------
The following code will print
A) True
True
B) True
False
C) False
True
D) False
False
------------------------------------------------------
Question 7
------------------------------------------------------
The following code will print
A) Compilation error.
B) No compilation error, but runtime exception.
C) Prints "True".
D) Prints "False".
[email protected]
-5-
------------------------------------------------------
Question 8
------------------------------------------------------
The following code will give
1: public class Q8
2: {
3: int i = 20;
4: static
5: {
6: int i = 10; //local to this block
7:
8: }
9: public static void main(String[] args)
10: {
11: Q8 a = new Q8();
12: System.out.println(a.i);
13: }
14: }
------------------------------------------------------
Question 9
------------------------------------------------------
The following code will give
------------------------------------------------------
Question 10
------------------------------------------------------
What will happen if you compile/run this code?
------------------------------------------------------
Question 11
------------------------------------------------------
What will happen if you compile/run the following code?
------------------------------------------------------
Question 12
------------------------------------------------------
What is the output of the following code?
1: class Test
2: {
3: Test(int i)
4: {
5: System.out.println("Test(" +i +")");
6: }
7: }
8:
9: public class Q12
[email protected]
-7-
10: {
11: static Test t1 = new Test(1);
12:
13: Test t2 = new Test(2);
14:
15: static Test t3 = new Test(3);
16:
17: public static void main(String[] args)
18: {
19: Q12 Q = new Q12();
20: }
21: }
Static variable are initialized when the class is loaded to JVM but Instance variable are initialized
when the object is created.
A) Test(1)
Test(2)
Test(3)
B) Test(3)
Test(2)
Test(1)
C) Test(2)
Test(1)
Test(3)
D) Test(1)
Test(3)
Test(2)
------------------------------------------------------
Question 13
------------------------------------------------------
What is the output of the following code?
1: int i = 16;
2: int j = 17;
3:
4: System.out.println("i >> 1 = " + (i >> 1));
5: System.out.println("j >> 1 = " + (j >> 1));
A) Prints i >> 1 = 8
j >> 1 = 8
B) Prints i >> 1 = 7
j >> 1 = 7
C) Prints i >> 1 = 8
j >> 1 = 9
D) Prints i >> 1 = 7
j >> 1 = 8
------------------------------------------------------
[email protected]
-8-
Question 14
------------------------------------------------------
What is the output of the following code?
1: int i = 45678;
2: int j = ~i;
3:
4: System.out.println(j);
------------------------------------------------------
Question 15
------------------------------------------------------
What will happen when you invoke the following method?
1: void infiniteLoop()
2: {
3: byte b = 1;
4:
5: while ( ++b > 0 )
6: ;
7: System.out.println("Welcome to Java");
8: }