compiler expressions sol

8
CmSc150 Fundamentals of Computing I Homework 05 due 02/10 by 5 pm SOLUTIONS 2. Questions and answers True/False (Write T or F immediately after the number of the question) 1. T In Java, the modulus operator (%) requires integer operands. 2. F The statement myVar++; causes 1 to be subtracted from myVar. 3. F In a Java expression, all additions are performed before any subtractions. 4. T Execution of the statement someInt = 3 * (int)someDouble; does not change the contents of the variable someDouble in memory. 5. F In a Java expression without parentheses, all operations are performed in order from left to right. 6. F If someDouble is a variable of type double, the statement someDouble = 395; causes someDouble to contain an integer rather than floating- point value. 7. F The value of the expression a+b * c+d is different from the value of the expression a + b*c + d. 1

Upload: ism33

Post on 30-Jan-2016

217 views

Category:

Documents


0 download

DESCRIPTION

compiler

TRANSCRIPT

Page 1: compiler Expressions Sol

CmSc150 Fundamentals of Computing IHomework 05 due 02/10 by 5 pm SOLUTIONS

2. Questions and answers

True/False (Write T or F immediately after the number of the question)

1. T In Java, the modulus operator (%) requires integer operands.

2. F The statement

myVar++;

causes 1 to be subtracted from myVar.

3. F In a Java expression, all additions are performed before any subtractions.

4. T Execution of the statement

someInt = 3 * (int)someDouble;

does not change the contents of the variable someDouble in memory.

5. F In a Java expression without parentheses, all operations are performed in order from left to right.

6. F If someDouble is a variable of type double, the statement

someDouble = 395;

causes someDouble to contain an integer rather than floating-point value.

7. F The value of the expression a+b * c+d is different from the value of the expression a + b*c + d.

8. F If the int variable someInt contains the value 26, the statement

System.out.println( "someInt");

outputs the value 26.

Fill-In

1

Page 2: compiler Expressions Sol

1. In a Java method, the statements enclosed by a { } pair are known as the body of the method.

2. A programming language is said to be case-sensitive if it considers uppercase letters to be different from lowercase letters.

3. A(n) variable is a location in memory, referenced by an identifier, in which a data value that can be changed is stored.

4. The set of rules that determines the meaning of instructions written in a programming

language is called semantics.

5. A(n) assignment is a statement that stores the value of an expression into a variable.

6. A(n) constant is a location in memory, referenced by an identifier, where a data value that cannot be changed is stored.

7. Syntax is the formal rules governing how valid instructions are written in a programming language.

8. The expression (int)someFloat is an example of a(n) type cast operation.

9. The Java library is a collection of prewritten classes that are available for use by any Java programmer.

Multiple Choice (use boldface or underline to answer)

1. Which of the following statements about the Java main method are false?a. Every program must have a method named main. b. Program execution begins with the first executable statement in the main

method.c. The main method must call (invoke) at least one other method .

Explanation: Applets do not need a main method because they are executed by a browser.

It is not required that a method invokes another method.

2. Which of the following can be assigned to a char variable?a. 't'b. '2'c. '$'d. All of the above

Explanation: a char literal is enclosed in single quotes. a), b) and c) are literals enclosed in single quotes, therefore they can be assigned to a variable of type char.

2

Page 3: compiler Expressions Sol

3. Which assignment statement could be used to store the letter A into the char variable someChar?a. someChar = "A";b. someChar = A;c. someChar = 'A'; d. a and b abovee. a, b, and c above

Explanation: “A” is a string literal and therefore cannot be assigned to a char variable

A must be a name of a variable/constant, and it may contain anything, not necessarily the character ‘A’

‘A’ is a char literal and it can be assigned to a char varaiable.

4. Among the Java operators +, -, *, /, and %, which ones have the lowest precedence?a. + and - b. * and /c. *, /, and %d. +, -, and %e. +, -, and *

5. The value of the Java expression 3 / 4 * 5 is:a. 0.0

b. 0 c. 3.75d. 3e. 0.15

Explanation: the expression 3 / 4 * 5 involves integer division, and the operations are performed from left to right. First 3 / 4 is computed, which results in 0 (integer division), and then 0 is multiplied by 5, so the final result is 0.

6. Assuming all variables are of type float, the Java expression for ( a + b ) c

d+eis:

a. a + b * c / d + eb. (a + b) * c / d + ec. (a + b) * c / (d + e) d. (a + b * c) / d + ee. (a + b) c / (d + e)

Explanation: Expression in (a) will not compute (a + b)c because a + b is not in parentheses. Expression (b) will divide (a+b)c by d and then add e to the result of the division. Expression (d) computes a + bc instead of (a + b)c in the numerator. Expression (e) is not syntactically correct – the operation “multiplication” should be denoted by the character ‘*’

7. Given that x is a double variable and num is an int variable containing the value 5, what will x contain after execution of the following statement:

3

Page 4: compiler Expressions Sol

x = num + 2;

a. 7b. 7.0 c. 5d. 5.0e. nothing; a compile-time error occurs

Explanation: num contains integer 5, which is added to 2, and the result is the integer 7. The variable on the left of the assignments statement is of type double, so before 7 is stored into x, it is transformed to a double value 7.0

8. Given that x is a double variable containing the value 84.7 and num is an int variable, what will num contain after execution of the following statement:

num = x + 2;

a. 86.7b. 86c. 87d. 87.0e. nothing; a compile-time error occurs

Explanation: The expression computed in the right part of the assignment statement is of type double, while the type of the variable on the left side is int. Implicit narrowing conversions are not allowed in Java and so a compile-time error will occur.

9. The value of the Java expression 11 + 22 % 4 is: a. 13

b. 1c. 8d. 16e. none of the above

Explanation: The % operator gives the remainder in integer division. It has higher precedence than the addition. So first the remainder is computed (which is 2) and then it is added to 11. Thus the result is 13.

10. If the int variables int1 and int2 contain the values 4 and 5, respectively, then the value of the expression (double)(int1 / int2) is:

a. 0.8b. 0

c. 0.0 d. 1.0e. 1

4

Page 5: compiler Expressions Sol

Explanation: Since the division is enclosed in parentheses, first the integer division is performed, resulting in 0. Then the integer 0 is transformed to a double 0.0

11. What is the output of the following program fragment?

System.out.println( "Barrel" );System.out.print (" ");System.out.print ( "of");System.out.println( "Laughs" );

a. Barrel of

Laughs

b. Barrelof Laughs

c. Barrel ofLaughs

d. Barrel of Laughs

e. Barrel ofLaughs

Explanation: The answer is not (a) because the first printing statement is “println” meaning that “of” will appear on a new line. It is not (b) because the second printing statement prints a space before “of” and in (b) there is no space before “of”. It is not (c) because the third printing statement is “print” meaning that the next print will appear on the same line as “of”. It is not (d) because the third and the fourth statements do not print a space between “of” and “Laughs”.

12. Categorize each of the following situations as a compile-time error, run-time error, or logical error (check the appropriate box)

Compile-time Run-time Logical

multiplying two numbers when you meant to add them X

dividing by zero X

forgetting a semicolon at the end of a programming statement

X

spelling a word wrong in the output X

producing inaccurate results X

typing a { when you should have typed ( X

Explanation:

Logical errors are:

“multiplying two numbers when you meant to add them”, “spelling a word wrong in the output”, “producing inaccurate results”. This is so because the compiler has no knowledge about English

5

Page 6: compiler Expressions Sol

spelling rules, as well as about the algorithm of the program. The correctness of the algorithm is entirely the programmer’s responsibility.

Dividing by 0 is a run-time error. The compiler cannot know in advance (before the program is executed) what will be the particular value of an expression used as a denominator, so it cannot catch such errors. Moreover, since the results depend on the input, this error will not always occur, only when the input data are such that we have division by 0.

Compile-time errors are:

“forgetting a semicolon at the end of a programming statement” and “typing a { when you should have typed (“.

These errors will be caught by the compiler and reported. The compiler can recognize such errors because it has knowledge about the syntax of Java.

6