4 - Java OCA 8 - Operators

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Given: public class Cowboys { public static void main(String[] args) { int x = 12; int a = 5; int b = 7; System.out.println (x/a + " " + x/b) ; }} What is the result? (Choose all that apply.) A. 2 1 B. 2 2 C. 3 1 D. 3 2 E. An exception is thrown at runtime

A is correct. The program will compile and print: 2 1 ===Code Step Through=== 1: public class Cowboys { 2: public static void main(String[] args) { 3: int x = 12; 4: int a = 5; 5: int b = 7; 6: System.out.println (x/a + " " + x/b) ; 7: }} On the line 3, the value 12 is assigned to the primitive into variable 'x'. Next, on the line 4, the value 5 is assigned to the primitive into variable 'a'. Next, on the line 5, the value 7 is assigned to the primitive into variable 'b'. ========2======== 1: public class Cowboys { 2: public static void main(String[] args) { 3: int x = 12; 4: int a = 5; 5: int b = 7; 6: System.out.println (x / a + " " + x/b) ; 7: }} On the line 6, based on the rules of precedence, the sub-expression 'x/a' is executed first as outlined below: x / a +" " + x/b --↓--- 12 / 5-- --↓--- -2.4--- --↓---- --2--- Note: In the above calculation, the value (2.4) is cast into a primitive 'int' and rounded down to 2. When dividing primitive type int values, remainders are always rounded down. Following the execution of the above, the expression, on the line 6, will be in the current state: "2 " + x/b ========3======== 1: public class Cowboys { 2: public static void main(String[] args) { 3: int x = 12; 4: int a = 5; 5: int b = 7; 6: System.out.println (x/a + " " + x / b) ; 7: }} On the line 6, the next aspect of the expression to be executed is 'x/a' and this is outlined below: "2 " + x / b -------↓--- -----12 / 7-- -------↓--- -----1.714-- -------↓--- -------1--- Note: In the above calculation, the output value (1.714) is cast into an int type and rounded down to 1. The string object "2 " and the value 1 are appended together, resulting in final output: "2 1"

Given: public class Hind { int id; Wind(int i) { id = i; } public static void main(String[] args) { new Wind(3).go(); // commented line } void go() { Wind wl = new Wind(1); Wind w2 = new Wind(2); System.out.println(w1.id + " "+ w2.id); }} When execution reaches the commented line, which are true? (Choose all that apply.) A The output contains 1 B The output contains 2 C The output contains 3 D Zero Wind objects are eligible for garbage collection E One Wind object is eligible for garbage collection F Two Wind objects are eligible for garbage collection G Three Wind objects are eligible for garbage collection

A, B, and G are correct. A The output contains 1. B The output contains 2. G Three Wind objects are eligible for garbage collection. ===Code Step Through=== 1: public class Hind { 2: int id; 3: Wind(int i ) {id = i ; } 4: public static void main(String[] args) { 5: new Wind(3).go(); 6: // commented line 7: } 8: void go() { 9: Wind w1 = new Wind(1); 10: Wind w2 = new Wind(2); 11: System.out.println(w1.id + " "+ w2.id); 12: }} On the line 5, a Wind object is created and the digit 3 is passed into the constructor of the subject. On the line 3, the digit 3 which was passed from the line 5, enters the Wind constructor and is stored in the instance variable id on the line 2. A pseudocode depiction of the above Wind object in memory is outlined below with an arbitrary heap address &221: Object Wind(Heap Address &221) { id -> 3 } Note: The above object does not have a reference to it. ========2======== 1: public class Hind { 2: int id; 3: Wind(int i) { id = i; } 4: public static void main(String[] args) { 5: new Wind(3).go(); 6: // commented line 7: } 8: void go() { 9: Wind w1 = new Wind(1); 10: Wind w2 = new Wind(2); 11: System.out.println(w1.id + " "+ w2.id); 12: }} On the line 5, the go() method is called from the Wind object instance with Heap Address &221. ========3======== 1: public class Hind { 2: int id; 3: Wind(int i) { id = i; } 4: public static void main(String[] args) { 5: new Wind(3).go(); 6: // commented line 7: } 8: void go() { 9: Wind w1 = new Wind(1 ); 10: Wind w2 = new Wind(2 ); 11: System.out.println(w1.id + " "+ w2.id); 12: } } On the line 8, the go() method is entered. On the the line 9, a new Wind object instance is created with reference w1 and with the digit 1 passed into its constructor and stored in instance variable id. On the the line 10, another new Wind object instance is created with reference w2 and with the digit 2 passed into its constructor and stored in instance variable id. A pseudocode depiction of the above Wind objects created in memory are outlined below: w1 -> Object Wind(Heap Address @762) { id -> 1 } w2 -> Object Wind(Heap Address &231) { id -> 2 } Current Objects Stored In Memory: Object Wind(Heap Address &221) { id -> 3 } w1 -> Object Wind(Heap Address @762) { id -> 1 } w2 -> Object Wind(Heap Address &231) { id -> 2 } ========4======== 1: public class Hind { 2: int id; 3: Wind(int i) { id = i; } 4: public static void main(String[] args) { 5: new Wind(3).go(); 6: // commented line 7: } 8: void go() { 9: Wind w1 = new Wind(1 ); 10: Wind w2 = new Wind(2 ); 11: System.out.println(w1.id + " "+ w2.id); 12: } } On the line 11, the contents stored in the instance variable id of the the Wind object w1 and w2 are printed to the screen i.e. 1 and 2 get printed to the screen, which shows that that answers A and B are correct. Current Objects Stored In Memory: Object Wind(Heap Address &221) { id -> 3 } w1 -> Object Wind(Heap Address @762) { id -> 1 } w2 -> Object Wind(Heap Address &231) { id -> 2 } ========5======== 1: public class Hind { 2: int id; 3: Wind(int i) { id = i; } 4: public static void main(String[] args) { 5: new Wind(3).go(); 6: // commented line 7: } 8: void go() { 9: Wind w1 = new Wind(1 ); 10: Wind w2 = new Wind(2 ); 11: System.out.println(w1.id + " "+ w2.id); 12: } } Program flow control jumps to the end of the go() method on the line 12 and back into the main method to the line 6. On the line 6, none of the Wind objects created above (with memory addresses &221, @762 and &231) will not be used again so therefore these three Wind objects will be available for garbage collection. This shows that G is correct. Final Answer: A The output contains 1. B The output contains 2. G Three Wind objects are eligible for garbage collection.

Given: public class A{ public static void main(String[] args) { int mask = 0; int count = 0; if( ((5<7) || ( ++count < 10)) | mask++ < 10 ) mask =mask+1; if( (6 > 8) ^ false) mask=mask+10; if( !(mask > 1) && ++count > 1) mask=mask+100; Line 10: System.out.println(mask + " " + count); } } Which two are true about the value of mask and the value of count at line 10? (Choose two.) A. mask is 0 B. mask is 1 C. mask is 2 D. mask is 10 E. mask is greater than 10 F. count is 0 G. count is greater than 0

C and F are correct C. mask is 2 F. count is 0 ===Code Step Through=== 1: public class A { 2: public static void main(String[] args) { 3: int mask = 0; 4: int count = 0; 5: if( ((5<7) || ( ++count < 10)) | mask++ < 10 ) 6: mask =mask+1; 7: if( (6 > 8) ^ false) 8: mask=mask+10; 9: if( !(mask > 1) && ++count > 1) 10: mask=mask+100; 11: System.out.println(mask + " " + count); 12: } 13: } On the line 3, the int variable named mask is assigned 0. On the line 4, the int variable named count is assigned 0. ========2======== 1: public class A { 2: public static void main(String[] args) { 3: int mask = 0; 4: int count = 0; 5: if( ((5 < 7) || ( ++count < 10)) | mask++ < 10 ) 6: mask =mask+1; 7: if( (6 > 8) ^ false) 8: mask=mask+10; 9: if( !(mask > 1) && ++count > 1) 10: mask=mask+100; 11: System.out.println(mask + " " + count); 12: } 13: } On the line 5, the value 5 is checked to see if it is less than the value 7. As 5 is less than 7 then this part of the if statement is true: if( ((true) || ( ++count < 10)) | mask++ < 10 ) .... ========3======== 1: public class A { 2: public static void main(String[] args) { 3: int mask = 0; 4: int count = 0; 5: if( ((5<7) || ( ++count < 10)) | mask++ < 10 ) 6: mask =mask+1; 7: if( (6 > 8) ^ false) 8: mask=mask+10; 9: if( !(mask > 1) && ++count > 1) 10: mask=mask+100; 11: System.out.println(mask + " " + count); 12: } 13: } Continuing on the line 5, as the left-hand side of the operator is true (i.e. 5<7), the right-hand side of the logical OR ' || ' operator ( ++count < 10) is not executed and count is therefore not incremented. This is because of logical OR '||' short-circuiting. It is important to memorize the logical and bitwise operator tables. These are outlined in the note section at the end of this answer. On the line 5, the current state of the if block would currently look like: if( (true) || ( <block skipped>)) | mask++ < 10 ) ================ Current values stored in memory: mask = 0 count = 0 ========4======== 1: public class A { 2: public static void main(String[] args) { 3: int mask = 0; 4: int count = 0; 5: if( ((5<7) || ( ++count < 10)) | mask++ < 10 ) 6: mask =mask+1; 7: if( (6 > 8) ^ false) 8: mask=mask+10; 9: if( !(mask > 1) && ++count > 1) 10: mask=mask+100; 11: System.out.println(mask + " " + count); 12: } 13: } The current state of the if block looks like: if( (true) || ( <block skipped>)) | mask++ < 10 ) The left-hand side and right-hand side of the bitwise OR ( | ) are checked. On the right-hand side of the bitwise OR operator, the variable mask is incremented after checking if the current value stored in the mask variable is less than 10 (i.e. 0<10). The state of the if block would then look like: if( (true) || ( <block skipped>)) | true ) 🠇 if( (true) | true ). As a consequence, the mask variable gets incremented by 1 using the postfix operator '++', the if block is then entered on the line 8 . ================ Current values stored in memory: mask = 1 count = 0 ========5======== 1: public class A { 2: public static void main(String[] args) { 3: int mask = 0; 4: int count = 0; 5: if( ((5<7) || ( ++count < 10)) | mask++ < 10 ) 6: mask = mask + 1; 7: if( (6 > 8) ^ false) 8: mask=mask+10; 9: if( !(mask > 1) && ++count > 1) 10: mask=mask+100; 11: System.out.println(mask + " " + count); 12: } 13: } On the line 6, the variable mask is incremented by 1. This means the variable mask now contains the value 2. ================ Current values stored in memory: mask = 2 count = 0 ========6======== 1: public class A { 2: public static void main(String[] args) { 3: int mask = 0; 4: int count = 0; 5: if( ((5<7) || ( ++count < 10)) | mask++ < 10 ) 6: mask =mask+1; 7: if( ( 6 > 8 ) ^ false ) 8: mask=mask+10; 9: if( !(mask > 1) && ++count > 1) 10: mask=mask+100; 11: System.out.println(mask + " " + count); 12: } 13: } On the line 7, the left-hand side of the logical XOR ' ^ ' operator is executed first. As the number 6 is not greater than 8, the left-hand side of the operator is false. The if block state is currently: if( (false) ^ false). This results in an overall value of false resulting in this if block getting skipped. See note below on logical XOR operator table ================ Current values stored in memory: mask = 2 count = 0 ========7======== 1: public class A { 2: public static void main(String[] args) { 3: int mask = 0; 4: int count = 0; 5: if( ((5<7) || ( ++count < 10)) | mask++ < 10 ) 6: mask =mask+1; 7: if( (6 > 8) ^ false) 8: mask=mask+10; 9: if( ! ( mask > 1 ) && ++count > 1) 10: mask=mask+100; 11: System.out.println(mask + " " + count); 12: } 13: } On line 9, the current value stored in the variable 'mask' is 2. As the variable 2 is greater than 1, the result is true. The the 'not' ( ! ) operator is applied to the left hand side of the && operator. This results in the true becoming false. The current state of the if block is: if( ! ( mask > 1 ) && ++count > 1) --------🠇------- if (! ( true ) && ++count>1) -------🠇------ if ( false && ++count>1) As the left hand side of the logical AND '&&' operator is false , the right hand side of the && operator does not get checked because of short circuiting of the Logical AND ' && ' operator. Therefore, this if block gets skipped (i.e. line 10 gets skipped) ================ Current values stored in memory: mask = 2 count = 0 ========8======== 1: public class A { 2: public static void main(String[] args) { 3: int mask = 0; 4: int count = 0; 5: if( ((5<7) || ( ++count < 10)) | mask++ < 10 ) 6: mask =mask+1; 7: if( (6 > 8) ^ false) 8: mask=mask+10; 9: if( !(mask > 1) && ++count > 1) 10: mask=mask+100; 11: System.out.println(mask + " " + count); 12: } 13: } On the line 11, the contents of the variable 'mask ' and 'count' are appended to the empty string and output to the screen. Therfore Output: 2 0 C. mask is 2 F. count is 0 Note: Logical Truth Table Understanding logical truth tables would be an imperative aspect of the course. These type of questions pop up very regularly. You will need to understand and memorize this table for the exam (Where 'T' is true and 'F' is false): You will need to memorize this table for the exam: A B---A&&B---- A||B T T -----T----------T T F -----F---------T F T-----F----------T F F-----F----------F Short-Circuit - Boolean Operators One of the aspects that you will also need to know for the exam is the short-circuit evaluation of boolean operators. This short-circuiting for the '&&' and '||' operators are explained below: Logical '&&' Short-Circuit: Let's look at the highlighted aspects of the table which outlines the logical '&&' short-circuit: A B---A&&B T T ------T T F ------F F T------F F F------F Looking at the table above, if A is false (F), then the operator '&&' does not check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to false (F). Logical '||' Short-Circuit: Likewise, looking at the highlighted aspects of the table which outlines the logical '||' short-circuit: A B-----A||B T T ------T T F ------T F T ------T F F ------F Likewise, looking at the table above, if A is false (T), then the operator '||' doesn't check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to true (T). Note: Bitwise operators like & | ^ are not on the exam Java 6 Java 7 Java 8

Given: 4: public class SpecialOps { 5: public static void main(String[] args) { 6: String s = ""; 7: boolean bl = true; 8: boolean b2 = false; 9: if((b2 = false) | (21%5) > 2) s += "x"; 10: if(bl || (b2 = true)) s += "y"; 11: if(b2 == true) s += "z"; 12: System.out.println(s); 13: } 14: } Which are true? (Choose all that apply.) A. Compilation fails B. x will be included in the output C. y will be included in the output D. z will be included in the output E. An exception is thrown at runtime

C is correct. The literal value 'y' will be included in the output. ===Code Step Through=== 4: public class SpecialOps { 5: public static void main(String[] args) { 6: String s = ""; 7: boolean b1 = true; 8: boolean b2 = false; 9: if((b2 = false) | (21%5) > 2) s += "x"; 10: if(b1 || (b2 = true)) s += "y"; 11: if(b2 == true) s += "z"; 12: System.out.println(s); 13: } 14: } On the line 6, an empty string is assigned to the String variable s . On the line 7, the boolean variable b1 is assigned the value true. On the line 8, the boolean variable b2 is assigned the value false. ================ Current values stored in memory: s = "" b1 = true b2 = false ========2======== 4: public class SpecialOps { 5: public static void main(String[] args) { 6: String s = ""; 7: boolean b1 = true; 8: boolean b2 = false; 9: if((b2 = false) | (21%5) > 2) s += "x"; 10: if(b1 || (b2 = true)) s += "y"; 11: if(b2 == true) s += "z"; 12: System.out.println(s); 13: } 14: } On the line 9, the boolean variable b2 is reassigned false. After that assignment, the if block looks like: if( false | (21%5) s += "x"; ================ Current values stored in memory: s = "" b1 = true b2 = false ========3======== 4: public class SpecialOps { 5: public static void main(String[] args) { 6: String s = ""; 7: boolean b1 = true; 8: boolean b2 = false; 9: if((b2 = false) | (21%5) > 2) s += "x"; 10: if(b1 || (b2 = true)) s += "y"; 11: if(b2 == true) s += "z"; 12: System.out.println(s); 13: } 14: } Continuing on the line 9, in the 'if block' statement, there is a bitwise OR ' | ' operator. This checks both the left-hand side and right hand side of the OR ' | ' operator. It is important for this question to revise Logical and Bitwise operators which are outlined in the note section at the end of the answer. ========4======== 4: public class SpecialOps { 5: public static void main(String[] args) { 6: String s = ""; 7: boolean b1 = true; 8: boolean b2 = false; 9: if((b2 = false) | (21 % 5) > 2) s += "x"; 10: if(b1 || (b2 = true)) s += "y"; 11: if(b2 == true) s += "z"; 12: System.out.println(s); 13: } 14: } On the line 9, the modulus operator ' % ' returns the remainder of two numbers divided together. In this case, the number 21 is divided by 5 and the resultant remainder is 1. After the modulus gets executed, the if block looks like: if( false | 1 > 2 ) As 1 is not greater than 2 , the resultant if block looks like: if( false | false) As false | false = false, the overall result of the if block is false on the line 9 and therefore said if block is skipped. ================ Current values stored in memory: s = "" b1 = true b2 = false ========5======== 4: public class SpecialOps { 5: public static void main(String[] args) { 6: String s = ""; 7: boolean b1 = true; 8: boolean b2 = false; 9: if((b2 = false) | (21%5) > 2) s += "x"; 10: if( b1 || (b2 = true)) s += "y"; 11: if(b2 == true) s += "z"; 12: System.out.println(s); 13: } 14: } On the line 10, as b1 currently contains true, the current state of the if block looks like: if( true || (b2 = true)) s += "y"; As the left hand side of the logical OR (' || ') operator is true, then as a result of short-circuiting, compiler skips the right hand side of the '||' operator (i.e. b2 is not assigned variable true). This results in the if block getting executed. Inside the if block, on the line 10, the string s is appended with the value "y" using the compound assignment operator '+=' ================ Current values stored in memory: s = "y" b1 = true b2 = false ========6======== 4: public class SpecialOps { 5: public static void main(String[] args) { 6: String s = ""; 7: boolean b1 = true; 8: boolean b2 = false; 9: if((b2 = false) | (21%5) > 2) s += "x"; 10: if(b1 || (b2 = true)) s += "y"; 11: if( b2 == true ) s += "z"; 12: System.out.println(s); 13: } 14: } On the line 11, the contents of the variable b2 is checked to see if it equals true. As b2 does not equal true, this if block is skipped. ================ Current values stored in memory: s = "y" b1 = true b2 = false ========7======== 4: public class SpecialOps { 5: public static void main(String[] args) { 6: String s = ""; 7: boolean b1 = true; 8: boolean b2 = false; 9: if((b2 = false) | (21%5) > 2) s += "x"; 10: if(b1 || (b2 = true)) s += "y"; 11: if(b2 == true) s += "z"; 12: System.out.println( s ); 13: } 14: } On the line 12, the contents of the string s is output to the screen i.e. "y" Note: Logical Truth Table Understanding logical truth tables would be an imperative aspect of the course. These type of questions pop up very regularly. You will need to understand and memorize this table for the exam (Where 'T' is true and 'F' is false): You will need to memorize this table for the exam: A B---A&&B---- A||B T T -----T----------T T F -----F---------T F T-----F----------T F F-----F----------F Short-Circuit - Boolean Operators One of the aspects that you will also need to know for the exam is the short-circuit evaluation of boolean operators. This short-circuiting for the '&&' and '||' operators are explained below: Logical '&&' Short-Circuit: Let's look at the highlighted aspects of the table which outlines the logical '&&' short-circuit: A B---A&&B T T ------T T F ------F F T------F F F------F Looking at the table above, if A is false (F), then the operator '&&' does not check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to false (F). Logical '||' Short-Circuit: Likewise, looking at the highlighted aspects of the table which outlines the logical '||' short-circuit: A B-----A||B T T ------T T F ------T F T ------T F F ------F Likewise, looking at the table above, if A is false (T), then the operator '||' doesn't check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to true (T). Note: Bitwise operators like & | ^ are not on the exam Java 6 Java 7 Java 8

Given: public class Dog { String name; Dog(String s) { name = s; } public static void main(String[] args) { Dog d1 = new Dog('Boi"); Dog d2 = new Dog('Tyri"); System.out.print((d1 == d2) + " "); Dog d3 = new Dog('Boi") ; d2 = d1; System.out.print((d1 == d2) + " "); System.out.print((d1 == d3) + " "); }} What is the result? A. true true true B. true true false C. false true false D. false true true E. false false false F. An exception will be thrown at runtime

C is correct. The program outtput will be: false true false The '==' operator tests for reference variable equality not object equality. ===Code Step Through=== 1: public class Dog { 2: String name; 3: Dog(String s) { name = s; } 4: public static void main(String[] args) { 5: Dog d1 = new Dog("Boi"); 6: Dog d2 = new Dog("Tyri"); 7: System.out.print((d1 == d2) + " "); 8: Dog d3 = new Dog('Boi") ; 9: d2 = d1; 10: System.out.print((d1 == d2) + " "); 11: System.out.print((d1 == d3) + " "); }} On the line 5, inside the main() method, a new object is created of type Dog. The string "Boi" is passed into the constructor of the object and is stored in the instance variable 'name' on the line 2. A pseudocode depiction of the above Dog object stored in heap memory with unique arbitrary (&823) heap address is outlined below: d1 🠚 Object Dog(Heap Address &823) { name 🠢 "Boi" } ========2======== 1: public class Dog { 2: String name; 3: Dog(String s) { name = s; } 4: public static void main(String[] args) { 5: Dog d1 = new Dog("Boi"); 6: Dog d2 = new Dog("Tyri"); 7: System.out.print((d1 == d2) + " "); 8: Dog d3 = new Dog("Boi") ; 9: d2 = d1; 10: System.out.print((d1 == d2) + " "); 11: System.out.print((d1 == d3) + " "); }} On the line 6, a new object is created of type Dog. The string "Tyri" is passed into the constructor of the object and is stored in the instance variable 'name' on the line 2. A pseudocode depiction of the above Dog object stored in heap memory with unique arbitrary heap address (@536) is outlined below: d2 🠚 Object Dog(Heap Address @536) { name 🠢 "Tyri" } ========3======== A pseudocode depiction of all the Dog objects created in memory so far are outlined below: d1 🠚 Object Dog(Heap Address &823) { name🠢 "Boi" } d2 🠚 Object Dog(Heap Address @536) { name🠢 "Tyri" } ========4======== 1: public class Dog { 2: String name; 3: Dog(String s) { name = s; } 4: public static void main(String[] args) { 5: Dog d1 = new Dog("Boi"); 6: Dog d2 = new Dog("Tyri"); 7: System.out.print((d1 == d2) + " "); 8: Dog d3 = new Dog("Boi") ; 9: d2 = d1; 10: System.out.print((d1 == d2) + " "); 11: System.out.print((d1 == d3) + " "); }} On the line 7, the memory address of an object instance Dog that is pointed to by the reference d1 is compared to see if it is the same address of the object that is pointed to by the reference d2. As the memory address of the object instance pointed to by d1 is &823 and the memory address of the object instance pointed to by d2 is @536 then the memory addresses are not equal, therefore, the output is false. ========5======== 1: public class Dog { 2: String name; 3: Dog(String s) { name = s; } 4: public static void main(String[] args) { 5: Dog d1 = new Dog("Boi"); 6: Dog d2 = new Dog("Tyri"); 7: System.out.print((d1 == d2) + " "); 8: Dog d3 = new Dog("Boi"); 9: d2 = d1; 10: System.out.print((d1 == d2) + " "); 11: System.out.print((d1 == d3) + " "); }} On the line 8, a new object is created of type Dog. The string "Boi" is passed into the constructor of the object and is stored in the instance variable 'name' on the line 2. A pseudocode depiction of the above Dog object stored in heap memory with unique arbitrary heap address (&324) is outlined below: d3 🠚 Object Dog(Heap Address &324) { name🠢 "Boi" } ========6======== A pseudocode depiction of all the Dog objects created in memory after the line 8 is executed are outlined below: d1 🠚 Object Dog(Heap Address &823) { name🠢 "Boi" } d2 🠚 Object Dog(Heap Address @536) { name🠢 "Tyri" } d3 🠚 Object Dog(Heap Address &324) { name🠢 "Boi" } ========7======== 1: public class Dog { 2: String name; 3: Dog(String s) { name = s; } 4: public static void main(String[] args) { 5: Dog d1 = new Dog("Boi"); 6: Dog d2 = new Dog("Tyri"); 7: System.out.print((d1 == d2) + " "); 8: Dog d3 = new Dog("Boi") ; 9: d2 = d1; 10: System.out.print((d1 == d2) + " "); 11: System.out.print((d1 == d3) + " "); }} On the line 9, the object reference d2 is reassigned to the same object that is pointed to by the reference d1. There is a null reference (represented by the character 'Ø') to the object with memory address @536. A pseudocode depiction of all the Dog objects created in memory after the line 9 is executed are outlined below: d2 🠚 d1 🠚 Object Dog(Heap Address &823) { name🠢 "Boi" } Ø 🠚Object Dog(Heap Address @536) { name🠢 "Tyri" } d3 🠚 Object Dog(Heap Address &324) { name🠢 "Boi" } Note: In the above pseudocode, you can see that there is no longer a reference to the object with heap memory address @536. This means that the object is available for garbage collection. Also note that there are now two object references pointing to the object with address &823 ========8======== 1: public class Dog { 2: String name; 3: Dog(String s) { name = s; } 4: public static void main(String[] args) { 5: Dog d1 = new Dog("Boi"); 6: Dog d2 = new Dog("Tyri"); 7: System.out.print((d1 == d2) + " "); 8: Dog d3 = new Dog("Boi") ; 9: d2 = d1; 10: System.out.print((d1 == d2) + " "); 11: System.out.print((d1 == d3) + " "); }} On the line 10, the object reference d1 is compared with the object reference d2 to see if they are pointing to the same Dog object with unique address. Looking at the pseudocode description above, you can see that both d1 and d2 are pointing to the same object reference and therefore the output is true. ========9======== A pseudocode depiction of all the Dog objects created in memory this far are outlined below: d2 🠚 d1 🠚 Object Dog(Heap Address &823) { name🠢 "Boi" } Ø 🠚Object Dog(Heap Address @536) { name🠢 "Tyri" } d3 🠚 Object Dog(Heap Address &324) { name🠢 "Boi" } ========10======== 1: public class Dog { 2: String name; 3: Dog(String s) { name = s; } 4: public static void main(String[] args) { 5: Dog d1 = new Dog("Boi"); 6: Dog d2 = new Dog("Tyri"); 7: System.out.print((d1 == d2) + " "); 8: Dog d3 = new Dog("Boi"); 9: d2 = d1; 10: System.out.print((d1 == d2) + " "); 11: System.out.print((d1 == d3) + " "); }} On the line 11, the object reference d1 is compared with the object reference d3 to see if they are pointing to the same Dog object with unique address. Looking at the pseudocode description above, you can see that d1 is pointing to an object with memory address &823 and d3 is pointing to an object with memory address &324. As the memory addresses of the objects are not equal, the output is false. Final output: false true false

Given: interface Vessel { } interface Toy { } class Boat implements Vessel { } class Speedboat extends Boat implements Toy { } public class Tree { public static void main(String[] args) { String s = "0"; Boat b = new Boat(); Boat b2 = new Speedboat(); Speedboat s2 = new Speedboat(); if((b instanceof Vessel) && (b2 instanceof Toy)) s += "1"; if((s2 instanceof Vessel) && (s2 instanceof Toy)) s += "2"; System.out.printIn(s); }} What is the result? A. 0 B. 01 C. 02 D. 012 E. Compilation fails F. An exception is thrown at runtime

D is correct. The program will compile and print: "012" For this question, you need to understand the concept of interface implementation and class extension. It would also be highly advisable do a quick sketch of the classes, interfaces and their relationships. After this, sketch out 'associations' among the classes/interfaces. Doing a class sketch for this example will yield the following: [ Vessel ] 🠅 implements 🠅 [ Boat ] 🠇 extends 🠇 [ Speedboat ] 🠇 implements 🠇 [ Toy ] The above is explained as follows: The class Boat implements the interface Vessel. The class Speedboat extends the class Boat and implements the interface Toy. Note: A class can extend only one other class and but implement multiple interfaces. ===Code Step Through=== 1: interface Vessel { } interface Toy { } 2: class Boat implements Vessel { } 3: class Speedboat extends Boat implements Toy { } public class Tree { 4: public static void main(String[] args) { 5: String s = "0"; 6: Boat b = new Boat(); 7: Boat b2 = new Speedboat(); 8: Speedboat s2 = new Speedboat(); 9: if((b instanceof Vessel) && (b2 instanceof Toy)) 10: s += "1"; 11: if((s2 instanceof Vessel) && (s2 instanceof Toy)) 12: s += "2"; 13: System.out.printIn(s); 14: } 15: } On line 1, the interface Vessel and Toy are declared on the same line. This is perfectly fine. On line 5, the String object s is assigned an empty string. On line, 6, a Boat instance object is created with a reference named 'b'. On line 7, another reference of type Boat named 'b2' is assigned to an object of type Speedboat. This is fine as Speedboat is a subtype of Boat. On line 8, a Speedboat instance object is created with a reference named 's2'. ================ Current value stored in memory: s = "0" ========2======== 1: interface Vessel { } interface Toy { } 2: class Boat implements Vessel { } 3: class Speedboat extends Boat implements Toy { } public class Tree { 4: public static void main(String[] args) { 5: String s = "0"; 6: Boat b = new Boat(); 7: Boat b2 = new Speedboat(); 8: Speedboat s2 = new Speedboat(); 9: if((b instanceof Vessel ) && (b2 instanceof Toy)) 10: s += "1"; 11: if((s2 instanceof Vessel) && (s2 instanceof Toy)) 12: s += "2"; 13: System.out.printIn(s); 14: } 15: } On line 9, in the if block, the left-hand side of the '&&' checks to see if reference Boat b is pointing to an object that is an instance of interface Vessel [ Vessel ] ⇽ [ Boat ]⟵ [ Speedboat ]⇾ [ 𝑇𝑜𝑦 ] Looking at the class diagram sketch above, class Boat implements interface Vessel. Therefore, class Boat is an instance of Vessel. Therefore, the left-hand side of the '&&' is true. ========3======== 1: interface Vessel { } interface Toy { } 2: class Boat implements Vessel { } 3: class Speedboat extends Boat implements Toy { } public class Tree { 4: public static void main(String[] args) { 5: String s = "0"; 6: Boat b = new Boat(); 7: Boat b2 = new Speedboat(); 8: Speedboat s2 = new Speedboat(); 9: if((b instanceof Vessel) && (b2 instanceof Toy)) 10: s+= "1"; 11: if((s2 instanceof Vessel) && (s2 instanceof Toy)) 12: s += "2"; 13: System.out.printIn(s); 14: } 15: } On line 9, in the if block, on the right hand side of the '&&' the compiler checks to see if reference Boat b2 is pointing to an object that is an instance of interface Toy. The compiler checks to see is the Speedbooat object an instance of Toy the interface? [ 𝑉𝑒𝑠𝑠𝑒𝑙 ] ⇽ [ Boat ]⟵ [ Speedboat ]⇾ [ Toy ] Looking at the class diagram sketch above, class Speedboat implements interface Toy. Therefore, class Speedboat is an instance of Toy. As Speedboat implements interface Toy then the right hand side of the '&&' operand is true. As both sides of the '&&' are true, then the overall if block is true and the if block is entered. On line 10, a string containing "1" is appended to the string 's' ================ Current value stored in memory: s = "01" ========4======== 1: interface Vessel { } interface Toy { } 2: class Boat implements Vessel { } 3: class Speedboat extends Boat implements Toy { } public class Tree { 4: public static void main(String[] args) { 5: String s = "0"; 6: Boat b = new Boat(); 7: Boat b2 = new Speedboat(); 8: Speedboat s2 = new Speedboat(); 9: if((b instanceof Vessel) && (b2 instanceof Toy)) 10: s += "1"; 11: if((s2 instanceof Vessel ) && (s2 instanceof Toy)) 12: s += "2"; 13: System.out.printIn(s); 14: } 15: } On line 11, in the if block, on the left hand side of the '&&', the compiler checks to see if reference Speedbooat 's2' is pointing to an object that is an instance of interface Vessel. [ Vessel ] ⇽ [ Boat ]⟵ [ Speedboat ]⇾ [ Toy ] Looking at the class diagram sketch above, class Speedboat extends class Boat and class Boat implements interface Vessel. Therefore, class Speedboat is of type Vessel . In other words, Speedboat is an instance of interface Vessel. Therefore, the left-hand side of the '&&' operator is true. ========5======== 1: interface Vessel { } interface Toy { } 2: class Boat implements Vessel { } 3: class Speedboat extends Boat implements Toy { } public class Tree { 4: public static void main(String[] args) { 5: String s = "0"; 6: Boat b = new Boat(); 7: Boat b2 = new Speedboat(); 8: Speedboat s2 = new Speedboat(); 9: if((b instanceof Vessel) && (b2 instanceof Toy)) 10: s += "1"; 11: if((s2 instanceof Vessel) && (s2 instanceof Toy )) 12: s += "2"; 13: System.out.printIn(s); 14: } 15: } On line 11, in the if block, on the right hand side of the '&&', the compiler checks to see if reference Speedboat 's2' is pointing to an object that is an instance of interface Toy [ 𝑉𝑒𝑠𝑠𝑒𝑙 ] ⇽ [ Boat ]⟵ [ Speedboat ]⇾ [ Toy ] Looking at the class diagram sketch above, class Speedboat implements interface Toy. Therefore, class Speedboat is an instance of Toy. Therefore, the right hand side of the '&&' is true. As the left-hand side and right hand side of the operand '&&' are true, therefore the if block is true and as a consequence, the if block is entered. On line 12, a String containing "2" is appended to the string 's' ================ Current value stored in memory: s = "012" ========6======== 1: interface Vessel { } interface Toy { } 2: class Boat implements Vessel { } 3: class Speedboat extends Boat implements Toy { } public class Tree { 4: public static void main(String[] args) { 5: String s = "0"; 6: Boat b = new Boat(); 7: Boat b2 = new Speedboat(); 8: Speedboat s2 = new Speedboat(); 9: if((b instanceof Vessel) && (b2 instanceof Toy)) 10: s += "1"; 11: if((s2 instanceof Vessel) && (s2 instanceof Toy)) 12: s += "2"; 13: System.out.printIn(s); 14: } 15: } On line 13, the string s is printed to the screen: "012" Note 1: Java keyword 'instanceof' can look up through multiple levels of an inheritance tree. Also, remember that 'instanceof' is commonly used before attempting a downcast. So in this case, after line 13, it would be possible to state: Speedboat s3 = (Speedboat ) b2; Note 2: Logical Truth Table Understanding logical truth tables would be an imperative aspect of the course. These type of questions pop up very regularly. You will need to understand and memorize this table for the exam (Where 'T' is true and 'F' is false): You will need to memorize this table for the exam: A B---A&&B---- A||B T T -----T----------T T F -----F---------T F T-----F----------T F F-----F----------F Short-Circuit - Boolean Operators One of the aspects that you will also need to know for the exam is the short-circuit evaluation of boolean operators. This short-circuiting for the '&&' and '||' operators are explained below: Logical '&&' Short-Circuit: Let's look at the highlighted aspects of the table which outlines the logical '&&' short-circuit: A B---A&&B T T ------T T F ------F F T------F F F------F Looking at the table above, if A is false (F), then the operator '&&' does not check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to false (F). Logical '||' Short-Circuit: Likewise, looking at the highlighted aspects of the table which outlines the logical '||' short-circuit: A B-----A||B T T ------T T F ------T F T ------T F F ------F Likewise, looking at the table above, if A is false (T), then the operator '||' doesn't check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to true (T). Note: Bitwise operators like & | ^ are not on the exam Java 6 Java 7 Java 8

Given: class Hexy { public static void main(String[] args) { int i = 42; String s = (i<40)?"life": (i>50)?"universe":"everything"; System.out.println(s); }} What is the result? A. null B. life C. universe D. everything E. Compilation fails F. An exception is thrown at runtime

D is correct. The program will compile in print: "everything" This is a ternary operator nested in another ternary operator. Both ternary expressions are false. ===Code Step Through=== 1: class Hexy { 2: public static void main(String[] args) { 3: int i = 42; 4: String s = 5: (i<40)?"life":(i>50)?"universe":"everything"; 6: System.out.println(s); 7: }} On the line 3, the variable i is assigned the value 42. ========2======== 1: class Hexy { 2: public static void main(String[] args) { 3: int i = 42; 4: String s = 5: ( i <40) ? "life" : (i>50)?"universe" : "everything"; 6: System.out.println(s); 7: }} On the line 5, the value stored in variable i is compared to see if it is less than the value 40. As the current value of variable i is 42 and 42 is not less than 40, the right hand side of the ternary operator is executed i.e. (i>50)?"universe" : "everything" ========3======== 1: class Hexy { 2: public static void main(String[] args) { 3: int i = 42; 4: String s = 5: (i<40)?"life":( i >50) ? "universe" : "everything"; 6: System.out.println(s); 7: }} On the line 5, as stated above, the right-hand side of the ternary operator is executed. The value stored in variable i is compared to see if is greater than the value 50. As current value of variable i is 42 and 42 is not greater than 50, the right hand side of the ternary operator is executed. Therefore, the string "everything" is assigned to the string 's'. ========4======== 1: class Hexy { 2: public static void main(String[] args) { 3: int i = 42; 4: String s = 5: (i<40)?"life":(i>50)?"universe":"everything"; 6: System.out.println(s); 7: }} On the line 6, the current value stored within the string s is printed to the screen: "everything"

Which of the following will not compile? (Choose all that apply) a) System.out.println('a'=='0u0003'); b) System.out.println('a'=='\u0003'); c) System.out.println('a'=='a'); d) System.out.println('a'=='b'); e) System.out.println(5!=6); f) System.out.println(5.0==5.0L); g) System.out.println(5.0==5L);

a) and f) will not compile. ➤ a) System.out.println('a'=='0u0003'); In the above, the value '0u0003' is of incorrect syntax for a Unicode character type, therefore a compiler error occurs. Note: A char literal is represented by a single character in single quotes: char a = 'a'; char b = 'b'; You can also use the Unicode value of a character, using the Unicode notation of prefixing the value with \u as follows: char letterN = '\u004E' ➤ f) System.out.println(5.0==5.0L); Likewise, in the above, the value 5.0L is of incorrect syntax for a double literal, therefore a compiler error occurs. Note: The correct syntax for a double literal is either 5.0 or 5L and not 5.0L The Following Will Compile and Run Fine: ➤ b) System.out.println('a' == '\u0003'); In the above, the character value 'a' and Unicode character value '\u0003' are of the correct syntax and can be compared. The code compiles and runs fine and the resultant output is false. ➤ c) System.out.println('a' == 'a'); In the above, the value 'a' is of the correct syntax for a character literal, therefore the code compiles and runs fine. The resultant output is true. ➤ d) System.out.println('a' == 'b'); Comparing the above two chars outputs false. ➤ e) System.out.println(5 != 6); The above compares two int literals to check if they are not equal. As this is the case, the output is true. ➤ g) System.out.println(5.0 == 5L); The above compares two double literals to check if they are not equal. As this is the case, the output is true.

Given: short s = 3.0; int i = 3; System.out.println(i==s); What is the result? (Choose all that apply) a) compiler error b) Runtime error c) compile and print false d) none of the above

a) is correct. The program will give a compiler error short s = 3.0; int i = 3; System.out.println(i==s); Change the value to short s = 3 and it will compile and print true.

Given: public class Testing { public static void main(String[] args) { Integer i = new Integer(3); Short s = new Short("3"); System.out.println(i==s); }} What is the result? (Choose all that apply) a) compiler error b) Runtime error c) compile and print true d) none of the above

a) is correct. The program will give a compiler error. public class Testing { public static void main(String[] args) { Integer i = new Integer(3); Short s = new Short("3"); System.out.println(i==s); }} If two objects are wrapped types that are of different type and unrelated, the program would give a compiler error Integer and Short are wrapped types that are not the same type and are unrelated. The compiler knows this and gives a compiler error. Solution: Use equals() method to compare the contents of objects instead of '==' which compares addresses of objects. e.g. public class Testing { public static void main(String[] args) { Integer i = new Integer(3); Short s = new Short("3"); System.out.println(i .equals(s)); }} The above will return false. public class Testing { public static void main(String[] args) { Short s1 = new Short("3"); Short s2 = new Short("3"); System.out.println(s1.equals(s2)); }} The above will return true the two objects are of the same type and contain the same content i.e. "3"

Which of the following are not valid operators? (Choose all that apply) a) << b) <<< c) >>> d) instanceof e) equals

b) and e) are correct. b) '<<<' is not a valid operator. d) equals() is a method, not an operator

When an object has no more references to it first time, what two stages occur after this? a) Stage 1: The objects final() method gets called implicitly and this saves the object from garbage collection Stage 2: The second time that the object is about to be garbage collected, the object's final() method does not get called and the object is ready for garbage collection. b) Stage 1: The objects finalize() method gets called implicitly and this saves the object from garbage collection Stage 2: The second time that the object is about to be garbage collected, the object's finalize() method does not get called and the object is ready for garbage collection. c) Stage 1: The objects finalize() method gets called explicitly and this saves the object from garbage collection Stage 2: The second time that the object is about to be garbage collected, the object's finalize() method does not get called and the object is ready for garbage collection. d) Stage 1: The objects finally() method gets called implicitly and this saves the object from garbage collection Stage 2: The second time that the object is about to be garbage collected, the object's finally() method does not get called and the object is ready for garbage collection.ages occur after this?

b) is correct. Stage 1: The objects finalize() method gets called implicitly and this saves the object from garbage collection Stage 2: The second time that the object is about to be garbage collected, the object's finalize() method does not get called and the object is ready for garbage collection. Note: The finalize() method is inherited from the class Object. It is it is advisable not to override the finalize() method

Given: System.out.println(3+100/10*2-13); What is the result when the above line of code is executed? a) compiler error b) 10 c) 10.1010101010101 d) none of the above

b) is correct. The program will compile and print: 10 In order to undertake this question, it would be necessary to know the Rules for Operator Order of Precedence. The note at the end of the answer provides a detailed explanation of the Rules for Operator Order of Precedence. Lets goes through the execution of the expression: 3+100/10*2-13 According to the rules of the operator order of precedence, multiplication and division are executed first. 3+(100/10*2)-13 ------↓--------- --(100/10*2)--- Multiplication and division have equal precedence, therefore, according to the rules of operator precedence, the expression is executed from left to right (i.e. division executed first) followed by multiplication. This results in the value '10', as outlined below: 3+(100/10*2)-13 -----↓---------- (100/10*2) -----↓---------- --100/10------- -----↓---------- ----10---------- Incorporating the above result '10', into the current expression we get: 3+(10*2)-13 -----↓------ 3+20-13 From looking at the above expression, the next operations to be executed are addition and subtraction. As addition and subtraction have equal precedence, the expression is executed from left to right with addition executed first: 3+20-13 -↓------ 23 This results in the expression finally resolving to 10 as outlined below, which is the correct answer: 23-13 ----↓--- ---10-- Note: Operator Order of Precedence This question tests your knowledge on the Operator Order of Precedence (Rules of Precedence). This is very important to learn for the exam. Expressions, Operators and Operands: Firstly, it's important to explain expressions, operators and operands. Below is an example of an expression. 1 + 2 + "3" Expressions are comprised of operands(e.g. numbers and/or objects). Below is an outline of operands in an expression i.e. 1,3 and the String object "3": 1 + 2 + "3" Expressions are also comprised of operators (e.g. +, -, %, /, etc). Below is an outline of the operators in the question: 1 + 2 + "3" The Problem: Where does an expression (equation) start when executed? On the left-hand side, right hand side or somewhere else in the equation? Java has well-defined rules for solving the above problem. Solution The Operator Order of Precedence Rules solves this. Below outlines 16 rules in order of precedence. The rules are as follows where: Rule 1 has the highest priority operator in the expression (equation) execute first. Rule 2 has the second highest priority operator in an expression executed next and so forth. Below also outlines the 16 rules in relation to associativity. Associativity: Associativity is important when an expression has more than one operator of the same kind (e.g. 3 - 2 - 1 where '-' is the minus symbol). Do we start with subtracting 2 from 3 or do we start with subtracting 1 from 2 first? The order of precedence provides the answer to this also. It would be advisable to memorise the operator order of precedence with the following suggested 16 rule abbreviation (write this down in the exams as a reference): B-PUC-MAS-REBBLLTA Where: B = Brackets P = Postfix i++ etc. Operator Order of Precedence: 1 Brackets e.g. () [] method(); (Associativity: Starting left to right) 2 Postfix e.g. i++, i-- (Associativity: Starting left to right) 3 Unary e.g. ++i, --j (Associativity: Starting right to left) 4 Cast or object creation e.g. (int), newObject() (Associativity: Starting right to left) 5 Multiplication, Division, mod % e.g. *, /, % (Associativity: Starting left to right) 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) 7 Shift e.g. >>, <<, >>> (Associativity: Starting left to right) 8 Relational e.g. <, <=, >, >= instanceof (Associativity: Starting left to right) 9 Equality e.g. ==, != (Associativity: Starting left to right) 10 Bitwise AND, & (Associativity: Starting left to right) 11 Bitwise XOR ^ (Associativity: Starting left to right) 12 Bitwise OR | (Associativity: Starting left to right) 13 Logical AND && (Associativity: Starting left to right) 14 Logical OR || (Associativity: Starting left to right) 15 Tiernary e.g. (boolean)?varA:varB; (Associativity: Starting right to left) 16 Assignment e.g. = =, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=, >>>= (Associativity: Starting right to left) It would also be prudent to memorise for the exam the following associativity rules which executes right to left instead of the majority left-to-right as outlined above: U-CAST-TA 3 Unary e.g. ++i, --j (Associativity: Starting right to left) 4 Cast or object creation e.g. (int), newObject() (Associativity: Starting right to left) 15 Tiernary e.g. (boolean)?varA:varB; (Associativity: Starting right to left) 16 Assignment e.g. = =, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=, >>>= (Associativity: Starting right to left) The Operator Order of Precedence can be explained as with the following example: System.out.println(1+2+"3"); 1 + 2 + "3" is an expression with two operators that are the same (i.e. +) Rule 6 applies to the above expression: 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) with associativity starting from left to right 1+2+"3" 3+"3" After that, the expression is comprised of the digit 3 and the String object containing "3" outlined above. According to the rules of precedence again: 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) Therefore the digit 3 gets appended to the string object "3". This results in the final output: "33"

Given: System.out.println(1+2+"3"); What is the result? (Choose all that apply) a) compiler error b) compile and print 33 c) compile and print 6 d) none of the above

b) is correct. The program will compile and print: 33 System.out.println(1+2+"3"); Based on the Rules of Precedence, the execution of the '+' operator starts on the left hand side of the expression and moves to the right (goes from left to right): 1+2+"3" ↓ 3+"3" ↓ "33" Operator Order of Precedence This question tests your knowledge on the Operator Order of Precedence (Rules of Precedence). This is very important to learn for the exam. Expressions, Operators and Operands: Firstly, it's important to explain expressions, operators and operands. Below is an example of an expression. 1 + 2 + "3" Expressions are comprised of operands(e.g. numbers and/or objects). Below is an outline of operands in an expression i.e. 1,3 and the String object "3": 1 + 2 + "3" Expressions are also comprised of operators (e.g. +, -, %, /, etc). Below is an outline of the operators in the question: 1 + 2 + "3" The Problem: Where does an expression (equation) start when executed? On the left-hand side, right hand side or somewhere else in the equation? Java has well-defined rules for solving the above problem. Solution The Operator Order of Precedence Rules solves this. Below outlines 16 rules in order of precedence. The rules are as follows where: Rule 1 has the highest priority operator in the expression (equation) execute first. Rule 2 has the second highest priority operator in an expression executed next and so forth. Below also outlines the 16 rules in relation to associativity. Associativity: Associativity is important when an expression has more than one operator of the same kind (e.g. 3 - 2 - 1 where '-' is the minus symbol). Do we start with subtracting 2 from 3 or do we start with subtracting 1 from 2 first? The order of precedence provides the answer to this also. It would be advisable to memorise the operator order of precedence with the following suggested 16 rule abbreviation (write this down in the exams as a reference): B-PUC-MAS-REBBLLTA Where: B = Brackets P = Postfix i++ etc. Operator Order of Precedence: 1 Brackets e.g. () [] method(); (Associativity: Starting left to right) 2 Postfix e.g. i++, i-- (Associativity: Starting left to right) 3 Unary e.g. ++i, --j (Associativity: Starting right to left) 4 Cast or object creation e.g. (int), newObject() (Associativity: Starting right to left) 5 Multiplication, Division, mod % e.g. *, /, % (Associativity: Starting left to right) 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) 7 Shift e.g. >>, <<, >>> (Associativity: Starting left to right) 8 Relational e.g. <, <=, >, >= instanceof (Associativity: Starting left to right) 9 Equality e.g. ==, != (Associativity: Starting left to right) 10 Bitwise AND, & (Associativity: Starting left to right) 11 Bitwise XOR ^ (Associativity: Starting left to right) 12 Bitwise OR | (Associativity: Starting left to right) 13 Logical AND && (Associativity: Starting left to right) 14 Logical OR || (Associativity: Starting left to right) 15 Tiernary e.g. (boolean)?varA:varB; (Associativity: Starting right to left) 16 Assignment e.g. = =, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=, >>>= (Associativity: Starting right to left) It would also be prudent to memorise for the exam the following associativity rules which executes right to left instead of the majority left-to-right as outlined above: U-CAST-TA 3 Unary e.g. ++i, --j (Associativity: Starting right to left) 4 Cast or object creation e.g. (int), newObject() (Associativity: Starting right to left) 15 Tiernary e.g. (boolean)?varA:varB; (Associativity: Starting right to left) 16 Assignment e.g. = =, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=, >>>= (Associativity: Starting right to left) The Operator Order of Precedence can be explained as with the following example: System.out.println(1+2+"3"); 1 + 2 + "3" is an expression with two operators that are the same (i.e. +) Rule 6 applies to the above expression: 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) with associativity starting from left to right 1+2+"3" 3+"3" After that, the expression is comprised of the digit 3 and the String object containing "3" outlined above. According to the rules of precedence again: 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) Therefore the digit 3 gets appended to the string object "3". This results in the final output: "33"

Given: 1: public static void m(int n) { 2: String s; 3: System.out.println(s); 4: if(n>0) 5 s="true"; 6: else 7: s="false"; 8: } What line will cause the compiler error: (Choose all that apply) a) 1 b) 3 c) 4 d) none of the above

b) is correct. The program will give a compiler error on line 3 1: public static void m(int n) { 2: String s; 3: System.out.println(s); 4: if(n>0) 5 s="true"; 6: else 7: s="false"; 8: } You must initialize local variables otherwise compiler error. Note: If line 3 was removed then it would not give compiler error as the variable s is not used.

Given: 1: class A { 4: public static void main(String[] args) { 5: boolean happy = true; 6: System.out.println(happy?"I'm happy":777); 7: System.out.println(happy?new Integer(12):777); 8: System.out.println(happy?4+2:777); 9: }} What is the result? (Choose all that apply) a) compiler error line 7 b) compiler error line 6 c) compile and print fine d) none of the above

c) is correct. The code will compile and print: I'm happy 12 6 ===Code Step Through=== 1: class A { 4: public static void main(String[] args) { 5: boolean happy = true; 6: System.out.println(happy ? "I'm happy" : 777); 7: System.out.println(happy?new Integer(12):777); 8: System.out.println(happy?4+2:777); 9: }} On the line 6, the variable happy contains true therefore "I'm happy" is printed to the screen. If the variable happy contatined false, the value printed to the screen would be 777. Note: On the lines 6, 7, 8 displays the ternary operator (? : ). The syntax is as follows: boolean result = BooleanValue? ValueA : ValueB Where result is a boolean type which stores a either true or false. If BooleanValue is true then ValueA is assigned to result variable else ValueB is assigned to result variable. ========2======== 1: class A { 4: public static void main(String[] args) { 5: boolean happy = true; 6: System.out.println(happy?"I'm happy":777); 7: System.out.println(happy ? new Integer(12) : 777); 8: System.out.println(happy?4+2:777); 9: }} On the line 7, the variable happy contains true therefore new Integer(12) gets auto unboxed and the value 12 gets printed to the screen. ========3======== 1: class A { 4: public static void main(String[] args) { 5: boolean happy = true; 6: System.out.println(happy?"I'm happy":777); 7: System.out.println(happy?new Integer(12):777); 8: System.out.println(happy ? 4+2 : 777); 9: }} On the line 8, the variable happy contains true, therefore the exrpession 4+2 gets executed and the resultant value 6 gets printed to the screen. Final Output: I'm happy 12 6

Given: public class Inty { public static void main(String[] args) { Integer i = new Integer(3); Integer f = new Integer(3); System.out.println(i==f); }} What is the result? (Choose all that apply) a) compiler error b) Runtime error c) compile and print false d) none of the above

c) is correct. The program will compile and print: false public class Inty { public static void main(String[] args) { Integer i = new Integer(3); Integer f = new Integer(3); System.out.println(i==f); }} The equality operator '==' compares memory addresses of objects and returns false as the addresses are different. Each object instance creates a unique address in memory of that particular object.

Given: boolean a = false; boolean b = false; boolean c = false; boolean bool = (a=true)||(b=true)&&(c=true); System.out.println(a+" "+b+" "+c); What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print true true true d) none of the above

d is correct. The answer will be: 'none of the above'. It will print: true false false For this question, it is imperative that you understand and have memorized the Logical Truth Tables for the operators '&&' and '||'. See the notes at the end of the question which explains this in fine detail. Let's step to the code: ===Code Step Through=== 1: public class A { 2: public static void main(String[] args) { 3: boolean a = false; 4: boolean b = false; 5: boolean c = false; 6: boolean bool = (a=true) || (b=true) && (c=true); 7: System.out.println(a+" "+b+" "+c); 8: }} On the lines 3, 4 and 5, the boolean variables 'a', 'b', and 'c' are initialized false. ========2======== 1: public class A { 2: public static void main(String[] args) { 3: boolean a = false; 4: boolean b = false; 5: boolean c = false; 6: boolean bool = (a=true) || (b=true) && (c=true); 7: System.out.println(a+" "+b+" "+c); 8: }} Next, on the line 6, the value 'a' is assigned to 'true' The current state of the expression would look like: (true) || (b=true) && (c=true); ========3======== 1: public class A { 2: public static void main(String[] args) { 3: boolean a = false; 4: boolean b = false; 5: boolean c = false; 6: boolean bool = (a=true) || (b=true) && (c=true); 7: System.out.println(a+" "+b+" "+c); 8: }} For the next aspect of the expression to execute, we need to reference the Logical Truth Table (Short-Circuit): Logical '||' Short-Circuit: The following table outlines the logical '||' short-circuit: A B-----A||B T T ------T T F ------T F T ------T F F ------F Looking at the table above, if A is false (T), then the operator '||' doesn't check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to true (T). Looking at the current state of the expression on the line 6, we can see that the left-hand side of the '||' operator is true: (true) || (b=true) && (c=true) Therefore, the variables 'b' and 'c' on the right hand side of the '||' operator are NOT assigned to true, they remain false as initialized on line 4 and 5. Therefore the output on the line 7 is: System.out.println(true+" "+false+" "+false); Note 1: Logical Truth Table Understanding logical truth tables would be an imperative aspect of the course. These type of questions pop up very regularly. You will need to understand and memorize this table for the exam (Where 'T' is true and 'F' is false): You will need to memorize this table for the exam: A B---A&&B---- A||B T T -----T----------T T F -----F---------T F T-----F----------T F F-----F----------F Short-Circuit - Boolean Operators One of the aspects that you will also need to know for the exam is the short-circuit evaluation of boolean operators. This short-circuiting for the '&&' and '||' operators are explained below: Logical '&&' Short-Circuit: Let's look at the highlighted aspects of the table which outlines the logical '&&' short-circuit: A B---A&&B T T ------T T F ------F F T------F F F------F Looking at the table above, if A is false (F), then the operator '&&' does not check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to false (F). Logical '||' Short-Circuit: Likewise, looking at the highlighted aspects of the table which outlines the logical '||' short-circuit: A B-----A||B T T ------T T F ------T F T ------T F F ------F Likewise, looking at the table above, if A is false (T), then the operator '||' doesn't check the value of B to see whether or not it is true (T) or false (F), it simply short-circuits to the result to true (T). Note: Bitwise operators like & | ^ are not on the exam Java 6 Java 7 Java 8

Given: int x = 21; x %= 4; System.out.println(x); What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print 4 d) none of the above

d) is correct. None of the answers are correct. The program will compile and print: 1 This question tests your knowledge of the modulus compound assignment operator '%='. Compound assignment operators are more efficient. The expression would execute as follows: int x = 21; x %= 4; ---↓--- 21÷4 --↓---- 20 and 1 remainder --------↓--------- ----Modulus--- The above number 1 result is the remainder which is also the modulus.

Given: public class Ship { public static void main(String [] args) { short s = 3; Integer i = new Integer(3); System.out.println(i==s); }} What is the result? (Choose all that apply) a) compiler error b) Runtime error c) compile and print false d) none of the above

d) is correct. None of the answers are correct. The program will compile in print: true. public class Ship{ public static void main(String [] args) { short s = 3; Integer i = new Integer(3); System.out.println(i==s); }} Any two primitives can be compared with the equality operator '==' Examples of primitive types would be: byte short int long float double boolean char Any primitive type and wrapped type can be compared with the equality operator '==' Examples of Wrapped types would be: Byte Short Integer Long Float Double Boolean Character If two objects are wrapped types and are incompatible /unrelated then, then using '==' to compare them will give a compiler error. Boolean b = new Boolean("true"); Integer i = new Integer(3); System.out.println((b == i )); The above will give a compiler error as the wrapped class Boolean is incompatible to the wrapped class Integer. ================ Short s = new Short("3"); Integer i = new Integer(3); System.out.println((s == i )); The above will give a compiler error as the wrapped class Short is unrelated to the wrapped class Integer. ================ Integer i1 = new Integer("3"); Integer i2 = new Integer(3); System.out.println((i1 == i2)); The above will run fine and the output would be false. This is because the '==' compares the addresses of objects to see if they are equal and not the contents of the objects. As Integer references 'i1' and 'i2' are referring to separate and distinct objects that have different addresses in memory, then the result is false. ================ Integer i1 = new Integer("3"); Integer i2 = new Integer(3); System.out.println(i1.equals(i2)); The above would run fine and the output would be true. This is because the 'equals()' method compares the contents of objects to see if they are equal. If two objects are wrapped types then using equals() to compare them would compile fine: Short s = new Short("3"); Integer i = new Integer(3); System.out.println(i .equals( s )); The above prints: true Boolean b = new Boolean ("true"); Integer i = new Integer(3); System.out.println(b .equals( i )); The above prints: false

Given: public class Bx { public static void main(String [] args) { int k = 2; boolean b = (k>3); System.out.println(b); } } What is the result? (Choose all that apply) a) compiler error b) Runtime error c) compile and print true d) none of the above

d) is correct. The program will compile and print false. 1: public class Bx { 2: public static void main(String [] args) { 3: int k = 2; 4: boolean b = (k>3); 5: System.out.println(b); 6: } 7: } On the line in 4, if the value stored within the variable k is greater than 3 then the value stored in the boolean b would be true. Alternatively, the value stored in the boolean variable b would be false. As the contents of the variable k contains 2 which is not greater than 3, boolean value is false is a stored in variable b. On the line 5, false is printed to the screen.


Set pelajaran terkait

wordly wise 3000 book 7 lesson 12

View Set

Chapter 2 - The Story of Abraham Lincoln Part 1

View Set

Chapter 21 - Industrial Revolution

View Set

Cardiac Medications Comprehensive

View Set

Food Safety and Nutrition: Chapter 5-9

View Set

Real Estate Principles Chapter 9

View Set