1) What will be the output of this program?
public class MainClass
{
static int i = 1;
static
{
i = i-- + --i;
}
{
i = i++ - ++i;
}
int methodOfTest()
{
return i + i - i * i / i;
}
public static void main(String[] args)
{
System.out.println(new MainClass().methodOfTest());
}
}
Ans : -2
2) What will be the output of the below program?
class A
{
static String s = "AAA";
static
{
s = s + "BBB";
}
{
s = "AAABBB";
}
}
class B extends A
{
static
{
s = s + "BBBAAA";
}
{
System.out.println(s);
}
}
public class MainClass
{
public static void main(String[] args)
{
B b = new B();
}
}
Ans : AAABBB
3) What will be the output of this program?
abstract class X
{
int i = 11;
int methodOneOfX()
{
return i *= i /= i;
}
abstract void methodTwoOfX(int i);
}
class Y extends X
{
@Override
void methodTwoOfX(int i)
{
System.out.println(i);
}
}
public class Test
{
public static void main(String[] args)
{
new Y().methodTwoOfX(new Y().methodOneOfX());
}
}
Ans : 11
4) What will be the output of this program?
public class Test
{
int i = 10;
static Test t;
public static void main(String[] args)
{
System.out.println(Test.t.t.t.t.t.t.i);
}
}
Ans : It throws NullPointerException at run time. Because, ‘t’ is not initialized.
5) How do you access field ‘i’ of InnerClassFour in the below example?
class OuterClass
{
class InnerClassOne
{
class InnerClassTwo
{
class InnerClassThree
{
class InnerClassFour
{
int i;
}
}
}
}
}
Ans : new OuterClass().new InnerClassOne().new InnerClassTwo().new InnerClassThree().new InnerClassFour().i
6) Does below program compile successfully?
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println("This is try block");
}
System.out.println("statement after try block");
catch(Exception ex)
{
System.out.println("This is catch block");
}
System.out.println("statement after catch block");
finally
{
System.out.println("This is finally block");
}
}
}
Ans : No, you can’t put other statements in between try-catch blocks and catch-finally blocks.
7) Will this program run successfully without catch block?
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println("This is try block");
}
finally
{
System.out.println("This is finally block");
}
}
}
Ans : Yes, output will be
This is try block
This is finally block
8) What is wrong with this program. why it is showing compilation error?
public class Test
{
public static void main(String[] args)
{
String s;
System.out.println(s);
}
}
Ans : ‘s’ is a local variable. Local variables must be initialized before they are used.
9) Which line of the below program shows compilation error?
public class Test
{
static
{
d = 10;
System.out.println(d);
}
static double d;
}
Ans : Line 7. Because, variables can be initialized before they are declared. But you can’t use the variables before they are declared.
10) Does the below code compile successfully?
class A
{
int method()
{
return 0;
}
}
class B extends A
{
@Override
Integer method()
{
return 10;
}
}
Ans : No, return type of the overridden method is incompatible with it’s super class method.
11) What happens when you compile and run this program?
public class ArraysInJava
{
public static void main(String[] args)
{
int[] i = new int[0];
System.out.println(i[0]);
}
}
Ans : You will get ArrayIndexOutOfBoundsException at run time.
12) What will be the output of this program?
public class ArraysInJava
{
public static void main(String[] args)
{
int[] a = new int[3];
a[1] = 50;
Object o = a;
int[] b = (int[])o;
b[1] = 100;
System.out.println(a[1]);
((int[])o)[1] = 500;
System.out.println(a[1]);
}
}
Ans :
100
500
13) What will be the outcome of the following program?
public class ArraysInJava
{
static final int[] a;
static
{
a = new int[] {1, 2, 3};
}
public static void main(String[] args)
{
a = new int[5];
}
}
Ans : Compile time error.
14)What will be the outcome of this program?
public class ArraysInJava
{
public static void main(String[] args)
{
int[] a = {1, 2, 3, 4, 5, 8};
System.out.println(a[-1]);
}
}
Ans : ArrayIndexOutOfBoundsException at run time.
15) What happens when you compile and run the following program?
public class ArraysInJava
{
public static void main(String[] args)
{
int[][] a = {{1,2,}, {3,4}};
int[] b = (int[]) a[1];
Object o1 = a;
int[][] a2 = (int[][]) o1;
int[] b2 = (int[]) o1;
System.out.println(b[1]);
}
}
Ans : Line 13 throws ClassCastException at run time.
16) What will be the output of this program?
public class ArraysInJava
{
static void methodOne(int[] a)
{
int[] b = new int[5];
a = b;
System.out.print(a.length);
System.out.print(b.length);
}
public static void main(String[] args)
{
int[] a = new int[10];
methodOne(a);
System.out.print(a.length);
}
}
Ans : 5510
17) Will this program compiles and runs successfully?
public class ArraysInJava
{
public static void main(String[] args)
{
int[] a = {1};
int[] b[] = {{1}};
int[][] c[] = {{{1}}};
int[][] d [][] = {{{{1}}}};
}
}
Ans : Yes.
18) What will be the output of this program?
public class ArraysInJava
{
public static void main(String[] args)
{
String[][][][] colors =
{
{
{
{"RED", "GREEN", "BLUE"},
{"GREEN", "RED", "BLUE"}
},
{
{"ORANGE", "GREEN", "WHITE"},
{"BLACK", "INDIGO", "BLUE"}
}
},
{
{
{"SKY BLUE", "ALMOND", "AQUA"},
{"APPLE GREEN", "PINK", "BLUE GREEN"}
},
{
{"VIOLET", "BRASS", "GREY"},
{"BROWN", "INDIGO", "CHERRY"}
}
}
};
System.out.println(colors[1][0][1][0]);
System.out.println(colors[0][1][0][1]);
System.out.println(colors[0][0][0][2]);
System.out.println(colors[1][1][1][2]);
System.out.println(colors[0][0][0][0]);
System.out.println(colors[1][1][1][1]);
}
}
Ans :
APPLE GREEN
GREEN
BLUE
CHERRY
RED
INDIGO
19) Does below program compile successfully?
class A
{
int i = 10;
}
class B extends A
{
int j = 20;
}
class C extends B
{
int k = 30;
}
class D extends C
{
int m = 40;
}
public class ArraysInJava
{
public static void main(String[] args)
{
A[] a = {new A(), new B(), new C(), new D()};
System.out.println(a[3].i);
System.out.println(a[2].j);
System.out.println(a[1].k);
System.out.println(a[0].m);
}
}
Ans : No, It gives compile time error
20) What will be the output of this program?
public class ArraysInJava
{
static Double[] methodOne(Double[] D)
{
D[1] = 36.25;
return methodTwo(D);
}
static Double[] methodTwo(Double[] D)
{
D[1] = 62.36;
return methodThree(D);
}
static Double[] methodThree(Double[] D)
{
D[1] = 93.58;
return D;
}
public static void main(String[] args)
{
Double[] D = {10.55, 25.36, 58.29, 74.32, 32.21};
D = methodOne(D);
System.out.println(D[1]);
}
}
Ans : 93.58
public class MainClass
{
static int i = 1;
static
{
i = i-- + --i;
}
{
i = i++ - ++i;
}
int methodOfTest()
{
return i + i - i * i / i;
}
public static void main(String[] args)
{
System.out.println(new MainClass().methodOfTest());
}
}
Ans : -2
2) What will be the output of the below program?
class A
{
static String s = "AAA";
static
{
s = s + "BBB";
}
{
s = "AAABBB";
}
}
class B extends A
{
static
{
s = s + "BBBAAA";
}
{
System.out.println(s);
}
}
public class MainClass
{
public static void main(String[] args)
{
B b = new B();
}
}
Ans : AAABBB
3) What will be the output of this program?
abstract class X
{
int i = 11;
int methodOneOfX()
{
return i *= i /= i;
}
abstract void methodTwoOfX(int i);
}
class Y extends X
{
@Override
void methodTwoOfX(int i)
{
System.out.println(i);
}
}
public class Test
{
public static void main(String[] args)
{
new Y().methodTwoOfX(new Y().methodOneOfX());
}
}
Ans : 11
4) What will be the output of this program?
public class Test
{
int i = 10;
static Test t;
public static void main(String[] args)
{
System.out.println(Test.t.t.t.t.t.t.i);
}
}
Ans : It throws NullPointerException at run time. Because, ‘t’ is not initialized.
5) How do you access field ‘i’ of InnerClassFour in the below example?
class OuterClass
{
class InnerClassOne
{
class InnerClassTwo
{
class InnerClassThree
{
class InnerClassFour
{
int i;
}
}
}
}
}
Ans : new OuterClass().new InnerClassOne().new InnerClassTwo().new InnerClassThree().new InnerClassFour().i
6) Does below program compile successfully?
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println("This is try block");
}
System.out.println("statement after try block");
catch(Exception ex)
{
System.out.println("This is catch block");
}
System.out.println("statement after catch block");
finally
{
System.out.println("This is finally block");
}
}
}
Ans : No, you can’t put other statements in between try-catch blocks and catch-finally blocks.
7) Will this program run successfully without catch block?
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println("This is try block");
}
finally
{
System.out.println("This is finally block");
}
}
}
Ans : Yes, output will be
This is try block
This is finally block
8) What is wrong with this program. why it is showing compilation error?
public class Test
{
public static void main(String[] args)
{
String s;
System.out.println(s);
}
}
Ans : ‘s’ is a local variable. Local variables must be initialized before they are used.
9) Which line of the below program shows compilation error?
public class Test
{
static
{
d = 10;
System.out.println(d);
}
static double d;
}
Ans : Line 7. Because, variables can be initialized before they are declared. But you can’t use the variables before they are declared.
10) Does the below code compile successfully?
class A
{
int method()
{
return 0;
}
}
class B extends A
{
@Override
Integer method()
{
return 10;
}
}
Ans : No, return type of the overridden method is incompatible with it’s super class method.
11) What happens when you compile and run this program?
public class ArraysInJava
{
public static void main(String[] args)
{
int[] i = new int[0];
System.out.println(i[0]);
}
}
Ans : You will get ArrayIndexOutOfBoundsException at run time.
12) What will be the output of this program?
public class ArraysInJava
{
public static void main(String[] args)
{
int[] a = new int[3];
a[1] = 50;
Object o = a;
int[] b = (int[])o;
b[1] = 100;
System.out.println(a[1]);
((int[])o)[1] = 500;
System.out.println(a[1]);
}
}
Ans :
100
500
13) What will be the outcome of the following program?
public class ArraysInJava
{
static final int[] a;
static
{
a = new int[] {1, 2, 3};
}
public static void main(String[] args)
{
a = new int[5];
}
}
Ans : Compile time error.
14)What will be the outcome of this program?
public class ArraysInJava
{
public static void main(String[] args)
{
int[] a = {1, 2, 3, 4, 5, 8};
System.out.println(a[-1]);
}
}
Ans : ArrayIndexOutOfBoundsException at run time.
15) What happens when you compile and run the following program?
public class ArraysInJava
{
public static void main(String[] args)
{
int[][] a = {{1,2,}, {3,4}};
int[] b = (int[]) a[1];
Object o1 = a;
int[][] a2 = (int[][]) o1;
int[] b2 = (int[]) o1;
System.out.println(b[1]);
}
}
Ans : Line 13 throws ClassCastException at run time.
16) What will be the output of this program?
public class ArraysInJava
{
static void methodOne(int[] a)
{
int[] b = new int[5];
a = b;
System.out.print(a.length);
System.out.print(b.length);
}
public static void main(String[] args)
{
int[] a = new int[10];
methodOne(a);
System.out.print(a.length);
}
}
Ans : 5510
17) Will this program compiles and runs successfully?
public class ArraysInJava
{
public static void main(String[] args)
{
int[] a = {1};
int[] b[] = {{1}};
int[][] c[] = {{{1}}};
int[][] d [][] = {{{{1}}}};
}
}
Ans : Yes.
18) What will be the output of this program?
public class ArraysInJava
{
public static void main(String[] args)
{
String[][][][] colors =
{
{
{
{"RED", "GREEN", "BLUE"},
{"GREEN", "RED", "BLUE"}
},
{
{"ORANGE", "GREEN", "WHITE"},
{"BLACK", "INDIGO", "BLUE"}
}
},
{
{
{"SKY BLUE", "ALMOND", "AQUA"},
{"APPLE GREEN", "PINK", "BLUE GREEN"}
},
{
{"VIOLET", "BRASS", "GREY"},
{"BROWN", "INDIGO", "CHERRY"}
}
}
};
System.out.println(colors[1][0][1][0]);
System.out.println(colors[0][1][0][1]);
System.out.println(colors[0][0][0][2]);
System.out.println(colors[1][1][1][2]);
System.out.println(colors[0][0][0][0]);
System.out.println(colors[1][1][1][1]);
}
}
Ans :
APPLE GREEN
GREEN
BLUE
CHERRY
RED
INDIGO
19) Does below program compile successfully?
class A
{
int i = 10;
}
class B extends A
{
int j = 20;
}
class C extends B
{
int k = 30;
}
class D extends C
{
int m = 40;
}
public class ArraysInJava
{
public static void main(String[] args)
{
A[] a = {new A(), new B(), new C(), new D()};
System.out.println(a[3].i);
System.out.println(a[2].j);
System.out.println(a[1].k);
System.out.println(a[0].m);
}
}
Ans : No, It gives compile time error
20) What will be the output of this program?
public class ArraysInJava
{
static Double[] methodOne(Double[] D)
{
D[1] = 36.25;
return methodTwo(D);
}
static Double[] methodTwo(Double[] D)
{
D[1] = 62.36;
return methodThree(D);
}
static Double[] methodThree(Double[] D)
{
D[1] = 93.58;
return D;
}
public static void main(String[] args)
{
Double[] D = {10.55, 25.36, 58.29, 74.32, 32.21};
D = methodOne(D);
System.out.println(D[1]);
}
}
Ans : 93.58
No comments:
Post a Comment