November 26, 2014
Hot Topics:

Test Your Java Knowledge: Using Operators and Making Assignments, Part 4

  • January 4, 2001
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Questions

Lesson 10


Welcome

The purpose of this series of tutorial lessons is to help you learn Java by approaching it from a question and answer viewpoint.

I recommend that you also make use of my online Java tutorial lessons, which are designed from a more conventional textbook approach.  Those tutorial lessons are published at Gamelan.com.

For your convenience, I also maintain a consolidated Table of Contents on my personal web site that links to the individual lessons on the Gamelan site.

Insofar as possible, I will make use of Sun Java in these lessons.  However, it will not be possible for me to go back and do a full update each time Sun releases a new version, so over the course of time, I expect to use different versions of Sun Java.

Just in case you would like to sneak a peek, the answers to the questions, and the explanations of those answers are located (in reverse order) at the end of this file.

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.  It is recommended that you make your first pass through the questions in the order that they appear so as to avoid inadvertently seeing the answer to a question before you provide your own answer.



1.  What output is produced by the following program?
  • A.  A compiler error
  • B.  Exception
  • C.  0
  • D.  1
  • E.  -1
  • F.  None of the above
class Q92{
  public static void main(
                        String args[]){
    try{
      byte x = 127;
      byte y = (byte)(x >> 9);
      System.out.println(y);
    }catch(Exception e){
      System.out.println("Exception");
    }//end catch
  }//end main()
}//end class definition

Answer and Explanation

2.  What output is produced by the following program?

  • A.  A compiler error
  • B.  Exception
  • C.  0
  • D.  1
  • E.  -1
  • F.  None of the above
class Q93{
  public static void main(
                        String args[]){
    try{
      byte x = -127;
      byte y = (byte)(x >> 9);
      System.out.println(y);
    }catch(Exception e){
      System.out.println("Exception");
    }//end catch
  }//end main()
}//end class definition

Answer and Explanation

3.  True or false? The unsigned right-shift operator is ideal for use with the types short and byte.

Answer and Explanation

4.  True or false?  Java provides only two kinds of comparison operators:

  • Ordinal comparison operators, such as <=
  • Equality comparison operators, such as ==
Answer and Explanation

5.  There are four ordinal comparison operators.  List their functional names and show the symbols used for the operators.

Answer and Explanation

6.  True or false?  The ordinal comparison operators can be used only with the Java numeric types.

Answer and Explanation

7.  True or false?  As in C and C++, when using the ordinal comparison operators, a false comparison returns a zero integer and a true comparison returns a nonzero integer.

Answer and Explanation

8.  True or false?  Arithmetic promotions are applied whenever the ordinal comparison operators are used.

Answer and Explanation

9.  What output is produced by the following program?

  • A.  A compiler error
  • B.  A runtime error
  • C.  true
  • D.  false
class Q94{
  public static void main(
                        String args[]){
    int x = 5;
    boolean y = true;
    System.out.println(x < y);
  }//end main()
}//end class definition

Answer and Explanation

10.  What output is produced by the following program?

  • A.  A compiler error
  • B.  Exception Thrown
  • C.  true
  • D.  false
  • E.  None of the above
import java.awt.*;
class Q95{
  public static void main(
                        String args[]){
    try{
      System.out.println(
              new Button() instanceof
                            Component);
    }catch(Exception e){
      System.out.println(
                   "Exception Thrown");
    }//end catch
  }//end main()
}//end class definition

Answer and Explanation



Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

baldwin.richard@iname.com



 

Answers and Explanations

Answer 10

C.  true

Back to Question 10

Explanation 10

The instanceof operator can be used to test the class of an object at runtime.

The left-hand operand of the instanceof operator can be any object reference expression.  The right-hand operand must be a class, interface, or array type.

The right-hand operand can also be a superclass type of the left operand.

In this program, Component is a superclass of Button.  Therefore, when a reference to a Button object is compared to the name of the class Component, using the instanceof operator, the result of the comparison is true.  To use terminology from the OOD world, a Button isa Component.

Answer 9

A.  A compiler error

Back to Question 9

Explanation 9

While the ordinal comparison operators can be applied to the primitive numeric and char types, and it is allowable to mix these types when making a comparison, the ordinal comparison operators cannot be applied to boolean or class-type operands.

Answer 8

True

Back to Question 8

Explanation 8

Arithmetic promotions are applied when the ordinal operators are used with operands of different types.

I will have a lot more to say about arithmetic promotion in subsequent lessons.  For the time being, suffice it to say that the ordinal comparison operators can be applied to numeric and char types, and the types can be mixed.

For example, the following is a valid comparison:
(64.0f < 'A').
This comparison returns true, because the Unicode value of the upper case A is 65.

When this comparison is made, the char value for 'A' is promoted to a float type having the same value and the ordinal comparison is made on the basis of the two float values.

Answer 7

False

Back to Question 7

Explanation 7

Unlike C and C++, Java does not recognize the integer values in a boolean sense.  In Java, boolean is a type, and the comparison operators return the boolean values true and false.

Answer 6

False

Back to Question 6

Explanation 6

The ordinal comparison operators are applicable to all numeric types and also to the char type.  When applied to the char type, the test is made on the numeric values that represent the characters.

Because the numeric values that represent the letters and the numbers in the Unicode character set increase from 'A' to 'Z', from 'a' to 'z', and from '0' to '9', the use of the ordinal comparison operators with type char, will usually produce the kind of results that you would probably expect.  For example, the comparison
('A' < 'B')
returns true, which is probably what you would expect.

However, you may be surprised when you compare upper and lower case characters.  For example,  The following comparison
('a' < 'A')
returns false due to the positions of the upper and lower case alphabets in the Unicode sequence.

Answer 5

The four ordinal comparison operators are:
  • Less than: <
  • Less than or equal to:  <=
  • Greater than:  >
  • Greater than or equal to:  >=
Back to Question 5

Explanation 5

The ordinal comparison operators are used to compare values using expressions such as:
(x <= y)
 

Answer 4

False

Back to Question 4

Explanation 4

Java also provides the instanceof comparison operator that can be used to test the class of an object at runtime.

Answer 3

False

Back to Question 3

Explanation 3

Because types short and byte are always promoted to type int before shifting, the use of the unsigned right-shift operator with short and byte will often produce results that are different than would be the case if the operands were not promoted prior to shifting.

Therefore, you will often get results that are different from what you might expect unless you are careful to take the promotion into account.

For example, if you do a one-bit unsigned right shift on an eight-bit entity containing the value -128 (without promotion to an int), the bit pattern of the result would be:

0100 0000

This bit pattern has a decimal value of 64.

Doing the same thing with promotion to an int first and then casting back to a byte produces the following bit pattern:

1100 0000

This bit pattern has a decimal value of -64.

Even if you aren't thinking in terms of decimal values, you can still see that the two approaches produce different bit patterns.  So, if you do unsigned right-shift operations on types byte or short, be sure to take promotion into account.

Answer 2

E.  -1

Back to Question 2

Explanation 2

The bit pattern for -127 in eight-bit twos-complement notation is:

1000 0001

Because a byte type is promoted to an int having the same value, with sign extension, before performing the shift, it is possible to shift a byte type by more than 8 bit positions.

After promotion to an int with sign extension, the bit pattern is:

1111 1111 1000 0001

Shifting right by 9 bit positions causes the rightmost 9 bits, including the original 8 bits to be shifted off the right end.

For a nine-bit arithmetic right shift on a negative value, nine ones  are pulled in on the left end.  The bit pattern following the shift is:

1111 1111 1111 1111

In twos-complement notation, this is a value of -1.

In this case, the concept of division by powers of two breaks down because no matter how many bits the original negative value is shifted to the right (up to 31), the result will still be -1.  (For a right shift of 32 or greater, the actual number of bits shifted is modulo 32 so different results will be produced.)

In this case, the cast back to a byte type had no effect on the final value.
 

Answer 1

C.  0

Back to Question 1

Explanation 1

The bit pattern for 127 in eight-bit twos-complement notation is:

0111 1111

Because a byte type is promoted to an int having the same value, with sign extension, before performing the shift, it is possible to shift a byte type by more than 8 bit positions.

After promotion to an int with sign extension, the bit pattern is:

0000 0000 0111 1111

Shifting right by 9 bit positions causes the rightmost 9 bits, including the original 8 bits to be shifted off the right end.

For a nine-bit arithmetic right shift on a positive value, nine zeros  are pulled in on the left end.  The bit pattern following the shift is:

0000 0000 0000 0000

In twos-complement notation, this is a value of 0.

In this case, the cast back to a byte type had no effect on the final value.



Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

baldwin.richard@iname.com






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel