Skip to main content

Section 1.12 Expressions-WE4-P2

Subsection 1.12.1 Subgoals for evaluating an assignment statement

  1. Determine resultant data type of expression

  2. Update variable for pre-increment or pre-decrement operators (side effect)

  3. Evaluate arithmetic expression according to operator precedence

  4. If an assignment statement (=), is Left Hand Side (LHS) a variable? Check data type of value against data type of variable.

  5. Update variable for post-increment or post-decrement operators (side effect)

For the assignment statements below, either give the new value of the assigned variable, or explain why the statement is invalid. Each problem is independent of the others (e.g. Question 2 does not depend on Question 1, only the “Given” code). If you must round a numeric answer, use 4 decimal places of precision.

Subsection 1.12.2 Expressions-WE4-P2

Given

int alpha = 2, beta = 1, delta = 3, eta, gamma;
double omega = 2.5, theta = -1.3, kappa = 3.0, lambda, rho;

Exercises Exercises

1.

Q27: lambda = alpha / kappa + delta;

2.

    Q28: eta = alpha * beta + (omega – theta) * kappa;

    Why is this statement invalid?

  • cannot assign a double to an int variable

  • eta does not have a value

  • eta is a constant

  • cannot assign an int to a double variable

Given

int r = 8;
double volume;
3.

    Q29: Which statement will calculate the volume of a sphere most accurately?

  • volume = 4 / 3 * 3.14 * r * r * r;

  • volume = 4 / 3 * 3.14159 * r * r * r;

  • volume = 4.0 / 3 * 3.14 * r * r * r;

  • An accurate result is not possible with these data types.

You have attempted of activities on this page.