November 23, 2014
Hot Topics:

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

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

Questions

Lesson 11


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 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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

7.  True or false?  The equality operators (==) and (!=) cannot be used with class-type operands.

Answer and Explanation

8.  True or false?  The equality operators (==) and (!=) cannot be used with boolean operands.

Answer and Explanation

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

Answer and Explanation

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

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

The answer is E, 2.

Back to Question 10

Explanation 10

The bitwise representation of 7 is 111.  The bitwise representation of 5 is 101.  The bitwise exclusive or of 111 and 101 is 010, which, when displayed according to base-ten notation is 2.

Answer 9

The answer is C, 5.

Back to Question 9

Explanation 9

The bitwise representation of 7 is 111.  The bitwise representation of 5 is 101.  The bitwise and of 111 and 101 is 101, which, when displayed according to base-ten notation is 5.

Answer 8

False.

Back to Question 8

Explanation 8

The equality operators can be used with boolean operands.  We frequently use them with boolean operands in conditional expressions.

Answer 7

False.

Back to Question 7

Explanation 7

The equality operators can be used with class-type operands, but they don't determine if the objects referred to by the operands are equal or not equal.  They simply determine if two references refer to the same object or not.

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

The answer is C, true.

Back to Question 6

Explanation 6

The isArray() method of the Class class can be used to determine if a reference refers to an array object.

Answer 5

The answer is A, compiler error.

Back to Question 5

Explanation 5

The instanceof operator cannot be used to determine if a reference is to a general array of any type ([]).  Rather, it can only be used to to test if a reference is to a specific array type such as Component[] (or a subclass of that type).
 

Answer 4

The answer is D, false.

Back to Question 4

Explanation 4

If the left operand of the instanceof operator is null, the test simply returns false.  It does not throw an exception.

Answer 3

The answer is C, true.

Back to Question 3

Explanation 3

You can use the instanceof operator to determine if a reference refers to an array.

The test checks for two things.

  1. It checks to determine if the object is an array.
  2. 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).
If both are true, then the test returns true.

Answer 2

The answer is C, true true.

Back to Question 2

Explanation 2

The right operand of the instanceof operator can be the name of an interface type, in addition to the name of a class type.
 

Answer 1

The answer is A, compiler error.

Back to Question 1

Explanation 1

The right operand of the instanceof operator cannot be a reference to a java.lang.Class object or its string name.



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