Date: 18/03/2019
[Link](Hons) Comp. Sc II Sem Assignment Java Programming 2019
Q. No(1)
What will be the output of the program?
class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;
void set(boolean [] x, int i)
{
x[i] = true;
++count;
}
public static void main(String [] args)
{
BoolArray ba = new BoolArray();
[Link](ba.b, 0);
[Link](ba.b, 2);
[Link]();
}
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
[Link]("count = " + count);
}
}
Q. No(2)
What will be the output of the program?
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
[Link]();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
[Link](a1[0] + a1[1] + a1[2] + "
");
[Link](a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
Q.(3)What will be the output of the program?
public class ExamQuestion6
{
static int x;
boolean catch()
{
x++;
return true;
}
public static void main(String[] args)
{
x=0;
if ((catch() | catch()) || catch())
x++;
[Link](x);
}
}
[Link](4)
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
after line 11 runs, how many objects are eligible
for garbage collection?
[Link](5)
What will be the output of the program?
public class Test
{
public static void main(String[] args)
{
final StringBuffer a = new StringBuffer();
final StringBuffer b = new StringBuffer();
new Thread()
{
public void run()
{
[Link]([Link]("A"));
synchronized(b)
{
[Link]([Link]("B"));
}
}
}.start();
new Thread()
{
public void run()
{
[Link]([Link]("C"));
synchronized(a)
{
[Link]([Link]("D"));
}
}
}.start();
}
[Link](6)
What will be the output of the program?
int I = 0;
outer:
while (true)
{
I++;
inner:
for (int j = 0; j < 10; j++)
{
I += j;
if (j == 3)
continue inner;
break outer;
}
continue outer;
}
[Link](I);
[Link](7)
What will be the output of the program?
public abstract class AbstractTest
{
public int getNum()
{
return 45;
}
public abstract class Bar
{
public int getNum()
{
return 38;
}
}
public static void main (String [] args)
{
AbstractTest t = new AbstractTest()
{
public int getNum()
{
return 22;
}
};
[Link] f = [Link] Bar()
{
public int getNum()
{
return 57;
}
};
[Link]([Link]() + " " +
[Link]());
}
}
[Link](8)What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
[Link](x + " " + y);
}
}
[Link].(9)
What will be the output of the program?
public class Test
{
public static void main (String[] args)
{
String foo = args[1];
String bar = args[2];
String baz = args[3];
[Link]("baz = " + baz); /*
Line 8 */
}
}
Q. No(10)
What will be the output of the program?
public class ObjComp
{
public static void main(String [] args )
{
int result = 0;
ObjComp oc = new ObjComp();
Object o = oc;
if (o == oc)
result = 1;
if (o != oc)
result = result + 10;
if ([Link](oc) )
result = result + 100;
if ([Link](o) )
result = result + 1000;
[Link]("result = " + result);
}
}
[Link](11)
What will be the output of the program?
public class ObjComp
{
public static void main(String [] args )
{
int result = 0;
ObjComp oc = new ObjComp();
Object o = oc;
if (o == oc)
result = 1;
if (o != oc)
result = result + 10;
if ([Link](oc) )
result = result + 100;
if ([Link](o) )
result = result + 1000;
[Link]("result = " + result);
}
}
[Link](12)What is the output of this program?
class Alligator
{
public static void main(String[] args)
{
int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
int [][]y = x;
[Link](y[2][1]);
}
}
[Link](13)
What is the output of this program?
class output {
public static void main(String args[]){
char c[]={'A', '1', 'b' ,' ' ,'a' , '0'}; for
(int i = 0; i < 5; ++i){
i++;
if([Link](c[i]))
[Link](c[i]+" is a digit");
if([Link](c[i]))
[Link](c[i]+" is a Whitespace
character");
if([Link](c[i]))
[Link](c[i]+" is an Upper case
Letter");
if([Link](c[i]))
[Link](c[i]+" is a lower case
Letter");
i++;
}
}
}
[Link](14)What is the output of this program?
class overload
{
int x;
double y;
void add(int a , int b)
{
x = a + b;
}
void add(double c , double d)
{
1. y = c + d;
}
overload()
{
this.x = 0;
this.y = 0;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 2;
double b = 3.2;
[Link](a, a);
[Link](b, b);
[Link](obj.x + " " +
obj.y);
}
}
[Link](15)The following method was written to
determine whether its String parameter reads
identically left-to-right and right-to-left (the so
called palindrome).
boolean isPalindrome(String s) {
int i = 0, j = [Link]() - 1;
while (i != j && [Link](i) == [Link](j)) {
i++;
j--;
}
return (i == j);
}
This method compiles fine, yet unfortunately, the
code contains a logic error, which
may result in a run-time error, or wrong output.
1. Find the error and explain what problem it will
cause
2. Fix the error (write the correct statements