17 What lines are printed by the following program? (Choose all that apply.)1: public class WaterBottle { 2: private String brand; 3: private boolean empty; 4: public static float code; 5: public static void main(String[] args) { 6: WaterBottle wb = new WaterBottle(); 7: System.out.println("Empty = " + wb.empty); 8: System.out.println("Brand = " + wb.brand); 9: System.out.println("Code = " + code); 10: } }Line 8 generates a compiler error.Line 9 generates a compiler error.Empty =Empty = falseBrand =Brand = nullCode = 0.0Code = 0f
18 Which of the following statements about var are true? (Choose all that apply.)A var can be used as a constructor parameter.The type of a var is known at compile time.A var cannot be used as an instance variable.A var can be used in a multiple variable assignment statement.The value of a var cannot change at runtime.The type of a var cannot change at runtime.The word var is a reserved word in Java.
19 Which are true about the following code? (Choose all that apply.)var num1 = Long.parseLong("100"); var num2 = Long.valueOf("100"); System.out.println(Long.max(num1, num2));The output is 100.The output is 200.The code does not compile.num1 is a primitive.num2 is a primitive.
20 Which statements about the following class are correct? (Choose all that apply.)1: public class PoliceBox { 2: String color; 3: long age; 4: public void PoliceBox() { 5: color = "blue"; 6: age = 1200; 7: } 8: public static void main(String []time) { 9: var p = new PoliceBox(); 10: var q = new PoliceBox(); 11: p.color = "green"; 12: p.age = 1400; 13: p = q; 14: System.out.println("Q1="+q.color); 15: System.out.println("Q2="+q.age); 16: System.out.println("P1="+p.color); 17: System.out.println("P2="+p.age); 18: } }It prints Q1=blue.It prints Q2=1200.It prints P1=null.It prints P2=1400.Line 4 does not compile.Line 12 does not compile.Line 13 does not compile.None of the above.
21 What is the output of executing the following class?1: public class Salmon { 2: int count; 3: { System.out.print(count+"-"); } 4: { count++; } 5: public Salmon() { 6: count = 4; 7: System.out.print(2+"-"); 8: } 9: public static void main(String[] args) { 10: System.out.print(7+"-"); 11: var s = new Salmon(); 12: System.out.print(s.count+"-"); } }7-0-2-1-7-0-1-0-7-2-1-7-0-2-4-0-7-1-The class does not compile because of line 3.The class does not compile because of line 4.None of the above.
22 Given the following class, which of the following lines of code can independently replace INSERT CODE HERE to make the code compile? (Choose all that apply.)public class Price { public void admission() { INSERT CODE HERE System.out.print(amount); } }int Amount = 0b11;int amount = 9L;int amount = 0xE;int amount = 1_2.0;double amount = 1_0_.0;int amount = 0b101;double amount = 9_2.1_2;double amount = 1_2_.0_0;
23 Which statements about the following class are true? (Choose all that apply.)1: public class River { 2: int Depth = 1; 3: float temp = 50.0; 4: public void flow() { 5: for (int i = 0; i < 1; i++) { 6: int depth = 2; 7: depth++; 8: temp--; 9: } 10: System.out.println(depth); 11: System.out.println(temp); } 12: public static void main(String… s) { 13: new River().flow(); 14: } }Line 3 generates a compiler error.Line 6 generates a compiler error.Line 7 generates a compiler error.Line 10 generates a compiler error.The program prints 3 on line 10.The program prints 4 on line 10.The program prints 50.0 on line 11.The program prints 49.0 on line 11.
Chapter 2 Operators
OCP EXAM OBJECTIVES COVERED IN THIS CHAPTER:
Handling date, time, text, numeric and boolean valuesUse primitives and wrapper classes including Math API, parentheses, type promotion, and casting to evaluate arithmetic and boolean expressions
The previous chapter talked a lot about defining variables, but what can you do with a variable once it is created? This chapter introduces operators and shows how you can use them to combine existing variables and create new values. It shows you how to apply operators to various primitive data types, including introducing you to operators that can be applied to objects.
Understanding Java Operators
Before we get into the fun stuff, let's cover a bit of terminology. A Java operator is a special symbol that can be applied to a set of variables, values, or literals—referred to as operands—and that returns a result. The term operand, which we use throughout this chapter, refers to the value or variable the operator is being applied to. Figure 2.1 shows the anatomy of a Java operation.
FIGURE 2.1 Java operation
The output of the operation is simply referred to as the result. Figure 2.1 actually contains a second operation, with the assignment operator (=
) being used to store the result in variable c
.
We're sure you have been using the addition (+) and subtraction (-
) operators since you were a little kid. Java supports many other operators that you need to know for the exam. While many should be review for you, some (such as the compound assignment operators) may be new to you.
Types of Operators
Java supports three flavors of operators: unary, binary, and ternary. These types of operators can be applied to one, two, or three operands, respectively. For the exam, you need to know a specific subset of Java operators, how to apply them, and the order in which they should be applied.
Java operators are not necessarily evaluated from left-to-right order. In this following example, the second expression is actually evaluated from right to left, given the specific operators involved:
int cookies = 4; double reward = 3 + 2 * --cookies; System.out.print("Zoo animal receives: "+reward+" reward points");
In this example, you first decrement cookies
to 3
, then multiply the resulting value by 2
, and finally add 3
. The value then is automatically promoted from 9
to 9.0
and assigned to reward
. The final values of reward
and cookies
are 9.0
and 3
, respectively, with the following printed:
Zoo animal receives: 9.0 reward points
If you didn't follow that evaluation, don't worry. By the end of this chapter, solving problems like this should be second nature.
Operator Precedence
When reading a book or a newspaper, some written languages are evaluated from left to right, while some are evaluated from right to left. In mathematics, certain operators can override other operators and be evaluated first. Determining which operators are evaluated in what order is referred to as operator precedence. In this manner, Java more closely follows the rules for mathematics. Consider the following expression:
var perimeter = 2 * height + 2 * length;
Let's apply some optional parentheses to demonstrate how the compiler evaluates this statement:
var perimeter = ((2 * height) + (2 * length));
The multiplication operator (*
) has a higher precedence than the addition operator (+
), so the height and length are both multiplied by 2
before being added together. The assignment operator (=
) has the lowest order of precedence, so the assignment to the perimeter variable is performed last.
Unless overridden with parentheses, Java operators follow order of operation, listed in Table 2.1, by decreasing order of operator precedence. If two operators have the same level of precedence,