| 
   Java 
  TOPIC 04 – TEST I REVIEW 
    
    
    
  DETAILS & REVIEW 
    
    
    
  DETAILS 
    
  The test
  covers the units on Programming Basics, If Statements and Loops from the
  grade 11 course.  All notes.  All work. 
    
  Your test
  will be fully written.  You will not
  have a computer.  You do not need to
  remember any import statements. 
    
  Minor
  programming errors will not count against you. 
    
    
    
  REVIEW 
    
  QUESTION 1 
  You should be
  aware of the meaning of the following terms. 
  The light gray terms are less likely to appear on the test. 
    
  
   
    | 
     Programming
    language 
    Computer
    program 
    Syntax 
    Statement 
    Machine
    language 
    High
    level language 
    Source
    code 
    Compiler 
    Interpreter 
    Java
    compiler 
    Java
    virtual machine 
    IDE 
    Main
    function/method 
     | 
    
     Output
    statement 
    Indenting 
    Variable 
    Data
    type 
    Declaring a
    variable 
    Initializing
    a variable 
    Assignment
    statement 
    Literals 
    Arithmetic
    expression 
    String 
    Function
    argument 
    Function
    return-type 
    Function
    call 
     | 
    
     Boolean
    variable      
    Boolean expression 
    Control
    flow structure 
    Condition 
    Conditional operator 
    Iteration 
    Loop 
    Infinite
    loop 
    Control
    variable 
    Java
    Run-time Environment 
    Java
    Development Kit 
      
     | 
    
   
    
  QUESTION 2 
  What do the
  following abbreviations/symbols stand for? 
    
  IDE 
  JDK 
  JRE 
  >  
  >= 
  <  
  <= 
  == 
  != 
  ! 
  && 
  || 
  ! 
  % 
    
  QUESTION 3 
  What will be outputted to screen?  There may be cases where nothing is
  outputted at all. 
  
  
   
    | 
     a) 
     | 
    
     int x = 0; 
    if (x >= 0) 
    { 
       System.out.println("A"); 
    } 
    System.out.println("B"); 
     | 
    
   
    | 
     b) 
     | 
    
     int x = 0; 
    if (x >= 0) 
    { 
       System.out.println("A"); 
    } 
    else 
    { 
       System.out.println("B"); 
    } 
     | 
    
   
    | 
     c) 
     | 
    
     int x = 10; 
    if (x > 0) 
    { 
       System.out.println("A"); 
    } 
    else if (x > 5) 
    { 
       System.out.println("B"); 
    } 
     | 
    
   
    | 
     d) 
     | 
    
     int x = 10; 
    if (x > 0) 
    { 
       System.out.println("A"); 
    } 
    if (x > 5) 
    { 
       System.out.println("B"); 
    } 
     | 
    
   
    | 
     e) 
     | 
    
     int x = 0; 
    while (x > 3) 
    { 
       System.out.println(x); 
       x++; 
    } 
     | 
    
   
    | 
     f) 
     | 
    
     int y = 4; 
    while (y > 7) 
    { 
       System.out.println("y"); 
       y++; 
    } 
     | 
    
   
    | 
     g) 
     | 
    
     int z = 10; 
    while (z > 0) 
    { 
       if (z % 2 == 0) 
       { 
          System.out.println(z); 
       } 
    } 
     | 
    
   
    | 
     h) 
     | 
    
     int a = 42; 
    if (a != 42) 
    { 
       System.out.println("yo"); 
    } 
    else 
    { 
       while (a == 42) 
       { 
          System.out.println("ya"); 
          a++; 
       } 
    } 
     | 
    
   
    | 
     i) 
     | 
    
     int a=4; 
    int b=3; 
    while (a + b < 10) 
    { 
       System.out.println(a); 
       a = a – 1; 
       b = b + 2; 
    } 
     | 
    
   
    | 
     j) 
     | 
    
     int a=5; 
    int b=5; 
    while (10 – b == a) 
    { 
       a++; 
       b--; 
       if (b == 2) 
       { 
          System.out.println("yoda"); 
       } 
       else if (b == 1) 
       { 
          System.out.println("schmoda"); 
          b++; 
       } 
    } 
     | 
    
   
    | 
     k) 
     | 
    
     for(int x=1;
    x<6; x++) 
    { 
       System.out.println(x); 
    } 
     | 
    
   
    | 
     l) 
     | 
    
     for(int x=1;
    x<4; x++) 
    { 
       int z = 10 – x; 
       System.out.println(z); 
    } 
     | 
    
   
    | 
     m) 
     | 
    
     for(int x=1;
    x<8; x--) 
    { 
       System.out.println(x); 
    } 
     | 
    
   
    | 
     n) 
     | 
    
     for(int x=1;
    x<31; x=x+5) 
    { 
       System.out.println(x); 
    } 
     | 
    
   
    | 
     o) 
     | 
    
     for(int a=2;
    a<9; a++) 
    { 
       if (10 % a == 0) 
       { 
          System.out.println(a); 
       } 
    } 
     | 
    
   
    | 
     p) 
     | 
    
     for(int x=1;
    x<3; x++) 
    { 
       for(int y=1; y<=2; y++) 
       { 
          System.out.println("clicky"); 
       } 
    } 
     | 
    
   
    | 
     q) 
     | 
    
     String s =
    "***"; 
    for(int x=1; x<=4; x++) 
    { 
       System.out.println(s+s); 
    } 
     | 
    
   
    | 
     r) 
     | 
    
     for(int x=1;
    x<=3; x++) 
    { 
       for(int y=1; y<=3; y++) 
       { 
          System.out.println("***"); 
       } 
    } 
     | 
    
   
    | 
     s) 
     | 
    
     for(int x=1;
    x<=3; x++) 
    { 
       for(int y=1; y<=4; y++) 
       { 
          System.out.print("*"); 
       } 
       System.out.println(""); 
    } 
     | 
    
   
    | 
     t) 
     | 
    
     for(int x=1;
    x<=5; x++) 
    { 
       for(int y=1; y<=x; y++) 
       { 
          System.out.print("*"); 
       } 
       System.out.println(""); 
    } 
     | 
    
   
    | 
     u) 
     | 
    
     for(int x=1;
    x<=3; x++) 
    { 
       if (x == 2) 
       { 
          System.out.print("o"); 
       } 
       else if (x == 1) 
       { 
         
    System.out.print("camp"); 
       } 
       else  
       { 
          System.out.println("
    rocks"); 
       } 
    } 
     | 
    
   
    | 
     v) 
     | 
    
     for(int x=1;
    x<=5; x++) 
    { 
       System.out.println("b"); 
       while(x <= 2) 
       { 
          if(x % 2 != 0) 
          { 
              System.out.println("a"); 
          } 
          x++; 
       } 
    } 
     | 
    
   
    | 
     w) 
     | 
    
     int x = 5; 
    int y = 2; 
    System.out.println(5/2); 
     | 
    
   
    | 
     x) 
     | 
    
     int x = 5; 
    int y = 2; 
    double z = 5/2; 
    System.out.println(z); 
     | 
    
   
   
    
    
  QUESTION 4 
  Write the
  following programs. 
   
  Reminder
  about Scanner: The Scanner class does not correctly read an int and then a
  line (String).  You can either use two
  Scanner objects or you can read all inputs as Strings and convert them to int
  or double afterwards.  Ask your teacher
  for help.  Note that you do not need to
  worry about this on the test. 
   
   
  a)   
  Ask
  the user for their age.  Then ask the
  user if their bday has passed yet this year.  Use the info to calculate and output the
  year of their birth.   
   
  Sample output: 
   
            How old are you? 
            12 
            Has your bday
  passed this year (n/y)? 
            n 
            You were born in the year 2000. 
   
   
  b)   
  Ask
  the user for the name of an item, its price and the tax rate (3 inputs in
  total).  The program should output a
  sentence with the items name and it’s full price after taxes. 
   
  Here is some sample output: 
   
       Enter your item’s name: 
       Socks 
       Enter the price: 
       4.00 
       Enter the tax rate (whole number): 
       13 
       Socks cost 4.52 in total. 
  Write the program that asks the user
  for the current hour during the day. 
  It then asks if it is in the AM or PM. 
  Based on the provided information, the program outputs one of the
  following messages: 
            11PM
  to 3AM – Get to bed! 
            4AM to 6AM – Already up? 
            7AM to 11AM – Good morning  
            12PM to 5PM – Good afternoon 
            6PM to 10PM – Good evening 
   
  QUESTION 5 
  a) Use the while loop to output all
  numbers from 1 to 15 on the screen. 
  b) Use the for loop to output all
  numbers from 1 to 15 on the screen. 
   
  QUESTION 6 
  Write a program that asks the user for an upper bound and a lower bound.  It then asks if the user wants the even
  numbers or the odd numbers and displays all such numbers that exist between
  the bounds. 
  Sample
  output: 
       Enter a lower number. 
       6 
       Enter a higher number. 
       15 
       Do you want odd or even numbers? 
       odd 
       Here are all odd numbers between 6
  and 15: 
       7 
       9 
       11 
       13 
       15 
   
  QUESTION
  7 
  Write a program that uses nested for loops to output a diagonal line of
  * as shown below.  The number of stars
  should be provided by the user. 
    
  Sample
  output 1 
    
  How many stars? 
  4 
    
  * 
   * 
    * 
     * 
    
  Sample
  output 2 
    
  How many stars? 
  7 
    
  * 
   * 
    * 
     * 
      * 
       * 
        * 
    
  QUESTION 8 
  Alter your program from Question 7 so that your line is outputted in the
  opposite direction. 
    
  Sample output 
    
  How many stars? 
  6 
    
       * 
      * 
     * 
    * 
   * 
  * 
   
   
    
    
   |