Which two statements are true about wrapper or&n

Loading

Practice Java.lang Class –

Which two statements are true about wrapper or String classes?

  1. If x and y refer to instances of different wrapper classes, then the fragment x.equals(y) will cause a compiler failure.
  2. If x and y refer to instances of different wrapper classes, then x == y can sometimes be true.
  3. If x and y are String references and if x.equals(y) is true, then x == y is true.
  4. If xy, and z refer to instances of wrapper classes and x.equals(y) is true, and y.equals(z) is true, then z.equals(x) will always be true.
  5. If x and y are String references and x == y is true, then y.equals(x) will be true.

What will be the output of the program (in jdk1.6 or above)?

Loading

Practice Java.lang Class –

What will be the output of the program (in jdk1.6 or above)?

public class BoolTest {    
public static void main(String [] args) {
Boolean b1 = new Boolean("false");
boolean b2;
b2 = b1.booleanValue();
if (!b2) {
b2 = true;
System.out.print("x");
}
if (b1 & b2) /* Line 13 */ {
System.out.print("y ");
}
System.out.println("z");
}
}

 

What will be the output of the program?


			  		

Loading

Practice Java.lang Class –

What will be the output of the program?

public class BoolTest  {     
public static void main(String [] args) {
int result = 0;
Boolean b1 = new Boolean("TRUE");
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("tRuE");
Boolean b4 = new Boolean("false");
if (b1 == b2) /* Line 10 */
result = 1;
if (b1.equals(b2) ) /* Line 12 */
result = result + 10;
if (b2 == b4) /* Line 14 */
result = result + 100;
if (b2.equals(b4) ) /* Line 16 */
result = result + 1000;
if (b2.equals(b3) ) /* Line 18 */
result = result + 10000;
System.out.println("result = " + result);
}
}

 

What will be the output of the program?


			  		

Loading

Practice Java.lang Class –

What will be the output of the program?

public class WrapTest {     
public static void main(String [] args) {
int result = 0;
short s = 42;
Long x = new Long("42");
Long y = new Long(42);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);
if (x == y) /* Line 13 */
result = 1;
if (x.equals(y) ) /* Line 15 */
result = result + 10;
if (x.equals(z) ) /* Line 17 */
result = result + 100;
if (x.equals(x2) ) /* Line 19 */
result = result + 1000;
if (x.equals(z2) ) /* Line 21 */
result = result + 10000;
System.out.println("result = " + result);
}
}

 

What will be the output of the program?


			  		

Loading

Practice Exceptions –

What will be the output of the program?

public class X {      
public static void main(String [] args) {
try {
badMethod();
System.out.print("A");
}
catch (Exception ex) {
System.out.print("B");
}
finally {
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {

}
}

 

What will be the output of the program?


			  		

Loading

Practice Exceptions –

What will be the output of the program?

public class X {      
public static void main(String [] args) {
try {
badMethod();
System.out.print("A");
}
catch (RuntimeException ex) /* Line 10 */ {
System.out.print("B");
}
catch (Exception ex1) {
System.out.print("C");
}
finally {
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod() {
throw new RuntimeException();
}
}

 

What will be the output of the program?


			  		

Loading

Practice Exceptions –

What will be the output of the program?

public class X {       
public static void main(String [] args) {
try {
badMethod();
System.out.print("A");
}
catch (Exception ex) {
System.out.print("B");
}
finally {
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {
throw new Error(); /* Line 22 */
}
}

 

What will be the output of the program?


			  		

Loading

Practice Exceptions –

What will be the output of the program?

try {      
int x = 0;
int y = 5 / x;
}
catch (Exception e) {
System.out.println("Exception");
}
catch (ArithmeticException ae) {
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");

 

What will be the output of the program?


			  		

Loading

Practice Exceptions –

What will be the output of the program?

public class Foo {       
public static void main(String[] args) {
try {
return;
}
finally {
System.out.println( "Finally" );
}
}
}

 

public Object m() 
{

Loading

Practice Garbage Collections –

public Object m() 
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}

When is the Float object, created in line 3, eligible for garbage collection?

public class X {     
public

Loading

Practice Garbage Collections –

public class X {     
public static void main(String [] args) {
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx) {
mx = new X();
return mx;
}
}

After line 8 runs. how many objects are eligible for garbage collection?

class Test  {       
private

Loading

Practice Garbage Collections –

class Test  {       
private Demo d;
void start() {
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo) {
demo = null;
demo = new Demo();
}
}

When is the Demo object eligible for garbage collection?

class Bar { }  
class Test {

Loading

Practice Garbage Collections –

class Bar { }  
class Test {
Bar doBar() {
Bar b = new Bar(); /* Line 6 */
return b; /* Line 7 */
}
public static void main (String args[]) {
Test t = new Test(); /* Line 11 */
Bar newBar = t.doBar(); /* Line 12 */
System.out.println("newBar");
newBar = new Bar(); /* Line 14 */
System.out.println("finishing"); /* Line 15 */
}
}

At what point is the Bar object, created on line 6, eligible for garbage collection?