Published January 23, 2001
By Richard G. Baldwin
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 Q104{ public static void main( String args[]){ try{ byte x = 7; byte y = 5; System.out.println(x | y); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition |
2. What output is produced by the following program?
class Q105{ public static void main( String args[]){ try{ boolean x = true; boolean y = false; System.out.print((x & y) + " " + (x & x)); System.out.print(" "); System.out.print( (x ^ y) + " " + (x ^ x) + " " + (y ^ y)); System.out.print(" "); System.out.println( (x | y) + " " + (x | x) + " " + (y | y)); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition |
3. What output is produced by the following program?
class Q106{ public static void main( String args[]){ try{ byte x = 1; boolean y = false; System.out.println( (boolean)x & y); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition |
4. True or false? Java provides "short-circuit" AND, OR, and XOR operators.
5. True or false? As with the bitwise (Boolean) operators discussed in Question 2 above ( & and | ), the short-circuit logical operators ( && and || ) can be applied to either boolean or integer types.
6. True or false? The following expressions describe the behavior of the short-circuit logical operators, regardless of the value of the boolean variable X.
7. True or False? Given the following expression:
a = x ? b : c;
The following statements are all true?
8. What output is produced by the following program?
class Q107{ public static void main( String args[]){ String x = new String("100"); String y = new String("100"); if(x == y) System.out.println("Equal"); else System.out.println("Not Equal"); }//end main() }//end class definition |
9. What output is produced by the following program?
class Q108{ public static void main( String args[]){ String x = new String("100"); String y = new String("100"); if(x.equals(y)) System.out.println("Equal"); else System.out.println("Not Equal"); }//end main() }//end class definition |
10. What output is produced by the following program?
class Q109{ public static void main( String args[]){ String x = "100"; String y = "100"; if(x == y) System.out.println("Equal"); else System.out.println("Not Equal"); }//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.
According to Roberts, Heller, and Ernest, "Since String objects are immutable, literal strings are inevitably constant strings, so the compiler re-uses the same String object if it sees the same literal value occur more than once in the source."
Therefore, in this case, a single literal String object gets used twice. The reference variables x and y refer to the same literal String object. Therefore, when x and y are tested using the == operator, the test returns true.
With respect to the String class, the equals() method can be used to determine if two objects instantiated from the class contain the same string value. In this case, they do, so the equals() method returns true.
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-