Summary: This module contains review questions and answers keyed to the module titled Jb0210: Java OOP: Operators.
This module contains review questions and answers keyed to the module titled Jb0210: Java OOP: Operators .
The questions and the answers are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back again.
An operator performs an action on what? Provide the name.
What do we call an operator that operates on only one operand?
What do we call an operator that operates on two operands?
Is the minus sign a unary or a binary operator, or both? Explain your answer.
Describe operator overloading.
True or false? Java programmers may overload operators.
Show the symbols used for the following operators in Java: assignment , not equal , addition , cast .
Are any operators automatically overloaded in Java? If so, identify one and describe its overloaded behavior.
What is the purpose of the cast operator?
True or false? The increment operator is a binary operator.
Show the symbol for the increment operator.
Describe the appearance and the behavior of the increment operator with both prefix and postfix notation. Show example, possibly incomplete, code fragments illustrating both notational forms.
Show the output that would be produced by the Java application in Listing 1 .
Listing 1: Listing for Question 13. 


True or false? Binary operators use outfix notation. If your answer is False, explain why.
In practice, what does it mean to say that an operator that has performed an action returns a value (or evaluates to a value) of a given type?
Show and describe at least five of the binary arithmetic operators supported by Java (Clarification: binary operators does not mean bitwise operators).
In addition to arithmetic addition, what is another use for the plus operator (+) ? Show an example code fragment to illustrate your answer. The code fragment need not be a complete statement.
When the plus operator (+) is used as a concatenation operator, what is the nature of its behavior if its left operand is of type String and its right operand is not of type String ? If the right operand is a variable that is not of type String , what is the impact of this behavior on that variable.
Show and describe four unary arithmetic operators supported by Java.
What is the type returned by relational operators in Java?
Show and describe six different relational operators supported by Java.
Show the output that would be produced by the Java application shown in Listing 2 .
Listing 2: Listing for Question 22. 


Show and describe three operators (frequently referred to as conditional or logical operators) that are often combined with relational operators to construct more complex expressions (often called conditional expressions) . Hint: The  operator returns true if either the left operand, the right operand, or both operands are true. What are the other two and how do they behave?
Describe the special behavior of the  operator in the following expression for the case where the value of the variable a is less than the value of the variable b .
(a < b)  (c < d)
Show the symbols used for the bitwise and operator and the bitwise inclusive or operator.
Show and describe seven operators in Java that perform actions on the operands one bit at a time (bitwise operators) .
True or false? In Java, the signed right shift operation populates the vacated bits with the zeros, while the left shift and the unsigned right shift populate the vacated bits with the sign bit. If your answer is False, explain why.
True or false? In a signed rightshift operation in Java, the bits shifted off the right end are lost. If your answer is False, explain why.
Using the symbols 1 and 0, construct a truth table showing the four possible combinations of 1 and 0. Using a 1 or a 0, show the result of the bitwise and operation on these four combinations of 1 and 0.
Using the symbols 1 and 0 construct a truth table showing the four possible combinations of 1 and 0. Using a 1 or a 0, show the result of the bitwise inclusive or operation on these four combinations of 1 and 0.
Using the symbols 1 and 0 construct a truth table showing the four possible combinations of 1 and 0. Using a 1 or a 0, show the result of the bitwise exclusive or operation on these four combinations of 1 and 0.
True or false? For the exclusive or , if the two bits are different, the result is a 1. If the two bits are the same, the result is a 0. If your answer is False, explain why.
Is the assignment operator a unary operator or a binary operator. Select one or the other.
True or false? In Java, when using the assignment operator, the value stored in memory and represented by the right operand is copied into the memory represented by the left operand. If your answer is False, explain why.
Show two of the shortcut assignment operators and explain how they behave by comparing them with the regular (nonshortcut) versions. Hint: the (^=) operator is a shortcut assignment operator.
Write a Java application that clearly illustrates the difference between the prefix and the postfix versions of the increment operator. Provide a termination message that displays your name.
Write a Java application that illustrates the use of the following relational operators:
<
>
<=
>=
==
!=
Provide appropriate text in the output. Also provide a termination message with your name.
write a Java application that illustrates the use of the following logical or conditional operators:
&&

!
Provide appropriate text in the output. Also provide a termination message with your name.
What is the meaning of the following two images?
This image was inserted here simply to insert some space between the questions and the answers to keep them from being visible on the screen at the same time.
The image is also an example of the kinds of things that we do in my course titled ITSE 2321, ObjectOriented Programming.
This image was also inserted for the purpose of inserting space between the questions and the answers.
Listing 3: Listing for Answer 38. 


Listing 4: Listing for Answer 37. 


Listing 5: Listing for Answer 36. 


Java supports the following list of shortcut assignment operators. These operators allow you to perform an assignment and another operation with a single operator.
+=
=
*=
/=
%=
&=
=
^=
<<=
>>=
>>>=
For example, the two statements that follow perform the same operation.
The behavior of each of the shortcut assignment operators follows this same pattern.
True.
The assignment operator is a binary operator.
True.
The answer for the bitwise exclusive or is:
The answer for the bitwise inclusive or is:
The answer for the bitwise and is:
True: Bits shifted off the right end are lost.
False: In Java, the signed right shift operation populates the vacated bits with the sign bit, while the left shift and the unsigned right shift populate the vacated bits with zeros.
The following table shows the seven bitwise operators supported by Java.
Operator Typical Use Operation
>> OpLeft >> Dist Shift bits of OpLeft right by
Dist bits (signed)
<< OpLeft << Dist Shift bits of OpLeft left by
Dist bits
>>> OpLeft >>> Dist Shift bits of OpLeft right
by Dist bits (unsigned)
& OpLeft & OpRight Bitwise and of the two
operands
 OpLeft  OpRight Bitwise
The bitwise and operator and the bitwise inclusive or operator are shown below.
& bitwise and
 bitwise inclusive or
An important characteristic of the behavior of the logical and operator and the logical or operator in Java is that the expressions containing them are evaluated from left to right. The evaluation of the expression is. terminated as soon as the result of evaluating the expression can be determined. For example, in the expression given in Question 24 , if the variable a is less than the variable b , there is no need to evaluate the right operand of the  operator to determine the value of the entire expression. Therefore, evaluation will terminate as soon as it is determined that a is less than b .
The following three logical or conditional operators are supported by Java.
Operator Typical Use Returns true if
&& Left && Right Left and Right are both true
 Left  Right Either Left or Right is true
! ! Right Right is false
This program produces the following output:
The relational 6<5 is false
The relational 6>5 is true
Java supports the following set of relational operators:
Operator Returns true if
> Left operand is greater than right operand
>= Left operand is greater than or equal
to right operand
< Left operand is less than right operand
<= Left operand is less than or equal
to right operand
== Left operand is equal to right operand
!= Left operand is not equal to right operand
Relational operators return the boolean type in Java.
Java supports the following four unary arithmetic operators.
Operator Description
+ Indicates a positive value
 Negates, or changes algebraic sign
++ Adds one to the operand,
both prefix and postfix
 Subtracts one from operand,
both prefix and postfix
The operator coerces the value of the right operand to a string representation for use in the expression only. If the right operand is a variable, the value stored in the variable is not modified in any way.
The plus operator (+) is also used to concatenate strings as in the following code fragment:
"MyVariable has a value of "
+ MyVariable + " in this program."
Java support various arithmetic operators on floating point and integer numbers. The following table lists five of the binary arithmetic operators supported by Java.
Operator Description
+ Adds its operands
 Subtracts the right operand from the left
operand
* Multiplies the operands
/ Divides the left operand by the right
operand
% Remainder of dividing the left operand by
the right operand
As a result of performing the specified action, an operator can be said to return a value (or evaluate to a value) of a given type. The type depends on the operator and the type of the operands. To evaluate to a value means that after the action is performed, the operator and its operands are effectively replaced in the expression by the value that is returned.
False: Binary operators use infix notation, which means that the operator appears between its operands.
The output from this Java application follows:
The increment operator may be used with both prefix and postfix notation. Basically, the increment operator causes the value of the variable to which it is applied to be increased by one.
With prefix notation, the operand appears to the right of the operator (++X) , while with postfix notation, the operand appears to the left of the operator (X++) .
The difference in behavior has to do with the point in the sequence of operations that the increment actually occurs.
With the prefix version, the variable is incremented before it is used to evaluate the larger overall expression in which it appears. With the postfix version, the variable is used to evaluate the larger overall expression and then the variable is incremented.
The symbol for the increment operator is two plus signs with no space between them (++).
False: The increment operator is a unary operator.
The cast operator is used to purposely convert from one type to another.
The plus sign (+) is automatically overloaded in Java. The plus sign can be used to perform arithmetic addition. It can also be used to concatenate strings. However, the plus sign does more than concatenate strings. It also performs a conversion to String type. When the plus sign is used to concatenate strings and one operand is a string, the other operand is automatically converted to a character string before being concatenated with the existing string.
The operators listed in order are:
where the cast operator is being used to cast to the type char .
Java does not support operator overloading by programmers.
For those languages that support it (such as C++) operator overloading means that the programmer can redefine the behavior of an operator with respect to objects of a new type defined by that program.
Both. As a binary operator, the minus sign causes its right operand to be subtracted from its left operand. As a unary operator, the minus sign causes the algebraic sign of the right operand to be changed.
An operator that operates on two operands is called a binary operator.
An operator that operates on only one operand is called a unary operator.
An operator performs an action on one or two operands.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a preprinted version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
end