Review for Java test

1          Java has rules for the identifiers programmers can use.  Identifiers are names for variables, methods and classes. They must start with a ______ and can include ___________, _____________ and __________. ( no blank spaces or special symbols) The name of a class should start with a _______________ and variables and methods start with ___________________ .

2          In Java, a large programming task can be broken into large pieces which are classes.  The classes themselves are normally separated into still smaller pieces.  What are these smaller pieces called and how should their names be chosen? ___________________________________________________

3.          Assume x has been declared as an int.  What number will be printed after each section of code is executed?
x = 7.9; System.out.println ( (int) x );       ______________
x = 7.1; System.out.println ( (int) x );       ______________

4          Evaluate each Java expression below.

  1. 3  +  4 / 2                        ______________
  2. 3  +  4 / 2.0                     ______________
  3. ( 3  +  4 ) / 2                    ______________
  4. (int) 3  +  4 / 2.0              ______________
  5. (int) ( 3  +  4 )/ 2.0       ______________
  6. (int) ( 3  +  4 / 2.0 )   ______________
  7.  3  /  2.0                          ______________
  8.  15  /  2                           ______________
  9. 12   /    2    *   3             ______________
  10. 12  +  2  /   7                  ______________

5          How does the Java execution of the System.out.print(message) method differ from the way it executes the System.out.println(message) method?

6             The code segment below is intended to print " Passing score" only when the value of the variable “testScore” is at least 70.  Assume the variable “testScore” has been declared and initialized properly as an int. 
if (testScore >= 70) {
     System.out.println(" Passing score" );
}

  1. Modify the code to produce a syntax error.
    if (testScore >= 70) {
         System.out.println(" Passing score" );
    }
  1. Modify the code to produce a logic error.
    if (testScore >= 70) {
         System.out.println(" Passing score" );
    }
  1. Modify the code to produce a run time error.
    if (testScore >= 70) {
         System.out.println(" Passing score" );
    }

7          Given variables x and y in the logical expressions below have been declared and initialized properly describe the value(s) of these variable(s) that make each expression evaluate as true?

    1. x < 5 && y > 9    ________________________________________________________
    2. x < 5 || y > 9    ____________________________________________________
    3. x >= 5 && y < 9    ___________________________________________________
    4. x > 5 || x <= 9      _____________________________________________________
    5. 9 > 5 || x <  9   ____________________________________________________
    6. 5 > 5 && x <  9    ________________________________________________________

8          Complete each empty cell of the Java operator table below.

Operator

Description

Relational Operators

= =

 
 

not equal to

> 

 
 

greater than or equal to

< 

 
 

less than or equal to

Logical Operators

 

and

||

 

!

 

Simple Assignment Operator

 

assigns a value to a variable

Compound Assignment Operators

 

Add and assign the result

-=

 

*=

 

 

Divide and assign the result


9          Given the code below answer the following questions.

public class IfElseDemo { 
 public static void main(String[] args) { >
   int testScore = 79; 
   char grade; 
   if (testScore >= 92) { 
   	grade  = 'A'; 
   } else if (testScore >= 84) { 
   	grade  = 'B'; 
   } else if (testScore >= 76) { 
   	grade  = 'C'; 
   } else if (testScore >= 68) { 
   	grade  = 'D'; 
   } else { 
   	grade  = 'F'; 
   } 
   System.out.println("Grade = " + grade); 
   } 
}
  1. What is the lowest value the variable testScore can be assigned and still print a 'C' as a grade? _________
  2. What is the highest value that can be assigned to the variable testScore and print B as a grade? __________
  3. What is the highest value that can be assigned to the variable testScore and print F as a grade? __________

10 Circle the expressions that are not relational expressions. For these expressions, identify the type of the expression or statement.(assignment statement, math expression or error)

  1. x = = 3         ______________________________________
  2. x = 3     ______________________________________
  3. x >= 3    ______________________________________
  4. x * 3            ______________________________________
  5. 3 < x            ______________________________________
  6. x - 3 <= 10______________________________________

11. Given the following code what is the last value of “i” to be printed? _________

      int stop =  25;
      int i;
      for (i = 0; i  < stop; i++){
            System.out.println(  "The value of i: " + i );
      }
12.          Based on the following program segment describe what values entered for the int amount will cause the body of the loop to be executed? _____________________________________
   Scanner sc = new  Scanner(); 
   int amount =  sc.nextInt();
   
   while ( amount >= 50 || amount < 60   ) {
         System.out.print( "Number  is out of range.”);
         amount = sc.nextInt(  );
   }

13          Based on the above program segment describe what values entered for the int amount will cause the body of the loop to be exited? ____________________________________________

14          Rewrite the conditional expression in the previous problem so the body of the loop is entered only when the amount entered is at least 12 but less than 22.________________________________________

 

2 things we have not covered yet: Strings and arrays

15  Given the String str is is at least 10 characters long (it may longer).  Write a Java expression that returns the following about the String str.

  1. Its length                  _______________________________________
  2. A string with only its last character  _______________________________________
  3. A string with only its first three characters   _______________________________________
  4. A string with only the last 4 characters _______________________________________
  5. A string with all but the first character in str _______________________________________
  6. The index of the letter q in str or -1 if 'q' is not there _______________________________________
16          An array of doubles with at least 12 values (it may have many more) named fishWeights is fully populated.  That is each location in the array has been assigned a double.  Write and expression that returns:

17          Continuing from the last problem .  A method named sumFish receives an array of doubles as a parameter and returns the sum of the array’s elements.  Complete the method body below to return the total of all the fish it receives.  Your method may not use any identifiers declared outside the method and its header.
public double sumFish(double [ ] fish){

 

 

}

18          Java has two operators that are used to accumulate totals.  What are they?          


19          The following code is intended to print  the number of times the value of min changes as an array is searched for its  minimum value.
   int [ ] miles = {250, 350, 150,  100, 325, 400, 290};
   int min = Integer.MAX_VALUE;
   int i = 0;
   while (i <  miles.length){
     int  count = 0;
     if  ( miles[ i ] < min ){
       min  = miles[ i ];
       count++;
     }
     i++;
   }
   System.out.println(  "Changes:  " + count) ;

When compiled Java returns an error message, “count cannot be resolved” even though count is declared and initialized in the code.  What is the source of the error and how can it be fixed?