# Connexions

You are here: Home » Content » Jb0210r Review

## Navigation

### Recently Viewed

This feature requires Javascript to be enabled.

# Jb0210r Review

Module by: Richard Baldwin. E-mail the author

Summary: This module contains review questions and answers keyed to the module titled Jb0210: Java OOP: Operators.

## Preface

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.

## Questions

### Question 1 .

An operator performs an action on what? Provide the name.

### Question 2

What do we call an operator that operates on only one operand?

### Question 3

What do we call an operator that operates on two operands?

### Question 4

Is the minus sign a unary or a binary operator, or both? Explain your answer.

### Question 5

Describe operator overloading.

### Question 6

True or false? Java programmers may overload operators.

### Question 7

Show the symbols used for the following operators in Java: assignment , not equal , addition , cast .

### Question 8

Are any operators automatically overloaded in Java? If so, identify one and describe its overloaded behavior.

### Question 9

What is the purpose of the cast operator?

### Question 10

True or false? The increment operator is a binary operator.

### Question 11

Show the symbol for the increment operator.

### Question 12

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.

### Question 13

Show the output that would be produced by the Java application in Listing 1 .

1
Listing 1: Listing for Question 13.

class incr01 { //define the controlling class
public static void main(String[] args){ //define main
int x = 5, X = 5, y = 5, Y = 5;
System.out.println("x = " + x );
System.out.println("X = " + X );
System.out.println("x + X++ = " + (x + X++) );
System.out.println("X = " + X );
System.out.println();
System.out.println("y = " + y );
System.out.println("Y = " + Y );
System.out.println("y + ++Y = " + (y + ++Y) );
System.out.println("Y = " + Y );
}//end main
}//End incr01 class.  Note no semicolon required
//End Java application


### Question 14

True or false? Binary operators use outfix notation. If your answer is False, explain why.

### Question 15

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?

### Question 16

Show and describe at least five of the binary arithmetic operators supported by Java (Clarification: binary operators does not mean bitwise operators).

### Question 17

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.

### Question 18

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.

### Question 19

Show and describe four unary arithmetic operators supported by Java.

### Question 20

What is the type returned by relational operators in Java?

### Question 21

Show and describe six different relational operators supported by Java.

### Question 22

Show the output that would be produced by the Java application shown in Listing 2 .

2
Listing 2: Listing for Question 22.

class relat01 { //define the controlling class
public static void main(String[] args){ //define main
System.out.println("The relational 6<5 is " + (6<5 ));
System.out.println("The relational 6>5 is " + (6>5 ));
}//end main
}//End relat01 class.  Note no semicolon required
//End Java application


### Question 23

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?

### Question 24

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 .

#### Note:


(a < b) || (c < d)

### Question 25

Show the symbols used for the bitwise and operator and the bitwise inclusive or operator.

### Question 26

Show and describe seven operators in Java that perform actions on the operands one bit at a time (bitwise operators) .

### Question 27

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.

### Question 28

True or false? In a signed right-shift operation in Java, the bits shifted off the right end are lost. If your answer is False, explain why.

### Question 29

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.

### Question 30

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.

### Question 31

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.

### Question 32

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.

### Question 33

Is the assignment operator a unary operator or a binary operator. Select one or the other.

### Question 34

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.

### Question 35

Show two of the shortcut assignment operators and explain how they behave by comparing them with the regular (non-shortcut) versions. Hint: the (^=) operator is a shortcut assignment operator.

### Question 36

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.

### Question 37

Write a Java application that illustrates the use of the following relational operators:

#### Note:


<
>
<=
>=
==
!=

Provide appropriate text in the output. Also provide a termination message with your name.

### Question 38

write a Java application that illustrates the use of the following logical or conditional operators:

#### Note:

Logical or conditional operators

&&
||
!

Provide appropriate text in the output. Also provide a termination message with your name.

## Listings

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, Object-Oriented Programming.

This image was also inserted for the purpose of inserting space between the questions and the answers.

## Answers

### Answer 38

3
Listing 3: Listing for Answer 38.

/*File SampProg09.java from module 22
Copyright 1997, R.G.Baldwin
Without reviewing the following solution, 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.
*********************************************************/
class SampProg09 { //define the controlling class
public static void main(String[] args){ //define main
System.out.println("true and true is "
+ (true && true) );
System.out.println("true and false is "
+ (true && false) );

System.out.println("true or true is "
+ (true || true) );
System.out.println("true or false is "
+ (true || false) );
System.out.println("false or false is "
+ (false || false) );

System.out.println("not true is " + (! true) );
System.out.println("not false is " + (! false) );

System.out.println("Terminating, Dick Baldwin");
}//end main
}//End SampProg09 class.  Note no semicolon required


### Answer 37

4
Listing 4: Listing for Answer 37.

/*File SampProg08.java from module 22
Copyright 1997, R.G.Baldwin
Without reviewing the following solution, 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.
*********************************************************/
class SampProg08 { //define the controlling class
public static void main(String[] args){ //define main
System.out.println("The relational 6<5 is "
+ (6<5 ) );
System.out.println("The relational 6>5 is "
+ (6>5 ) );
System.out.println("The relational 5>=5 is "
+ (5>=5 ) );
System.out.println("The relational 5<=5 is "
+ (5<=5 ) );
System.out.println("The relational 6==5 is "
+ (6==5 ) );
System.out.println("The relational 6!=5 is "
+ (6!=5 ) );
System.out.println("Terminating, Dick Baldwin");
}//end main
}//End SampProg08 class.  Note no semicolon required


### Answer 36

5
Listing 5: Listing for Answer 36.

/*File SampProg07.java from module 22
Copyright 1997, R.G.Baldwin
Without reviewing the following solution, 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.
*********************************************************/
class SampProg07{
static public void main(String[] args){
int x = 3;
int y = 3;
int z = 10;
System.out.println("Prefix version gives  "
+ (z + ++x));
System.out.println("Postfix version gives "
+ (z + y++));
System.out.println("Terminating, Dick Baldwin");
}//end main
}//end class SampProg07


### Answer 35

Java supports the following list of shortcut assignment operators. These operators allow you to perform an assignment and another operation with a single operator.

#### Note:


+=
-=
*=
/=
%=
&=
|=
^=
<<=
>>=
>>>=

For example, the two statements that follow perform the same operation.

• x += y;
• x = x + y;

The behavior of each of the shortcut assignment operators follows this same pattern.

True.

### Answer 33

The assignment operator is a binary operator.

True.

### Answer 31

The answer for the bitwise exclusive or is:

• 11 1 xor 1 produces 0
• 10 1 xor 0 produces 1
• 01 0 xor 1 produces 1
• 00 0 xor 0 produces 0

### Answer 30

The answer for the bitwise inclusive or is:

• 11 1 or 1 produces 1
• 10 1 or 0 produces 1
• 01 0 or 1 produces 1
• 00 0 or 0 produces 0

### Answer 29

The answer for the bitwise and is:

• 11 1 and 1 produces 1
• 10 1 and 0 produces 0
• 01 0 and 1 produces 0
• 00 0 and 0 produces 0

### Answer 28

True: Bits shifted off the right end are lost.

### Answer 27

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.

### Answer 26

The following table shows the seven bitwise operators supported by Java.

#### Note:

Bitwise operators

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 

### Answer 25

The bitwise and operator and the bitwise inclusive or operator are shown below.

#### Note:

Two bitwise operators

& bitwise and
| bitwise inclusive or

### Answer 24

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 .

### Answer 23

The following three logical or conditional operators are supported by Java.

#### Note:

The logical or conditional operators

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

### Answer 22

This program produces the following output:

#### Note:


The relational 6<5 is false
The relational 6>5 is true

### Answer 21

Java supports the following set of relational operators:

#### Note:

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

### Answer 20

Relational operators return the boolean type in Java.

### Answer 19

Java supports the following four unary arithmetic operators.

#### Note:

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

### Answer 18

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.

### Answer 17

The plus operator (+) is also used to concatenate strings as in the following code fragment:

#### Note:

String concatenation

"MyVariable has a value of  "
+ MyVariable + " in this program."

### Answer 16

Java support various arithmetic operators on floating point and integer numbers. The following table lists five of the binary arithmetic operators supported by Java.

#### Note:

Binary arithmetic operators

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

### Answer 15

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.

### Answer 14

False: Binary operators use infix notation, which means that the operator appears between its operands.

### Answer 13

The output from this Java application follows:

• x = 5
• X = 5
• x + X++ = 10
• X = 6
• y = 5
• Y = 5
• y + ++Y = 11
• Y = 6

### Answer 12

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.

### Answer 11

The symbol for the increment operator is two plus signs with no space between them (++).

### Answer 10

False: The increment operator is a unary operator.

### Answer 9

The cast operator is used to purposely convert from one type to another.

### Answer 8

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.

### Answer 7

The operators listed in order are:

• =
• !=
• +
• (char)

where the cast operator is being used to cast to the type char .

### Answer 6

Java does not support operator overloading by programmers.

### Answer 5

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.

### Answer 4

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.

### Answer 3

An operator that operates on two operands is called a binary operator.

### Answer 2

An operator that operates on only one operand is called a unary operator.

### Answer 1

An operator performs an action on one or two operands.

## Miscellaneous

This section contains a variety of miscellaneous information.

### Note:

Housekeeping material
• Module name: Jb0210r Review: Operators
• File: Jb0210r.htm
• Originally published: 1997
• Published at cnx.org: 11/23/12
• Revised: 11/17/13

### Note:

Disclaimers:

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 pre-printed 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-

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

### Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

### Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks