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.
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" );
}
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?
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); } }
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)
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.
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?