Chapter 2: Assignments

Written Assignments:
T/F 1-8

Short Answers

2.1 What is encapsulation and what is the purpose?
2.2 What output is produced by the following code fragment? Explain.

System.out.print (“Test this if you are not sure.”);
System.out.print (“Another.”);
System.out.println ();

2.3 How do you print the following statement with double quotes?
“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 is the output 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 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;

◗ iResult = num1 / num4;
◗ fResult = num1 / num4;
◗ fResult = val1 / val2;
◗ iResult = num1 / num2;
◗ fResult = (double) num1 / num2;
◗ fResult = num1 / (double) num2;
◗ fResult = (double) (num1 / num2
◗ fResult = (int) ((double) num1 / num2); ◗ iResult = num3 % num4;
◗ iResult = num 2 % num3; ◗ iResult = num3 % num2;
◗ iResult = num2 % num4;

2.7 For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator.

◗ a – b + c – d
◗ a / b * c * d
◗ a % b / c *d
◗ a % b % c % d
◗ (a + b) * c+ d * e
◗ (a + b) * (c / d) % e

2.8 What output is produced by the following code fragment?
String m1, m2, m3;
m1 = “Quest for the Holy Grail”;
m2 = m1.toLowerCase();
m3 = m1 + “ “ + m2;
System.out.println (m3.replace(‘h’, ‘z’));

2.9 Write an assignment statement that computes the square root of the sum of num1 and num2 and assigns the result to num3.

2.10 Write a single statement that computes and prints the absolute value of total.

2.11 Assuming that a Random object has been created called generator, what is the range of the result of each of the following expressions?

generator.nextInt(20)
generator.nextInt(8) + 1

2.12 Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Then write a list of expressions using the nextInt method that generates random numbers in the following specified ranges, including the endpoints. Use the version of the nextInt method that accepts a single integer parameter.

◗ 0 to 10 including both numbers

◗ 25 to 50 including both numbers

◗ 1 to 10 including both number

◗ –10 to 15 including both numbers

2.13. List all primitive data types you know.

2.14. What is the difference between a character and a string.

2.15. Do you need to cast when assigning a double to an integer? why or why not?

2.16. Do you need to cast when assigning an integer to a double? why or why not?

Self-review questions at the end of the chapter.

Coming up next week: Chapter 2 Test
10 free responses similar to the review questions
3 String ADT related MC questions – look into the String methods
1 for-loop MC question
1 short program similar to this review question:
Write a main method that prompts the use for two numbers, x and y, and it displays it back the following format: (x,y).