<< Chapter < Page Chapter >> Page >

Not the same as rounding

If each of the values assigned to the variables named w and x in this program were rounded to the nearest integer, the result would be 4 and -4, not 3 and -3as produced by the program.

Back to Question 5

Answer 4

A. Compiler Error

Explanation 4

Cannot cast a boolean type

A boolean type cannot be cast to any other type. This program produces the following compiler error:

Ap042.java:13: inconvertible types found : booleanrequired: int int y = (int)x;

Back to Question 4

Answer 3

D. B

Explanation 3

The logical and operator

The logical and operator shown below

The logical and operator &&

performs an and operation between its two operands, both of which must be of type boolean . If both operands are true, the operator returns true. Otherwise, it returns false.

The boolean negation operator

The boolean negation operator shown below

The boolean negation operator

!

is a unary operator, meaning that it always has only one operand. That operand must be of type boolean , and the operand always appears immediately to the right of the operator.

The behavior of this operator is to change its right operand from true to false, or from false to true.

Evaluation from left to right

Now, consider the following code fragment from this program.

int x = 5, y = 6; if(!(x<y)&&!(y<x/0)){ System.out.println("A");}else{ System.out.println("B");}//end else

The individual operands of the logical and operator are evaluated from left to right.

Consider the left operand of the logical and operator that reads:

!(x<y)

The following expression is true

(x<y)

In this case, x is less than y , so the expression inside the parentheses evaluates to true.

The following expression is false

!(x<y)

The true result becomes the right operand for the boolean negation operator at this point.

You might think of the state of the evaluation process at this point as being something like

not true .

When the ! operator is applied to the true result, the combination of the two become a false result.

Short-circuit evaluation applies

This, in turn, causes the left operand of the logical and operator to be false .

At that point, the final outcome of the logical expression has been determined. It doesn't matter whether the right operand is true or false. Thefinal result will be false regardless.

No attempt is made to evaluate the right operand

Therefore, no attempt is made to evaluate the right operand of the logical and operator in this case.

No attempt is made to divide the integer variable x by zero, no exception is thrown, and the program doesn't terminate abnormally. It runs to completion anddisplays a B on the screen.

Back to Question 3

Answer 2

C. A

Explanation 2

Short-circuit evaluation

Question 1 was intended to set the stage for this question.

This Question, in combination with Question 1 , is intended to help you understand and remember the concept of short-circuit evaluation.

What is short-circuit evaluation?

Logical expressions are evaluated from left to right. That is, the left operand of a logical operator is evaluated before the right operand of the sameoperator is evaluated.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask