Review 2 (Year 3 Sem 1)

Review 2 is the second review in semester 1 of year 3, and the second review in the total of year 3. It is a review for conditionals and loops, 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 2 [C3 & C4])
 * All reviewed (Score at least 80% for Review Exercise 2 [C3 & C4])
 * Running Infinitely (Score at least 80% for Review Exercise 2 [C3 & C4])

Question
In general, which of the following expressions is the correct way to test whether two floating-point variables, x and y, are approximately equal in value? The constant EPSILON is assumed to be defined as follows:

final double EPSILON = 1E-8;

Options

 * x == y
 * x-y <= EPSILON
 * x <= y+EPSILON || x >= y-EPSILON
 * -EPSILON <= x–y && x-y <= EPSILON
 * -EPSILON <= x–y || x-y <= EPSILON

Question
Consider the following output.

1 1 1 1 1 2 2 2 2  3 3 3  4 4  5

Which of the following code segment will produce the above output?

Options

 * for ( int j= 1 ; j&lt;= 5 ;  j++){    for ( int k= 1 ; k&lt;= 5 ;  k++) {        System. out .print(j + " " );    }    System. out .println; }


 * for ( int j= 1 ; j&lt;= 5 ;  j++){    for ( int k= 1 ; k&lt;=j;  k++){        System. out .print(j + " " );    }    System. out .println; }


 * for ( int j= 1 ; j&lt;= 5 ;  j++){    for ( int k= 1 ; k&gt;= 1 ;  k++){        System. out .print(j + " " );    }    System. out .println; }


 * for ( int j= 1 ; j&lt;= 5 ;  j++){    for ( int k= 1 ; k&gt;=j;  k++) {        System. out .print(j + " " );    }    System. out .println; }


 * for ( int j= 1 ; j&lt;= 5 ;  j++){    for ( int k=j; k&lt;= 5 ;  k++){        System. out .print(j + " " );    }    System. out .println; }

Question
What is the result of the following code?

public class Mystery{   public static void main(String[] args) {        for ( int f = 0 ; f &lt; 3 ; ++f)            for ( int g = 0 ; g &lt; 2 ; g++)                System. out .print(f + " " + g + " " );   } }

Options

 * There is no output
 * 0 1 1 2 2 3 3 4 4 4 5 5
 * 0 0 0 1 2 0 1 1 1 0 2 1
 * 0 0 0 1 1 0 1 1 2 0 2 1
 * 0 0 0 1 1 0 1 2 2 1 2 2

Question
What is the result of the following code?

public class Headache {   public static void main(String[] args) {        int sum = 0, j = 0 ;        do {            j++;            for ( int i = 5 ; i &gt; j; i-- ){                sum = sum + (i + j) ;            }        } while ( j &lt; 3 ) ;        System. out .println(sum);   } }

Options

 * 46
 * 51
 * 60
 * 73
 * 75

Question
What is the result of the following code?

public class switchCase {   public static void main(String[] args) {        int x = 7 ;        String s = "" ;        for ( int y = 0 ; y &lt;= 3 ; y++){            x++;            switch (x){                case 8 :  s += "8 " ;                case 9 :  s += "9 " ;                case 10 : s += "10 " ; break ;                default : s += "d " ;                case 13 : s += "13 " ; break ;                case 7 : s += "7 " ;            }        }        System. out .println(s);   } }

Options

 * 9 10 d
 * 8 9 10 d 13
 * 7 8 9 10
 * 8 9 10 10 d
 * 8 9 10 9 10 10 d 13
 * The code cannot compile

Question
What will be the output when the following code is executed?

public class Test {   public static void main(String[] args){        for ( int i = 0 ;; i++)            System. out .println( "HELLO" );   } }

Options

 * Compile time error
 * Prints HELLO once
 * Prints HELLO infinitely
 * Runtime error
 * None of the above

Question
What will be the output when the following code is executed?

public class Test {   public static void main(String[] args){        int x = 10 ;        if (x) {            System. out .println( "HELLO" );       } else {            System. out .println( "BYE" );       }    } }

Options

 * Compile time error
 * Prints HELLO once
 * Prints HELLO infinitely
 * Runtime error
 * None of the above

Question
What will be the output when the following code is executed?

public class Test {   public static void main(String[] args) {        int i = 0, j = 9 ;        do {            i++;            if (j-- &lt; i++) {                break ;            }        } while (i &lt; 5 );        System. out .println(i + "" + j);   } }

Options

 * 44
 * 55
 * 66
 * 77
 * None of the above

Question
What will be the output when the following code is executed?

public class Test {   public static void main(String[] args) {        int x = 10 ;        if (++x &lt; 10 &amp;&amp; (x / 0 &gt; 10 )) {            System. out .println( "A" );       } else {            System. out .println( "B" );       }    } }

Options

 * Compile time error
 * Runtime error
 * Prints A
 * Prints B
 * None of the above

Solution
Due to short-circuit evaluation, ++x &lt; 10 is false, hence control flow will go to else statement.