javaQuestions

16
1) Imagine a proper class has been defined. What would be the output of the following program? void changeObjects(String str, StringBuffer sb) { str = "123"; str = str + "123"; sb.append("123"); } .... String s = "abc"; StringBuffer sb = new StringBuffer("abc"); changeObjects (s, sb); System.out.println(s); System.out.println(sb); 2) What is the output of the program? class Level1Exception extends Exception {} class Level2Exception extends Level1Exception {} class Level3Exception extends Level2Exception {} class Purple { public static void main(String args[]) { int a,b,c,d,f,g,x; a = b = c = d = f = g = 0; x = 1; try { try { switch (x) { case 1: throw new Level1Exception(); case 2: throw new Level2Exception(); case 3: throw new Level3Exception(); } a++; } catch (Level2Exception e) {b++;} finally {c++;} } catch (Level1Exception e) { d++;}

Transcript of javaQuestions

Page 1: javaQuestions

1) Imagine a proper class has been defined. What would be the output of the following program?

void changeObjects(String str, StringBuffer sb){str = "123"; str = str + "123"; sb.append("123");}

....String s = "abc";StringBuffer sb = new StringBuffer("abc");changeObjects (s, sb);System.out.println(s); System.out.println(sb);

2) What is the output of the program?

class Level1Exception extends Exception {}class Level2Exception extends Level1Exception {}class Level3Exception extends Level2Exception {}class Purple { public static void main(String args[]) { int a,b,c,d,f,g,x; a = b = c = d = f = g = 0; x = 1; try { try { switch (x) { case 1: throw new Level1Exception(); case 2: throw new Level2Exception(); case 3: throw new Level3Exception(); } a++; } catch (Level2Exception e) {b++;} finally {c++;} } catch (Level1Exception e) { d++;} catch (Exception e) {f++;} finally {g++;} System.out.print(a+","+b+","+c+","+d+","+f+","+g);}}

Page 2: javaQuestions

3) If the ascii value of ( is 40. what’s the output?

char v1 = '(';int i = ~v1;System.out.println(i);

4) Whats the output of the program?

public class StringArrayTest{ public static void main(String args[]) { String[][][] arr = { { { "a", "b" , "c"}, { "d", "e", null } }, { {"x"}, null }, {{"y"}}, { { "z","p"}, {} } }; System.out.println(arr[0][1][2]); }}

5) Class Pass { public static int x, y; public void test(int x, int z) { x += x << 3; y += z << 2; } public static void main(String[] args) { Pass pass = new Pass(); x = 6; y = 5; pass.test(x, y); System.out.println(x + y); }}

6) String test = new String("hello");String test1 = new String("hello");

Page 3: javaQuestions

7)public class Test{private int i = giveMeJ();private int j;

private int giveMeJ(){return j;}

public static void main(String args[]){System.out.println((new Test()).i);}}

8)public class Test{public static void main(String args[]){StringBuffer str1="Hello";StringBuffer str2=" there";StringBuffer str3=str1+str2;System.out.println(str3);}}

9)boolean b = false;if(b = false) {System.out.println("b is false");} else if(b = true){System.out.println("b is true");} else {System.out.println("b is not true or false");}

10)public class Test{

Page 4: javaQuestions

public static void main(String args[]){

int j=0;for (int i = 0; i < 2; ) {

System.out.println(i);i = i++;System.out.println(i);j++;if(j==100)

break;}

}}

11)class JMM101 {public static void main(String[] args) {int i = 0;while (i++ < args.length) {System.out.print(args[i]);}}}

12)abstract class test{abstract public void checkThis() throws SecurityException;public void checkThat(){};}public class Test extends test{public void checkThis() throws SecurityException{}}

13)abstract class test{

abstract public void checkThis() throws SecurityException;}class Test1 extends test{

public void checkThis() throws SecurityException{}

Page 5: javaQuestions

}public class Test extends Test1{

public void checkThis(int x) throws SecurityException,ClassCastException{

}}

14)class EBH023 { static String m1(boolean b){return b?"T":"F";} public static void main(String [] args) { boolean b1 = false?false:true?false:true?false:true; boolean b2 = false?false : ( true?false : ( true?false:true)); boolean b3 = ((false?false:true)?false:true)?false:true; System.out.println(m1(b1) + m1(b2) + m1(b3));}}

15)class A{B b;A(){b=new B();System.out.println(b.toString());}public String toString(){return "I am A ";}}class B{A a;B(){a=new A();System.out.println(a.toString());}public String toString(){return "I am B ";}}public class Test{public static void main(String[] ar){

Page 6: javaQuestions

A a=new A();B b=new B();}}

16)int i=0, j = ++i + ++i * ++i;System.out.print(j);

17)class Fish{ public void bite(int howHard) { System.out.println("Fish::bite"); }} class Shark extends Fish{ public void bite(long howHard) { System.out.println("Shark::bite"); } public static void main(String[] arg) { Shark s = new Shark(); Fish f = new Shark(); s.bite(10); f.bite(10); }}

18)public class Test {public static void main (String [] args) {short[][] b = new short[4][4];short[][] big = new short[2][2];short b3 = 8;short b2[][][] = new short[2][3][2][2];//insert code here}}

19)

Page 7: javaQuestions

class A11 {

public String toString() {

return "A11"; }}

class A12 { public static void main(String[] arg) { A11[] a1 = new A11[1]; // 1 A11[][] a2 = new A11[2][]; // 2 A11[][][] a3 = new A11[3][][]; // 3 a1[0] = new A11(); // 4

a2[0] = a2[1] = a1; // 5 a3[0] = a3[1] = a3[2] = a2; // 6

System.out.print(a3[2][1][0]); // 7}}

20)class B{ { System.out.println("5"); } static { System.out.println("4"); } { System.out.println("6"); }} class A extends B{ {

Page 8: javaQuestions

System.out.println("2"); } static{ System.out.println("1"); } A(){ System.out.println("3"); } public static void main (String[] args){ A test = new A(); } }

21)public class Test{ int a; public Test() { a = 100; } public void show() { System.out.println(this); System.out.println("the value of a is "+a); } public void set(int a)

{ System.out.println("In base set");

this.a = a;}

public static void main(String args[]) { Test t = new drived(); t.show(); t.set(t.a); t.show(); }

Page 9: javaQuestions

} class drived extends Test{ int a; public drived() { a = 200; } public void show() { System.out.println("The value of b is " + a); } public void set(int a) { System.out.println("In drived set"); this.a = a; }}

22)

class t1{ public static void main (String st[]) { m1(1); } static void m1(byte b) {System.out.println("Byte");} static void m1(short b) {System.out.println("Short");} static void m1(int b) {System.out.println("Integer");} static void m1(long b) {System.out.println("Long");} static void m1(float b) {System.out.println("Float");} static void m1(double b) {System.out.println("Double");} }

23)class t{ public static void main(String []a){ new t().gn(); } public byte gn(){ return 1;

Page 10: javaQuestions

}}

24)class ValHold1{ public int i = 10;} public class ObParm1{public static void main(String argv[]){ ObParm1 o = new ObParm1(); o.amethod(); } public void amethod(){ int i = 99; ValHold1 v = new ValHold1(); v.i=30; another(v,i); System.out.println(v.i); }//End of amethod

public void another(ValHold1 v, int i){ i=0; v.i = 20; ValHold1 vh = new ValHold1();

v = vh ; ------------------> What this will do?? System.out.println(v.i+ " "+i); }//End of another }

25)

int i = -1;byte b = 2;short s = 3;short s2 = 10;char c = 'd' - 'b';long l = 5;float f = 1.0f;double d = 1;

Page 11: javaQuestions

Float test = new Float(b); //1Float test1 = new Float(s);//2Float test2 = new Float(c);//3Float test3 = new Float(l);//4Float test4 = new Float(f);//5Float test5 = new Float(d);//6

26)

public class Test { public static void main(String[] args) { foo("foo"); foo(new String("foo")); foo("bar"); }

private static void foo(String arg) { if (arg == "foo") { arg = "1"; } else if (arg.equals("foo")) { arg = "2"; } else if (arg.equals("baR")) { arg = "3"; } else { arg = "4"; }

System.out.print(arg); } }

27)

class ClassA { public static void main(String[] args) { int i = 1999; i = 1 + (new Object() { public int getI() { return i; } }).getI(); System.out.println("i = "+i); } }

28)

Page 12: javaQuestions

public class MyTest{ public static void main(String args []) { int i = 10 ; // line 1 i = ++i ; // line 2 i = i++; // line 3 System.out.println(i++); // line 4 }}

29)

public class FinallyReturning{

public int sampleMethod(int y){

try{

System.out.println("Hello World");throw new Exception("Something bad happened");

}finally{

return y;}

}}

30)

class EBH202 {static boolean a, b, c;public static void main (String[] args) {boolean x = (a = true) || (b = true) && (c = true);System.out.print(a + "," + b + "," + c);}}

31)

public class Test2 extends Base{Test2(long i){super(2);}Test2(){super(2);//2}

Page 13: javaQuestions

public static void main(String[] args){new Test2(2);}}

class Base{Base(int i){System.out.println(i);}

}

32)

class LoopTest100{static int i,j;public static void main(String[] args) {for (i = 0;i <3;i++,System.out.print(i)) { }System.out.println();for (j = 0;j<3;++j) {System.out.print(j);}}}

33)class StringTest1 {

public static void main(String[] args) {String s1 = " Arg "; s1= s1.trim();System.out.println(s1+ args[1] );System.out.println();

String s2 = " Arg "+ args[1] ;s2= s2.trim();System.out.println(s2);

}

}

34)

public class Question24 {public static void main(String[] args){Question24 q24 = null;int i = q24.throwDice(); //line 1int j = getDice().throwDice(); //line 2System.out.println("Result: "+i+","+j);

Page 14: javaQuestions

}private static int throwDice(){return 1+(int)(Math.random()*6);}private static Question24 getDice(){return null;}}