Date: 18/03/2019 B.SC (Hons) Comp. SC II Sem Assignment Java Programming 2019 Q. No
Date: 18/03/2019 B.SC (Hons) Comp. SC II Sem Assignment Java Programming 2019 Q. No
Q. No(1)
What will be the output of the program?
class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("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();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + "
");
System.out.println(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++;
System.out.println(x);
}
}
Q.No(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?
Q.No(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()
{
System.out.print(a.append("A"));
synchronized(b)
{
System.out.print(b.append("B"));
}
}
}.start();
new Thread()
{
public void run()
{
System.out.print(b.append("C"));
synchronized(a)
{
System.out.print(a.append("D"));
}
}
}.start();
}
Q.No(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;
}
System.out.println(I);
Q.No(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;
}
};
AbstractTest.Bar f = t.new Bar()
{
public int getNum()
{
return 57;
}
};
System.out.println(f.getNum() + " " +
t.getNum());
}
}
Q.No(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++;
}
}
System.out.println(x + " " + y);
}
}
Q.No.(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];
System.out.println("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 (o.equals(oc) )
result = result + 100;
if (oc.equals(o) )
result = result + 1000;
System.out.println("result = " + result);
}
}
Q.No(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 (o.equals(oc) )
result = result + 100;
if (oc.equals(o) )
result = result + 1000;
System.out.println("result = " + result);
}
}
Q.No(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;
System.out.println(y[2][1]);
}
}
Q.No(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(Character.isDigit(c[i]))
System.out.println(c[i]+" is a digit");
if(Character.isWhitespace(c[i]))
System.out.println(c[i]+" is a Whitespace
character");
if(Character.isUpperCase(c[i]))
System.out.println(c[i]+" is an Upper case
Letter");
if(Character.isLowerCase(c[i]))
System.out.println(c[i]+" is a lower case
Letter");
i++;
}
}
}
Q.No(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;
obj.add(a, a);
obj.add(b, b);
System.out.println(obj.x + " " +
obj.y);
}
}
boolean isPalindrome(String s) {
int i = 0, j = s.length() - 1;
while (i != j && s.charAt(i) == s.charAt(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