Скачать книгу

int hearing = 2 * ear;No change; it compiles as is.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.

      4 What is the output of the following code snippet?3: boolean canine = true, wolf = true; 4: int teeth = 20; 5: canine = (teeth != 10) ^ (wolf=false); 6: System.out.println(canine+", "+teeth+", "+wolf);true, 20, truetrue, 20, falsefalse, 10, truefalse, 20, falseThe code will not compile because of line 5.None of the above.

      5 Which of the following operators are ranked in increasing or the same order of precedence? Assume the + operator is binary addition, not the unary form. (Choose all that apply.)+, *, %, --++, (int), *=, ==, !(short), =, !, **, /, %, +, ==!, ||, &^, +, =, +=

      6 What is the output of the following program?1: public class CandyCounter { 2: static long addCandy(double fruit, float vegetables) { 3: return (int)fruit+vegetables; 4: } 5: 6: public static void main(String[] args) { 7: System.out.print(addCandy(1.4, 2.4f) + ", "); 8: System.out.print(addCandy(1.9, (float)4) + ", "); 9: System.out.print(addCandy((long)(int)(short)2, (float)4)); } }4, 6, 6.03, 5, 63, 6, 64, 5, 6The code does not compile because of line 9.None of the above.

      7 What is the output of the following code snippet?int ph = 7, vis = 2; boolean clear = vis> 1 & (vis < 9 || ph < 2); boolean safe = (vis> 2) && (ph++> 1); boolean tasty = 7 <= --ph; System.out.println(clear + "-" + safe + "-" + tasty);true-true-truetrue-true-falsetrue-false-truetrue-false-falsefalse-true-truefalse-true-falsefalse-false-truefalse-false-false

      8 What is the output of the following code snippet?4: int pig = (short)4; 5: pig = pig++; 6: long goat = (int)2; 7: goat -= 1.0; 8: System.out.print(pig + " - " + goat);4 - 14 - 25 - 15 - 2The code does not compile due to line 7.None of the above.

      9 What are the unique outputs of the following code snippet? (Choose all that apply.)int a = 2, b = 4, c = 2; System.out.println(a> 2 ? --c : b++); System.out.println(b = (a!=c ? a : b++)); System.out.println(a> b ? b < c ? b : 2 : 1);123456The code does not compile.

      10 What are the unique outputs of the following code snippet? (Choose all that apply.)short height = 1, weight = 3; short zebra = (byte) weight * (byte) height; double ox = 1 + height * 2 + weight; long giraffe = 1 + 9 % height + 1; System.out.println(zebra); System.out.println(ox); System.out.println(giraffe);123456The code does not compile.

      11 What is the output of the following code?11: int sample1 = (2 * 4) % 3; 12: int sample2 = 3 * 2 % 3; 13: int sample3 = 5 * (1 % 2); 14: System.out.println(sample1 + ", " + sample2 + ", " + sample3);0, 0, 51, 2, 102, 1, 52, 0, 53, 1, 103, 2, 6The code does not compile.

      12 The _________ operator increases a value and returns the original value, while the _______ operator decreases a value and returns the new value.post-increment, post-incrementpre-decrement, post-decrementpost-increment, post-decrementpost-increment, pre-decrementpre-increment, pre-decrementpre-increment, post-decrement

      13 What is the output of the following code snippet?boolean sunny = true, raining = false, sunday = true; boolean goingToTheStore = sunny & raining ^ sunday; boolean goingToTheZoo = sunday && !raining; boolean stayingHome = !(goingToTheStore && goingToTheZoo); System.out.println(goingToTheStore + "-" + goingToTheZoo + "-" +stayingHome);true-false-falsefalse-true-falsetrue-true-truefalse-true-truefalse-false-falsetrue-true-falseNone of the above

      14 Which of the following statements are correct? (Choose all that apply.)The return value of an assignment operation expression can be void.The inequality operator (!=) can be used to compare objects.The equality operator (==) can be used to compare a boolean value with a numeric value.During runtime, the & and | operators may cause only the left side of the expression to be evaluated.The return value of an assignment operation expression is the value of the newly assigned variable.In Java, 0 and false may be used interchangeably.The logical complement operator (!) cannot be used to flip numeric values.

      15 Which operators take three operands or values? (Choose all that apply.)=&&*=? :&++/

      16 How many lines of the following code contain compiler errors?int note = 1 * 2 + (long)3; short melody = (byte)(double)(note *= 2); double song = melody; float symphony = (float)((song == 1_000f) ? song * 2L : song);01234

      17 Given the following code snippet, what are the values of the variables after it is executed? (Choose all that apply.)int ticketsTaken = 1; int ticketsSold = 3; ticketsSold += 1 + ticketsTaken++; ticketsTaken *= 2; ticketsSold += (long)1;ticketsSold is 8.ticketsTaken is 2.ticketsSold is 6.ticketsTaken is 6.ticketsSold is 7.ticketsTaken is 4.The code does not compile.

      18 Which of the following can be used to change the order of operation in an expression? (Choose all that apply.)[ ]< >( )\ /{ }" "

      19 What is the result of executing the following code snippet? (Choose all that apply.)3: int start = 7; 4: int end = 4; 5: end += ++start; 6: start = (byte)(Byte.MAX_VALUE + 1);start is 0.start is -128.start is 127.end is 8.end is 11.end is 12.The code does not compile.The code compiles but throws an exception at runtime.

      20 Which of the following statements about unary operators are true? (Choose all that apply.)Unary operators are always executed before any surrounding numeric binary or ternary operators.The - operator can be used to flip a boolean value.The pre-increment operator (++) returns the value of the variable before the increment is applied.The post-decrement operator (--) returns the value of the variable before the decrement is applied.The ! operator cannot be used on numeric values.None of the above

      21 What is the result of executing the following code snippet?int myFavoriteNumber = 8; int bird = ~myFavoriteNumber; int plane = -myFavoriteNumber; var superman = bird == plane ? 5 : 10; System.out.println(bird + "," + plane + "," + --superman);-7,-8,9-7,-8,10-8,-8,4-8,-8,5-9,-8,9-9,-8,10None of the above

       OCP EXAM OBJECTIVES COVERED IN THIS CHAPTER:

       Controlling Program FlowCreate program flow control constructs including if/else, switch statements and expressions, loops, and break and continue statements

       Utilizing Java Object-Oriented ApproachImplement polymorphism and differentiate object type versus reference type. Perform type casting, identify object types using instanceof operator and pattern matching

      Like many programming languages, Java is composed primarily of variables, operators, and statements put together in some logical order. In the last chapter, we covered how to create and manipulate variables. Writing software is about more than managing variables, though; it is about creating applications that can make intelligent decisions. In this chapter, we present the various decision-making statements available to you within the language. This knowledge will allow you to build complex functions and class structures that you'll see throughout this book.

      Java operators allow you to create a lot of complex expressions, but they're limited in the manner in which they can control program flow. Imagine you want a method to be executed only under certain conditions that cannot be evaluated until runtime. For example, on rainy days, a zoo should remind patrons to bring an umbrella, or on a snowy day, the zoo might need to close. The software doesn't change, but the behavior of the software should, depending on the inputs supplied in the moment. In this section, we discuss decision-making statements including if and else, along with the new pattern matching feature.

      Statements and Blocks

      As you may recall from Chapter 1, “Building Blocks,” a Java statement is a complete unit of execution in Java, terminated with a semicolon (;). In this chapter, we introduce you to various Java control flow statements. Control flow statements break up the flow of execution by using decision-making, looping, and branching,

Скачать книгу