Review 3 (Year 3 Sem 1)

Review 3 is the third review in semester 1 of year 3, and the third review in the total of year 3. It is a review for methods and Java API, and is considered to be moderately easy.

Description
Please note that the purpose of Review Exercises is to prepare you for the WRITTEN TEST.

Hence, please do the questions after revising the topics, and think through the options properly.

You should NOT try to execute the code when attempting the questions.

Unlocked after completion

 * OCD (Score at least 60% for Review Exercise 3 [C5 & C6])
 * All reviewed (Score at least 80% for Review Exercise 3 [C5 & C6])
 * Ready for Sprint (Score at least 80% for Review Exercise 3 [C5 & C6])

Question
Given the following overloaded methods:

public static int m( int a, int b) { return a * b; } public static int m( int a, double b) { return a + ( int ) b; } public static int m( double a, int b) { return ( int ) a - b; }

The call m(3.0, 5.0) is made.

Which of the following statements correctly describes what would happen?

Options

 * The call returns the value 15 because none of the methods matches the call, so the first method is used.
 * The call returns the value 8 because the first closest match is used.
 * The call returns the value -2 because the last closest match is used.
 * The call throws a MismatchedMethodException.
 * The call causes a compilation error.

Question
Consider the following code:

String str = new String( "Goodbye" ); for ( int i= 0 ; i &lt; str.length; i+= 2 ){   System. out .print(str.charAt(i)); } System. out .println( "#" );

Write down the final output printed.

Question
Method signature consists of

Options

 * Method Name, Return Type and Number Of Arguments
 * Access Modifier, Method Name and Types Of Arguments
 * Method Name, Number Of Arguments, Types Of Arguments and Order Of Arguments
 * Return Type, Access Modifier and Order Of Arguments
 * None of the above

Question
Consider the following code:

public class Test {   public static void main(String[] args) {        //insert code here     }    public static void m1( long n){        System. out .println(n+ "long" );   }    public static void m1( int n){        System. out .println(n+ "int" );   }    public static void m1( double n){        System. out .println(n+ "double" );   } }

Write the code to be added at //insert code here such that the output  will be seen.

Note: Do not include any whitespace in your answer.

Question
Consider the following code:

public class Test {   public static void main(String[] args) {        String w = "hello" ;    w.toUpperCase;        System. out .println(w);   } }

Write the output that will be printed when the above code is executed.

Solution
Note that w.toUpperCase is not assigned back to w. Hence, the effect of toUpperCase is not "saved".

Question
Consider the following code:

import java.util.Random; public class Test {    public static void main(String[] args) {        Random r = new Random;        int a = r.nextInt( 15 ) - 9 ;        System. out .println(a);   } }

Which of the following cannot be an output when the program is executed?

Options

 * -9
 * 6
 * 15
 * 0
 * 5

Question
Consider the following code segment:

public class Main {   public static void main(String[] args){        String s1 = "Hello" ;        String s2 = new String( "Hello" );        String s3 = "Hello" ;        String s4 = new String( "Hello" );               /* add code here */       } }

Which of the following statement, when added to /* add code here */ will result in an output of true?

Options

 * System.out .println(s1 == s2);
 * System.out .println(s2 == s3);
 * System.out .println(s1 == s3);
 * System.out .println(s2 == s4);
 * None of the above

Question
Consider the following code segment:

public class Main {   public static void main(String[] args){        String s1 = "Hello" ;        String s2 = "Hello" ;        String s3 = s1;        s1 = "Bye" ;        s3 = "Bye" ;        s2 = new String( "Bye" );        / * do stuff */    } } How many String objects will be eligible for garbage collection when line /* do stuff */ is reached?

Options

 * 0
 * 1
 * 2
 * 3
 * 4