Sie sind auf Seite 1von 6

What will happen when you attempt to compile and run the following code?

public class HarHam{ public static void main(String argv[]){ HarHam hh = new HarHam(); hh.go(); } public void go(){ String har = new String ("har"); String ham = new String("har"); collar: for(int i=0; i < 2 ; i ++){ if(har==ham){ break collar; } if(i > 0){ continue collar; } System.out.print(i); } } }

Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 Compile time error, System.out has no print method No output at runtime Output of 0 Compile error, break cannot occur after its target label

You selected 1 option The Correct Answer is 3) Output of 0 The == operator should never be used to test the equivalence of strings as it will only test the reference, not the sequence of characters. To test if the Strings match use the String equals() method. If you create strings with the new keyword you are guaranteed they are different objects. If you create them without the user of new i.e. as
String s= "har"; String s1= "har";

They references will point to the same object in the String pool and thus the test
if(s == s1)

will be true.

Which of the following statements are true? Incorrect options are shown by strikethrough, any options you chose are marked by a tick

1 2 3 4

a try clause must have a matching catch and finally clause A try clause can be used without a matching finally or catch clause A try clause can be used with a matching finally clause and no catch clause a try clause may be declared as public or private but not protected.

You selected 1 option The Correct Answer is 3) A try clause can be used with a matching finally clause and no catch clause Option 4 is nonsense, try clauses are not used with the visibility modifiers Given the following code which option would be valid on the line after the comment //here
public class Crushton{ public static void main(String argv[]){ new Crushton(); } Crushton(){ String s="1"; int i = 9; //here }

Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 String s2=s+i; String s3=i+s; int j= i + Integer.parseInt(s); int k = i + Integer.intValue(s);

You selected 1 option The Correct Answer is 1) String s2=s+i; 2) String s3=i+s; (if you think this won't compile, try actually keying it into the code and compiling it) 3) int j= i + Integer.parseInt(s); What will happen when you attempt to compile and run the following code ?
class Biddy{ public Biddy(){ System.out.print(" Biddy "); } } class Val extends Biddy{ public Val(){ System.out.print(" Val "); } } public class BluePeter extends Val{

public static void main(String argv[]){ BluePeter bp = new BluePeter(); bp.shep(3); } public void shep(float i){ System.out.println(i); }

Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 Compilation and output of Val Biddy 3 Compilation and output of Val Biddy 3.0 Compilation and output of Biddy Val 3.0 Compile time error, wrong parameter type passed to method shep

You selected 1 option The Correct Answer is 3) Compilation and output of Biddy Val 3.0 Constructors are executed in order of the oldest ancester down, so the output from Biddy is seen before Val. Although the the paremeter for shep has no floating point component, a widening conversion involves no loss of precision and thus the value is cast to a float by the method shep. By default a float will have a decimal component and thus it has a trailing zero in this example What will happen when you attempt to compile and run the following code?
public class Mickle { public static void main(String argv[]){ new Mickle(); } public Mickle() { String s[][] = new String[2][2]; System.out.println(s[1][1]); System.out.println(s[1][2]); } }

Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 Compile time error Compilation and output of two blanks (with carriage returns) Compilation and output of null followed by ArrayIndexOutOfBoundsException Compilation and output of blanke followed by ArrayIndexOutOfBoundsException

You selected 1 option The Correct Answer is 3) Compilation and output of null followed by ArrayIndexOutOfBoundsException An uninitialised Object that is an array element will be set to null by default. The second call to println because all arrays start from 0, so an array with an element of size 2 has elements 0 and 1 but no element 2.

Which of the following statements are true? Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 An array may contain only primitives not objects An array may contain primitives or objects The elements of an array may themselves be arrays Arrays have a size method that returns the number of elements

You selected 2 options The Correct Answer is 2) An array may contain primitives or objects3) The elements of an array may themselves be arrays A count of the elements in an array is contained in the length field Given the following code, which of the following options if inserted after the comment //here will allow the code to compile without error?
interface Remote{ public void test();

} public class Moodle{ public static void main(String argv[]){ Moodle m = new Moodle(); } public void go(){ //here } }

Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 Remote r = new Remote(){ public void test(){} }; Remote remote = new Remote(); test(); this.main();

You selected 1 option The Correct Answer is 1) Remote r = new Remote(){ public void test(){} }; This code implements an anonymous inner class that implements the Remote interface and will compile without error. Option 2 should be obvious nonsense as you cannot directly instantiate an interface What will happen when you attempt to compile and run the following code?
class Archer { public void finalize(){ System.out.println("finalizing"); } }

public class Shula{ String[] sa= new String[10]; public static void main(String argv[]){ new Shula(); } public Shula(){ String s = new String("Shula"); int i = 99; i = null; Archer a = new Archer(); a=null; System.gc(); }

Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 Compile time error Compilation and output of "finalizing" at runtime Compilation but no output at runtime Compilation but runtime exception

You selected 1 option The Correct Answer is 1) Compile time error. A primitive cannot be set to null

Which of the following statements are true about correctly overriden hashCode() and equals() methods? Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 the equals() must return the same value as using the == operator equals() and hashCode() code must return the same value. If two objects are different according to equals() they will return different hashCode() values. If two objects are equal according to equals() they will return the same hashCode value.

You selected 1 option The Correct Answer is 4) If two objects are equal according to equals() they will return the same hashCode value. Option 1 should be obvious nonsense, using the == operator on an instance of a class will simply compare the memory address. Although this is the default operation of equals it is only one of many ways to compare two class instances. Option 2 should be instinctivly incorrect because you should know that equals returns a boolean value and hashCode returns an int value. Option 3 looks plausible but is not correct.

Which of the following statements are true of a method local inner class? Incorrect options are shown by strikethrough, any options you chose are marked by a tick 1 2 3 4 It has access to all members of its enclosing class It can only access final variables of its enclosing method It can be marked as static It can only access final members of its enclosing class

You selected 2 options The Correct Answer is 1) It has access to all members of its enclosing class 2) It can only access final variables of its enclosing method There are no restrictions on on what a method local inner class can access from the enclosing class, though it may only access final members of its enclosing method.

Das könnte Ihnen auch gefallen