Chapter 2 Data and Expressions Flashcards

1
Q

What is a string literal?

A

A string literal is a sequence of characters delimited by double quotes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between the print and println methods?

A

Both the print and println methods of the System.out object write a string of characters to the monitor screen. The difference is that, after printing the characters, the println performs a carriage return so that whatever’s printed next appears on the next line. The print method allows subsequent output to appear on the same line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a parameter?

A

A parameter is data that is passed into a method when it is invoked. The method usually uses that data to accomplish the service that it provides. For example, the parameter to the println method indicates what characters should be printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What output is produced by the following code fragment?

System.out.println(“One “);
System.out.print(“Two “);
System.out.println(“Three “);

A

The output produced by the code fragment is
One

Two Three

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What output is produced by the following code fragment?

System.out.print("Ready ");
System.out.println();
System.out.println("Set ");
System.out.println();
System.out.println("Go ");
A

The output produced by the code fragment is
Ready
Set

Go

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What output is produced by the following statement? What is produced if the inner parentheses are removed?

System.out.println(“It is good to be “ + (5 + 5));

A

The output produced by the statement is
It is good to be 10

The + operator in the sub-expression (5 + 5) represents integer addition, since both of its operands are integers. If the inner parentheses are removed, the + operators represent string concatenation and the
output produced is
It is good to be 55

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an escape sequence? Give some examples.

A

An escape sequence is a series of characters that begins with the backslash () and that implies that the following characters should be treated in some special way. Examples: \n represents the newline character, \t
represents the tab character, and " represents the quotation character (as opposed to using it to terminate a string).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a single println statement that will output the following exactly as shown (including line breaks and quotation marks).

“I made this letter longer than usual because I lack the
time to make it short.”
Blaise Pascal

A

System.out.println(“"I made this letter longer than “
+ “usual because I lack the time to\nmake it short."”
+ “\n\tBlaise Pascal”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a variable declaration?

A

A variable declaration establishes the name of a variable and the type of data that it can contain. A declaration may also have an optional initialization, which gives the variable an initial value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given the following variable declarations, answer each question.

int count = 0, value, total;
final int MAX_VALUE = 100;
int myValue = 50;

a. How many variables are declared?
b. What is the type of these declared variables?
c. Which of the variables are given an initial value?
d. Based on the above declarations, is the following
assignment statement legal? Explain.
myValue = 100;

e. Based on the above declarations, is the following
assignment statement legal? Explain.
MAX_VALUE = 50;

A

Given those variable declarations, the answers are:

a. Five variables are declared: count, value, total,
MAX_VALUE, and myValue.
b. They are all of type int.
c. count, MAX_VALUE, and myValue are each given an
initial value.
d. Yes, it is legal. myValue is a variable of type int and 100
is an int literal.
e. No, it is not legal. MAX_VALUE is declared as a final
variable and therefore it cannot be assigned a value
other than its initial value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Your program needs a variable of type int to hold the number of CDs in a music collection. The initial value should be zero. Write a declaration statement for the variable

A

The variable name you choose should reflect the purpose of the variable. For example:
int numCDs = 0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Your program needs a variable of type int to hold the number of feet in a mile (5,280). Write a declaration statement for the variable.

A

The variable name you choose should reflect the purpose of the variable. Since the number of feet in a mile will not change, it is a good idea to declare a constant. For example:
final int FT_PER_MILE = 5280;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Briefly describe three reasons for using a constant in a program instead of a literal value.

A

First, by carefully choosing the name of the constant, you can make your program more understandable than if you just use the literal value. Second, using a constant ensures that the literal value represented by the variable will not be inadvertently changed somewhere in the program. Third, if you ever do have to rewrite the program using a different literal
value, you will only need to change that value once, as the initial value of the constant, rather than many places throughout the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is primitive data? How are primitive data types different from objects?

A

Primitive data are basic values such as numbers or characters. Objects are more complex entities that usually contain primitive data that help define them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How many values can be stored in an integer variable?

A

An integer variable can store only one value at a time. When a new value is assigned to it, the old one is overwritten and lost

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the four integer data types in Java? How are they different?

A

The four integer data types in Java are byte, short, int, and long. They differ in how much memory space is allocated for each and therefore how large a number they can hold.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What type does Java automatically assign to an integer literal? How can you indicate that an integer literal should be considered a different type?

A

Java automatically assigns an integer literal the data type int. If you append an L or an l on the end of an integer literal, for example 1234L, Java will assign it the type long

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What type does Java automatically assign to a floating point literal?
How can you indicate that a floating point literal should be considered a different type?

A

Java automatically assigns a floating point literal the data type double. If you append an F or an f on the end of a floating point literal, for example 12.34f, Java will assign it the type float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a character set?

A

A character set is a list of characters in a particular order. A character set defines the valid characters that a particular type of computer or programming language will support. Java uses the Unicode character set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How many characters are supported by the ASCII character set, the extended ASCII character set, and the Unicode character set?

A

The original ASCII character set supports 2^7 = 128 characters, the extended ASCII character set supports 2^8 = 256 characters, and the UNICODE character set supports 2^16 = 65,536 characters

21
Q

What is the result of 19%5 when evaluated in a Java expression? Explain.

A

The result of 19%5 in a Java expression is 4. The remainder operator % returns the remainder after dividing the second operand into the first.
The remainder when dividing 19 by 5 is 4

22
Q

What is the result of 13/4 when evaluated in a Java expression? Explain.

A

The result of 13/4 in a Java expression is 3 (not 3.25). The result is an integer because both operands are integers. Therefore, the / operator performs integer division, and the fractional part of the result is truncated

23
Q

If an integer variable diameter currently holds the value 5, what is its value after the following statement is executed? Explain.

diameter = diameter * 4;

A

After executing the statement, diameter holds the value 20. First, the current value of diameter (5) is multiplied by 4, and then the result is stored back in diameter.

24
Q

What is operator precedence?

A

Operator precedence is the set of rules that dictates the order in which operators are evaluated in an expression.

25
Q

What is the value of each of the following expressions?

a. 15 + 7 * 3
b. (15 + 7) * 3
c. 3 * 6 + 10 / 5 + 5
d. 27 % 5 + 7 % 3
e. 100 / 2 / 2 / 2
f. 100 / ( 2 / 2) / 2

A

The evaluations of the expressions are

a. 15 + 7 * 3 = 15 + 21 = 36
b. (15 + 7) * 3 = 22 * 3 = 66
c. 3 * 6 + 10 / 5 + 5 = 18 + 2 + 5 = 25
d. 27 % 5 + 7 % 3 = 2 + 1 = 3
e. 100 / 2 / 2 / 2 = 50 / 2 / 2 = 25 / 2 = 12
f. 100 / ( 2 / 2) / 2 = 100 / 1 / 2 = 100 / 2 = 50

26
Q

For each of the following expressions state whether they are valid or invalid. If invalid, explain why.

a. result = ( 5 + 2 );
b. result = ( 5 + 2 * ( 15 - 3 );
c. result = ( 5 + 2 (;
d. result = ( 5 + 2 ( 4 ) );

A

Expression a is valid. Expression b is invalid because there are two open parentheses but only one close parenthesis. Similarly with expression c, where there are two open parentheses but no close parenthesis.
Expression d might be a valid algebraic expression in an algebra book, but it is not a valid expression in Java. There is no operator between the operands 2 and ( 4 ).

27
Q

What value is contained in the integer variable result after the following sequence of statements is executed?

result = 27;
result = result + 3;
result = result / 7;
result = result * 2;
A

After the sequence of statements, the value in result is 8

28
Q

What value is contained in the integer variable result after the following sequence of statements is executed?

int base;
int result;
base = 5;
result = base + 3;
base = 7;
A

After the sequence of statements, the value in result is 8. Note that even though result was set to base + 3, changing the value of base to 7 does not retroactively change the value of result.

29
Q

What is an assignment operator?

A

An assignment operator combines an operation with assignment. For example, the += operator performs an addition, then stores the value back into the variable on the left-hand side.

30
Q

If an integer variable weight currently holds the value 100, what is its value after the following statement is executed? Explain.

weight -= 17;

A

After executing the statement, weight holds the value 83. The assignment operator −= modifies weight by first subtracting 17 from the current value (100), then storing the result back into weight.

31
Q

Why are widening conversions safer than narrowing conversions?

A

A widening conversion tends to go from a small data value, in terms of the amount of space used to store it, to a larger one. A narrowing conversion does the opposite. Information is more likely to be lost in a
narrowing conversion, which is why narrowing conversions are considered to be less safe than widening ones

32
Q

Identify each of the following conversions as either a widening conversion or a narrowing conversion.

a. int to long
b. int to byte
c. byte to short
d. byte to char
e. short to double

A

The conversions are:

a. widening,
b. narrowing,
c. widening,
d. widening,
e. widening.

33
Q

Assuming result is a float variable and value is an int variable, what type of variable will value be after the following assignment statement is executed? Explain.

result = value;

A

During the execution of the statement, the value stored in value is read and transformed into a float as it is being copied into the memory location represented by result. But the value variable itself is not changed, so value will remain an int variable after the assignment
statement

34
Q

Assuming result is a float variable that contains the value 27.32 and value is an int variable that contains the value 15, what are the values of each of the variables after the following assignment statement is
executed? Explain.

value = (int) result;

A

During the execution of the statement, the value stored in result is read and then transformed into an int as it is being copied into the memory location represented by value. But the result variable itself is not changed, so it remains equal to 27.32, whereas value becomes 27

35
Q

Given the following declarations, what result is stored by each of the following assignment statements.

int iResult, num1 = 17, num2 = 5;
double fResult, val1 = 12.0, val2 = 2.34;

a. iResult = num1 / num2;
b. fResult = num1 / num2;
c. fResult = val1 / num2;
d. fResult = (double) num1 / num2;
e. iResult = (int) val1 / num2;

A

The results stored are
a. 3 integer division is used since both operands are
integers.
b. 3.0 integer division is used since both operands are
integers,but then assignment conversion converts
the result of 3 to 3.0.
c. 2.4 floating point division is used since one of the
operands is a floating point.
d. 3.4 num1 is first cast as a double; therefore, floating
point division is used since one of the operands is a
floating point.
e. 2 val1 is first cast as an int; therefore, integer division
is used since both operands are integers.

36
Q

Identify which line of the GasMileage program does each of the following.
a. Tells the program that we will be using the Scanner class.
b. Creates a Scanner object.
c. Sets up the Scanner object scan to read from the standard input
stream.
d. Reads an integer from the standard input stream.

A

The corresponding lines of the GasMileage program are

a. import java.util.Scanner;
b. Scanner scan = new Scanner(System.in);
c. Scanner scan = new Scanner(System.in);
d. miles = scan.nextInt();

37
Q

Assume you already have instantiated a Scanner object named myScanner and an int variable named value as follows in your program:

Scanner myScanner = new Scanner(System.in);
int value = 0;

Write program statements that will ask the user to enter their age, and store their response in value.

A

Under the stated assumptions, the following code will ask users to enter their age and store their response in value.

System.out.print(“Enter your age in years: “);
value = myScanner.nextInt();

38
Q

How can a black and white picture be represented using 1s and 0s?

A

A black and white picture can be drawn using a series of dots, called pixels. Pixels that correspond to a value of 0 are displayed in white, and pixels that correspond to a value of 1 are displayed in black. By using thousands of pixels, a realistic black and white photo can be produced on a computer screen.

39
Q

Three corners of a rectangle drawn using the Java coordinate system have coordinates (3, 4), (3, 7), and (8, 7). What are the coordinates of the fourth corner?

A

The coordinates of the fourth corner are (3, 7), which is the top-right corner.

40
Q

Two corners of a square drawn using the Java coordinate system have coordinates (5, 10) and (8, 10). What are the coordinates of the other two corners?

A

We can tell from the given information that the side of the square has length 3. Therefore, the other two corners are (5, 13) and (8, 13).

41
Q

How many bits are needed to store a color picture that is 300 pixels wide and 200 pixels high? Assume color is represented using the RGB technique described in this chapter and that no special compression technique is used.

A

Eight bits per number, three numbers per pixel, and 300 by 200 pixels gives

8 * 3 * 300 * 200 = 1,440,000 bits

42
Q

What is the difference between a Java application and a Java applet?

A

A Java applet is a Java program that can be executed using a Web browser. Usually, the bytecode form of the Java applet is pulled across the Internet from another computer and executed locally. A Java application is a Java program that can stand on its own. It does not require a Web browser in order to execute.

43
Q

When is an applet’s paint method invoked?

A

An applet’s paint method is invoked by the system whenever the applet’s graphic element needs to be displayed (or “painted”) on the screen. Examples include when the applet first runs or when a window
that was covering the applet’s window is removed.

44
Q

What is wrong with the following HTML applet tag? Explain.

A

The code tag should indicate a bytecode file, such as DrawHouse.class, and not a source code file. The file indicated by the tag is supposed to be “ready to run” on a Java interpreter.

45
Q

What is a bounding rectangle?

A

A bounding rectangle is an imaginary rectangle that surrounds a curved shape, such as an oval, in order to define the shape’s width, height, and upper left corner.

46
Q

Assuming you have a Graphics object called page, write a statement that will draw a square with a side length of 50, such that its upper-left corner is at point (16, 12).

A

page.drawRect(16, 12, 50, 50);

47
Q

Assuming you have a Graphics object called page, write a sequence of statements that will draw a blue rectangle with a height of 20 and a width of 40, such that its upper-left corner is at point (30, 35).

A

page. setColor(Color.blue);

page. fillRect(30, 35, 40, 20);

48
Q

What would be the result of making each of the following changes separately to the Snowman program? You may make the change, compile and run the program, and observe and report the results. Briefly
explain what you observe.

a. The value of MID is set to 120 instead of 150.
b. The value of TOP is set to 25 instead of 50.
c. Just before the last two statements of the program
(the statements that draw the hat), we include the
statement

page.setColor(Color.blue);

d. In the statement that creates the smile, the 190 is
changed to a 10.
e. Just before the statement that creates the upper
torso, the foreground color is set to cyan. It is set back
to white immediately after the upper torso is created.

A

The results of the changes are

a. Since the value of MID is added to all the horizontal components of
the snowman figure, the snowman shifts a little bit to the left.
b. Since the value of TOP is added to all the vertical components of
the snowman figure, the snowman shifts upwards a little bit.
c. The hat is now blue
d. By changing the start angle of the smile arc to 10, the starting point
is now on the right side of the snowman’s face instead of the left. The
“direction” of the arc is still counterclockwise. Thus, the arc curves
downward instead of upward and the happy snowman is now sad.
e. The upper torso “disappears” since it merges with the background.