Test Your Java Knowledge: Using Operators and Making Assignments, Part 5
Questions
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 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 Q96{ public static void main( String args[]){ try{ Button var1 = new Button(); Class var2 = var1.getClass(); System.out.println( var1 instanceof var2); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition |
2. What output is produced by the following program?
- A. A compiler error
- B. Exception Thrown
- C. true true
- D. true false
- E. None of the above
class Q97{ public static void main( String args[]){ try{ System.out.print(( new AClass()instanceof AClass) + " "); System.out.println( new AClass() instanceof AnInterface); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition interface AnInterface{ //empty interface }//end interface definition class AClass implements AnInterface{ //empty class definition }//end class definition |
3. 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 Q98{ public static void main(String args[]){ try{ Button[] var = new Button[5]; System.out.println( var instanceof Component[]); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition |
4. 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 Q99{ public static void main( String args[]){ try{ Button[] var = null; System.out.println( var instanceof Component[]); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition |
5. What output is produced by the following program?
- A. A compiler error
- B. Exception Thrown
- C. true
- D. false
- E. None of the above
class Q100{ public static void main( String args[]){ try{ Button[] var = new Button[5]; System.out.println( var instanceof []); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition |
6. 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 Q101{ public static void main( String args[]){ try{ Button[] var = new Button[5]; System.out.println( var.getClass().isArray()); }catch(Exception e){ System.out.println( "Exception Thrown"); }//end catch }//end main() }//end class definition |
7. True or false? The equality operators (==) and (!=) cannot be used with class-type operands.
8. True or false? The equality operators (==) and (!=) cannot be used with boolean operands.
9. What output is produced by the following program?
- A. A compiler error
- B. Exception Thrown
- C. 5
- D. 7
- E. 2
- F. None of the above
class Q102{ 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 |
10. What output is produced by the following program?
- A. A compiler error
- B. Exception Thrown
- C. 5
- D. 7
- E. 2
- F. None of the above
class Q103{ 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 |
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
Answer 9
Explanation 9
Answer 8
Explanation 8
Answer 7
Explanation 7
To really determine if two objects are equal, you must use the equals() method (assuming that the method has been properly defined for the class of object being tested).
Answer 6
Explanation 6
Answer 5
Explanation 5
Answer 4
Explanation 4
Answer 3
Explanation 3
The test checks for two things.
- It checks to determine if the object is an array.
- It checks to determine if the element type of that array matches the element type of the right operand (or is some subclass of the element type of the right operand).
Answer 2
Explanation 2
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.
This article was originally published on January 26, 2001