0% found this document useful (0 votes)
495 views14 pages

Java Program Output Analysis

1) The program defines a changeObjects method that takes a String and StringBuffer as parameters. It modifies the String parameter but not the passed String variable. It modifies the StringBuffer parameter by appending to it. The method is called, passing a String and StringBuffer. The String is unchanged but the StringBuffer is modified. 2) The program defines several exception classes in a hierarchy and throws them in a try/catch block, printing the number of times each catch block is entered. 3) The program takes the ASCII value of an opening parenthesis, applies a bitwise NOT operator, and prints the result.

Uploaded by

Aircc Airccse
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
495 views14 pages

Java Program Output Analysis

1) The program defines a changeObjects method that takes a String and StringBuffer as parameters. It modifies the String parameter but not the passed String variable. It modifies the StringBuffer parameter by appending to it. The method is called, passing a String and StringBuffer. The String is unchanged but the StringBuffer is modified. 2) The program defines several exception classes in a hierarchy and throws them in a try/catch block, printing the number of times each catch block is entered. 3) The program takes the ASCII value of an opening parenthesis, applies a bitwise NOT operator, and prints the result.

Uploaded by

Aircc Airccse
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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";
[Link]("123");
}

....
String s = "abc";
StringBuffer sb = new StringBuffer("abc");
changeObjects (s, sb);
[Link](s);
[Link](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++;}
[Link](a+","+b+","+c+","+d+","+f+","+g);
}}
3) If the ascii value of ( is 40. what’s the output?

char v1 = '(';
int i = ~v1;
[Link](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"}, {} }
};
[Link](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;
[Link](x, y);
[Link](x + y);
}
}

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

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

private int giveMeJ()


{
return j;
}

public static void main(String args[])


{
[Link]((new Test()).i);
}
}

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

9)
boolean b = false;
if(b = false) {
[Link]("b is false");
} else if(b = true){
[Link]("b is true");
} else {
[Link]("b is not true or false");
}
10)
public class Test
{
public static void main(String args[])
{
int j=0;
for (int i = 0; i < 2; ) {
[Link](i);
i = i++;
[Link](i);
j++;
if(j==100)
break;
}
}
}

11)
class JMM101 {
public static void main(String[] args) {
int i = 0;
while (i++ < [Link]) {
[Link](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
{
}
}
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;
[Link](m1(b1) + m1(b2) + m1(b3));
}}

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

16)
int i=0, j = ++i + ++i * ++i;
[Link](j);

17)
class Fish
{
public void bite(int howHard)
{
[Link]("Fish::bite");
}
}

class Shark extends Fish


{
public void bite(long howHard)
{
[Link]("Shark::bite");
}

public static void main(String[] arg)


{
Shark s = new Shark();
Fish f = new Shark();

[Link](10);
[Link](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)
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

[Link](a3[2][1][0]); // 7
}}

20)
class B{
{
[Link]("5");

}
static {
[Link]("4");
}
{
[Link]("6");
}
}

class A extends B{
{
[Link]("2");
}
static{
[Link]("1");
}

A(){
[Link]("3");
}

public static void main (String[] args){

A test = new A();


}

21)
public class Test
{
int a;
public Test()
{
a = 100;
}
public void show()
{
[Link](this);
[Link]("the value of a is "+a);
}

public void set(int a)


{
[Link]("In base set");
this.a = a;
}

public static void main(String args[])


{
Test t = new drived();
[Link]();
[Link](t.a);
[Link]();

}
}

class drived extends Test


{
int a;
public drived()
{
a = 200;
}
public void show()
{
[Link]("The value of b is " + a);
}
public void set(int a)
{
[Link]("In drived set");
this.a = a;
}
}

22)

class t1
{
public static void main (String st[])
{
m1(1);
}

static void m1(byte b) {[Link]("Byte");}


static void m1(short b) {[Link]("Short");}
static void m1(int b) {[Link]("Integer");}
static void m1(long b) {[Link]("Long");}
static void m1(float b) {[Link]("Float");}
static void m1(double b) {[Link]("Double");}

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

public byte gn(){


return 1;

}
}

24)
class ValHold1{
public int i = 10;
}

public class ObParm1{


public static void main(String argv[]){
ObParm1 o = new ObParm1();
[Link]();
}

public void amethod(){


int i = 99;
ValHold1 v = new ValHold1();
v.i=30;
another(v,i);
[Link](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??
[Link](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;

Float test = new Float(b); //1


Float test1 = new Float(s);//2
Float test2 = new Float(c);//3
Float test3 = new Float(l);//4
Float test4 = new Float(f);//5
Float 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 ([Link]("foo")) {
arg = "2";
} else if ([Link]("baR")) {
arg = "3";
} else {
arg = "4";
}

[Link](arg);
}
}

27)

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

}
28)

public class MyTest


{
public static void main(String args [])
{
int i = 10 ; // line 1
i = ++i ; // line 2
i = i++; // line 3
[Link](i++); // line 4
}
}

29)

public class FinallyReturning


{
public int sampleMethod(int y)
{
try
{
[Link]("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);
[Link](a + "," + b + "," + c);
}}
31)
public class Test2 extends Base
{
Test2(long i)
{
super(2);
}
Test2()
{
super(2);//2
}

public static void main(String[] args)


{
new Test2(2);
}
}

class Base
{
Base(int i)
{
[Link](i);
}

}
32)

class LoopTest100{
static int i,j;
public static void main(String[] args) {
for (i = 0;i <3;i++,[Link](i)) { }
[Link]();
for (j = 0;j<3;++j) {
[Link](j);
}
}
}
33)
class StringTest1 {
public static void main(String[] args) {
String s1 = " Arg ";
s1= [Link]();
[Link](s1+ args[1] );
[Link]();

String s2 = " Arg "+ args[1] ;


s2= [Link]();
[Link](s2);
}
}

34)

public class Question24


{
public static void main(String[] args)
{
Question24 q24 = null;
int i = [Link](); //line 1
int j = getDice().throwDice(); //line 2
[Link]("Result: "+i+","+j);
}
private static int throwDice(){
return 1+(int)([Link]()*6);
}
private static Question24 getDice(){
return null;
}
}

You might also like