Self-Review Chapter 2
2.1 What are primitive data? How are primitive data types different from objects?
2.2 What is a string literal?
2.3 What is the difference between the print method and the println method?
2.4 What is a parameter?
2.5 What is an escape sequence? Give some Examples.
2.6 What is a variable declaration?
2.7 How many values can be stored in an integer variable at one time?
2.8 What are the four integer data types in Java? How are they different?
2.9 What is a character set?
2.10 What is operator precedence?
2.11 What is the result of 19%5 when evaluated in a Java expression? Explain.
2.12What is the result of 13/4 when evaluated in a Java expression? Explain.
2.13 If an integer variable diameter currently holds the value 5, what is its value after the following statement is executed? Explain.
diameter = diameter *4;
2.14 If an integer variable weight currently holds the value 100, what is its value after the following statement is executed? Explain.
weight -= 17;
2.15 Why are widening conversions safer than narrowing conversions?
2.1 Explain the following programming statement in terms of objects and the services they provide.
System.out.println (“I gotta be me!”) ;
2.2 What output is produced by the following code fragment? Explain.
System.out.print (“Here we go! ”) ;
System.out.println (“12345”) ;
System.out.print (“Test this if you are not sure. ”) ;
System.out.print (“Another.”) ;
System.out.println () ;
System.out.println (“All done.”) ;
2.3 What is wrong with the following program statement? How can it be fixed?
System.out.println (“To be or not to be, that is the question.”) ;
2.4 What output is produced by the following statement? Explain.
System.out.println (“50 plus 25 is “ + 50 + 25) ;
2.5 What output is produced by the following statement? Explain.
System.out.println (“He thrusts his fists\n\tagainst” +
“ the post\nand still insists\n\the sees the \”ghost\””) ;
2.6 What value is contained in the integer variable size after the following statements are executed?
size = 18 ;
size = size + 12 ;
size = size * 2 ;
size = size / 4 ;
2.7 What value is contained in the floating point variable depth after the following statements are executed?
depth = 2.4 ;
depth = 20 – depth * 4 ;
depth = depth / 5 ;
2.8 What value is contained in the integer variable length after the following statements are executed?
length = 5 ;
length *= 2 ;
length *= length ;
length /= 100 ;
2.9 Write four different program statements that increment the value of an integer variable total.
2.10 Given the following declarations, what result is stored in each of the listed assignment statements?
int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
double fResult, val1 = 17.0, val2 = 12.78;
a.) iResult = num1 / num4;
b.) fResult = num1 / num4;
c.) iResult = num3 / num4;
d.) fResult = num3 / num4;
e.) fResult = val1 / num4;
f.) fResult = val1 / val2;
g.) iResult = num1 / num2;
h.) fResult = (double) num1 /num2;
i.) fResult = num1 / (double) num2;
j.) fResult = (double) (num1 / num2);
k.) iResult = (int) (val1 / num4);
l.) fResult = (int) (val1 / num4);
m.) fResult = (int) ((double) num1 / num2);
n.) iResult = num3 % num4;
o.) iResult = num2 % num3;
p.) iResult = num3 % num2;
q.) iResult = num2 % num4;
2.11 For each of the following expressions indicate the order in which the operators will be evaluated by writing a number beneath each operator.
a.) a – b – c – d
b.) a – b + c – d
c.) a + b / c / d
d.) a + b / c * d
e.) a / b * c * d
f.) a % b / c * d
g.) a % b % c % d
h.) a – (b – c) – d
I.) (a – (b – c)) – d
j.) a – ((b – c) – d)
k.) a % (b % c) * d * e
l.) a + (b – c) * d – e
m. (a + B ) * c + d * e
n. (a + B ) * (c / d) % e