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.
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 |
2. What output is produced by the following program?
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 |
3. True or false? The unsigned right-shift operator is ideal for use with the types short and byte.
4. True or false? Java provides only two kinds of comparison operators:
5. There are four ordinal comparison operators. List their functional names and show the symbols used for the operators.
6. True or false? The ordinal comparison operators can be used only with the Java numeric types.
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.
8. True or false? Arithmetic promotions are applied whenever the ordinal comparison operators are used.
9. What output is produced by the following program?
class Q94{ public static void main( String args[]){ int x = 5; boolean y = true; System.out.println(x < y); }//end main() }//end class definition |
10. What output is produced by the following program?
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 |
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.
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.
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.
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.
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.
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.
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.
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.
-end-