Preview

05 - Expression and Assignment Data

 1. The following expression is a valid expression in Java. It will evaluate to 55
11 * 5  - +

  TRUE

  FALSE

 2. An expression is a combination of literals, operators, variable names, and parentheses used to _____________.

  create a class

  calculate a value

  finish a statement

  create a function

 3. Expressions contain operators and ___________. You already know what an operator is (a symbol such as +, -, *, or / that calls for an arithmetic operation).

 4. In the following expression: >> 13 - 5 the 13 and the 5 are the operators and the - is the operand.

  TRUE

  FALSE

 5. What is the definition of an operand?

  A value that is acted upon by an operator

  An arithmetic operator that acts as a value

  An assignment operator that is defined as a function

  A value that is protected from being acted upon in any way

 6. The following expression is correct/vald.
A - b/c + D

  TRUE

  FALSE

 7. Which answer best describes the following expression.
((m-n) + (w-x-z) / (p % q)

  This expressio n is incorrect. The parentheses are not balanced

  This expression is correct, but it is complex

  This expression is incorrect. The operators are in the wrong order

  This expression is incorrect. Java does not allow two parentheses in one expression

 8. The below expression creates a new string that is the _________________________.
"This is" + " a string" + " expression" 

  example given in Java of a non-expression

  concatenation of all the strings.

  addition (ascii values are used) of all numbers

  classic example of an expression that creates an error

 9. What is the output of the Java code snippet?
int a=10, b=5, c=3;
int d = a+c/2*b;
System.out.println(d);

Note:
======
/ and * have equal priority. 
So associativity of Left to Right is used. 
Remember that 3/2 is 1 not 1.5 as both
operands are integers.

  32.5

  30

  17.5

  15

 10. What is the output of Java code snippet below?
short p = 1;
short k = p + 2;
System.out.println(k);

  Compiler Error - an int value cannot be assigned to a short variable.

  1

  3

  2

 11. What is the result of evaluating the following expression?
1/2 + 1/2  =

  Answer: 3

  Answer: 2

  Answer: 1

  Answer: 0

 12. Assume the following declarations. (see below). Which of the following expressions will NOT be evaluated using double precision floating point?
short  a=12 ;
int    b=33 ;

float  x = 2.3;
double y = 3.14;

  a+x

  1.5+8.6

  a+b

  x/y

 13. What does the following program output?
class question4
{
  public static void main ( String[] args )
  {
    int totalCost = 6;    
    int items     = 12;
    System.out.println("cost per item: " + totalCost/items );
  }
}

  cost per item: 6/12

  cost per item: .0

  cost per item: .1

  cost per item: .5

 14. What is the result of evaluating the following expression?
( 1/2 + 3.5) * 2.0

  Answer: 7.0

  Answer: 0

  Answer: 8.0

  Answer: 8

 15. Given the following expression, which of the following is NOT a sub-expression?
(a - x)/(13 - z)

  a-x

  (a-x)

  x ) / (13

  (13-z)

 16. What is the value of -32 % 6?

  Answer: 2

  Answer: -2

  Answer: 5

  Answer: -5

 17. Assuming the numerical value is correct, the following declaration works for the constant pi.
final double pi = 3.14159265358979

  FALSE

  TRUE

 18. What type (integer or floating point) of operator is the / in the following:
(12 + 0.0) / 7

  Integer. Adding integer to floating point results in an integer.

  Neither. It is actually a string operator

  Floating point. Adding floating point 0.0 to the integer 12 results in a floating point 12.0

  Integer. Adding floating point 0.0 to the integer 12 results in an integer 12.

 19. Arithmetic operators +, -, /, * and % have which Associativity?

  Right to Left

  Left to Left

  Left to Right

  No Associativity

 20. If X is odd, what is X % 2?