Summary – Making Decisions with the Ternary Operator – 1Z0-829 Study Guide

Summary

This chapter covered a wide variety of Java operator topics for unary, binary, and ternary operators. Hopefully, most of these operators were review for you. If not, you need to study them in detail. It is important that you understand how to use all of the required Java oper-ators covered in this chapter and know how operator precedence and parentheses influence the way a particular expression is interpreted.

There will likely be numerous questions on the exam that appear to test one thing, such as NIO.2 or exception handling, when in fact the answer is related to the misuse of a particular operator that causes the application to fail to compile. When you see an operator involving numbers on the exam, always check that the appropriate data types are used and that they match each other where applicable.

Operators are used throughout the exam, in nearly every code sample, so the better you understand this chapter, the more prepared you will be for the exam.

Exam Essentials

Be able to write code that uses Java operators. This chapter covered a wide variety of operator symbols. Go back and review them several times so that you are familiar with them throughout the rest of the book.

Be able to recognize which operators are associated with which data types. Some oper-ators may be applied only to numeric primitives, some only to boolean values, and some only to objects. It is important that you notice when an operator and operand(s) are mis-matched, as this issue is likely to come up in a couple of exam questions.

Understand when casting is required or numeric promotion occurs. Whenever you mix operands of two different data types, the compiler needs to decide how to handle the result-ing data type. When you’re converting from a smaller to a larger data type, numeric promo-tion is automatically applied. When you’re converting from a larger to a smaller data type, casting is required.

Understand Java operator precedence. Most Java operators you’ll work with are binary, but the number of expressions is often greater than two. Therefore, you must understand the order in which Java will evaluate each operator symbol.

Be able to write code that uses parentheses to override operator precedence. You can use parentheses in your code to manually change the order of precedence.

Review Questions

The answers to the chapter review questions can be found in the Appendix.

  1. Which of the following Java operators can be used with boolean variables? (Choose all that apply.)
  1. ==
  • +
  • !
  • %
  • ~
  • Cast with (boolean)
  • What data type (or types) will allow the following code snippet to compile? (Choose all that apply.)
byte apples =5;
shortoranges=10;
_____bananas=apples + oranges;
  1. int
  • long
  • boolean
  • double
  • short
  • byte
  • What change, when applied independently, would allow the following code snippet to compile? (Choose all that apply.)
  • long ear = 10;
  • int hearing = 2 * ear;
  1. No change; it compiles as is.
  1. Cast ear on line 4 to int.
  • Change the data type of ear on line 3 to short.
  • Cast 2 * ear on line 4 to int.
  • Change the data type of hearing on line 4 to short.
  • Change the data type of hearing on line 4 to long.
  • What is the output of the following code snippet?
  • boolean canine = true, wolf = true;
  • int teeth = 20;
  • canine = (teeth != 10) ^ (wolf=false);
  • System.out.println(canine+”, “+teeth+”, “+wolf);
  1. true, 20, true
  1. true, 20, false
  • false, 10, true
  • false, 20, false
  • The code will not compile because of line 5.
  • None of the above.