1 - Java 8 OCP - Declarations, Access Control and Enums

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following are Java keywords: a) assert b) goto c) throw d) throws

All of the answers are Java keywords. a) assert b) goto c) throw d) throws Note: You will get asked questions to test your Java keyword knowledge. ➤ assert allows testing the correctness of any assumptions that have been made in the program. For example, the following code snippet will output false: int value = 15; assert value >= 20 : " Underweight"; System.out.println("value is "+value); ➤ goto is reserved as a keyword but is not used in Java. It's reserved just in case it maybe added to a later version of Java. ➤ throw is used to explicitly throw an exception from a method or any block of code. e.g. throw new Exception("Exception"); ➤ throws is used to declare an exception. It gives information to the programmer that there may occur an exception in the code block. For example, in the following code example, the method 'm()' declares that it throws an IOException: void m() throws IOException { throw new IOException("device error"); }

Would the following be the correct inheritance access modifier table? True or false? class | package | subclass | world private _Yes_ | __No_ | _No_ | _No_ default _Yes_ | _Yes_ | _No_ | _No_ protected Yes | _Yes_ | _No_ | _Yes_ public _Yes | _ Yes_ | _Yes_ | _ Yes_ No: Where the subclass is in a different package, otherwise Yes

False. Inheritance Access Control: The inheritance access modifier table is essential in helping to understand access control. This is the fundamental of aspect of object-oriented programming. The need to know rule of the contents of classes i.e. instance variables/methods. e.g. A private method or instance variable can be accessed only within the class where it was created. A default method or instance variable declared in a class can be accessed within its own class or within other classes of the same packageand so forth. The following table would be best memorized and quickly written out on paper during the exam: class | package | subclass | world private _Yes_ | _No_ | _No_ | _No_ default _Yes_ | _Yes_ | _No_ | _No_ protected Yes | _Yes_ | _Yes_ | _No_ public _Yes | _ Yes_ | _Yes_ | _ Yes_ No: Where the subclass is in a different package, otherwise Yes Interpreting the above table would be as follows: The column containing private, default, protected or public would represent the level of access for a method or instance variable of a class. The rows containing class, package, subclassand world would represent where a particular method or instance variable can be used/called (depending on its access modifier of being private, default, protected or public). e.g. A method/instance variable that is private can be used in a class but cannot be used in a package, subclass or the world (the world being any other class attempting to access that particular method/instance variable in the running program). On the other extreme, a method/instance variable that is public can be used in a class, package, subclass or the world (the world being any other class attempting to access that particular method/instance variable). A method/instance variable that is publicwould be contrary to the Principles of Object Orientated Programming as any class could change the said public method or instance variable thereby significantly increasing risks of code having bugs and making code almost impossible to maintain.

Which of these will compile: a) byte b1= 127; b) byte b2 = 128; c) byte b3 = (byte) 128; d) byte b4 = 4; byte b5 = b4+4;

a) and c) are correct. The following will compile: a) byte b1= 127; The maximum number (integer) that can be stored by a byte is 127. ================ c) byte b3 = (byte) 128; The above compiles ok as int 128 is casted to byte. Incorrect Answers: d) byte b4 = 4; byte b5 = b4 +4; The above will not compile without casting i.e. byte b5 = (byte) (b4+4); ================== b) byte b2 = 128; The above will give a compiler error as value of 'b2' is greater than 127. ================= Note: The Rules for Primitive Type Conversions are an essential aspect of the exam and will pop up regularly in questions. Primitive type conversion rules are comprised of: ➤Rules for Widening (not requiring casting) ➤Rules for Narrowing (requiring casting) In the case of Widening Type Conversion (i.e. not requiring casting), the following suggested abbreviation should be memorised for the exam: "b-silfd" where: b = byte, s = short, i = int, l = long, f = float, d = double With the above abbreviation, you can derive the following Widening Type Conversion "Tree" (it's best advised to memorise this for the exam as a reference) Widening Type Conversion "Tree" (not requiring casting): byte to short, int, long, float, double .................. ↲ short to int, long float, double .................. ↲ int to long float, double .............. ↲ long to float, double .................. ↲ float to double. In other words, when converting the above e.g. converting from byte to short, a cast is not needed. As an example: byte b1 = 13; short s1 = b1 ; System.out.println(s1); The above will output: 13 byte to short What this means is that a byte value can be assigned to a short type without casting. This is called Widening Type Conversion. =================== The opposite of the above is termed Narrowing Type Conversion In the case of Narrowing Type Conversion (i.e. requiring casting), the following suggested abbreviation should be memorised for the exam : "dbs-cilf" where: d = double, b =byte, s = short, c = char, i = int, l = long, f = float, With the above abbreviation, you can derive the following *Narrowing Type Conversion 'Tree'* (it's best advised to memorise this for the exam as a reference): Narrowing Type Conversion 'Tree' (requiring casting): double to byte, short, char, int, long, float ............................................................................. ↲ float to byte to short, char, int, long ................................................................ ↲ long to byte to short, char, int ........................................................ ↲ int to byte, short, char ................................... ↲ charto byte, short. .......................... ↲ short to byte or char. In other words, when converting the above, a cast is required. For instance converting from an int to byte type or a short to char type requires casting. As an example: byte b4 = 4; byte b5 = ( byte ) (b4+4); System.out.println(b5); When the addition operation is executed between b4 + 4, the result value (8) is of primitive type int This int value needs to be casted to byte to store byte value from the Narrowing Primitive Type 'tree': int to byte, short, char. By casting, the resultant output would be: 8

Given: public void do(){ System.out.println("Doing"); } The above code will give: a) compiler error b) runtime error c) compile and print Doing d) none of the above

a) is correct. It will give a compiler error public void do(){ System.out.println("Doing"); } 'do' is a keyword in java and cannot be used to declare a method

Given: class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(++z); }} The above code will give: a) compiler error b) runtime error c) compile and run fine d) none of the above

a) is correct. It would cause a compiler error final variable z cannot be modified within the method as it is final. ===Code Step Through=== class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(++z); }} An object of type A is created and method m() is called. Note: it is perfectly fine to have the literal reference of type A named 'a' as Java is case sensitive. In other words, 'A' is not the same as 'a' in Java. =======2======= class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(++z); }} m() gets called and inside its body, it assigns 8 to the int x. Then the method doMore(x) is called with the value of x (8) passed into its parameter. Note: 'this' refers to the current object instance of A which is calling the method doMore() i.e. A a = new A(); Meaning, the object instance which 'a' is referring to, is represented as 'this' =======3======= class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(++z); }} A copy of the value stored in the integer type 'x' (8) is passed to the doMore() method. This z value (containing 8) is then declared final which means that it cannot be changed inside the body of the doMore() method. =======4======= class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(++z); } } In the above, the final constant variable z has a prefix increment (++). This means that the value of z is attempted to be incremented. However, this z value is final (constant) and therefore cannot be changed in any way. This includes being incremented, therefore it gives a compiler error. In summary: final variable z cannot be modified within the method void doMore(final int z){ z++; } This will cause compiler error Solution: Remove the prefix increment i.e. class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(z); } } The above will then print 8. Alternatively, remove the final keyword which would then print 9.

What keyword prevents methods from being overwritten in a subclass and is often used to enforce the API functionality of a method? (Choose all that apply) a) 'final' b) 'static' and 'final' together c) 'static' d) none of the above

a) is correct. Keyword 'final'. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses.

The max number (integer) that can be stored in a byte is _____. a) 127 b) 128 c) -128 d) 264

a) is correct. The max number (integer) that can be stored in a byte is 127. All english letters and numbers which are greater than -128 and less than 127 e.g. Compiles fine: byte b1 = 127; Compiler Error: byte b1 = 128;

Given: class A { public static void main(String[] args) { try { throw new IOException(); } catch (Exception e) { System.out.println("Exception"); } catch (IOException e) { System.out.println("IOException"); }}} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and prints: IOException d) none of the above

a) is correct. The program gives a compiler error. class A { public static void main(String[] args) { try { throw new IOException(); } catch (Exception e) { System.out.println("Exception"); } catch (IOException e) { System.out.println("IOException"); }}} IOException is a subclass of Exception A catch for a subclass exception should occur before a catch for a superclass exception otherwise it will cause a compiler error Solution: class A { public static void main(String[] args) { try { throw new IOException(); } catch (IOException e) { System.out.println("IOException"); } catch (Exception e) { System.out.println("Exception"); }}} Will print IOException

Given: enum A { DOG("woof"),CAT("meow"),FISH("burble"); private A() { } A(String s){ sound =s; } String sound; } class B { static A a ; public static void main(String[] args){ System.out.println( a.DOG.sound+" "+a.FISH.sound); } } The above code will give: a) Compile and print: 'woof burble' b) Multiple compilation errors c) compilation fails due to an error on line 2 d) compilation fails due to an error on line 3 e) compilation fails due to an error on line 4 f) compilation fails due to an error on line 9

a) is correct. The program will compile and print: 'woof burble'. enums have constructors and variables. Let's step through the code: ===Code Step Through=== 1: enum A { 2: DOG("woof"),CAT("meow"),FISH("burble"); 3: private A() { 4: } 5: A(String s){ 6: sound =s; 7: } 8: String sound; 9: } 10: class B { 11: static A a ; 12: public static void main(String[] args){ 13: System.out.println( a.DOG.sound+" "+a.FISH.sound); 14: } } On the line 13, the enum variable DOG is called by calling the sound variable within the constructor which results in the string value getting returned: "woof" ========2======== 1: enum A { 2: DOG("woof"),CAT("meow"),FISH("burble"); 3: private A() { 4: } 5: A(String s){ 6: sound =s; 7: } 8: String sound; 9: } 10: class B { 11: static A a ; 12: public static void main(String[] args){ 13: System.out.println( a.DOG.sound+" "+a.FISH.sound); 14: } } On the line 13, the enum variable FISH is called by calling the sound variable within the constructor which results in the string value getting returned: "burble" Note: An enum constructor will never be explicitly called and will cause compiler error if it is called.

Given: class House { public void m1() throws Exception{ throw new Exception(); } public void m2() { m1(); } public static void main(String[] args) { House a = new House (); a.m2(); } } What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and run fine d) none of the above

a) is correct. The program will give a compiler error. ===Code Step Through=== 1: class House { 2: public void m1() throws Exception{ 3: throw new Exception(); 4: } 5: public void m2() { 6: m1(); 7: } 8: public static void main(String[] args) { 9: House a = new House (); 10: a.m2(); 11: }} On the line 10, the method m2() gets called in the main() method. ========2======== 1: class House { 2: public void m1() throws Exception{ 3: throw new Exception(); 4: } 5: public void m2() { 6: m1(); 7: } 8: public static void main(String[] args) { 9: House a = new House (); 10: a.m2(); 11: }} On the line 6, inside the m2() method, a call is made to method m1(). ========3======== 1: class House { 2: public void m1() throws Exception{ 3: throw new Exception(); 4: } 5: public void m2() { 6: m1(); 7: } 8: public static void main(String[] args) { 9: House a = new House (); 10: a.m2(); 11: }} On the line3, the method m1() throws an exception but the method (declared on line 5) m2() doesn't catch the checked exception or handle it. Therefore this causes a compiler error. Solution: Have m1() and main() declare that they throw an Exception: class House { public void m1() throws Exception{ throw new Exception(); } public void m2() throws Exception { m1(); } public static void main(String[] args) throws Exception { House a = new House(); a.m2(); }}

Given: package cert; class Beverage{} package cert.stuff; class Tea extends Beverage {} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and run fine d) none of the above

a) is correct. The program will not compile. package cert; class Beverage{} package cert.stuff; class Tea extends Beverage {} Tea won't compile because its superclass Beverage has default access and is in a different package. The solution: Put both classes in the same package or declare Beverage as public i.e. package cert; public class Beverage{} package cert.stuff; class Tea extends Beverage {} Inheritance Access Control: The inheritance access modifier table is essential in helping to understand access control. This is the fundamental of aspect of object-oriented programming. The need to know rule of the contents of classes i.e. instance variables/methods. e.g. A private method or instance variable can be accessed only within the class where it was created. A default method or instance variable declared in a class can be accessed within its own class or within other classes of the same packageand so forth. The following table would be best memorized and quickly written out on paper during the exam: class | package | subclass | world private _Yes_ | _No_ | _No_ | _No_ default _Yes_ | _Yes_ | _No_ | _No_ protected Yes | _Yes_ | _Yes_ | _No_ public _Yes | _ Yes_ | _Yes_ | _ Yes_ No: Where the subclass is in a different package, otherwise Yes Interpreting the above table would be as follows: The column containing private, default, protected or public would represent the level of access for a method or instance variable of a class. The rows containing class, package, subclassand world would represent where a particular method or instance variable can be used/called (depending on its access modifier of being private, default, protected or public). e.g. A method/instance variable that is private can be used in a class but cannot be used in a package, subclass or the world (the world being any other class attempting to access that particular method/instance variable in the running program). On the other extreme, a method/instance variable that is public can be used in a class, package, subclass or the world (the world being any other class attempting to access that particular method/instance variable). A method/instance variable that is publicwould be contrary to the Principles of Object Orientated Programming as any class could change the said public method or instance variable thereby significantly increasing risks of code having bugs and making code almost impossible to maintain.

Given: strictfp class A { public strictfp void m1(){} public void m2() { strictfp double d = 10.0; } } The above code will give: a) compiler error b) runtime error c) compile and run fine d) none of the above

a) is correct. ➤ strictfp can only be used to modify a: class or method ➤ strictfp cannot only be used to modify a variable: strictfp class A { public strictfp void m1(){} public void m2() { strictfp double d = 10.0; }} The above is attempting to make a variable (d) declared within a method, strictfp. This will give a compiler error.

Given: interface Gadget{ void doStuff(); } abstract class Tablet implements Gadget { void doStuff(); } public static void main(String[] args) { new Tablet().doStuff(); }} What is the result? (Choose all that apply) a) The class Tablet will not compile b) The interface Gadget will not compile c) The output will be "plug in show book" d) The abstract class Electronic will not compile e) The class tablet cannot both extend and implement

a) is correct. The class 'Tablet' will not compile. interface Gadget{ void doStuff(); } abstract class Tablet implements Gadget { void doStuff(); } public static void main(String[] args){ new Tablet().doStuff(); }} By default an interface's methods are implicitly public so the classes that implement its methods must be explicitly public otherwise accessor narrowing causes a compiler error ➤ Increasing Accessibility: private → default → protected → public ➤ Decreasing Accessibility: private ← default ← protected ← public The overriding method 'doStuff()' has decreased accessibility from public to default therefore a compiler error occurs. Note: if the method was protected or public it would compile and run fine.

Which of the following are Java keywords: a) transient b) char c) chars d) strictfp

a), b) and d) are Java keywords. The exam will test your knowledge of identifying Java keywords. The following are valid Java keywords: ➤ transient is a variables modifier used in serialization. At the time of serialization, if we don't want to save value of a particular variable in a file, then we use transient keyword. e.g. transient int user_password; ➤ char is a primitive data type tthat holds single characters e.g. char c = 'c'; ➤ strictfp is used for restricting floating-point calculations and ensuring same result on every platform while performing operations in the floating-point variable. For example the following method calculates the CPU speed on which the program runs: class CPUProcessor { strictfp void calculateCPUSpeed(){} } Note: c) gives a compiler error as chars is not a java keyword.

Given: 1: public class Electronic implements Device { 2: public void doIt(){} 3: } 4: abstract class Phone1 extends Electronic {} 5: abstract class Phone2 extends Electronic { 6: public void doIt(int x){}} 7: class Phone3 extends Electronic implements 8: Device { 9: public void doStuff(){}} 10: interface Device { public void doIt();} What is the result? choose all that apply a) compilation succeeds b) compilation fails with an error on line 1 c) compilation fails with an error on line 3 d) compilation fails with an error on line 5 e) compilation fails with an error on line 7 f) compilation fails with an error on line 9

a) is correct. The program compiles and runs fine. 1: public class Electronic implements Device{ 2: public void doIt(){} 3: } 4: abstract class Phone1 extends Electronic {} 5: abstract class Phone2 extends Electronic { 6: public void doIt(int x){} } 7: class Phone3 extends Electronic implements 8: Device{ 9: public void doStuff(){}} 10: interface Device { public void doIt(); } As Phone3 extends Electronic and as Electronic implements Device then Phone3 doesnt need to implement the interface method doIt() directly in its class Note: If class Phone3 implements Device { public void doStuff(){}} ..... Compilation fails UML Sketch It is advisable to do a quick UML sketch of the class names and their relationships. Looking at a simplified UML outline of the above we can see: [Electronic] ⇢ <Device> 🠅 [Phone1], [Phone2], [Phone3] where ⇢ is the UML symbol for implements and 🠅 is the UML symbol for extends.

Which of the following are false. Instance variables can be: a) abstract b) public c) transient d) default e) private f) volatile

a) is correct. ➤ Instance variables (member variables) cannot be abstract. ➤ Instance variables can be: private default protected public final transient volotile ➤ Instance variables cannot be: abstract synchronised strictfp native static Note: A class variable is a static variable A default instance variable is where there is no identifer before the instance variable and no literal default name e.g. class A{ //default instance variable// int i = 0; //class variable// static int j = 1; //compiler error// default int k = 2; } You do not explicitly write the word "default" before an instance variable in order to declare it default. To declare an instance variable default you simply put no modifier before the instance variable. E.g. int k= 2;

Which of the following will compile? (Choose all that apply) a) strictfp class A{ double n1 = 102; double calculate(){ return n1*3; } b) strictfp interface B { strictfp double compute(); } c) strictfp class C { public strictfp C(){ }} d) none of the above

a) will compile You use strictfp on class/methods but not constructors. Once strictfp is applied to a class it will be also applied to: ➤ Static and instant blocks ➤ All class variables ➤ All constructors a) strictfp class A{ double n1 = 102; double calculate(){ return n1*3; } The above ensures a floating-point is the same on all platforms e.g. used on distributed platform application like multiplayer game Force all precision floating -point calculations (float or double) to conform to IEEE 754 standard and not depend on hardware platform b) strictfp interface B { double compute(); } All methods are implicitly strictfp as the interface is strictfp Methods within a strictfp interface must not be explicitly declared strictfp which would give a Compiler error c) strictfp class C { public strictfp C(){ }} Constructors within a strictfp class are implicitly strictfp also, therefore it's not allowed to explicitly declare constructor strictfp which would give a Compiler error

Given: 1: enum BitCoinPrice { 2: HIGH(90), MEDIUM(50), LOW(20); 3: BitCoinPrice(int aPrice) { 4: this.______ = aPrice; 5: } 6: int getPrice() { 7: return thePrice; 8: } 9: private int thePrice; 10: } 11: class Client { 12: public static void main(String[] args){ 13: A price = ______.______ ; 14: int curPrice = price.______; 15: System.out.println(curPrice); 16: } } What code snippets, inserted above, will make the code run correctly a) Insert 'thePrice' on line 4 b) Insert 'getPrice()' on line 4 c) Insert 'A.HIGH' on line 13 d) Insert 'thePrice' on line 13 e) Insert 'thePrice' on line 14

a), b) and c) are correct. ➤ a) Insert 'thePrice' on line 4 ➤ b) Insert 'getPrice()' on line 4 ➤ c) Insert 'A.HIGH' on line 13 Below you can see the correct code which prints 90 to the screen: 1: enum BitCoinPrice { 2: HIGH(90), MEDIUM(50), LOW(20); 3: BitCoinPrice(int aPrice) { 4: this.thePrice = aPrice; 5: } 6: int getPrice() { 7: return thePrice; 8: } 9: private int thePrice; 10: } 11: class Client { 12: public static void main(String[] args){ 13: A price = A.HIGH; 14: int curPrice = price.getPrice(); 15: System.out.println(curPrice); 16: }} Characteristics of Enumerations: ➤ Enums are constants ➤ Enums allow one to restict a variable to having one of only a few predefined values ➤ Enums can help reduce bugs in code. ➤ You cannot declare an enum in a method ➤ Enum Personality can be placed in its own class called Personality.java ➤ Enum cannot be private or protected.

You cannot declare an enum in a: (Choose all that apply) a) static method b) class method c) main method d) Its own class

a), b) and c) are correct. You cannot declare an enum in a method of any kind i.e. ➤ static method ➤ class method ➤ main method e.g. 1: class A { 2: Integer i = 100; 3: void go() { 4: enum Personality {HAPPY, SAD, WORRIED} 5: } 6: static void go2() { 7: enum Personality {HAPPY, SAD, WORRIED} 8: } 9: public static void main(String[] args) { 10: enum Personality {HAPPY, SAD, WORRIED} 11: } } In the above code sample: On the line 4, an enumeration is declared within a class method which causes compiler error. On the line 7, an enumeration is declared within a static method which causes compiler error. On the line 10, an enumeration is declared within a main method which causes compiler error. Solution: Declare an enumeration within the class or within its own class. e.g. 1: class A { 2: Integer i = 100; 3: enum Personality {HAPPY, SAD, WORRIED} 4: void go() { 5: System.out.println(Personality.HAPPY); 6: } 7: static void go2() { 8: System.out.println(Personality.SAD); 9: } 10: public static void main(String[] args) { 11: A a = new A(); 12: a.go(); 13: go2(); 14: } } On the line 3, you can see the enumeration declared correctly within the class. You can also see, on the line 5, an enumeration call within the class method and also, on the line 8, an enumeration call within the static method.

Which of the following will compile: a) int _a; b) int $c; c) int 7g; d) int ________2_w;

a), b) and d) will compile. a) int _a; b) int $c; d) int ________2_w; ➤ Identifiers must start with a: Letter or Currency character (e.g. $, £,..) or Underscore (i.e. _ ) ➤ Identifiers cannot start with a digit c) int 7g; ➤ Wrapper class names can be used also as valid identifiers: double Double = 0; String String = "Hello"; ➤ The following are also illegal identifiers: int :b; int -d; int e#; int .f; int 7g;

Given: interface A{ 1: abstract static void m4(){;} 2: static int m1(){return 7;} 3: static void m5(); 4: public static void m2(){;} 5: final static void m3(){;} } Which lines of code would cause compiler errors? (choose all that apply): a) Line 1 b) Line 2 c) Line 3 d) Line 4 e) Line 5

a), c) and e) are correct. Line 1 , line 3 and line 5 will cause compiler errors. ===Code Step Through=== interface A{ 1: abstract static void m4(){;} 2: static int m1(){return 7;} 3: static void m5(); 4: public static void m2(){;} 5: final static void m3(){;} } On line 1, abstract is not allowed with a static method. An abstract method is defined only so that it can be overridden in a subclass. However, static methods cannot be overridden. Therefore, it is a compile-time error to have an abstract, static method. ========2======== interface A{ 1: abstract static void m4(){;} 2: static int m1(){return 7;} 3: static void m5(); 4: public static void m2(){;} 5: final static void m3(){;} } On line 3, as the method is static, it needs a method body ========3======== interface A{ 1: abstract static void m4(){;} 2: static int m1(){return 7;} 3: static void m5(); 4: public static void m2(){;} 5: final static void m3(){;} } The keyword final is not allowed. Declaring a method final static means that there can only ever be one distinct creation of that method. This indicates that the method cannot be overridden by subclasses. This defeats the whole purpose of an interface which enables the interfaces methods to be implemented by client classes.

Which lines of code below will compile: a) private enum Personality {HAPPY, SAD, WORRIED} b) public enum Personality {HAPPY, SAD, WORRIED} c) enum Personality {HAPPY, SAD, WORRIED} d) protected enum Personality {HAPPY, SAD, WORRIED}

b) and c) will compile. b) public enum Personality{ HAPPY, SAD, WORRIED } c) enum Personality{ HAPPY, SAD, WORRIED} The Following Will Give Compiler Errors: a) private enum Personality {HAPPY, SAD, WORRIED} d) protected enum Personality {HAPPY, SAD, WORRIED} Note: Just a few characteristics to point out in terms of enumerations: ➤ Enums are constants ➤ Enums allow one to restict a variable to having one of only a few predefined values ➤ Enums can help reduce bugs in code. ➤ You cannot declare an enum in a method ➤ Enum Personality can be placed in its own class called Personality.java ➤ Enum cannot be private or protected.

Which of the following are true: An interface can be declared: a) private interface A{} b) interface A{} c) protected interface A{} d) public interface A{}

b) and d) are correct An interface can be declared: b) interface A{} i.e. default d) public interface A{} ================ ➤ An interface cannot be declared: • private or • protected e.g. a) private interface A{} c) protected interface A{}

Which of the following will not compile: a) int $a=0; b) int e# = 2; c) int £a=3; d) int :a = 3;

b) and d) are correct. The following will not compile: b) int e*#* = 2; d) int *:*a = 3; In order to undertake this question, it's important to know the rules for declaring identifiers. Rules for Declaring Ientifiers: ➤ Each identifier must have at least one character. ➤ Identifiers must start with either: letter or currency character (e.g. $, £ etc) or underscore (i.e. _ ) ➤ The rest of the characters (besides the first) can be from: letter or currency character (e.g. $, £ etc) or underscore (i.e. _ ) or digit

Which of the following will not compile: a) public void a(int i, int int... j){} b) public void b(int... i , int int j){} c) public void c(int i... ){} d) public void d(int... i , int... j){}

b) and d) will not compile b) public void b(int... i , int int j){} A vararg parameter must always be the last one. In the above it is the 1st parameter. ================ d) public void d(int... i , int... j){} A method can only have one vararg. In the above there are two varag parameters Note: A vararg parameter of type T is the same as an array of type T.

Consider the following where 'x' is a package and 'SM' is a class and 'foo()' is a static method with 'SM'. You wish to call foo() in your code. Which of the following, inserted independtly, will compile to import 'foo()' static method into your code? (Choose all that apply) a) import static x.*; b) import static x.SM.*; c) import static x.foo(); d) import static x.foo; e) import static x.SM.foo; f) import static x.SM;

b) and e) are correct syntax ➤ b) import static x.SM.*; import all static methods and members ================ ➤ e) import static x.SM.foo; import the static method Note: A static import declaration has two forms: One that imports a particular static member (which is known as single static import) One that imports all static members of a class (which is known as static import on demand). The following syntax imports a particular static member: import static packageName.ClassName.staticMemberName; where packageName is the package of the class (e.g., java.lang), ClassName is the name of the class (e.g., Math) staticMemberName is the name of the static field or method (e.g., PI or abs). The following syntax imports all static members of a class: import static packageName.ClassName.*; where packageName is the package of the class (e.g., java.lang) and ClassName is the name of the class (e.g., Math). The asterisk (* ) indicates that all static members of the specified class should be available for use in the class(es) declared in the file.

Given: public class Rocket { private void blastOff() { System.out.println("bang"); } } public class Shuttle extends Rocket{ public static void main(String[] args){ new Shuttle ().go(); } public void go() { blastOff(); // Rocket.blastOff(); } private void blastOff() { System.out.println("sh-bang"); } } Which are true? Choose all that apply: a) As the code stands, the output is bang b) As the code stands, the output is sh-bang c) As the code stands compilation fails d) if line containing Rocket.blastOff(); is uncommented, output is bang bang e) if line containing Rocket.blastOff(); is uncommented, the output is sh-bang bang f) if line containing Rocket.blastOff(); is uncommented, compilation fails.

b) and f) are true. ➤ b) As the code stands, the output is: sh-bang ➤ f) If the line containing Rocket.blastOff(); is uncommented, compilation fails. ================= b) As the code stands, the output is sh-bang public class Rocket{ private void blastOff(){ System.out.println("bang"); } } public class Shuttle extends Rocket{ public static void main(String[] args){ new Shuttle().go(); } public void go(){ blastOff(); // Rocket.blastOff(); } private void blastOff(){ System.out.println("sh-bang"); } } Since class Rocket method blastOff(); is private it can't be overridden and it is invisible to class Shuttle. Therefore, class Shuttle method blastOff() gets called which prints to the screen: "sh-bang" ================== f) if line containing Rocket.blastOff(); is uncommented, compilation fails. public class Rocket{ private void blastOff(){ System.out.println("bang"); } } public class Shuttle extends Rocket{ public static void main(String[] args){ new Shuttle().go(); } public void go(){ blastOff(); Rocket.blastOff(); } private void blastOff(){ System.out.println("sh-bang"); } } Rocket method blastOff() is not a static (class) method. Therefore, if the line containing Rocket.blastOff(); is uncommented then compilation fails. A C D and E not true based on the above.

Given: double d = 5.0; int i=0; i+=d; System.out.println(i); What is the result? (Choose all that apply) a) Compiler error b) Prints 5 c) Prints 6 d) Runtime error

b) is correct The program will compile in print: 5 double d = 5.0; int i=0; i+=d; System.out.println(i); Compound assignment operators (e.g. =+) do internal cast, therefore there is no cast needed decimal part of the double number is cut when converting to int type.

What's the name of Java keyword that is used for calling subroutine from another language in general Choose the correct keyword: a) volatile b) native c) static d) synchronized e) abstract

b) is correct. 'native' the name of Java keyword that is used for calling subroutine from another language in general E.g. public native void cpuClockUp(){}

Interface methods cannot be defined... a) default abtract b) final, strictfp or native c) public abtract d) none of the above

b) is correct. Interface methods cannot be defined: final, strictfp or native otherwise they will give a compiler error.

Given: package a; class A{ public static String hello= "Hello"; } package testPackage; class Other { static String hello = "Hello"; } package testPackage; import a; class Test{ public static main(String args[]) { String hello="Hello"; String lo="lo"; System.out.println( testPackage.Other.hello == hello); System.out.println(a.A.hello == hello); System.out.println(hello == ("Hel"+"lo")); System.out.println(hello == ("Hel"+lo)); System.out.println(hello == (("Hel"+"lo")).intern()); }} What is the result? (Choose all that apply) a) compiler error b) compile and print: true true true false true c) compile and print true true true false false d) none of the above

b) is correct. Output is: true true true false true ===Code Step Through=== 1: package a; 2: class A{ 3: public static String hello= "Hello"; 4: } 5: package testPackage; 6: class Other { 7: static String hello = "Hello"; 8: } 9: package testPackage; 10: import a; 11: class Test{ 12: public static main(String args[]) { 13: String hello="Hello"; 14: String lo="lo"; 15: System.out.println( testPackage.Other.hello== hello); 16: System.out.println(a.A.hello == hello); 17: System.out.println(hello == ("Hel"+"lo")); 18: System.out.println(hello == ("Hel"+lo)); 19: System.out.println(hello == (("Hel"+"lo")).intern()); 20: }} On the line 15, Literal strings within different classes of the same package refer to the same String Therefore the output is: true ===========2============== 1: package a; 2: class A{ 3: public static String hello= "Hello"; 4: } 5: package testPackage; 6: class Other { 7: static String hello = "Hello"; 8: } 9: package testPackage; 10: import a; 11: class Test{ 12: public static main(String args[]) { 13: String hello="Hello"; 14: String lo="lo"; 15: System.out.println( testPackage.Other.hello == hello); 16: System.out.println(a.A.hello == hello); 17: System.out.println(hello == ("Hel"+"lo")); 18: System.out.println(hello == ("Hel"+lo)); 19: System.out.println(hello == (("Hel"+"lo")).intern()); 20: }} On the line 16, literal strings (e.g. "Hello") within different classes in different package represent references to the same String object: Therefore the output is: true ============3============ 1: package a; 2: class A{ 3: public static String hello= "Hello"; 4: } 5: package testPackage; 6: class Other { 7: static String hello = "Hello"; 8: } 9: package testPackage; 10: import a; 11: class Test{ 12: public static main(String args[]) { 13: String hello="Hello"; 14: String lo="lo"; 15: System.out.println( testPackage.Other.hello == hello); 16: System.out.println(a.A.hello == hello); 17: System.out.println(hello== ("Hel"+"lo")); 18: System.out.println(hello == ("Hel"+lo)); 19: System.out.println(hello == (("Hel"+"lo")).intern()); 20: }} On the line 17, Strings computed by constant expression are computed at compile time and then treated as if they were literals. Therefore the current output is: true ============4=========== 1: package a; 2: class A{ 3: public static String hello= "Hello"; 4: } 5: package testPackage; 6: class Other { 7: static String hello = "Hello"; 8: } 9: package testPackage; 10: import a; 11: class Test{ 12: public static main(String args[]) { 13: String hello="Hello"; 14: String lo="lo"; 15: System.out.println( testPackage.Other.hello == hello); 16: System.out.println(a.A.hello == hello); 17: System.out.println(hello == ("Hel"+"lo")); 18: System.out.println(hello == ("Hel"+lo)); 19: System.out.println(hello == (("Hel"+"lo")).intern()); 20: }} On the line 18, Strings computed at run time ("Hel"+lo) are newly created and will therefore not refer to the same String object as String objects are immutable. Therefore the output is: false ===========5=========== 1: package a; 2: class A{ 3: public static String hello= "Hello"; 4: } 5: package testPackage; 6: class Other { 7: static String hello = "Hello"; 8: } 9: package testPackage; 10: import a; 11: class Test{ 12: public static main(String args[]) { 13: String hello="Hello"; 14: String lo="lo"; 15: System.out.println( testPackage.Other.hello == hello); 16: System.out.println(a.A.hello == hello); 17: System.out.println(hello == ("Hel"+"lo")); 18: System.out.println(hello == ("Hel"+lo)); 19: System.out.println(hello == (("Hel"+"lo")).intern()); 20: }} On the line 19, a computed String with intern() is the same as a literal string therfore they are refering to the same String object Therefore the output is: true Total output: true true true false true Note: The String class is immutable (cannot be changed) i.e. Strings cannot be changed. Strings are stored in a String Pool. That means that when a String object is created, any new reference to the same object String would revert (point) to the exact same address in memory E.g. String s1 = "Hello"; String s2 = "Hello; ===Code Step Through=== String s1 = "Hello"; String s1 reference is pointing to a newly created string object named "Hello". String Memory Pool Snapshot: s1 --> "Hello" ===========6=========== String s2 = "Hello"; The JVM (Java virtual machine) checks the String Pool to see if the object string "Hello" already exists. As it does exist in the String Pool, String reference s2 refers to (points to) the same object string "Hello" that string reference s1 points to String Memory Pool Snapshot: s1 --> "Hello" <-- s2 ============7=========== If the string objects are not composed of identical characters then a new separate distinct String object is created in the String Pool E.g. String s1 = "Hello"; String s2 = "hello; Because the String objects do not have the exact same characters then two separate String objects are created in the string pool: I.e. String Memory Pool Snapshot: s1 --> "Hello" s2 --> "hello"

Given: int i = 207/200; What is the result? (Choose all that apply) a) compiler error b) Stores the value 1 in variable 'i' c) Stores the value 2 in variable 'i' d) runtime error

b) is correct. The program stores the value 1 in variable 'i': int i = 207/200; int/int = int 207/200 ↓ 1.035 ↓ 1 Decimal is not available in an 'int' type. Therefore the value '1.035' gets downcast to int. Decimal is available in 'double' type.

Given: 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){_______}; 4: A(int coinUnits){ 5: this.units= coinUnits; 6: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } public class B { public static void main(String[] args){ A wallet1 = A.HIGH; A wallet2 = A.LOW; System.out.println(wallet1.cryptoName()); System.out.println(wallet1.getUnits()); System.out.println(wallet2.cryptoName()); System.out.println(wallet2.getUnits()); }} What code snippet, inserted at line 3, enables the constant value 'HIGH' to override the method cryptoName() enabling the value 'HIGH' to have the name "Ethereum" instead of the default name "BitCoin" a) cryptoName(){ "Ethereum";} b) public String cryptoName(){ return "Ethereum"; } c) cryptoName()."Ethereum" d) public String getcryptoName(){ return "Ethereum"; } e) "ETHEREUM"

b) is correct. You can declare methods in an enumeration constant. In this case, the method declared on line 3, 'overrides' the method declared on the line 10. The overridden method is known as a Constant Specific Class Body 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} Let's do a full-code step through to see how enums can be declared and used: ===Code Step Through=== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On the line 1, an Enum A is declared. This enum ends on line 14. In essence, enums can be declared in their own class. For instance this enum A can be written within its own file named 'A.java'. ========2======== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A wallet1 = A.HIGH; 18: A wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On the line 17, an enum reference named 'wallet1' is declared which is of type A. This 'wallet1' reference is referring to the enum constant 'HIGH' and with 'unit' value implicitly declared 90. ========3======== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On the line 18, another enum reference named 'wallet2' is declared which is of type A. This 'wallet2' reference is referring to the enum constant 'LOW' and with 'unit' value implicitly declared 20. ========4======== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1 .cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On line 19, a call is made by the 'wallet1' reference to its method cryptoName(). As 'wallet1' is referring to a HIGH enum, it calls the 'overidden' method on line 3. The overridden method is known as a Constant Specific Class Body ========5======== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On line 3, the 'cryptoName()' is excuted which returns the string "Ethereum" to the main() method and prints it to the output stream. The current output will be at this stage in the code run: Ethereum ========6======== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1 .getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On the line 20, a call is made by the 'wallet1 ' reference to its method getUnits(). This returns the units for the 'HIGH' constant which is 90. ========7======== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1 .getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On the line 7, the getUnits() is entered and the units for the HIGH constant is returned to the main() method and printed to the output stream i.e. 90 The current output will be at this stage in the code run: Ethereum 90 ========8======== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On the line 21, a call is made by the 'wallet2' reference to its method cryptoName(). As 'wallet2' is referring to a LOW enum, it calls the 'cryptoName()' method on line 10 which returns "BitCoin" to the main() method and prints it to the output stream. The current output will be at this stage in the code run: Ethereum 90 BitCoin ========9======== 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On the line 22, a call is made by the 'wallet2' reference to its method getUnits(). This returns the units for the 'LOW' constant which is 20. ========10======= 1: enum A { 2: LOW(20), MEDIUM(50), 3: HIGH(90){ public String cryptoName(){ return "Ethereum"; } }; 4: A(int coinUnits){ 5: this.units= coinUnits; 5: } 7: public int getUnits(){ 8: return units; 9: } 10: public String cryptoName(){ 11: return "BitCoin"; 12: } 13: private int units; 14: } 15: public class B { 16: public static void main(String[] args){ 17: A .wallet1. = A.HIGH.; 18: A .wallet2 = A.LOW; 19: System.out.println(wallet1.cryptoName()); 20: System.out.println(wallet1.getUnits()); 21: System.out.println(wallet2.cryptoName()); 22: System.out.println(wallet2.getUnits()); 23: }} On the line 7, the getUnits() method is called by 'wallet2' on line 22. As 'wallet2' is referring to the constant LOW, the 'unit' 20 is returned to the main() method and gets printed to the output stream. The final output will be: Ethereum 90 BitCoin 20

Given the code below is correct: import static java.lang.System.*; public class _ { static public void main(String[] __A_V) { String $ =""; for(int x = 0; ++x<__A_V.length; ) { $ += __A_V[x]; out.println($); }}} and the command line: java _ - A % What is the result? a) - A b) A % c) -A % d) _ A % e) _ - A % f) compilation failed g) exception is thrown at runtime

b) is correct. The program compile and print: A % ' _ ' is an acceptable, although unusual, class name ' - A % ' args has 5 chars where ' ' is an empty space char i.e. __A_V[0]= - __A_V[1]= ' ' __A_V[2]= A __A_V[3]= ' ' __A_V[4]= % for(int x = 0; ++x<__A_V.length; ) { $ += __A_V[x]; out.println($); } ++x starts at index 1 (i.e. __A_V[ 1 ]) as it is incremented before going into loop: __A_V[1]= ' ' empty space char gets appended to the string first (when appended to String it gets removed) __A_V[2]= A char gets appended to the string second __A_V[3]= ' ' empty space char gets appended to the string third (when appended to String it gets removed) __A_V[4]= % char gets appended to the string fourth output: A % Note: you might get a compiler warning when compiling this code..

Given: class A { protected A(){} protected void A(){} } The above code will give: a) compiler error b) runtime error c) compile and run fine d) none of the above

c) is correct. The program will compile and run fine. class A { protected A(){} protected void A(){} } Its not a constructor if it has a return type or void i.e. protected void A(){} The above is not a constructor Note: class A { protected A(){} protected void A(){} } In the above code, protected A(){} is a constructor

Given: 3. public class TestDays { 4. public enum Days{MON,TUE,WED}; 5. public static void main(String[] args){ 6. for(Days d: Days.values()) { 7. ; 8. Days [] d2 = Days.values(); 9. System.out.println(d2[2]); 10. } 11. } } What is the result? a) TUETUETUE b) WEDWEDWED c) The output is unpredictable d) Compilation fails due to an error on line 4 e) Compilation fails due to an error on line 6 f) Compilation fails due to an error on line 8 g) Compilation fails due to an error on line 9

b) is correct. The programme will compile and print: WEDWEDWED Let's step through the code: ===Code Step Through=== 3. public class TestDays { 4. public enum Days{MON,TUE,WED}; 5. public static void main(String[] args){ 6. for(Days d: Days.values()) { 7. ; 8. Days [ ] d2 = Days.values(); 9. System.out.print(d2[2]); 10. } 11. } } On the line 6, the enhanced for loop method is executed which iterates through the enumeration named Days. On the line 7, there is a semicolon and this is skipped. On the line 8, the enhanced for loop stores the enumeration values into an array containing the enumeration of type Days. This is depicted below: d2[0] = MON d2[1] = TUE d2[2] = WED ========2======== 3. public class TestDays { 4. public enum Days{MON,TUE,WED}; 5. public static void main(String[] args){ 6. for(Days d: Days.values()) { 7. ; 8. Days [] d2 = Days.values(); 9. System.out.println(d2[2]); 10. } 11. } } On the line 9, within the enhanced for loop, the value stored within the array is printed to the screen in each iteration of the enhanced for loop i.e. WEDWEDWED Note: Every enum has a static values() method that returns an array of the enum's values in the order in which they are declared in the enum.

Which of the following will compile and run: a) public class A{ public abstract void m(); } b) public abstract class A{ public void m(){} } c) public abstract class A{ public void m(); }

b) is correct. b) public abstract class A{ public void m(){} } You can have an abstract class with no abstract methods. ================ The following will not compile: a) public class A{ public abstract void m(); } It is illegal to have abstract methods in a class that is not declared abstract. c) public abstract class A{ public void m(); } A non-abstract method (i.e. m()) must have the method body

All variables defined in an interface are .... a) default b) public static final c) public abtract d) none of the above

b) is the correct answer. All variables, defined in an interface, are public static final

Given: inteface A{ //insert code// } Which lines of code-inserted independently above will compile? (Choose all that apply) a) public static m1(){;} b) default void m2(){;} c) abstract int m3(); d) final short m4(){return 5;} e) default long m5(); f) static void m6(){;}

b), c) and f) are correct. ➤ b) default void m2(){;} As of Java 8, interfaces can have default and static methods (as long as they have a method body) ➤ c) abstract int m3(); Every method within an interface is implicitly public abstract ➤ f) static void m6(){;} It is perfectly okay to have a static method within the interface with a body thats empty. ➤ The method/member in an interface cannot be declared: Transient volatile synchronized As these keywords describe implementation properties rather than interface properties

Which of the following is not a Java keyword: a) long b) new c) implement d) exit e) implements

c) and d) are correct c) 'implement' is not a Java keyword but implements' is a Java keyword E.g. class Car implements EngineControl{..} d) exit is not the Java keyword as it is a method name exit() Java Keywords: a) long long is a primitive type keyword E.g. long l1 = 2123443; b) new The new keyword is used to instantiate (create) objects E.g. new String("Hello");

Given: 4. public class Announce { 5. public static void main(String[] args){ 6. for(int ___i = 0; ___i<lenght; ___i++) ; 7. int #lb = 7; 8. long [] x [5]; 9. Boolean []ba[]; 10. } 11. } What is the result? choose all that apply a) Compilation Succeeds b) Compilation fails with an error on line 6 c) Compilation fails with an error on line 7 d) Compilation fails with an error on line 8 e) Compilation fails with an error on line 9

c) and d) are correct. ➤ c) Compilation fails with an error on line 7 ➤ d) Compilation fails with an error on line 8 4. public class Announce { 5. public static void main(String[] args){ 6. for(int ___i = 0; ___i<lenght; ___i++) ; 7. int #lb = 7; 8. long [ ] x [5 ]; 9. Boolean []ba[]; 10. } 11. } On the line 7, contains incorrect syntax for declaring a variable: #lb On the line 8, contains the incorrect syntax for declaring a variable: long [ ] x [5 ] In order to undertake this question, it's important to know the rules for declaring identifiers. Rules for Declaring Ientifiers: ➤ Each identifier must have at least one character. ➤ Identifiers must start with either: letter or currency character (e.g. $, £ etc) or underscore (i.e. _ ) ➤ The rest of the characters (besides the first) can be from: letter or currency character (e.g. $, £ etc) or underscore (i.e. _ ) or digit

Given: 1: class Mood { 2: enum Personality {HAPPY, SAD, WORRIED} 3: Personality type; 4: } 5: class Person { 6: public static void main(String[] args) { 7: Mood feeling = new ______; 8: feeling.type = ______ . ______ .HAPPY; 9: System.out.println(feeling.type); 10: }}} What code snippets inserted above will make the code run correctly: (Choose all that apply) a) Insert 'Mood' on line 7 b) Insert 'Personality' on line 7 c) Insert 'Mood.Personality' on line 8 d) Insert 'Mood()' on line 7

c) and d) are correct. The following snippets, inserted in the code, will make the program run correctly ➤ c) Insert 'Mood.Personality' on line 8 ➤ d) Insert 'Mood()' on line 7 The following code will compile and run fine: 1: class Mood { 2: enum Personality {HAPPY, SAD, WORRIED} 3: Personality type; 4: } 5: class Person { 6: public static void main(String[] args) { 7: Mood feeling = new Mood(); 8: feeling.type = Mood.Personality.HAPPY; 9: System.out.println(feeling.type); 10: }}} The above will print HAPPY. Characteristics of Enumerations: ➤ Enums are constants ➤ Enums allow one to restict a variable to having one of only a few predefined values ➤ Enums can help reduce bugs in code. ➤ You cannot declare an enum in a method ➤ Enum Personality can be placed in its own class called Personality.java ➤ Enum cannot be private or protected.

Which of the following are correct statements? (Choose all that apply) a) Java is a dynamically typed programming language b) Java provides fine-grained control of memory through the use of pointers c) Java provides programmers the ability to create objects that are well encapsulated d) Java provides programmers the ability to send Java objects from one machine to another e) Java is an implementation of the ECMA standard f) Java's encapsulation capabilities provide its primary security mechanism

c) and d) correct. ➤ c) Java provides programmers the ability to create objects that are well encapsulated ➤ d) Java provides programmers the ability to send Java objects from one machine to another The Following Are Incorrect Answers: ➤ a) Java is a dynamically typed programming language incorrect as Java is a statically typed language ➤ b) Java provides fine-grained control of memory through the use of pointers They are incorrect because Java does not provide pointers in the true sense like C/C++ programming language. Note: There is no pointer keyword in Java. It maybe stated that references refer to (point to) object instances but this is for the purposes of describing how references work. ➤ e) Java is an implementation of the ECMA standard Incorrect because JavaScript is the implementation of the ECMA ➤ f) Java's encapsulation capabilities provide its primary security mechanism Incorrect because the use of byte code and the JVM provides Java primary security mechanisms

Given that the Integer class is in the java.lang package and given: public class StatTest { public static void main(String[] args){ System.out.println(Integer.MAX_VALUE); }} Which of the following pieces of code, inserted indepently, must be included at the top of the code? (Choose all that apply) a) import static java.lang; b) import static java.lang.Integer; c) import static java.lang.Integer.*; d) static import java.lang.Integer.*; e) import static java.lang.Integer.MAX_VALUE; f) None of the above statements are valid import syntax

c) and e) are correct ➤ c) import static java.lang.Integer.*; import all static constants and methods in Integer class ================ ➤ e) import static java.lang.Integer.MAX_VALUE; import the static constant MAX_VALUE The Following Will Not Compile: a) import static java.lang; There are no constant classes or constants or methods in lang which causes a compiler error ================ b) import static java.lang.Integer; There are no constant classes or constants or methods in lang.Integer which causes a compiler error ================ d) static import java.lang.Integer.*; syntax error which causes a compiler error

Which of the following are Java keywords: a) equal b) equals c) const d) instance e) instanceof

c) and e) are valid Java keywords. ➤ const is a reserved keyword in Java but is not used and has no function. For defining constants in Java, use the 'final' keyword. ➤ instanceof is a Java operator used to test whether the object is an instance of the specified type (class or subclass or interface). It returns either true or false. The Following Are Not Java Keywords: a) equal b) equals d) instance Note: The exam has questions on testing your knowledge of identifying Java keywords.

All interface methods are implicitly.... a) default b) public c) public abtract d) none of the above

c) is correct All interface methods are implicitly public abstract e.g. What you see: interface Bounceable{ void bounce(); void setBounceFactor(int bf); } What the compiler sees: interface Bounceable{ public abstract void bounce(); public abstract void setBounceFactor(int bf); }

What is the name of the keyword that guarantees the most up-to-date value of a variable that can be read by multiple threads What is the result? a) native b) volatilze c) volatile d) synchronize e) synchronized

c) is correct Keyword 'volatile' guarantees the most up-to-date value of a variable that can be read by multiple threads e.g. volatile int j = 0; The volatile modifier tells the JVM that the thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory.

Given: class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(z); } } The above code will give: a) compiler error b) runtime error c) compile and run fine d) none of the above

c) is correct. It prints 8. ===Code Step Through=== class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(z); } } An object of type A is created and method m() is called. Note: it is perfectly fine to have the literal reference of type A named 'a' as Java is case sensitive. In other words, 'A' is not the same as 'a' in Java. =======2======= class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(z); } } m() gets called and inside its body, it assigns 8 to the int x. Then the method doMore(x) is called with the value of x (i.e. 8) passed into its parameter. Note: 'this' refers to the current object instance of A which is calling the method doMore() i.e. A a = new A(); Meaning, the object instance which 'a' is referring to, is represented as 'this' =======3======= class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(z); } } A copy of the value stored in the integer type 'x' (i.e. 8) is passed to the doMore() method. This z value (containing 8) is then declared final which means that it cannot be changed inside the body of the doMore() method. System.out.println(z); The output of the value of the i.e. 8 is printed to the screen. Note: class A { void m(){ int x = 8; this.doMore(x); } public static void main(String[] args) { A a = new A(); a.m(); } void doMore(final int z){ System.out.println(++z); } } In the above, the final constant variable z has a prefix increment (++). This means that the value of z will be incremented when the doMore() method is executed. However, this z value is final (constant) and therefore cannot be changed in any way. This includes being incremented, therefore it would give a compiler error. In summary: final variable z cannot be modified within the method void doMore(final int z){ z++; } This will cause compiler error.

Given: package cert; public class Beverage{} package cert.stuff; class Tea extends Beverage {} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and run fine d) none of the above

c) is correct. Program will compile and run fine. If Beverage had default access then it would give a compiler error e.g. package cert; class Beverage{} package cert.stuff; class Tea extends Beverage {}

Given: class A { public static void main(String[] args) { try { throw new IOException(); } catch (IOException e) { System.out.println("IOException"); } catch (Exception e) { System.out.println("Exception"); }}} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print: IOException d) none of the above

c) is correct. The program will compile and print: 'IOException' class A { public static void main(String[] args) { try { throw new IOException(); } catch (IOException e) { System.out.println("IOException"); } catch (Exception e) { System.out.println("Exception"); }}} IOException is a subclass of Exception A catch for a subclass exception should occur before a catch for a superclass exception otherwise it will cause a compiler error

Given: interface Foo{ void go(); int theBar=42; } What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and run fine d) none of the above

c) is correct. The program will compile and run fine. Interface constants dont have to declare explicitly as public static final The compiler sees the following: interface Foo{ public abstract void go(); public static final int theBar=42; }

Given: interface A{ default int m1(){return 1;} public default void m2(){;} } What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and run fine d) none of the above

c) is correct. The program will compile and run fine. interface A{ default int m1(); public default void m2(){;} } In Java 8, interfaces can include inheritable methods with concreteimplementations. These concrete methods are called default methods. They must include body and cannot be marked static. For example, the following program will give a compiler error: interface A{ default int m1(); static default void m2(){;} } default int m1(); would give a compiler error as default must have a method body static default void m2(){;} would give a compiler error as default cannot be marked static.

The range of numbers (integers) that can be stored in a byte is a) Greater than -127 and less than 127 b) Greater than -127 and less than 128 c) Greater than -128 and less than 127 d) Greater than -128 and less than 128

c) is correct. The range of numbers (integers) that can be stored in a byte is greater than -128 and less than 127 Anything outside that range gives a compiler error.

Name the keyword that makes a floating-point value exactly the same across platforms Choose the correct keyword: a) volatile b) native c) strictp d) synchronized e) none of these

c) is correct. strictfp makes a floating-point value exactly the same across platforms. strictfp can only be used to modify a class or method but never a variable e.g. public strictfp void cpuClockUp(){} Further example: strictfp class A { double n1= 10e+102; double n2 = 6e+08; double calc(){ return n1+n2; }} The above ensures that floating-point computations are the same on all platforms. For instance, strictfp is used on distributed platform applications like multiplayer games or high-frequency trading software applications.

Which of the following are true. Instance variables can be: a) abstract b) synchronised c) transient d) strictfp e) static f) volatile

c) is correct. ➤ Instance variables can be transient ➤ Instance variables (member variables) cannot be abstract. ➤ Instance variables can be: private default protected public final transient volotile ➤ Instance variables cannot be: abstract synchronised strictfp native static Note: A class variable is a static variable A default instance variable is where there is no identifer before the instance variable and no literal default name e.g. class A{ //default instance variable// int i = 0; //class variable// static int j = 1; //compiler error// default int k = 2; } You do not explicitly write the word "default" before an instance variable in order to declare it default. To declare an instance variable default you simply put no modifier before the instance variable. E.g. int k = 2;

Which are true? Choose all that apply. a) "X extends Y" is correct if and only if X is a class and Y is an interface b) "X extends Y" is correct if and only if X is a interface and Y is an class c) "X extends Y" is correct if and only if X and Y is are interfaces d) "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.

c) is true. "X extends Y" is correct if and only if X and Y are interfaces Incorrect Answers: ➤ a) "X extends Y" is correct if and only if X is a class and Y is an interface The above is incorrect because classes implement interfaces they don't extend them. ================ ➤ b) "X extends Y" is correct if and only if X is a interface and Y is an class The above is incorrect because classes implement interfaces and only inherit from other interfaces ================ ➤ d) "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces. The above is incorrect based on the preceding rules.

Which of the following will compile: a) public final class A { } public class B extends A { } b) public class A { public final void m(){} } public class B extends A { public final void m(){} } } c) public class A { void m() { int i=1; System.out.println(i); } } class B extends A { final void m(){ int i=2; System.out.println(i); } } d) public class A{ final int i = 1; public final void m(){ i=3; }}

c) will compile. c) public class A { void m() { int i=1; System.out.println(i); }} class B extends A { final void m(){ int i=2; System.out.println(i); } } You can declare an inherited method final, as in the above code example. A few rules for the use of the keyword 'final': ➤ A final class cannot be subclassed ➤ A final method cannot be overridden by a subclass ➤ A final variable cannot be assigned a new value *The Following Will Not Compile*: a) public final class A { } public class B extends A { } The above is attempting to inherit a final class but this will cause a compiler error. ================ b) public class A { public final void m(){} } public class B extends A { public final void m(){} }} A final method cannot be overridden.

Which of the following will compile: a) in :b; b) int -d; c) double Double; d) int .f;

c) will compile. ➤ Wrapper class names can be used also as valid identifiers e.g. double Double = 0; String String = "Hello"; ➤ Identifiers must start with a: Letter or Currency character (e.g. $, £,..) or Underscore (i.e. _ ) ➤ Identifiers cannot start with a digit. ➤ The following are also illegal identifiers: int :b; int -d; int e#; int .f; int 7g;

Given: enum A { HIGH(90), MEDIUM(50), LOW(20); A(int coinUnits){ this.units= coinUnits; } public int getUnits(){ return units; } private int units; } 1: public class B { 2: public static void main(String[] args){ 3: A curUnit = ____._____; 4: System.out.println(_____); 5: System.out.println(_____._____()); 6: }} What code snippets, inserted above, will make the code run correctly: (Choose all that apply) a) Insert 'A.HIGH' on line 5 b) Insert 'curUnit' on line 5 c) Insert 'A.HIGH' on line 3 d) Insert 'curUnit.getUnits()' on line 5 e) Insert 'curUnit.getUnits()' on line 3 f) Insert 'curUnit' on line 4

c), d) and f) are correct. ➤ c) Insert 'A.HIGH' on line 3 ➤ d) Insert 'curUnit.getUnits()' on line 5 ➤ f) Insert 'curUnit' on line 4 Below is the correct code. As you can see, 'enum A' can be encapsulated in its own class named 'A.java' and with B class can be located in a file named 'B.java': enum A { HIGH(90), MEDIUM(50), LOW(20); A(int coinUnits){ this.units= coinUnits; } public int getUnits(){ return units; } private int units; } 1: public class B { 2: public static void main(String[] args){ 3: A curUnit = A.HIGH; 4: System.out.println(curUnit); 5: System.out.println(curUnit.getUnits()); 6: }} Output: HIGH 90 Note: The enum constructor will never be called. In fact, if were to be called, it would give a compiler error. enum constructor is implicitly used instead.

Given two files: 1. package pkgA; 2. public class Foo { 3. int a = 5; 4. protected int b = 6; 5. public int c =7; 6. } 3. package pkgB; 4. import pkgA.*; 5. public class Baz { 6. public static void main(String[] args){ 7. Foo f = new Foo(); 8. System.out.println(" " +f.a); 9. System.out.println(" " +f.b); 10. System.out.println(" " +f.c); 11. } 12. } What is the result?: a) 5 6 7 b) 5 followed by an exception c) Compilation fails with an error on line 7 d) Compilation fails with an error on line 8 e) Compilation fails with an error on line 9 f) Compilation fails with an error on line 10

d) and e) are correct. ➤ d) Compilation fails with an error on line 8 ➤ e) Compilation fails with an error on line 9 Looking at the code below we can see that • Variable 'a' has default access so it cannot be accessed from outside its package pkgA. • Variable 'b' has protected access so it cannot be accessed from outside its package pkgA. 1. package pkgA; 2 public class Foo{ 3. int a = 5; 4. protected int b = 6; 5. public int c =7; 6. } 3. package pkgB; 4. import pkgA.*; 5. public class Baz { 6. public static void main(String[] args){ 7. Foo f = new Foo(); 8. System.out.println(" " +f.a); 9. System.out.println(" " +f.b); 10. System.out.println(" " +f.c); 11. } 12. } Therfore, on the line 8 and 9, compile errors occur. Inheritance Access Control: The inheritance access modifier table is essential in helping to understand access control. This is the fundamental of aspect of object-oriented programming. The need to know rule of the contents of classes i.e. instance variables/methods. e.g. A private method or instance variable can be accessed only within the class where it was created. A default method or instance variable declared in a class can be accessed within its own class or within other classes of the same packageand so forth. The following table would be best memorized and quickly written out on paper during the exam: class | package | subclass | world private _Yes_ | _No_ | _No_ | _No_ default _Yes_ | _Yes_ | _No_ | _No_ protected Yes | _Yes_ | _Yes_ | _No_ public _Yes | _ Yes_ | _Yes_ | _ Yes_ No: Where the subclass is in a different package, otherwise Yes Interpreting the above table would be as follows: The column containing private, default, protected or public would represent the level of access for a method or instance variable of a class. The rows containing class, package, subclassand world would represent where a particular method or instance variable can be used/called (depending on its access modifier of being private, default, protected or public). e.g. A method/instance variable that is private can be used in a class but cannot be used in a package, subclass or the world (the world being any other class attempting to access that particular method/instance variable in the running program). On the other extreme, a method/instance variable that is public can be used in a class, package, subclass or the world (the world being any other class attempting to access that particular method/instance variable). A method/instance variable that is publicwould be contrary to the Principles of Object Orientated Programming as any class could change the said public method or instance variable thereby significantly increasing risks of code having bugs and making code almost impossible to maintain.

Which of the following will give a compiler error: (Choose all that apply) a) int [ ] k; b) double j [ ]; c) Thread thread [ ]; d) int [ ] a[ ] = new int[ ][2]; e) int [3] i;

d) and e) will give a compiler error. ➤ d) int [ ] a[ ] = new int [ ][2]; You must initialize the row first. int [ ] a[ ] = new int [Row][Column]; The following is ok: int [ ][ ] a = new int [2][2]; ================== ➤ e) int [3] i; The above will not compile as you cannot include the size of the array (or any digit) in your declaration. The following will compile: ➤ c) Thread thread [ ]; The above is legal but less readable ================= The following will not compile: String[ ] string = new String[ ];

Given: public class A { int i=1; public void m() { int i=2; System.out.println(i); } public static void main(String[] args){ A a1 = new A(); a1.m(); }} The above code will give: a) compiler error b) runtime error c) compile and print 1 d) none of the above

d) is correct The code will print: 2 Shadowing: This code is an example of variable shadowing. Shadowing is where the local variable has the same name as an instance variable: 1: public class A { 2: int i=1; 3: public void m() { 4: int i=2; 5: System.out.println(i); 6: } 7: public static void main(String[] args){ 8: A a1 = new A(); 9: a1.m(); 10: }*}* On the line 2, is the instance variable int i containing the value 1 while on the 4 is the local variable int i containing the value 2. Therefore, it's the local variable that gets printed i.e. 2

Given: 4: public class Frodo extends Hobbit { 5: public static void main(String[] args){ 6: int myGold = 7; 7: System.out.println(countGold(myGold,6)); 8: }} 9: public class Hobbit { 10: int countGold(int x, int y) { 11: return x+y; 12: }} What is the result? a) 13 b) Compilation fails due to multiple errors c) Compilation fails due to an error on line 6 d) Compilation fails due to an error on line 7 e) Compilation fails due to an error on line 11

d) is correct. The program fails to due to an error on line 7. 4. public class Frodo extends Hobbit { 5. public static void main(String[] args){ 6. int myGold = 7; 7. System.out.println(countGold(myGold,6)); 8. } } 9. public class Hobbit { 10. int countGold(int x, int y) { 11. return x+y; 12. } } The method countGold(myGold,6) is a non-static method. In order to call this method within a static method (like main()) you need to declare its reference e.g. 4. public class Frodo extends Hobbit { 5. public static void main(String[] args){ 6. int myGold = 7; Hobbit h = new Hobbit(); 7. System.out.println(h.countGold(myGold,6)); 8. } } 9. public class Hobbit { 10. int countGold(int x, int y) { 11. return x+y; 12. } }

The ______ keyword indicates that a method can be accessed by only one thread at the time. Choose the correct keyword: a) volatile b) final c) static d) synchronized e) abstract

d) is correct. The synchronized keyword indicates that a method can be accessed by only one thread at a time. e.g. public synchronized void turnstile(){ theTicketsSold++; } Like a turnstile at a football ground with one ticketholder fan being the thread and the turnstile (box office) being the synchronized method - only one fan can enter the turnstile at the time to purchase a ticket. The synchronized keyword only applies to methods or blocks like below and not variables-classes or public void turnstile(){ synchronized(this){ theTicketsSold++; } }

Given: 1: class A { 2: void m(){ 3: private int x = 8; 4: this.doMore(x); 5: } 6: void doMore(int z){ 7: z++; 8: }} The above code will give: (Choose all that apply) a) compiler error at line 4 b) compiler error at line 7 c) compile and run fine d) none of the above

d) is correct. Compiler error on line 3. 1: class A { 2: void m(){ 3: private int x = 8; 4: this.doMore(x); 5: } 6: void doMore(int z){ 7: z++; 8: }} On the line 3, a local variable of type int named 'x' is declared. Access modifiers (private, default, protected, public) cannot be applied to local variables.

Given: interface A{ 1: default int m1(){return 2;} 2: static default void m2(){;} 3: public default void m2(){;} 4: default void m2(); } Which lines of code will cause compiler errors: (Choose all that apply) a) Line 1 b) Lines 1 and 2 c) Lines 2 and 3 d) Lines 2 and 4

d) is correct. Lines 2 and 4 will cause compiler errors ===Code Step Through=== interface A{ 1: default int m1(){return 2;} 2: static default void m2(){;} 3: public default void m2(){;} 4: default void m2(); } On the line 2, declaring the method static default will give a compiler error as default cannot be marked static ========2======== interface A{ 1: default int m1(){return 2;} 2: static default void m2(){;} 3: public default void m2(){;} 4: default void m2(); } On the line 4, the above will give a compiler error as default must have a method body

Which of the following are false. Local variables cannot be: a) private b) default c) protected d) public e) transient f) static g) volatile h) final

h) is false. Local variables can be final E.g. public void method() { final int i = 9; } The above is a local variable declared within the method body and it can be declared final (constant)


Ensembles d'études connexes

Chapter 9 Net Present Value and Other Investment Criteria

View Set

Pharm chapter 6 and 7 children and older adults

View Set

Chapter 2: Theories and Therapies

View Set

Qualitative and quantitative methods

View Set

Life Insurance Policy Provisions, Options and Riders

View Set

WMU Marketing 2500 Online Quiz Chapter 10

View Set