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

What do you get when you use the == operator to compare  references to literal String objects that contain the same value? Show off your smarts or learn a thing or two in our quiz series.

Published January 23, 2001
By Richard G. Baldwin

Questions

Lesson 12


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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

4.  True or false?  Java provides "short-circuit" AND, OR, and XOR operators.

Answer and Explanation

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.

Answer and Explanation

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.

Answer and Explanation

7.  True or False?  Given the following expression:

a = x ? b : c;

The following statements are all true?

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

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

C.  Equal

Back to Question 10

Explanation 10

Note that although this program is very similar to the program in Question 8, this program is not identical to that program.  In the program of Question 8, two String objects were instantiated using the new operator.  In this program, two literal String objects were instantiated without the use of the new operator.

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.

Answer 9

The answer is C,  Equal

Back to Question 9

Explanation 9

The equals() method of a class can be used to determine if two objects instantiated from that class are equal.  Of  course, since the equals() method is overridden by the author of the class, the notion of equal depends on what that concept means to the author.

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.

Answer 8

The answer is D,  Not Equal

Back to Question 8

Explanation 8

When the == operator is applied to object references, it doesn't test to determine if the two objects are equal.  Rather, it tests to determine if the two references refer to the same object.  In this case, even though the contents of the two String objects contain the same value, they are different objects.  Therefore, the test returns false.

Answer 7

The answer is True.

Back to Question 7

Explanation 7

According to Roberts, Heller, and Ernest, all three of the statements in Question 7 are true.

Answer 6

The answer is true.

Back to Question 6

Explanation 6

In these two cases, the value of the boolean variable X doesn't matter.  The short-circuit operators won't even bother to look at the value of X, because the result of evaluating the expression doesn't depend on the value of X.  That is why they are called short-circuit operators.

Answer 5

The answer is False.

Back to Question 5

Explanation 5

The short-circuit operators can be applied only to boolean types.
 

Answer 4

The answer is False.

Back to Question 4

Explanation 4

There is no XOR short-circuit operator.

Answer 3

The answer is A, compiler error.

Back to Question 3

Explanation 3

The operands of the operator must be of compatible types.  That is not true in this case.  Furthermore, you cannot cast an integer type to a boolean type (as was attempted in this case) to force the two operands to be of compatible types.

Answer 2

The answer is C,
false true  true false false  true true false

Back to Question 2

Explanation 2

According to The Complete Java 2 Certification Study Guide, by Roberts, Heller, and Ernest, even though these bitwise operators are usually applied to integer types, it is also permitted to apply them to boolean operands.
 

Answer 1

The answer is D, 7.

Back to Question 1

Explanation 1

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



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

-end-