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 function

  create a class

  finish a statement

  calculate a value

 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.

  FALSE

  TRUE

 5. What is the definition of an operand?

  A value that is acted upon by an operator

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

  An arithmetic operator that acts as a value

  An assignment operator that is defined as a function

 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

  addition (ascii values are used) of all numbers

  concatenation of all the strings.

  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.

  30

  32.5

  15

  17.5

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

  2

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

  3

  1

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

  Answer: 3

  Answer: 0

  Answer: 1

  Answer: 2

 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+b

  a+x

  x/y

  1.5+8.6

 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: .0

  cost per item: .1

  cost per item: .5

  cost per item: 6/12

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

  Answer: 0

  Answer: 8

  Answer: 7.0

  Answer: 8.0

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

  a-x

  x ) / (13

  (13-z)

  (a-x)

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

  Answer: 5

  Answer: -2

  Answer: 2

  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 floating point 0.0 to the integer 12 results in an integer 12.

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

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

  Neither. It is actually a string operator

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

  No Associativity

  Left to Right

  Left to Left

  Right to Left

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