Questions
Lesson 9
Welcome
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 is the operator for the bit shift and rotate operation in Java?
2. True or false? Integer division by zero, including a modulo (%) operation results in an ArithmeticException. No other arithmetic operation in Java can throw an exception. Instead, all other arithmetic operations proceed to a result, even though that result might be arithmetically incorrect.
3. What output is produced by the following program?
- A. A compiler error
- B. Exception
- C. 5
- D. 10
- E. 15
- F. 20
- G. 25
class Q84{ |
4. What output is produced by the following program?
- A. A compiler error
- B. Exception
- C. 5
- D. 10
- E. 15
- F. 20
- G. 25
class Q85{ |
5. What output is produced by the following program?
- A. A compiler error
- B. Exception
- C. 32
- D. 64
- E. 128
- F. 256
- G. None of the above.
class Q86{ |
6. What output is produced by the following program?
- A. A compiler error
- B. Exception
- C. -4
- D. -8
- E. -16
- F. -32
- G. None of the above
class Q87{ |
7. What output is produced by the following program?
- A. A compiler error
- B. Exception
- C. -4
- D. -8
- E. -16
- F. -32
- G. None of the above
class Q88{ |
8. What output is produced by the following program?
- A. A compiler error
- B. Exception
- C. -4
- D. -8
- E. -16
- F. -32
- G. None of the above
class Q89{ |
9. What output is produced by the following program?
- A. A compiler error
- B. Exception
- C. -3 -3
- D. -3 -4
- E. -4 -4
- F. None of the above
class Q90{ |
10. 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 Q91{ |
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.
Answers and Explanations
Answer 10
Explanation 10
If the specified number of bits to be shifted exceeds this number, then the number of bit positions actually shifted is the specified number modulo the number of bits in the type.
In this case, the number of bits actually shifted is 33 modulo 32 or 1. This gives a result of 16384/2 or 8192.
Answer 9
Explanation 9
Answer 8
Explanation 8
Answer 7
Explanation 7
int y = x >>> 2;
An unsigned right shift pulls a zero into the most-significant bit as each shift takes place, regardless of whether the original value was positive or negative.
The original value of x expressed in decimal was:
-32
The original value of x, corresponding to -32 and expressed as a hexadecimal string was:
ffffffe0
After the shift, the value of y expressed as a hexadecimal string was:
3ffffff8
When expressed as a decimal value, this is:
1073741816
In comparison, if this were a signed right shift, the four values in the same order as above would be:
-32
ffffffe0
fffffff8
-8
Thus, an unsigned right shift on a negative value does not preserve the sign and does not result in values that are easily explained in terms of simple division by a factor of two. Generally speaking, the unsigned right-shift operator should not be thought of in terms of arithmetic division. Rather, it has other purposes.
Answer 6
Explanation 6
Answer 5
Explanation 5
Answer 4
Explanation 4
Each time an integer value is shifted one bit to the left, its value is multiplied by a factor of two, provided that no data is lost in the process.
Therefore, a two-bit left shift multiplies the operand by a factor of 4, provided no data is lost in the process. In this case, no data was lost, and the value assigned to y was equal to the value stored in x multiplied by four, giving a result of 20.
Answer 3
Explanation 3
In this case, the shifted result must be explicitly cast back to type byte before being assigned to a byte variable. Of course, the use of a cast operator following a left shift can sometimes cause a loss of data in the higher-order bits that are discarded in the conversion.
Under JDK 1.3, the compiler error reads as follows:
Q84.java:8: possible loss of precision
found : int
required: byte
byte y = x << 2;
Other compilers may provide a different error message that means the same thing.
Answer 2
Explanation 2
For example, the largest positive value that can be stored in type int, as indicated by the constant named Integer.MAX_VALUE, is 2147483647. The following statement is intended to produce a result that is twice the maximum value.
System.out.println(
2*Integer.MAX_VALUE);
However, it produces the output value -2 instead. This indicates serious arithmetic overflow problems.
It is very important to understand that this arithmetic error does not throw an exception. Rather, it simply produces an incorrect result.
One way to avoid this particular problem is to force the arithmetic to be performed as type long as shown below:
System.out.println(
(long)2*Integer.MAX_VALUE);
This statement produces the output 4294967294 as expected.
If you do this, however, you must be careful not to cast the result back to type int, or you will simply reintroduce the same problem as before.
Answer 1
Explanation 1
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.