Introduction to Java Unit 2. Exercises Note: Curly brackets {} are optional if there is only one statement associated with the if (or else) statement. Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextInt(); 1. If the user enters 82, what is displayed? if ( x < 7 ) System.out.println( "Good" ); else System.out.println( "Bad" ); Bad Ok 2. If the user enters 2, what is displayed? Good Ok 3. If the user enters 10, what is displayed? AA BB CC 4. If the user enters 12, what is displayed? AA CC if ( x > 0 ) System.out.println( "Ok" ); Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextInt(); System.out.println( "AA" ); if ( x <= 11 ) { System.out.println( "BB" ); } System.out.println( "CC" ); 5. If the user enters 10, what is displayed? 30 Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextInt(); 6. If the user enters 4, what is displayed? if ( x != 10 ) x = x + 5; 29 7. If the user enters 2, what is displayed? 37 if ( x > 7 ) x = x + 20; else x = x + 30; System.out.println( x ); 8. If x has an initial value of 33, what is its final value? 39 8. If x has an initial value of 62, what is its final value? // x is declared and assigned a value if ( x > 30 && x <= 50 ) { x = x + 10; } x = x - 4; if ( x < 40 && x > 60 ) page 1 62 x = x + 2; page 2 10. If x has a value of -5 and y has a value of 63, what is displayed? H K // x and y are declared and initialized 11. If x has a value of 47 and y has a value of 47, what is displayed? if ( x < 100 || x > 40 ) System.out.println( "H" ); G if ( y < 10 || y > 50 ) System.out.println( "K" ); if ( x > 30 || y >= 60 ) System.out.println( "G" ); H 12. Select the TRUE statement. a) H is never printed. b) H is always printed. c) H is only printed sometimes. 13. If y has an initial value of 12, what is displayed? 23 Scanner x = new Scanner( System.in ); System.out.println( "Number?" ); int y = x.nextInt(); 14. If y has an initial value of 26, what is displayed? if ( y > 10 ) y += 10; 37 15. If y has an initial value of 7, what is displayed? 7 16. The code to the right does not compile. The “else” is highlighted and the error message is: ‘else’ without ‘if’ Why? It says a If without an else because you need to put } in the beginning of else then click enter and put { then tab and put the line of code and the the end of the code you need to put } To close it. 17. Given that the above error is fixed by the addition of two characters, what will be displayed if the user enters 12? 24 18. If x has an initial value of -8, what is its final value? -3 19. If x has an initial value of 9, what is its final value? 11 20. If x has an initial value of 15, what is its final value? if ( y > 20 ) y++; System.out.println( y ); Scanner read = new Scanner( System.in ); System.out.println( "Number?" ); int a = read.nextInt(); if ( a > 10 && a < 20 ) { a = 2 * a; System.out.println( a ); }else { a--; System.out.println( a ); } // x is declared and assigned a value if ( x > 10 || x < 20 ) x = x + 2; if ( x < 10 || x > 20 ) x = x + 3; // x is declared and assigned a value if ( x > 10 && x < 20 ) x = x + 2; page 3 17 20. If x has an initial value of 22, what is its final value? if ( x < 10 && x > 20 ) x = x + 3; if ( x > 10 && x > 20 ) x = x + 10; 21. 32 22. If x has a final value of 6, what was its initial value? 23. 6 page 4