String d = "bookkeeper"; d.substring(1,7); d = "w" + d; d.append("woo"); /* Line 4 */ System.out.println(d);
int i = 1, j = 10; do { if(i++ > --j) /* Line 4 */ { continue; } } while (i < 5); System.out.println("i = " + i + "and j = " + j); /* Line 9 */
if(i > j)
if(2 > 9)
if(3 > 8)
if(4 > 7)
if(5 > 6) at this point i is not less than 5, therefore the loop terminates and line 9 outputs the values of i and j as 5 and 6 respectively.
The continue statement never gets to execute because i never reaches a value that is greater than j.
int i = (int) Math.random();
The value after the decimal point is lost when you cast a double to int and you are left with 0.
public class StringRef { public static void main(String [] args) { String s1 = "abc"; String s2 = "def"; String s3 = s2; /* Line 7 */ s2 = "ghi"; System.out.println(s1 + s2 + s3); } }
String x = "xyz"; x.toUpperCase(); /* Line 2 */ String y = x.replace('Y', 'y'); y = y + "abc"; System.out.println(y);
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); } }
System.out.println(Math.sqrt(-4D));
public class NFE { public static void main(String [] args) { String s = "42"; try { s = s.concat(".5"); /* Line 8 */ double d = Double.parseDouble(s); s = Double.toString(d); int x = (int) Math.ceil(Double.valueOf(s).doubleValue()); System.out.println(x); } catch (NumberFormatException e) { System.out.println("bad number"); } } }
String s = "ABC"; s.toLowerCase(); s += "def"; System.out.println(s);
Line 2 returns a string object but does not change the originag string object s, so after line 2 s is still "ABC".
So what's happening on line 3? Java will treat line 3 like the following:
s = new StringBuffer().append(s).append("def").toString();
This effectively creates a new String object and stores its reference in the variable s, the old String object containing "ABC" is no longer referenced by a live thread and becomes available for garbage collection.
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); } }
try { Float f1 = new Float("3.0"); int x = f1.intValue(); byte b = f1.byteValue(); double d = f1.doubleValue(); System.out.println(x + b + d); } catch (NumberFormatException e) /* Line 9 */ { System.out.println("bad number"); /* Line 11 */ }
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.