algorithm 28
assignment operator ( = ) 32
assignment statement 32
backslash ( \ ) 43
byte type 34
casting 40
char type 41
constant 33
data type 28
debugger 58
debugging 58
declaration 31
decrement operator ( “ “ ) 39
double type 34
encoding 42
final 33
float type 34
floating-point number 28
expression 32
expression statement 40
identifier 30
increment operator ( ++ ) 39
incremental development and testing 30
indentation 56
int type 34
literal 36
logic error 58
long type 34
narrowing (of types) 40
operator 32
primitive data type 28
runtime error 57
short type 34
syntax error 57
supplementary Unicode 42
Unicode 42
Unix
epoch 51variable 29
widening (of types) 54
whitespace 54
Java provides four integer types (
byte
,
short
,
int
,
long
) that represent integers of four different sizes, and two floating-point types (
float
,
double
) that represent floating-point
Java provides operators that perform numeric operations: + (addition), “ (subtraction), * (multiplication), / (division), and % (remainder). Integer division ( / ) yields an integer result. The remainder operator ( % ) yields the remainder of the division.
The increment operator ( ++ ) and the decrement operator ( “ “ ) increment or decrement a variable by 1 . If the operator is prefixed to the variable, the variable is first incremented or decremented by 1 , then used in the expression. If the operator is a suffix to the variable, the variable is incremented or decremented by 1 , but then the original old value is used in the expression.
All the numeric operators can be applied to
You can use casting to convert a value of one type into another type. Casting a variable of a type with a small range to a variable of a type with a larger range is known as widening a type . Casting a variable of a type with a large range to a variable of a type with a smaller range is known as narrowing a type . Widening a type can be performed automatically without explicit casting. Narrowing a type must be performed explicitly.
Programming errors can be categorized into three types: syntax errors, runtime errors, and logic errors. Errors that occur during compilation are called syntax errors or compilation errors. Runtime errors are errors that cause a program to terminate abnormally. Logic errors occur when a program does not perform the way it was intended to.
| 2.1 |
Which of the following identifiers are valid?
applet , Applet , a++ , a , 4#R , $4 , #44 , apps |
| 2.2 |
Which of the following are Java keywords?
class , public , int , x , y , radius |
| 2.3 |
Translate the following pseudocode into Java code:
What is kilometer after Step 4? |
| 2.4 | What are the benefits of using constants? Declare an int constant SIZE with value 20 . |
| 2.5 |
Assume that
int a = 1
and
double d = 1.0
, and that each expression is independent. What are the results of the following expressions?
a = 46 / 9 ; a = 46 % 9 + 4 * 4 - 2 ; a = 45 + 43 % 5 * ( 23 * 3 % 2 ); a %= 3 / a + 3 ; d = 4 + d * d + 4 ; d += 1.5 * 3 + (++a); d -= 1.5 * 3 + a++; |
| 2.6 |
Show the result of the following remainders.
56 % 6 78 % -4 -34 % 5 -34 % -5 5 % 1 1 % 5 |
| 2.7 | Find the largest and smallest byte , short , int , long , float , and double . Which of these data types requires the least amount of memory? |
| 2.8 | What is the result of 25 / 4 ? How would you rewrite the expression if you wished the result to be a floating-point number? |
| 2.9 |
Are the following statements correct? If so, show the output.
System.out.println( "the output for 25 / 4 is " + 25 / 4 ); System.out.println( "the output for 25 / 4.0 is " + 25 / 4.0 ); |
| 2.10 |
How would you write the following arithmetic expression in Java?
|
| 2.11 |
Which of these statements are true?
|
| 2.12 |
Which of the following are correct literals for floating-point
12.3 , 12.3e+2 , 23.4e2 , -334.4 , 20 , 39F , 40D |
| 2.13 |
Identify and fix the errors in the following code:
1 public class Test { 2 public void main(string[] args) { 3 int i; 4 int k = 100.0 ; 5 int j = i + 1 ; 6 7 System.out.println( "j is " + j + " and 8 k is " + k); 9 } 10 } |
| 2.14 | Can different types of numeric values be used together in a computation? |
| 2.15 |
What does an explicit conversion from a
double
to an
int
do with the
|
| 2.16 |
Show the following output:
float f = 12.5F ; int i = ( int )f; System.out.println( "f is " + f); System.out.println( "i is " + i); |
| 2.17 | Use print statements to find out the ASCII code for '1' , 'A' , 'B' , 'a' , 'b' . Use print statements to find out the character for the decimal code 40 , 59 , 79 , 85 , 90 . Use print statements to find out the character for the hexadecimal code 40 , 5A , 71 , 72 , 7A . |
| 2.18 |
Which of the following are correct literals for
'1' , '\u345dE' , '\u3fFa' , '\b' , \t |
| 2.19 | How do you display characters \ and " ? |
| 2.20 |
Evaluate the following:
int i = '1' ; int j = '1' + '2' ; int k = 'a' ; char c = 90 ; |
| 2.21 |
Can the following conversions involving casting be allowed? If so, find the converted result.
char c = 'A' ; i = ( int )c; float f = 1000.34f ; int i = ( int )f; double d = 1000.34 ; int i = ( int )d; int i = 97 ; char c = ( char )i; |
| 2.22 |
Show the output of the following statements:
System.out.println( "1" + 1 ); System.out.println( '1' + 1 ); System.out.println( "1" + 1 + 1 ); System.out.println( "1" + ( 1 + 1 )); System.out.println( '1' + 1 + 1 ); |
| 2.23 |
Evaluate the following expressions:
1 + "Welcome " + 1 + 1 1 + "Welcome " + ( 1 + 1 ) 1 + "Welcome " + ( '\u0001' + 1 ) 1 + "Welcome " + 'a' + 1 |
| 2.24 | How do you convert a decimal string into a double value? How do you convert an integer string into an int value? |
| 2.25 | How do you obtain the current minute using the System.currentTimeMillis() method? |
| 2.26 | How do you denote a comment line and a comment paragraph? |
| 2.27 |
What are the naming conventions for class names, method
MAX_VALUE, Test, read, readInt
|
|
|
|
| 2.28 |
Reformat the following program according to the programming style and documentation guidelines. Use the
public class Test { // Main method public static void main(String[] args) { /** Print a line */ System.out.println( "2 % 3 = " + 2 % 3 ); } } |
| 2.29 | Describe syntax errors, runtime errors, and logic errors. |