It is really great experience to have 310-066 braindumps.

310-066 online exam | 310-066 exam prep | 310-066 model question | 310-066 test prep | 310-066 essay questions - Killexams.com



310-066 - Upgrade for the Sun Certified Java Programmer. SE 6.0 - Dump Information

Vendor : SUN
Exam Code : 310-066
Exam Name : Upgrade for the Sun Certified Java Programmer. SE 6.0
Questions and Answers : 93 Q & A
Updated On : June 26, 2017
PDF Download Mirror : 310-066 Brain Dump
Get Full Version : Pass4sure 310-066 Full Version


Got no problem! 3 days preparation of 310-066 Actual Questions is required.

Killexams is the best and accurate way I have ever come across to prepare and pass IT exams. The thing is, it gives you accurately and EXACTLY what you need to know for 310-066 exam. My friends used Killexams for Cisco, Oracle, Microsoft, ISC and other certifications, all good and valid. Totally reliable, my personal favorite.

Observed maximum 310-066 Questions in Latest Braindumps that I prepared.

If you want high quality 310-066 dumps, then Killexams is the ultimate choice and your only solution. it provides amazing and excellent test dumps which I am saying with full confidence. I always thought that 310-066 dumps are of no uses but Killexams proved me wrong as the dumps provided by them were of great use and helped me score high. If you are worrying for 310-066 dumps as well, then you need not to worry and join Killexams.

I found a very good source of 310-066 material.

Hi Team, I have completed 310-066 in first attempt and thanks a lot for your useful question bank.

Do not spill huge amount at 310-066 guides, checkout these q and a.

I wanted to have certification in Test 310-066 and i get it with Killexams. Perfect pattern of latest modules facilitate me to attempt all the 38 questions within the given timeframe. I score more than 87. I must say that I could never ever have done it on my own what I was able to achieve with Killexams Q&A. Killexams Q&A provide the latest module of questions and cover the related topics. Thanks to Killexams Q&A.

These 310-066 Latest Braindumps works in the real test.

If you want to change your destiny and make sure that happiness is your fate, you need to work hard. Working hard alone is not enough to get to destiny, you need some direction that will lead you towards the path. It was destiny that I found this Killexams during my exams because it lead me towards my fate. My fate was getting good grades and this Killexams and its teachers made it possible my teaching we so well that I couldnt possibly fail by giving me the substance for my 310-066 exam.

That was Awsome! I got braindumps of 310-066 exam.

I am one among the high achiever in the 310-066 exam. What a fantastic Q&A material they provided. Within a short time I grasped everything on all the relevant topics. It was simply superb! I suffered a lot while preparing for my previous attempt, but this time I cleared my exam very easily without tension and worries. It is truly admirable learning journey for me. Thanks a lot Killexams for the real support.

Exam questions are changed, where can i find new questions and answers?

Killexams materials cover every aspect of 310-066 , around which the 310-066 exam is built. So if youre new to it, this is a must. I needed to step up my understanding of 310-066 qa has helped me a lot. I passed the 310-066 exam thanks to Killexams and have been recommending it to my friends and colleagues.

Right place to find 310-066 braindumps paper.

I passed this exam 310-066 today with a 92% score. Killexams was my main preparation resource, so if you plan to take this exam, you can totally count on this 310-066 questions source. All information is relevant, the 310-066 questions are correct. I am very happy with Killexams. This is the first time I used it, but now Im confident Ill come back to this website for all my 310-066 certification exams

Is there someone who passed 310-066 exam?

Hats down the best 310-066 exam preparation option. I passed my 310-066 exam last week, and this set of exam questions and answers has been very helpful. This stuff from Killexams is authentic. Before making a purchase, I contacted customer support with questions about how updated their materials are, and they confirmed that they update all exams on nearly daily basis. They add updates where necessary, or simply double check the content to make sure its up to date. It justifies paying for an exam brain dump. With Killexams, I know that I can rely on the latest exam materials, not some book that can become out of date a week after its published. So I think this is the best exam preparation option. I think I will expand my certification portfolio into a few other vendors, Im just not sure which ones yet. But what Im sure about is that I will be using Killexams as my main preparation resource.

What do you mean by 310-066 exam?

I got a good result with this bundle. Very good quality, questions are accurate and I got most of them on the exam. After I have passed it, I recommended Killexams to my colleagues, and everyone passed their exams, too (some of them took Cisco exams, others did Microsoft, VMware, etc). I have not heard a bad review of Killexams, so this must be the best IT training you can currently find online.

Latest Exams added on Killexams

1Z0-453 | 210-250 | 300-210 | 500-205 | 500-210 | 70-765 | 9A0-409 | C2010-555 | C2090-136 | C9010-260 | C9010-262 | C9020-560 | C9020-568 | C9050-042 | C9050-548 | C9050-549 | C9510-819 | C9520-911 | C9520-923 | C9520-928 | C9520-929 | C9550-512 | CPIM-BSP | C_TADM70_73 | C_TB1200_92 | C_TBW60_74 | C_TPLM22_64 | C_TPLM50_95 | DNDNS-200 | DSDPS-200 | E20-562 | E20-624 | E_HANABW151 | E_HANAINS151 | JN0-1330 | JN0-346 | JN0-661 | MA0-104 | MB2-711 | NSE6 | OMG-OCRES-A300 | P5050-031 |

See more dumps on Killexams

850-001 | 190-801 | C9030-634 | 000-558 | 000-114 | 00M-235 | 000-648 | 1Y0-610 | 000-208 | 1Z0-219 | P2070-048 | 1Z0-878 | 000-956 | A2180-317 | 1T6-530 | 000-X01 | MB3-214 | 310-615 | C2010-651 | M2040-641 | 000-084 | MB6-889 | E20-260 | HP0-746 | MOS-A2K | HP2-H17 | 1Y0-A26 | 1Z0-562 | HP2-B100 | VCS-273 | H12-224 | 6001-1 | 001-ARXConfig | 70-545-CSharp | NS0-501 | C_TCRM20_72 | 000-N14 | 000-016 | 650-196 | 70-463 | 000-778 | JN0-101 | 000-633 | M9510-747 | 1Y1-456 | 000-175 | 000-453 | 050-684 | 1Z0-809 | 000-M62 |

310-066 Questions and Answers

310-066


Which statement is true about the set variable on line 12?


  1. The set variable contains all six elements from the coll collection, and the order is guaranteed to be preserved.

  2. The set variable contains only three elements from the coll collection, and the order is guaranteed to be preserved.

  3. The set variable contains all six elements from the coll collection, but the order is NOT guaranteed to be preserved.

  4. The set variable contains only three elements from the coll collection, but the order is NOT guaranteed to be preserved.


Answer: D


QUESTION: 86

A programmer has an algorithm that requires a javA. util.List that provides an efficient implementation of add(0, object), but does NOT need to support quick random access. What supports these requirements?


  1. javA. util.Queue

  2. javA. util.ArrayList

  3. javA. util.LinearList

  4. javA. util.LinkedList


Answer: D


QUESTION: 8

Given that the elements of a PriorityQueue are ordered according to natural ordering, and:

  1. import javA. util.*;

  2. public class GetInLine {

  3. public static void main(String[] args) {

  4. PriorityQueue pq = new PriorityQueue();

  5. pq.add("banana");

  6. pq.add("pear");

  7. pq.add("apple");

  8. System.out.println(pq.poll() + " " + pq.peek());

  9. }

  10. }

What is the result?


  1. apple pear

  2. banana pear

  3. apple apple

  4. apple banana

  5. banana banana


    Answer: D


    QUESTION: 88

    Given:

    1. import javA. util.*; 2.

  1. public class LetterASort{

  2. public static void main(String[] args) {

  3. ArrayList strings = new ArrayList();

  4. strings.add("aAaA");

  5. strings.add("AaA");

  6. strings.add("aAa");

  7. strings.add("AAaa");

  8. Collections.sort(strings);

  9. for (String s : strings) { System.out.print(s + " "); }

  10. }

  11. }

What is the result?


  1. Compilation fails.

  2. aAaA aAa AAaa AaA

  3. AAaa AaA aAa aAaA

  4. AaA AAaa aAaA aAa

  5. aAa AaA aAaA AAaa

  6. An exception is thrown at runtime.


Answer: C


QUESTION: 89

Given:

  1. import javA. util.*;

  2. public class Explorer2 {

  3. public static void main(String[] args) {


  4. TreeSet s = new TreeSet();

  5. TreeSet subs = new TreeSet(); 17. for(int i = 606; i < 613; i++)

  1. if(i%2 == 0) s.add(i);

  2. subs = (TreeSet)s.subSet(608, true, 611, true); 20. s.add(629);

21. System.out.println(s + " " + subs);22. }

23. }

What is the result?


  1. Compilation fails.

  2. An exception is thrown at runtime. C. [608, 610, 612, 629] [608, 610]

D. [608, 610, 612, 629] [608, 610, 629]

E. [606, 608, 610, 612, 629] [608, 610]

F. [606, 608, 610, 612, 629] [608, 610, 629]


Answer: E


QUESTION: 90

Given:

  1. import javA. util.*;

  2. public class Explorer3 {

  3. public static void main(String[] args) {

  4. TreeSet s = new TreeSet();

  5. TreeSet subs = new TreeSet(); 17. for(int i = 606; i < 613; i++)

  1. if(i%2 == 0) s.add(i);

  2. subs = (TreeSet)s.subSet(608, true, 611, true);

  3. subs.add(629);

  4. System.out.println(s + " " + subs);22. }

23. }

What is the result?


  1. Compilation fails.

  2. An exception is thrown at runtime. C. [608, 610, 612, 629] [608, 610]

D. [608, 610, 612, 629] [608, 610, 629]

E. [606, 608, 610, 612, 629] [608, 610]

F. [606, 608, 610, 612, 629] [608, 610, 629]


Answer: B


QUESTION: 91

Given a pre-generics implementation of a method:

  1. public static int sum(List list) {

  2. int sum = 0;

  3. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {

  4. int i = ((Integer)iter.next()).intValue();

  5. sum += i;

  6. }

  7. return sum;

  8. }

What three changes allow the class to be used with generics and avoid an unchecked warning? (Choose three.)


  1. Remove line 14.

  2. Replace line 14 with "int i = iter.next();".

  3. Replace line 13 with "for (int i : intList) {".

  4. Replace line 13 with "for (Iterator iter : intList) {".

  5. Replace the method declaration with "sum(List intList)".

  6. Replace the method declaration with "sum(List intList)".


Answer: A, C, F


QUESTION: 92

Given:

  1. import javA. util.*;

  2. public class G1 {

  3. public void takeList(List list) {

  4. // insert code here 7. }

  1. }

    Which three code fragments, inserted independently at line 6, will compile? (Choose three. )


    1. list.add("foo");

    2. Object o = list;

    3. String s = list.get(0);

    4. list = new ArrayList();

    5. list = new ArrayList();


Answer: B, C, D


QUESTION: 93

Given:

  1. // insert code here

  2. private N min, max;

  3. public N getMin() { return min; }

  4. public N getMax() { return max; }

  5. public void add(N added) {

  6. if (min == null || addeD. doubleValue() < min.doubleValue())17. min = added;

18. if (max == null || addeD. doubleValue() > max.doubleValue())19. max = added;

  1. }

  2. }

Which two, inserted at line 11, will allow the code to compile? (Choose two.)


  1. public class MinMax {

  2. public class MinMax {

  3. public class MinMax {

  4. public class MinMax {

  5. public class MinMax {

  6. public class MinMax {


Answer: D, F


SUN 310-066 Exam (Upgrade for the Sun Certified Java Programmer. SE 6.0) Detailed Information

Article by ArticleForge

">310-066 Sun Upgrade Exam for the Sun Certified Java Programmer. SE 6.0 Visit:

1 Sun Upgrade Exam for the Sun Certified Java Programmer. SE 6.0 : Pass4sureofficialm is a reputable IT certification examination guide, study guides and audio exam provider, we not only ensure that you pass your exam in first attempt, but also you can get a high score to acquire Sun certification. If you use pass4sureofficial Certification questions and answers, you will experience actual exam questionsanswers. We know exactly what is needed and have all the exam preparation material required to pass the exam. Our Sun exam prep covers over 95% of the questions and answers that may be appeared in your exam. Every point from pass4sure PDF, review will help you take Sun exam much easier and become Sun certified. All the QuestionsAnswers are taken from real exams. Here's what you can expect from the Pass4sureOfficial Sun course: * Up-to-Date Sun questions taken from the real exam. * 100% correct Sun answers you simply can't find in other courses. * All of our tests are easy to download. Your file will be saved as a PDF. * Sun brain dump free content featuring the real test questions. Sun certification exam is of core importance both in your Professional life and Sun certification path. With Sun certification you can get a good job easily in the market and get on your path for success. Professionals who passed Sun exam training are an absolute favorite in the industry. You will pass Sun certification test and career opportunities will be open for you.

2 QUESTION: 1 Given: 11. public class Test { 12. public enum Dogs {collie, harrier, shepherd}; 13. public static void main(String [] args) { 14. Dogs myDog = Dogs.shepherd; 15. switch (myDog) { 16. case collie: 17. System.out.print("collie "); 18. case default: 19. System.out.print("retriever "); 20. case harrier: 21. System.out.print("harrier "); 22. } 23. } 24. } What is the result? A. harrier B. shepherd C. retriever D. Compilation fails. E. retriever harrier F. An exception is thrown at runtime. Answer: D QUESTION: 2Given: 10. interface Foo {} 11. class Alpha implements Foo {} 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args ) { 15. Beta x = new Beta(); 16. insert code here 17. } 18. } Which code, inserted at line 16, will cause a javA. lanG. ClassCastException? A. Alpha a = x; B. Foo f = (Delta)x;

3 C. Foo f = (Alpha)x; D. Beta b = (Beta)(Alpha)x; Answer: B QUESTION: 3 A UNIX user named Bob wants to replace his chess program with a new one, but he is not sure where the old one is installed. Bob is currently able to run a Java chess program starting from his home directory homebob using the command: java -classpath test:homebobdownloads*.jar games.Chess Bob'sCLASSTHisset(atlogintime) to:usrlib:homebobclasses:optjavalib:optjavalib*.jarWhat is a possible location for the Chess.class file? A. testChess.class B. homebobChess.class C. testgamesChess.class D. usrlibgamesChess.class E. homebobgamesChess.class F. inside jarfile optjavalibGames.jar (with a correct manifest) G. inside jarfile homebobdownloadsGames.jar (with a correct manifest) Answer: C QUESTION: 4 Given: 21. class Money { 22. private String country = "Canada"; 23. public String getC() { return country; } 24. } 25. class Yen extends Money { 26. public String getC() { return superuntry; } 27. } 28. public class Euro extends Money { 29. public String getC(int x) { return super.getC(); } 30. public static void main(String[] args) { 31. System.out.print(new Yen().getC()+ " " + new Euro().getC()); 32. }

4 } What is the result? A. Canada B. null Canada C. Canada null D. Canada Canada E. Compilation fails due to an error on line 26. F. Compilation fails due to an error on line 29. Answer: E QUESTION: 5 Given: 3. import javA. util.*; 4. public class Hancock { 5. insert code here 6. list.add("foo"); 7. } 8. } Which two code fragments, inserted independently at line 5, will compile without warnings? (Choose two.) A. public void addStrings(List list){ B. public void addStrings(List list){ C. public void addStrings(List list){ D. public void addStrings(List list){ Answer: B, C QUESTION: 6 Click the Exhibit button. ***Missing Exhibit*** Which three statements are true? (Choose three. ) A. Compilation fails. B. The code compiles and the output is 2. C. If lines 16, 17 and 18 were removed, compilation would fail.

5 Pass4SureOfficialm Lifetime Membership Features; Pass4SureOfficial Lifetime Membership Package includes over 2500 Exams. All exams Questions and Answers are included in package. All Audio Guides are included free in package. All Study Guides are included free in package. Lifetime login access. Unlimited download, no account expiry, no hidden charges, just one time $99 payment. Free updates for Lifetime. Free Download Access to All new exams added in future. Accurate answers with explanations (If applicable). Verified answers researched by industry experts. Study Material updated on regular basis. Questions, Answers and Study Guides are downloadable in PDF format. Audio Exams are downloadable in MP3 format. No authorization code required to open exam. Portable anywhere. 100% success Guarantee. Fast, helpful support 24x7. View list of All exams (Q&A) downloads View list of All Study Guides (SG) downloads View list of All Audio Exams (AE) downloads Download All Exams Samples To purchase $99 Lifetime Full Access Membership click here 3COM ADOBE APC Apple BEA BICSI CheckPoint Cisco Citrix CIW CompTIA ComputerAssociates CWNP DELL ECCouncil EMC Enterasys ExamExpress Exin ExtremeNetworks Filemaker Fortinet Foundry Fujitsu GuidanceSoftware HDI Hitachi HP Huawei Hyperion IBM IISFA Intel ISACA ISC2 ISEB ISM Juniper Legato Lotus LPI McAfee McData Microsoft Mile2 NetworkAppliance Network-General Nokia Nortel Novell OMG Oracle PMI Polycom RedHat Sair SASInstitute SCP See-Beyond SNIA Sun Sybase Symantec TeraData TIA Tibco TruSecure Veritas Vmware

Article by ArticleForge

Core Java Technologies Tech Tips

New Certification Courses Sun Certified Programmer for the Java Platform, Standard Edition 6-- The Sun Certified Programmer for Java Platform, Standard Edition 6 certification exam is for programmers experienced using the Java programming language. Achieving this certification provides clear evidence that a programmer understands the basic syntax and structure of the Java programming language and can create Java technology applications that run on server and desktop systems using Java SE 6.

Sun Certified Programmer for the Java Platform, Standard Edition 6 Upgrade Exam (CX-">310-066)-- The Sun Certified Programmer for Java Platform, Standard Edition 6 certification exam is for programmers experienced using the Java programming language. Achieving this certification provides evidence that a programmer understands the basic syntax and structure of the Java programming language and can create Java technology applications that run on server and desktop systems using Java Platform, Standard Edition 6 Sun strongly recommends that all new candidates interested in becoming a Sun Certified Java Programmer (SCJP) take this new version of the certification exam, rather than a previous version. In this way, candidates can demonstrate that they are knowledgeable in the latest technology. Sun also recommends that those certified on a previous version of SCJP update their credentials by taking the SCJP 6 Upgrade exam.

\*\*\*\*\*\*

Sun Microsystems Developer PlaygroundJoin Dana Nourie December 18th, Tues 9 AM PST and 7 PM PST, in Second Life at the Sun Microsystems Developer Playground to discover how easy it is to learn the Java platform through web programming. Watch a demo and have your questions answered.

\*\*\*\*\*\*

Article by ArticleForge

999 Questions From Cramming for Sun Java

Text

Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) SUN CERTIFIED PROGRAMMER FOR THE JAVA 2 PLATFORM 1.4 Available at: ized Prometric testing centres Exam number: CX-310-035 Prerequisites: None Exam type: Multiple choice and short answer Number of questions: 61 Pass score: 52% (32 of 61 questions) Time limit: 120 minutes Cost: US$150, or as priced in the country where the exam is taken 1 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) Exam Objectives 1 1.1 1.2 DECLARATIONS AND ACCESS CONTROL Write code that declares, constructs and initialises arrays of any base type using any of the permitted forms both for declaration and for initialisation. Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as public, final, static, abstract, etc.). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers. For a given class, determine if a default constructor will be created and if so state the prototype of that constructor. Identify legal return types for any method given the declarations of all related methods in this or parent classes. 1.3 1.4 2 2.1 2.2 FLOW CONTROL, ASSERTIONS, AND EXCEPTION HANDLING Write code using if and switch statements and identify legal argument types for these statements. Write code using all forms of loops including labeled and unlabeled, use of break and continue, and state the values taken by loop counter variables during and after loop execution. Write code that makes proper use of exceptions and exception handling clauses (try, catch, finally) and declares methods and overriding methods that throw exceptions. Recognize the effect of an exception arising at a specified point in a code fragment. Note: The exception may be a runtime exception, a checked exception, or an error (the code may include try, catch, or finally clauses in any legitimate combination). Write code that makes proper use of assertions, and distinguish appropriate from inappropriate uses of assertions. Identify correct statements about the assertion mechanism. 2.3 2.4 2.5 2.6 3 3.1 3.2 3.3 GARBAGE COLLECTION State the behaviour that is guaranteed by the garbage collection system. Write code that explicitly makes objects eligible for garbage collection. Recognize the point in a piece of source code at which an object becomes eligible for garbage collection. 2 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 4 4.1 LANGUAGE FUNDAMENTALS Identify correctly constructed package declarations, import statements, class declarations (of all forms including inner classes) interface declarations, method declarations (including the main method that is used to start execution of a class), variable declarations, and identifiers. Identify classes that correctly implement an interface where that interface is either java.lang.Runnable or a fully specified interface in the question. State the correspondence between index values in the argument array passed to a main method and command line arguments. Identify all Java programming language keywords. Note: There will not be any questions regarding esoteric distinctions between keywords and manifest constants. State the effect of using a variable or array element of any kind when no explicit assignment has been made to it. State the range of all primitive formats, data types and declare literal values for String and all primitive types using all permitted formats bases and representations. 4.2 4.3 4.4 4.5 4.6 5 5.1 OPERATORS AND ASSIGNMENTS Determine the result of applying any operator (including assignment operators and instance of) to operands of any type class scope or accessibility or any combination of these. Determine the result of applying the boolean equals (Object) method to objects of any combination of the classes java.lang.String, java.lang.Boolean and java.lang.Object. In an expression involving the operators &, |, &&, || and variables of known values state which operands are evaluated and the value of the expression. Determine the effect upon objects and primitive values of passing variables into methods and performing assignments or other modifying operations in that method. 5.2 5.3 5.4 6 6.1 OVERLOADING, OVERRIDING, RUNTIME TYPE AND OBJECT ORIENTATION State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and "has a". Write code to invoke overridden or overloaded methods and parental or overloaded constructors; and describe the effect of invoking these methods. Write code to construct instances of any concrete class including normal top level classes and nested classes. 6.2 6.3 3 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 7 7.1 7.2 7.3 7.4 THREADS Write code to define, instantiate and start new threads using both java.lang.Thread and java.lang.Runnable. Recognize conditions that might prevent a thread from executing. Write code using synchronized wait, notify and notifyAll to protect against concurrent access problems and to communicate between threads. Define the interaction among threads and object locks when executing synchronized wait, notify or notifyAll. 8 8.1 8.2 8.3 FUNDAMENTAL CLASSES IN THE JAVA.LANG CKAGE Write code using the following methods of the java.lang.Math class: abs, ceil, floor, max, min, random, round, sin, cos, tan, sqrt. Describe the significance of the immutability of String objects. Describe the significance of wrapper classes, including making appropriate selections in the wrapper classes to suit specified behavior requirements, stating the result of executing a fragment of code that includes an instance of one of the wrapper classes, and writing code using the following methods of the wrapper classes (e.g., Integer, Double, etc.): doubleValue floatValue intValue longValue parseXxx getXxx toString toHexString 9 9.1 9.2 THE COLLECTIONS FRAMEWORK Make appropriate selection of collection classesinterfaces to suit specified behaviour requirements. Distinguish between correct and incorrect implementations of hashcode methods. 4 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 1. Which two create an instance of an array? (Choose two.) A. B. C. D. E. int[ ] ia = new int[15]; float fa = new float[20]; char[ ] ca = "Some String"; Object oa = new float[20]; int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 }; Answer: AD Explanation: An array must be declared and constructed before it can be used. An array variable declaration has either the following syntax: [ ] ; or [ ]; Once an array has been declared it can be constructed for a specific number of elements of the element data type, using the new operator. The resulting array can be assigned to a variable of the corresponding type: = new []; The minimum value of is 0, i.e. arrays with zero elements can be constructed in Java. The array declaration and construction can be combined: [ ] = new []; However, here array type must be assignable to array type . When the array is constructed, all its elements are initialised to the default value for . This is true for both member and local arrays when they are constructed. Java provides the means of declaring, constructing and explicitly initialising an array in one language construct: [ ] = { }; This form of initialisation applies to both members as well as local arrays. The initialisation code in the block results in the construction and initialisation of the array. For example the following array, anIntArray, is declared as an array of ints. It is constructed to hold 10 elements (equal to the number of items in the comma-separated list in the block), where the first element is initialised to 1, the second element to 3, and so on. int[ ] anIntArray = {1, 3, 49, 2, 6, 7, 15, 2, 1, 5}; In summary, an array type is written as the name of an element type followed by some number of empty pairs of square brackets [ ]. The number of bracket pairs indicates the depth of array nesting. An array's length is not part of its type. The element type of an array may be any type, whether primitive or reference. 5 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) A is correct. It uses correct array declaration and correct array construction. B is incorrect. It generates a compiler error: incompatible types because the array variable declaration is not correct. The array construction expects a reference type, but it is supplied with a primitive type in the declaration. This is why B doesn’t work and why D does work. It would compile if it was changed to any of the following: float fa[ ] = new float[20]; float fa[]; … fa = new float[20]; (iii) float[] fa = new float[20]; (iv) float[] fa; … fa = new float[20]; C is incorrect. It generates a compiler error: incompatible types because a string literal is not assignable to a character type variable. The following is a suggested solution that compiles correctly (if an array of characters was required): char[ ] ca = {'S','o','m','e',' ','S','t','r','i','n','g'}; The following is a suggested solution that compiles correctly (if an array of strings was required): String[ ] ca = {"Some String"}; In both of the above, the data type returned by the array construction are of the correct data type to initialise the elements of array ca. D is correct. All classes directly or indirectly extend the Object class. The Object class is the root of every inheritance hierarchy. The Object class defines the basic functionality that all objects exhibit and which all classes inherit. This also applies for array, since arrays are genuine objects in Java. My opinion is that this is allowed because the rule of thumb for converting reference values is that conversions up the inheritance hierarchy are allowed (upcasting), but conversions down the hierarchy require explicit casting (downcasting). That is, conversions that preserve the inheritance is-a relationship (an array is-a object) are allowed. E is wrong, it generates a compiler error expected. The compiler thinks that you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create a 3 x 3 two-dimensional array. To correct the problem and make E compile you need to add an extra pair of curly brackets: int ia[ ] [ ] = { { 4, 5, 6 }, { 1,2,3 } }; Source code file: ArrayTest01.java (i) (ii) 6 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 2. Given: 01. 02. 03. 04. 05. 06. 07. 08. public class ArrayTest { public static void main(String[ ] args){ float f1[ ], f2[ ]; f1 = new float[10]; f2 = f1; System.out.println("f2[0] = " + f2[0]); } } What is the result? A. B. C. D. E. It prints f2[0] = 0.0 It prints f2[0] = NaN An error at line 5 causes compile to fail. An error at line 6 causes compile to fail. An error at line 6 causes an exception at runtime. Answer: A Explanation: When you create an array (line 04) the elements are initialises to the default values for the primitive data type (float in this case - 0.0), so f1 will contain 10 elements each with a value of 0.0. f2 has been declared but has not been initialised, it has the ability to reference or point to an array but as yet does not point to any array. Line 05 copies the reference (pointermemory address) of f1 into f2 so now f2 points at the array pointed to by f1. This means that the values returned by f2 are the values returned by f1. Changes to f1 are also changes to f2 because both f1 and f2 point to the same array. The following code illustrates this point: 01 02 03 04 05 06 07 08 09 10 11 public class ArrayTest02d { public static void main(String[] args){ float f1[], f2[]; f1 = new float[10]; for(int i = 0; i < 10; i++) f1[i] = i + 5; init the array f2 = f1; System.out.println("f2[0] = " + f2[0]); outputs 5.0 f1[0] = 99.99f; System.out.println("f2[0] = " + f2[0]); outputs 99.99 } } The following is a discussion on Not-a-Number (NaN). The Java Language Specification 2 The floating-point types are float and double, which are conceptually associated with the singleprecision 32-bit and double-precision 64-bit format IEEE 754 values and operations. The IEEE 754 standard includes not only positive and negative numbers that consist of a sign and magnitude, but also positive and negative zeros, positive and negative infinities, and special Not-a-Number values (also abbreviated as NaN). A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero. NaN constants of both float and double type are predefined as Float.NaN and Double.NaN. For the most part, the Java platform treats NaN values of a given type as though collapsed into a single canonical value (and hence this specification normally refers to an arbitrary NaN as though to a canonical value). However, version 1.3 the Java platform introduced methods enabling the programmer to distinguish between NaN values: the Float.floatToRawIntBits and Double.doubleToRawLongBits methods. Refer to the 7 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) specifications for the Float and Double classes for more information. In English the two floating-point types are float and double, they conform to the IEEE 754 specification. Many mathematical operations can yield results that have no expression in numbers (infinity, for example). To describe such non-numerical situations, both doubles and floats can take on values that are bit patterns that do not represent numbers. Rather, these patterns represent non-numerical values. The patterns are defined in the Float and Double classes and may be referenced as follows (NaN stands for Not a Number): Float.NaN Float.NEGATIVE_INFINITY Float.POSITIVE_INFINITY Double.NaN Double.NEGATIVE_INFINITY Double.POSITIVE_INFINITY The code below shows the use of these constants: 01 02 03 04 05 06 07 08 public class ArrayTest02a { public static void main(String[] args) { double d = -10.0 0.0; if (d == Double.NEGATIVE_INFINITY) { System.out.println( "d just exploded: } } } " + d); In this code fragment, the test on line 04 passes, so line 05 is executed. There is a significance associated with the NaN values. NaN values are used to indicate that a calculation has no result in ordinary arithmetic, such as some calculations involving infinity or the square root of a negative number. Two Nan values are defined in the java.lang package (Float.Nan and Double.Nan) and are considered non-ordinal for comparisons. This means that for any value of x, including NaN itself, all of the following comparisons will return false: x < Float.NaN x Float.NaN x >= Float.NaN In fact, the test Float.NaN != Float.NaN and the equivalent with Double.NaN return true, as you might deduce from the item above indicating that x == Float.NaN gives false even if x contains Float.NaN. The most appropriate way to test for a NaN result from a calculation is to use the Float.isNaN(float) or Double.isNaN(double) static methods provided in the java.lang package. Source code files: ArrayTest02.java ArrayTest02a.java ArrayTest02b.java ArrayTest02c.java ArrayTest02d.java 8 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 3. Which two cause a compiler error? (Choose two.) A. B. C. D. E. F. int[ ] scores = {3, 5, 7}; int [ ][ ] scores = {2,7,6}, {9,3,45}; String cats[ ] = {"Fluffy", "Spot", "Zeus"}; boolean results[ ] = new boolean [3] {true, false, true}; Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)}; String[ ] dogs = new String[ ] {new String("Fido"), new String("Spike"), new String("Aiko")}; Answer: BD Explanation: Read the explanations below in conjunction with those given for question 1. A does not cause a compiler error. Java provides the means of declaring, constructing and explicitly initialising an array in one language construct: [ ] = { }; This form of initialisation applies to both members as well as local arrays. The initialisation code in the block results in the construction and initialisation of the array. For example the following array, anIntArray, is declared as an array of ints. It is constructed to hold 10 elements (equal to the number of items in the comma-separated list in the block), where the first element is initialised to 1, the second element to 3, and so on. int[ ] anIntArray = {1, 3, 49, 2, 6, 7, 15, 2, 1, 5}; B generates a compiler error: expected. The compiler thinks you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create one 3 x 3 two-dimensional array. To correct the problem and make B compile you need to add an extra pair of curly brackets: int [ ] [ ] scores = { {2,7,6}, {9,3,45} }; C does not cause a compiler error. See the explanation above for A. D generates a compiler error: ';' expected. You cannot initialise an array in this manner because you are declaring the number of elements in two different places, between the brackets [3] and in the block {true, false, true}. Rewrite the line as follows to make it compile: boolean results[ ] = {true, false, true}; or boolean results[ ] = new boolean [ ] {true, false, true}; also works. Also see the explanation above for A. E compiles correctly. This code creates an array of arrays - it creates a one-dimensional array and initialises each element of this array according to the code in the block. See the explanation above for A. F compiles correctly. This code creates an array of string objects - it creates a onedimensional array and initialises each element of this array according to the code in the block. See the explanation above for both A and D. Source code file: ArrayTest03.java 9 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 4. Which three form part of correct array declarations? (Choose three.) A. B. C. D. E. F. public int a [ ] static int [ ] a public [ ] int a private int a [3] private int [3] a [ ] public final int [ ] a Answer: A, B, F Explanation: An array declaration tells the compiler the array’s name and what type its elements will be not the number of elements in the array. A, B and F are valid array declarations. C is not a correct array declaration. The compiler complains with: illegal start of type. The brackets are in the wrong place. The following would work: public int[ ] a D is not a correct array declaration. The compiler complains with: ']' expected. A closing bracket is expected in place of the 3. The following works: private int a [] An array declaration tells the compiler the array’s name and what type its elements will be not the number of elements in the array. E is not a correct array declaration. The compiler complains with 2 errors: ']' expected A closing bracket is expected in place of the 3 and expected A variable name is expected after a[ ] An array declaration tells the compiler the array’s name and what type its elements will be not the number of elements in the array. The following would work: private int[ ][ ] a Source code file: ArrayTest04.java 10 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 5. Which two cause a compiler error? (Choose two.) A. B. C. D. E. F. float[ ] f = new float(3); float f2[ ] = new float[ ]; float[ ]f1 = new float[3]; float f3[ ] = new float[3]; float f5[ ] = {1.0f, 2.0f, 2.0f}; float f4[ ] = new float[ ] {1.0f, 2.0f, 3.0f}; Answer: AB Explanation: A causes two compiler errors ( '[' expected and illegal start of expression) because the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax: float[ ] f = new float[3]; B causes a compiler error ( '{' expected ) because the array constructor does not specify the number of elements in the array. The following is the correct syntax: float f2[ ] = new float[3]; C, D, E and F compile without error. Source code file: ArrayTest04.java 11 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 6. Given: 12. 13. 14. float f[ ][ ][ ] = new float[3][][]; float f0 = 1.0f; float[ ][ ] farray = new float[1][1]; Which is valid? A. B. C. D. f[0] = f0; f[0] = farray; f[0] = farray[0]; f[0] = farray[0][0]; Answer: B Explanation: A is incorrect. It results in the compiler error: incompatible types found float required float[][]. This means that f[0] is not the same data type as f0, and, they are not. f[0] is an array element that stores a reference to another array and you cannot assign a primitive value (the float f0) to a reference value. B is correct. You can assign one reference type to another. f[0] is a reference to a two dimensional array, farray is also a reference to a two dimensional array therefore farray can be assigned to f[0]. C is incorrect. It results in the compiler error: incompatible types found float[] required[][].You can assign one reference type to another. f[0] is a reference to a two dimensional array, farray[0] is reference to a one dimensional array therefore farray[0] cannot be assigned to f[0]. D is incorrect. It results in the compiler error: incompatible types found float required float[][].You can assign one reference type to another. f[0] is a reference to a two dimensional array, farray[0][0] is not a reference - it is a float therefore the float value stored in farray[0][0] cannot be assigned to the reference f[0]. Source code file: ArrayTest06.java 12 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 7. Given: 01. 02. 03. 04. 05. 06. 07. 08. public abstract class Test { public abstract void methodA(); public abstract void methodB() { System.out.println("Hello"); } } Which two changes, independently applied, allow this code to compile? (Choose two.) A. B. C. D. E. add a method body to methodA replace lines 5 -7 with a semicolon (";") remove the abstract qualifier from the declaration of Test remove the abstract qualifier from the declaration of methodA remove the abstract qualifier from the declaration of methodB Answer: BE Explanation: Read this question carefully. How would you interpret the question if it had stated “simultaneously applied” rather than “independently applied”? I read this question as five differentseparateindependent questions: 1). “If A is applied to the code as given will it allow the code to compile. 2). “If B is applied to the code as given will it allow the code to compile. 3). “If C is applied to the code as given will it allow the code to compile. 4). “If D is applied to the code as given will it allow the code to compile. 5). “If E is applied to the code as given will it allow the code to compile. Now which two of the above will allow the code to compile. To business, compiling the code as supplied generates a compiler error: abstract methods cannot have a body A is incorrect. If you add a method body to methodA you just get the compiler error message given above for both methodA and methodB. B is correct. This turns methodB into an abstract method and eliminates all compiler errors. C is incorrect. An abstract class is a class that has one or more abstract methods and methodA is an abstract method. D is incorrect. This causes the compiler error: missing method body, or declare abstract. This is self-explanatory. E is correct. This makes methodB into an ordinary method. Source code file: ArrayTest06.java 13 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 8. You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective? A. B. C. D. E. public private protected transient default access Answer: C Explanation: Access modifiers dictate which classes, not which instances, may access features. Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way. private makes a member accessible only from within its own class protected makes a member accessible only to classes in the same package or subclass of the class default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible) final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised abstract declares a method that has not been implemented transient indicates that a variable is not part of the persistent state of an object volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable After examining the above it should be obvious that the access modifier that provides the most restrictions for methods to be accessed from the subclasses of the class from another package is C – protected. A is also a contender but C is more restrictive, B would be the answer if the constraint was the “same package” instead of “any package” in other words the subclasses clause in the question eliminates default. Source code file: 14 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 9. You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective? A. B. C. D. E. public private protected transient default access Answer: E Explanation: The only two real contenders are C and E. Protected access (C) makes a member accessible only to classes in the same package or subclass of the class. While default access (E) makes a member accessible only to classes in the same package. Review the explanation for question 8 for more information. Source code file: 15 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 10. You want to limit access to a method of a public class to members of the same class. Which access accomplishes this objective? A. B. C. D. E. public private protected transient default access Answer: B Explanation: Review the explanations for questions 8 and 9 for more information. Source code file: 16 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 11. Given: 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. public class Test { public static void main(String args[]) { class Foo { public int i = 3; } Object o = (Object)new Foo(); Foo foo = (Foo)o; System.out.println("i = " + foo.i); } } What is the result? A. B. C. D. i =3 Compilation fails. A ClassCastException is thrown at line 6. A ClassCastException is thrown at line 7. Answer: A Explanation: B is not correct because the code compiles and runs ok. C is not correct because you can always cast up the hierarchy tree. D is not correct because o is a reference of type Object that points to a Foo object therefore Foo can be cast to a reference type of Foo. Source code file: Test11.java and Test11a.java 17 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 12. Given: 01. 02. 03. 04. 05. 06. 07. abstract class AbstractIt { abstract float getFloat(); } public class AbstractTest extends AbstractIt { private float f1 = 1.0f; private float getFloat() { return f1; } } What is the result? A. B. C. D. Compilation succeeds An exception is thrown Compilation fails because of an error at line 2 Compilation fails because of an error at line 6 Answer: D Explanation: I got errors at line 4 – Class AbstractTest is public, should be declared in a file named Abstract Test.java And at line 6 – Abstract Test cannot override getFloat() in AbstractIt, attempting to assign weaker access privileges. This is probably the point of the question. The private on line 06 is more restrictive than the default access on line 02. Compilation fails so that rules out answers A and B. Line 2 compiles OK, ruling out answer C. So, by default the answer must be D. Source code file: 18 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 13 Given: 01. 02. 03. 04. public class OuterClass { private double d1 = 1.0; insert code here } Which two are valid if inserted at line 3 (Choose two) A. static class InnerOne { public double methoda() { return d1; } } static class InnerOne { static double methoda() { return d1; } } private class InnerOne { public double methoda() { return d1; } } protected class InnerOne { static double methoda() { return d1; } } public abstract class InnerOne { public abstract double methoda(); } B. C. D. E. Answer: C, E Explanation: A, B and D are incorrect because the non-static variable d1 cannot be referenced from a static context. Static inner classes do not have any reference to an enclosing instance, therefore you cannot use the this keyword, either implied or explicit. Source code file: OuterClass13.java 19 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 14. Click on the Exhibit button 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12 13. public class Test { public int aMethod() { static int i = 0; i++; return i; } public static void main(String args[]) { Test test = new Test(); test.aMethod(); int j = test.aMethod(); System.out.println(j); } } What is the result? A. B. C. D. 0 1 2 Compilation fails Answer: D Explanation: Compilation failed because static was an illegal start of expression method variables do not have a modifier (they are always considered local). Two more errors followed on from this saying on both occasions cannot resolve symbol. Now, if the line 03 from the code was moved from the method to the class (see code below) then C would be the correct answer. public class Test14a { static int i = 0; public int aMethod() { i++; return i; } public static void main(String args[]) { Test14a test = new Test14a(); test.aMethod(); int j = test.aMethod(); System.out.println(j); } } Source code file: Test14.java Test14a.java 20 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 15. Click on the Exhibit button ClassOne.java: 01. 02. 03. 04. 05. package com.abc.pkg1; public class ClassOne { private char var = 'a'; char getVar() { return var; } } ClassTest.java: 01. 02. 03. 04. 05. 06. 07. 08. package com.abc.pkg2; import com.abc.pkg1.ClassOne; public class ClassTest extends ClassOne { public static void main(String[] args) { char a = new ClassOne.getVar(); char b = new ClassOne.getVar(); } } What is the result? A. B. C. D. Compilation fails Compilation succeeds and no exceptions are thrown An exception is thrown at line 5 in ClassTest.java An exception is thrown at line 6 in ClassTest.java Answer: A Explanation: It wouldn’t compile giving two errors: Cannot resolve symbol in line 5 char a = new ClassOne.getVar(); Cannot resolve symbol in line 6 char b = new ClassOne.getVar(); If lines 5 and 6 are amended: line 5: char a = new ClassOne().getVar(); line 6: char b = new ClassOne().getVar(); then the compilation still fails because getVar() has default access and not public or protected. Default allows package access but does not allow access from a sub-class while protected and public do. In this case, this is the reason that compilation fails. Another clue is that the package details are given. Source code file: 21 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 16. Given: 01. 02. 03. 04. 01. 02. 03. 04. 05. 06. package test1; public class Test1 { static int x = 42; } package test2; public class Test2 extends test1.Test1 { public static void main(String[] args) { System.out.println("x = "+x); } } What is the result? A. B. C. D. E. x=0 x = 42 Compilation fails because of an error in line 2 of class Test2 Compilation fails because of an error in line 3 of class Test1 Compilation fails because of an error in line 4 of class Test2 Answer: E Explanation: I got an error in line 4 saying, x is not public in Test1. Test1 cannot be accessed from outside package System.out.println(“x = ” + x); Think it through: ► What access modifier is needed to access a class member in another package? (Answer: public protected.) ► What is the access modifier for x? (Answer: default.) ► What is the scope of default? (Answer: current package and not including sub classes.) From the compile error, in this case, it must be publicprotected. However if you make the variable x in Test1 public static, then all the code compiles and the number 42 is output. Source code file: Test16.java Test16a.java 22 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 17. Given 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. public class Outer { public void someOuterMethod() { Line 3 } public class Inner { } public static void main(String[] argv) { Outer o = new Outer(); Line 8 } } Which instantiates an instance of inner? A. B. C. D. new Inner(); At line 3 new Inner(); At line 8 new o.Inner(); At line 8 new Outer.Inner(); At line 8 Answer: A Explanation: A compiles without problem B gives error – non-static variable cannot be referenced from a static context C package o does not exist D gives error – non-static variable cannot be referenced from a static context Source code file: Outer.java 23 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 18. Click the Exhibit button 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. class Super{ public int i = 0; public Super(String text){ i = 1; } } class Sub extends Super{ public Sub(String text){ i = 2; } public static void main(String args[]){ Sub sub = new Sub("Hello"); System.out.println(sub.i); } } What is the result? A. B. C. D. 0 1 2 Compilation fails. Answer: D Explanation: A default no-args constructor is not created because there is a constructor supplied that has an argument, line 04. Therefore the sub-class constructor must explicitly make a call to the super class constructor: 10. 11. 12. 13. public Sub(String text){ super(text); this must be the first line constructor i = 2; } Super18.java Source code file: 24 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 19. Which three statements are true? (Choose three.) A. B. C. D. E. The default constructor initialises method variables. The default constructor has the same access as its class. The default constructor invokes the no-arg constructor of the superclass. If a class lacks a no-arg constructor, the compiler always creates a default constructor. The compiler creates a default constructor only when there are no other constructors for the class. Answer: B, C, E Explanation: A is the wrong answer because the default constructor does not initialise method variables. If you want to perform some initialisation you will have to write some constructors for your class. B sounds correct as in the example below class CoffeeCup { private int innerCoffee; public CoffeeCup() { } public void add(int amount) { innerCoffee += amount; } ... } The compiler gives default constructors the same access level as their class. In the example above, class CoffeeCup is public, so the default constructor is public. If CoffeeCup had been given package access, the default constructor would be given package access as well. C is correct. The Java compiler generates at least one instance initialisation method for every class it compiles. In the Java class file, the instance initialisation method is named "." For each constructor in the source code of a class, the Java compiler generates one () method. If the class declares no constructors explicitly, the compiler generates a default no-arg constructor that just invokes the superclass's no-arg constructor. As with any other constructor, the compiler creates an () method in the class file that corresponds to this default constructor. D is wrong because the compiler does not always create a default constructor the main reason being that the compiler will not do so if a constructor is already declared by the programmer. E is correct. The compiler creates a default constructor if you do not declare any constructors in your class. Source code file: 25 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 20. Given 01. 02. 03. 04. 05. 06. 07. 08. 09. A. B. C. D. class TestSuper{ TestSuper(int i) {} } class TestSub extends TestSuper{} class TestAll{ public static void main(String[] args){ new TestSub(); } } Compilation fails The code runs without exception An exception is thrown at line 7 An exception is thrown at line 2 Answer: A Explanation: The code does not compile therefore the correct answer is A (the compiler cannot resolve the constructor TestSuper()) TestSub doesn’t have a constructor therefore the default constructor for TestSub calls the no-args constructor of the superclass(TestSuper), but TestSuper doesn’t have a no-args constructor because it already has a constructor with arguments The code will compile and run if you do something like the following: class TestSuper{ TestSuper(int i) {} } class TestSub extends TestSuper{ TestSub() { super(12); } } class TestAll{ public static void main(String[] args){ new TestSub(); } } Source code file: 26 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 21. Given 01. 02. 03. 04. 05. 06. 07. 08. 09. class Base{ Base(){System.out.print("Base");} } public class Alpha extends Base{ public static void main(String[] args){ new Alpha(); new Base(); } } What is the result? A. B. C. D. E. Base BaseBase Compilation fails The code runs with no output An exception is thrown at runtime Answer: B Explanation: A is wrong. It would be correct if either the main class or the subclass had not been instantiated. B is correct. It would be correct if the code had compiled, and the subclass Alpha had been saved in its own file. In this case Java supplies an implicit call from the sub-class constructor to the no-args constructor of the super-class therefore line 06 causes Base to be output. Line 07 also causes Base to be output C is wrong. The code compiles. D is wrong. There is output output. E is wrong. Because there are no errors in the code. Source code file: Alpha.java 27 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 22. Which two allow the class Thing to be instantiated using new Thing( )? (Choose two.) A. B. public class Thing { } public class Thing { public Thing() {} } public class Thing { public Thing(void) {} } public class Thing { public Thing(String s) {} } public class Thing { public void Thing() {} public Thing(String s) {} } C. D. E. Answer: AB Explanation: A is correct because the compiler will add its own default constructor. public class Thing{ } class NextThing{ public static void main(String [] args){ new Thing(); } } B is correct because the programmer has added a constructor to the code. C is not correct. The compiler generates an error: illegal start of type. D is not correct. The code compiles when a string is added to the parameter. public class Thing{ public Thing(String s){} } class NextThing{ public static void main(String [] args){ Thing c1 = new Thing("hello"); } } But the question specifies new Thing( ) not new Thing(“Some king of string”), so this is wrong. E is not correct. This code compiles correctly, but, any code that tries to use the public void Thing() {} constructor will generate a compiler error (cannot resolve symbol) because of the void keyword. Constructors do not have a return type, we are dealing here with a method and not a constructor. Source code files: Thing22a.java, NextThing22a.java Thing22b.java, NextThing22b.java Thing22c.java Thing22d.java, NextThing22d.java Thing22e.java, NextThing22e.java 28 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 23. Given: 01. 02. 03. 04. 05. 06. class A { A( ) { } } class B extends A { } Which two statements are true? (Choose two.) A. B. C. D. Class B'S constructor is public. Class B'S constructor has no arguments. Class B'S constructor includes a call to this( ). Class B'S constructor includes a call to super( ). Answer: B, D Explanation: A is wrong. Class B inherits Class A’s constructor which uses default access. B is correct. Class B inherits Class A’s constructor which has no arguments. C is wrong. There is just no implicit call to this( ). D is correct. There is an implicit call to super( ) added by the compiler. class A{ A() { } } System.out.println("Constructing A. "); class B extends A { B() { System.out.println("Constructing B. "); } } class OrderOfConstruction { public static void main(String args[]) { B b = new B(); } } This code compiles and runs whether B( ) is included or not. Source code files: 29 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) The following is a summary of the key points about constructors: 1) The constructor name must match the name of the class. 2) Constructors must not have a return type. 3) It’s legal (but stupid) to have a method with the same name as the class, but that doesn’t make it a constructor. If you see a return type, it’s a method rather than a constructor. 4) Constructors are not inherited in the same way as normal methods. You can only create an object if the class defines a constructor with an argument list that matches the one your new call provides. 5) If you define no constructor in a class, then the compiler provides a default that takes no arguments. If you define even a single constructor, this default is not provided. The default constructor is always a no-arg constructor. If you want a no-arg constructor and you’ve typed any other constructor(s) into your class code, the compiler won’t provide the no-arg constructor (or any other constructor) for you. In other words, if you’ve typed in a constructor with arguments, you won’t have a no-arg constructor unless you type it in yourself ! 6) The default constructor: Has the same access modifier as the class. Has no arguments. ludes a no-arg call to the super constructor (super()). 7) It is common to provide multiple overloaded constructors – that is, constructors with different argument lists. One constructor can call another using the syntax this(arguments). 8) A constructor delays running its body until the parent parts of the class have been initialised. This commonly happens because of an implicit call to super( ) (no-arg) added by the compiler. You can provide your own call to super(arguments) to control the way the parent parts are initialised. If you do so, it must be the first statement of the constructor. You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs. 9) A constructor can use overloaded constructor versions to support its work. These are invoked using the syntax this(arguments) and if supplied, this call must be the first statement of the constructor. In such conditions, the initialization of the parent class is performed in the overloaded constructor. 10) The only way a constructor can be invoked is from within another constructor. In other words, you can’t write code that actually calls a constructor as follows: class Horse { Horse() { } constructor void doStuff() { Horse(); calling the constructor - illegal! } } 11) Every constructor must have as its first statement either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()). 12) A no-arg constructor is not necessarily the default constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you’re free to put in your own no-arg constructor. 13) You can access static variables and methods, although you can use them only as part of the call to super() or this(). (Example: super(Animal.DoThings())) 30 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 14) Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated. 15) Interfaces do not have constructors. Interfaces are not part of an object’s inheritance tree. 16) The following modifiers are the only ones that can be applied to constructors: public protected (default) – not a modifier but the name of the access if no modifier is specified private - means only code within the class itself can instantiate an object of that type, so if the private-constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class. 31 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 24. Given: 1. public class Test { } What is the prototype of the default constructor? A. B. C. D. E. Test( ) Test(void) public Test( ) public Test(void) public void Test( ) Answer: C Explanation: A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class). C is correct. Just because it is. D is wrong. The void makes the compiler think that this is a method specification – in fact if it were a method specification the compiler would spit it out. E is wrong because this is a method prototype (specification) with the method name the same as the class name – stupid. You know it is a method because it specifies a return type and constructors do not return anything – not even void. Source code files: Test24.java, Tester24.java 32 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 25. In which two cases does the compiler supply a default constructor for class A? (Choose two.) A. B. class A { } class A { public A( ) { } } class A { public A(int x) { } } class Z { } class A extends Z { void A( ) { } } C. D. Answer: A, D Explanation: If you define no constructor in a class, then the compiler provides a default no-args constructor. If you define even a single constructor, this default is not provided. A is correct. No constructor is programmed so the compiler supplies the default. B is wrong. The default constructor is not supplied because the programmer has defined a constructor. C is wrong. The default constructor is not supplied because the programmer has defined a constructor. D is correct. No constructor is programmed so the compiler supplies the default (void A( ) { } is a method specification). Source code files: A.java, B.java, C.java, Z.java 33 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 26. What produces a compiler error? A. class A { public A(int x) { } } class A { } class B extends A { B( ) {} } class A { A( ) { } } class B { public B( ) {} } class Z { public Z(int) {} } class A extends Z { } B. C. D. Answer: D Explanation: A is wrong because it compiles correctly. Class A has a constructor supplied – that’s fine. B is wrong because it compiles correctly. Class A has no constructor so the compiler will supply a default no-arg constructor. Class B extends class A and it has a constructor, now, the compiler will automatically supply the constructor in B with an implicit call to super( ) (no-arg) and this will call the default constructor for A. C is wrong because it compiles correctly. Both classes have constructors. They don’t interfere with each other in any way, that’s OK. D is correct. It does not compile. Class Z has a constructor supplied therefore it has no default constructor. Class A extends Z and has no constructor supplied therefore the compiler will supply a default constructor. Now, the default constructor: includes a no-arg call to the super constructor (super( )), but because class Z has no default constructor we get a compiler error. Equally you could argue that the compiler error is generated because the class Z constructor specification does not supply an identifier, if corrected it could look like this: public Z(int xyz) {}. Source code file: Zd26.java 34 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 27. Given: 01. 02. 03. 04. 05. 06. 07. 08. public class A{ void A() { System.out.println("Class A"); } public static void main(String[] args) { new A(); } } What is the result? A. B. C. D. E. Class A Compilation fails. An exception is thrown at line 2. An exception is thrown at line 6. The code executes with no output. Answer: E Explanation: A is wrong. B is wrong. C is wrong. The code runs without exception. D is wrong. The code runs without exception. E is correct. The specification at line 2 is for a method and not a constructor and this method is never called therefore there is no output. The constructor that is called is the default constructor. Source code file: A27.java 35 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 28. Given: 01. 02. 03. 04. 05. public class ReturnIt { returnType methodA(byte x, double y) { return (long)x y * 2; } } What is the narrowest valid returnType for methodA in line 2? A. B. C. D. E. F. int byte long short float double Answer: F Explanation: However A, B, C, D and E are all wrong. Each of these would result in a narrowing conversion. Whereas we want a widening conversion, therefore the only correct answer is F. Don’t be put off by the long cast, this applies only to the variable x and not the rest of the expression. It is the variable y (of type double) that forces the widening conversion to double. Java’s widening conversions are: - From a byte to a short, an int, a long, a float, or a double. - From a short, an int, a long, a float, or a double. - From a char to an int, a long, a float, or a double. - From an int to a long, a float, or a double. - From a long to a float, or a double. - From a float to a double. Source code file: ReturnIT28.java 36 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 29. Given: 01. 02. 03. 04. 05. public class ReturnIt { returnType methodA(byte x, double y) { return (short)x y * 2; } } What is the narrowest valid returnType for methodA in line 2? A. B. C. D. E. F. int byte long short float double Answer: F Explanation: See the explanation for question 28. Source code file: ReturnIt29.java 37 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 30. Given: 01. 02. 03. 04. 05. 06. 07. class Super { public float getNum( ) { return 3.0f; } } public class Sub extends Super { } Which method, placed at line 6, causes compilation to fail? A. B. C. D. public void getNum( ) { } public void getNum(double d) { } public float getNum( ) { return 4.0f; } public double getNum(float d) { return 4.0d; } Answer: A Explanation: The signature of a method consists of the name of the method and the number and types of formal parameters to the method. A class may not declare two methods with the same signature, or a compile-time error occurs. Java requires that overriding methods in a sub class MUST have :a) The same signature as the overridden method in the super class. (i.e. the same name as the method in the super class and the same arguments as the method in the super class) b) The same return type as the method in the super class (unless the method in the super class is “private”) c) If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private ; otherwise, a compile-time error occurs. (JLS 8.4.6.3) Permitted variations in overriding method definition – d) the subclass (overriding) method may NOT be more private than the superclass (overridden) method but may be less. (Boone & Stanek p. 210) (JLS 8.4.6.3) e) the subclass method cannot add Exception types to those defined in the superclass method declaration. f) Overloaded Methods. If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures. (JLS 8.4.7) Overloaded methods let you reuse the same method name in a class, but with different arguments (and optionally, a different return type). Overloading a method often means you’re being a little nicer to those who call your methods, because your code takes on the burden of coping with different argument types rather than forcing the caller to do conversions prior to invoking your method. The rules are simple: 38 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) Overloaded methods must change the argument list. Overloaded methods can change the return type. Overloaded methods can change the access modifier. Overloaded methods can declare new or broader checked exceptions. A method can be overloaded in the same class or in a subclass. The following table gives some examples of illegal overrides: Illegal Override Code private void eat() { } public void eat() throws IOException { } Problem with the Code Access modifier is more restrictive Declares a checked exception not declared by superclass version A legal overload, not an override, because the argument list changed Not an override because of the return type, but not an overload either because there’s no change in the argument list public void eat(String food) { } public String eat() { } Answer A fails to compile. Because of incompatible return type. The return type in the super class is float and the return type in A is void. It fails point b) above. In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed. Answer B – compiled. This is not an override because of the change in the return type, the compiler considers this to be an overloaded method because the argument list has changed. Answer C – compiled. Standard overriding - overrides the super class method, changing the implementation. Answer D – compiled. This is not an override because of the change in the return type, the compiler considers this to be an overloaded method because the argument list has changed. NOTE – as given compilation fails in any case – Super.java:5: class Sub is public, should be declared in a file named Sub.java public class Sub extends Super{ In order to compile anything – 1) make both classes access default. 2) make Super public and Sub default 3) make both classes public Source code file: 39 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 31. Click the Exhibit button. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. class Super { public int getLength() { return 4; } } public class Sub extends Super { public long getLength() { return 5; } public static void main(String[] args) { Super sooper = new Super(); Sub sub = new Sub(); System.out.println( sooper.getLength() + "," + sub.getLength()); } } What is the output? A. B. C. D. E. 4,4 4,5 5,4 5,5 Compilation fails. Answer: E Explanation: Refer to the explanation given in question 30 The return type of getLength( ) in the super class is int and the return type in the sub class is long. In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed. Source code file: 40 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 32. Click the Exhibit button. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. class Super { public Integer getLength() {return new Integer(4); } } public class Sub extends Super { public Long getLength() {return new Long(5); } public static void main(String[] args) { Super sooper = new Super(); Sub sub = new Sub(); System.out.println( sooper.getLength().toString() + “,” + sub.getLength().toString() ); } } What is the output? A. B. C. D. E. 4,4 4,5 5,4 5,5 Compilation fails. Answer: E Explanation: Refer to the explanation given in question 30 for more information. Answer E is correct, compilation fails – The return type of getLength( ) in the super class is an object of reference type Integer and the return type in the sub class is an object of reference type Long. In other words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed. Source code file: 41 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 33. Given: 01. 02. 03. 01. 02. 03. public class X { public X aMethod() {return this;} } public class Y extends X { } Which two methods can be added to the definition of class Y? (Choose two.) A. B. C. D. E. public void aMethod() {} private void aMethod() {} public void aMethod(String s) {} private Y aMethod() { return null;} public X aMethod() { return new Y(); } Answer: CE Explanation: Refer to the explanation given in question 30 for more information. Answer A fails - it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed. Answer B fails - the subclass (overriding) method may NOT be more private than the superclass (overridden) method. Answer C is acceptable – The sub class aMethod is not an override because of the change in the return type, it overloads the super class aMethod – matching name but different arguments. Answer D fails – - the subclass (overriding) method may NOT be more private than the superclass (overridden) method. Answer E is acceptable - The sub class aMethod overrides the super class aMethod – matching signature and return type. Source code file: 42 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 34. Click the Exhibit button. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. interface Beta {} class Alpha implements Beta { String testIt() { return "Tested"; } } public class Main1 { static Beta getIt() { return new Alpha(); } public static void main(String[] args) { Beta b = getIt(); System.out.println(b.testIt()); } } What is the result? A. B. C. D. Tested Compilation fails. The code runs with no output. An exception is thrown at runtime. Answer: B Explanation: a) A variable whose declared type is an interface type may have as its value a reference to any instance of a class which implements the specified interface. It is not sufficient that the class happen to implement all the abstract methods of the interface; the class or one of its superclasses must actually be declared to implement the interface, or else the class is not considered to implement the interface. JLS 9 So variable b of type Beta may hold a reference to class Alpha, which implements Beta – line 3. Method getIt() of Main1 class returns a reference to class Alpha so variable b should be created with that reference – line 14. b) The type of the object reference determines which variable you access. The type of the underlying object determines which method you invoke. “All-in-one Java 2 Exam Guide” Boone & Stanek p.109 Answer A – is wrong. Method testIt() is not a member of Beta so cannot be referred to by variable b (which is type Beta) – EXCEPT that the underlying reference is type Alpha and testIt() is a method of Alpha - see b) above? But the compiler can only use the compile-time reference (type Beta) to resolve the method invocation so compilation fails. If Beta declared testIt() then the reference would be valid and the overriding testIt() method in Alpha could be invoked. (providing the access modifiers are correct - the overriding method may NOT be more private than the overridden method but may be less. (JLS 8.4.6.3) Then A would be the correct answer. 43 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) Answer B is correct. The compiler complains that it “cannot resolve symbol” (the testIt() method). See A above. Answer C is wrong – cannot see a means by which this could be correct. Answer D is wrong – cannot see a means by which this could be correct. Source code file: 44 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 35. Click the Exhibit button. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. interface Animal { void soundOff(); } class Elephant implements Animal { public void soundOff() { System.out.print1n("Trumpet"); } } class Lion implements Animal { public void soundOff() { System.out.print1n("Roar"); } } class Alpha1 { static Animal get(String choice) { if (choice.equalsIgnoreCase("meat eater") ) { return new Lion(); } else { return new Elephant(); } } } Which compiles? A. B. C. D. new Animal().soundOff(); Elephant e = new Alpha1(); Lion l = Alpha1.get("meat eater"); new Alpha1().get("veggie").soundOff(); Answer: D Explanation: Refer to the explanation of question 34 for more information. Answer A fails –Animal is an interface and cannot be instantiated. Answer B fails - Alpha1 is not a subclass of Elephant so Elephant is ineligible to hold a reference to Alpha1. A reference to a class may only be assigned to a variable that has a class type of the same class or the reference must be to a subclass of the variable’s type. Answer C fails – ompatible types. Alpha1.get() returns a reference of type Animal. Animal is an interface implemented by Lion and Elephant. The actual reference returned is to either Elephant or Lion. So a variable of type Animal could hold a reference to class Lion (JLS 5.2) but not the reverse, but the variable l (type Lion) receives a reference of either type Lion or type Elephant which can be cast from type Animal (interface) to type Lion (class), a narrowing reference conversion (JLS 5.1.5). This would compile if the returned reference was cast to type Lion. Answer D will compile - Alpha1.get returns a reference of type Animal which contains a reference to either class Lion or class Elephant, both of which implement interface Animal. The method .soundOff () is implemented by both Elephant and Lion so can be invoked by a refernce to either class. Source code file: 45 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 36. Given: 01. 02. 03. class A { protected int method1(int a, int b) {return 0; } } Which two are valid in a class that extends class A? (Choose two.) A. B. C. D. E. public int method1(int a, int b) {return 0; } private int method1(int a, int b) { return 0; } private int method1(int a, long b) {return 0; } public short method1(int a, int b) { return 0; } static protected int method1(int a, int b) { return 0; } Answer: A, C Explanation: A is correct - because the class that extends A is just simply overriding method1. B is wrong - because it can't override as there are less access privileges in the subclass method1. C is correct - because it is overloading and not overriding (it has different arguments to the super class method). D is wrong - because to override it, the return type needs to be an integer. The different return type means that the method is not overriding but the same argument list means that the method is not overloading. Conflict – compile time error. E is wrong - because you can't override a method and make it a class method i.e. using static. Source code file: 46 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 37. Click the Exhibit button. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. class A { final public int methodl(int a, int b) {return 0; } } class B extends A { public int method1 (int a, int b) {return 1; } } public class Test { public static void main(String args[]) { B b; System.out.println("x = " + bhod1(0, 1)); } } What is the result? A. B. C. D. x=0 x=1 Compilation fails. An exception is thrown at runtime. Answer: C Explanation: The code doesn't compile because the method in class A is final and so cannot be overridden. Source code file: 47 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 38. Given: 11. 12. 13. 14. switch(x) { default: System.outprintln("Hello"); } Which two are acceptable types for x? (Choose two.) A. B. C. D. E. F. byte long char float Short Long Answer: A, C Explanation: Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables. Source code file: 48 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 39. Given: 11. 12. 13. 14. 15. int x = 3; int y = 1; if (x = y) { System.out.println("x =" + x); } What is the result? A. B. C. D. E. x=1 x=3 Compilation fails. The code runs with no output An exception is thrown at runtime. Answer: C Explanation: Line 13 uses an assignment as opposed to comparison. Because of this, the if statement receives an integer value instead of a boolean. And so the compilation fails. Source code file: 49 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 40. Click the Exhibit button. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. public class SwitchTest { public static void main(String[] args) { System.out.println("value =" + switchIt(4)); } public static int switchIt(int x) { int j = 1; switch (x) { case l: j++; case 2: j++; case 3: j++; case 4: j++; case 5: j++; default: j++; } return j + x; } } What is the result? A. B. C. D. E. F. value = 3 value = 4 value = 5 value = 6 value = 7 value = 8 Answer: F Explanation: Because there are no break statements, once the desired result is found, the program continues though each of the remaining options. Source code file: 50 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 41. Click the Exhibit button. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. public class Test { public static void main(String args[]) { int i = 1, j = 0; switch(i) { case 2: j += 6; case 4: j += 1; default: j += 2; case 0: j += 4; } System.out.println("j = " + j); } } What is the result? A. B. C. D. E. F. 0 2 4 6 9 13 Answer: D Explanation: Because there are no break statements, the program gets to the default case and adds 2 to j, then goes to case 0 and adds 4 to the new j. The result is j = 6. Source code file: 51 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 42. Given: 11. 12. 13. 14. 15. 16. Float f = new Float("12"); switch (f) { case 12: System.out.println("Twelve"); case 0: System.out.println("Zero"); default: System.out.println("Default"); } What is the result? A. B. C. D. Zero Twelve Default Twelve Zero Default Compilation fails. E. Answer: E Explanation: The switch statement can only be supported by integers or variables more "narrow" than an integer i.e. byte, char, short. Here a Float wrapper object is used and so the compilation fails. Source code file: 52 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 43. Given: 11. 12. 13. 14. 15. 16. 17. 18. 19. for(int i = 0; i < 3; i++) { switch(i) { case 0: break; case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); } } System.out.println("done"); What is the result? A. B. C. D. E. done one two done one two three done one two three two three done Compilation fails Answer: D Explanation: The variable i will have the values 0, 1 and 2. When i is 0, nothing will be printed because of the break in case 0. When i is 1, “one two three ” will be output because case 1, case 2 and case 3 will be executed (they don’t have break statements). When i is 2, “two three ” will be output because case 2 and case 3 will be executed (again no break statements). Finally, when the for loop finishes “done” will be output. Source code file: Q43.java 53 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 44. Click the Exhibit button. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. public void foo( boolean a, boolean b){ if( a ) { System.out.println("A"); } else if(a && b) { System.out.println( "A&&B"); } else { if ( !b ) { System.out.println( "notB") ; } else { System.out.println( "ELSE" ) ; } } } Which is correct? A. B. C. D. If a is true and b is true then the output is "A&&B" If a is true and b is false then the output is "notB" If a is false and b is true then the output is "ELSE" If a is false and b is false then the output is "ELSE" Answer: C Explanation: A is wrong. The output is “A”. When a is true, irrespective of the value of b, only the line 13 output will be executed. The condition at line 14 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be “A&&B”. B is wrong. The output is “A”. When a is true, irrespective of the value of b, only the line 13 output will be executed. C is correct. The output is "ELSE". Only when a is false do the output lines after 16 get some chance of executing. D is wrong. The output is “notB”. Source code file: Q44.java. 54 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 45. Given: 11. 12. 13. 14. 15. 16. 17. 18. public void test(int x) { int odd = x % 2; if(odd) { System.out.println("odd"); } else { System.out.println("even"); } } Which statement is true? A. B. C. D. E. Compilation fails. "odd" will always be output. "even" will always be output "odd" will be output for odd values of x, and "even" for even values. "even" will be output for odd values of x, and "odd" for even values. Answer: A Explanation: The compiler will complain because of incompatible types (line 13), the if expects a boolean but it gets an integer. Don’t be mislead by this question, it has nothing to do with modulus (remainder operator). Source code file: Q45.java. 55 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 46. Given: 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. boolean bool = true; if(bool = false) { System.out.println("a"); } else if(bool) { System.out.println("b"); } else if(!bool) { System.out.println("c"); } else { System.out.println("d"); } What is the result? A. B. C. D. E. a b c d Compilation fails. Answer: C Explanation: Whoever devised this question pulled a bit of a cunning stunt. Look closely at line 12, is this an equality check (==) or an assignment (=). The condition at line 12 evaluates to false and also assigns false to bool. bool is now false so the condition at line 14 is not true. The condition at line 16 checks to see if bool is not true ( if !(bool == true) ), it isn’t so line 17 is executed. Source code file: Q46.java. 56 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 47. Given: 11. 12. 13. 14. 15. 16. 17. int i = l, j = -1; switch (i) { case 0, 1: j = 1; case 2: j = 2; default: j = 0; } System.out.println("j = " + j); What is the result? A. B. C. D. E. j = -1 j=0 j=1 j=2 Compilation fails. Answer: E Explanation: The case statement takes only a single argument. The case statement on line 13 is given two arguments so the compiler complains. Source code file: Q47.java. 57 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 48. Given: 11. 12. 13. 14. 15. 16. 17. 18. int i = O; while(1) { if(i == 4) { break; } ++i; } System.out.println("i = " + i); What is the result? A. B. C. D. E. i=0 i=3 i=4 i=5 Compilation fails. Answer: E Explanation: Compilation fails because the argument of the while loop, the condition, must be of primitive type boolean. In Java, 1 does not represent the true state of a boolean, rather it is seen as an integer. A to D cannot be correct as the loop condition breaks down. Read the Notes On Unlabeled Statements after Q48. Source code file: 58 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) Notes On Unlabeled Statements Both the break statement and the continue statement can be unlabeled or labeled. Although it’s far more common to use break and continue unlabeled, the exam expects you to know how labeled break and continue work. A break statement (unlabeled) will exit out of the innermost looping construct and proceed with the next line of code beyond the loop block. The following example demonstrates a break statement: boolean problem = true; while (true) { if (problem) { System.out.println("There was a problem"); break; } } next line of code In the previous example, the break statement is unlabeled. The following is another example of an unlabeled continue statement: while (!EOF) { read a field from a file if (there was a problem) { move to the next field in the file continue; } } In this example, there is a file being read from one field at a time. When an error is encountered, the program moves to the next field in the file and uses the continue statement to go back into the loop (if it is not at the end of the file) and keeps reading the various fields. If the break command were used instead, the code would stop reading the file once the error occurred and move on to the next line of code. The continue statement gives you a way to say, “This particular iteration of the loop needs to stop, but not the whole loop itself. I just don’t want the rest of the code in this iteration to finish, so do the iteration expression and then start over with the test, and don’t worry about what was below the continue statement.” 59 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 49. Given: 11. 12. 13. 14. 15. 16. 17. 18. int i = 1, j = 10; do { if(i > j) { break; } j--; } while (++i < 5); System.out.println("i = " + i + " and j = " + j); What is the result? A. B. C. D. E. i = 6 and j = 5 i = 5 and j = 5 i = 6 and j = 4 i = 5 and j = 6 i = 6 and j = 6 Answer: D Explanation: This loop is a do-while loop, which always executes the code block within the block at least once, due to the testing condition being at the end of the loop, rather than at the beginning. This particular loop is exited prematurely if i becomes greater than j. The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and then tests the loop condition, where a pre-incremented by one i is tested for being lower than 5. The test is at the end of the loop, so i can reach the value of 5 before it fails. So it goes, start: 1, 10 2, 9 3, 8 4, 7 5, 6 loop condition fails. Read the Notes On Unlabeled Statements after Q48. Source code file: 60 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 50. Given: 11. 12. 13. 14. 15. 16. 17. 18. int i = 1, j = 10; do { if(i > j) { continue; } j--; } while (++i < 6); System.out.println("i = " + i + " and j = " + j); What is the result? A. B. C. D. E. i = 6 and j = 5 i = 5 and j = 5 i = 6 and j = 4 i = 5 and j = 6 i = 6 and j = 6 Answer: A Explanation: This is similar to the last question. j is decremented by one within the loop, i is incremented by one in the test condition for the do-while loop. The if condition within the loop is never accessed, since the only time i is greater than j arises in the loop test condition when i is incremented to six, breaking the condition. So the continue isn’t actually called. Once again the variables follow: 1,10 2, 9 3, 8 4, 7 5, 6 6, 5 break loop Read the Notes On Unlabeled Statements after Q48. Source code file: Q50.java. 61 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 51. Given: 11. 12. 13. 14. 15. 16. 17. int i = l, j = 10; do { if(i++ > --j) { continue; } } while (i < 5); System.out.println("i = " + i + " and j = " + j); What is the result? A. B. C. D. E. i = 6 and j = 5 i = 5 and j = 5 i = 6 and j = 4 i = 5 and j = 6 i = 6 and j = 6 Answer: D Explanation: D is correct. Again, it’s a do-while loop, the continue does not get called into play. The pattern is 1,10 2, 9 3, 8 4, 7 5, 6 do-while condition fails, line is printed with current i, j values. Read the Notes On Unlabeled Statements after Q48. Source code file: 62 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 52. Given: 11. 12. 13. 14. 15. int x = l, y = 6; while (y--) { x++; } System.out.println("x = " + x +" y = " + y); What is the result? A. B. C. D. E. x=6y=0 x=7y=0 x = 6 y = -1 x = 7 y = -1 Compilation fails. Answer: E Explanation: Compilation fails because the while loop demands a boolean argument for it’s looping condition, but in the code, it’s given an int argument. while(true){insert code here} Source code file: 63 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 53. Given: 11. 12. 13. 14. 15. 16. 17. 18. 19. int i = 0, j = 5; tp: for (;;) { i++; for (;;) { if(i > --j) { break tp; } } System.out.println("i =" + i + ", j = " + j); What is the result? A. B. C. D. E. i = 1, j = 0 i = 1, j = 4 i = 3, j = 4 i = 3, j = 0 Compilation fails. Answer: E Explanation: If you examine the code carefully you will notice a missing curly bracket, this would cause the code to fail. For more information see the Notes on Labelled Statements after Q53a. Source code file: Q53.java 64 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 53a. Given: 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. int i = 0, j = 5; tp: for (;;) { i++; for (;;) { if(i > --j) { break tp; } } } System.out.println("i =" + i + ", j = " + j); What is the result? A. B. C. D. E. i = 1, j = 0 i = 1, j = 4 i = 3, j = 4 i = 3, j = 0 Compilation fails. Answer: A Explanation: This is the same question as Q53 but I have inserted line 19 with a closing curly bracket. This code contains two nested infinite loops. The outer loop executes only once and sets the value of i to 1 (line 13). The second loop then starts and compares i with a decrementing j. The sequence of comparisons (line 15) goes like: i j 1 4 1 3 1 2 1 1 1 0 At this point the break to tp on line 16 is executed, causing the labelled loop (the outer one) to be exited and program execution to continue at line 20. For more information see the Notes on Labelled Statements after Q53a. Source code file: Q53a.java 65 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) Notes on Labelled Statements You need to understand the difference between labeled and unlabeled break and continue. The labeled varieties are needed only in situations where you have a nested loop, and need to indicate which of the nested loops you want to break from, or from which of the nested loops you want to continue with the next iteration. A break statement will exit out of the labeled loop, as opposed to the innermost loop, if the break keyword is combined with a label. An example of what a label looks like is in the following code: foo: for (int x = 3; x < 20; x++) { while(y > 7) { y--; } } The label must adhere to the rules for a valid variable name and should adhere to the Java naming convention. The syntax for the use of a label name in conjunction with a break statement is the break keyword, then the label name, followed by a semicolon. A more complete example of the use of a labeled break statement is as follows: outer: for(int i = 0; i < 10; i++) { while (y > 7) { System.out.println("Hello"); break outer; } end of inner loop System.out.println("Outer loop."); Won't print } end of outer for loop System.out.println("Good-Bye"); Running this code produces: Hello Good-Bye In this example the word Hello will be printed one time. Then, the labeled break statement will be executed, and the flow will exit out of the loop labeled outer. The next line of code will then print out Good-Bye. Let’s see what will happen if the continue statement is used instead of the break statement. The following code example is the same as the preceding one, with the exception of substituting continue for break: outer: for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { System.out.println("Hello"); continue outer; } end of inner loop System.out.println("outer"); Never prints } System.out.println("Good-Bye"); Running this code produces: Hello Hello … … Hello Good-Bye In this example, Hello will be printed ten times. After the continue statement is executed, the flow continues with the next iteration of the loop identified with the label. Finally, when the condition in the outer loop evaluates to false, the i loop will finish and Good-Bye will be printed. 66 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 53b. Given: 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. int i = 0, j = 5; tp: for (;;) { i++; for (;;) { if(i > --j) { break tp; } } System.out.println("i =" + i + ", j = " + j); } What is the result? A. B. C. D. E. i = 1, j = 0 i = 1, j = 4 i = 3, j = 4 i = 3, j = 0 Compilation fails. Answer: E Explanation: This is the same question as Q53 (see also Q53a) but I have inserted line 20 with a closing curly bracket. Compilation will fail on this code, because the line: System.out.println("i =" + i + "j = " + j); cannot be called. The compiler returns unreachable statement error. When the loop breaks, it returns to the label, which is before the original for loop. The program goes to the loop, increments and decrements the variables, until breaking condition is met, then exits the loop to the first executable statement after line 20 (the end of the labelled block). For more information see the Notes on Labelled Statements after Q53a. Source code file: Q53b.java 67 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 54. Given: 11. 12. 13. 14. for (int i = 0; i < 4; i += 2) { System.out.print(i + “ “); } System.out.println(i); What is the result? A. B. C. D. E. 024 0245 01234 Compilation fails. An exception is thrown at runtime. Answer: D Explanation: Compilation fails on the line 14 - System.out.println(i); as the variable i has only been declared within the for loop. It is not a recognised variable outside the code block of loop. Source code file: See also: Q54.java. Q195 68 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 55. Given: 10. 11. 12. 13. 14. int i = 0; for (; i < 4; i += 2) { System.out.print(i + “ “); } System.out.println(i); What is the result? A. B. C. D. E. 024 0245 01234 Compilation fails. An exception is thrown at runtime. Answer: A Explanation: As the variable i was declared outside the loop it can be referenced at any point after its declaration, both inside and outside the for loop. Source code file: Q55.java. 69 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 56. Click the Exhibit button. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. public class Alpha1 { public static void main( String[] args ) { boolean flag; int i = 0; do { flag = false; System.out.println( i++ ); flag = i < 10; continue; } while ( (flag)? true:false); } } What is the result? A. B. C. D. E. F. 000000000 0123456789 Compilation fails The code runs with no output The code enters an infinite loop An exception is thrown at runtime Answer: B Explanation: A “do..while” loop will always run at least once, however while flag is set to true the loop will continue to run. On each loop flag is set to true if i is less than 10. Once i is 10 or greater, flag will be set to false and the loop will stop. A continue statement may occur only in a while, do, or for statement; statements of these three kinds are called iteration statements. Control passes to the loop-continuation point of an iteration statement. A continue statement with no label attempts to transfer control to the innermost enclosing while, do, or for statement of the immediately enclosing method or initializer block; this statement, which is called the continue target, then immediately ends the current iteration and begins a new one. In the code above the loop-continuation point is located at line 9. This leads to the conclusion that there is no need for the continue statement at line 8. If lines of code were inserted between lines 8 and 9 then the compiler would complain about unreachable code. For more information on the continue statement see Notes On Unlabeled Statements after Q48. Source code file: Q56.java Q56a.java 70 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 57. Given: 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. public class Delta { static boolean foo(char c) { System.out.print; return true; } public static void main( String[] argv ) { int i = 0; for (foo('A'); foo('B')&&(i < 2); foo('C')) { i++; foo('D'); } } } What is the result? A. B. C. D. ABDCBDCB ABCDABCD Compilation fails. An exception is thrown at runtime. Answer: A Explanation: ‘A’ is only printed once at the very start as it is in the initialisation section of the for loop. The loop will only initialise that once. ‘B’ is printed as it is part of the test carried out in order to run the loop. ‘D’ is printed as it is in the loop. ‘C’ is printed as it is in the increment section of the loop and will ‘increment’ only at the end of each loop. Here ends the first loop. Again ‘B’ is printed as part of the loop test. ‘D’ is printed as it is in the loop. ‘C’ is printed as it ‘increments’ at the end of each loop. Again ‘B’ is printed as part of the loop test. At this point the test fails because the other part of the test (i0); return sal; } private int getAge(Employee e) { assert validEmployee( e); int age = lookupAge(e); assert (age>0); return age; } Which line is a violation of appropriate use of the assertion mechanism? A. B. C. D. line 21 line 23 line 27 line 29 Answer: A Explanation: A is the correct answer. It is a violation of the appropriate use of the assertion mechanism. You do not use the assertion mechanism to validate the arguments of a public method. By convention, pre-conditions on public methods are enforced by explicit checks that throw particular, specified exceptions. For example: ** * Sets the refresh rate. * * param rate refresh rate, in frames per second. * throws IllegalArgumentException if rate MAX_REFRESH_RATE. * public void setRefreshRate(int rate) { Enforce specified precondition in public method if (rate MAX_REFRESH_RATE) throw new IllegalArgumentException("Illegal rate: " + rate); setRefreshInterval(1000rate); } This convention is unaffected by the addition of the assert construct. Do not use assertions to check the primitive parameters of a public method. An assert is inappropriate because the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError. B is wrong. It is not a violation of the appropriate use of the assertion mechanism. It is OK to assert the post-conditions of a method no matter whether it is a public or a private method. C is wrong. It is not a violation of the appropriate use of the assertion mechanism. It is OK to assert the pre-conditions of a private method. 86 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) D is wrong. Line 29 does not violate the appropriate use of the assertion mechanism. You can use an assertion to test a nonpublic method's prepostcondition that you believe will be true no matter what a client does with the class. For example, an assertion is appropriate in the following "helper method" that is invoked by the previous method (see explanation B above): ** * Sets the refresh interval * (which must correspond to a legal frame rate). * * param interval refresh interval in milliseconds. * private void setRefreshInterval(int interval) { Confirm adherence to precondition in nonpublic method assert interval > 0 && interval x); more code assuming y is greater than x } Simple private void doStuff() { assert (y > x): "y is " + y " " x is " + x; more code assuming y is greater than x } The difference between them is that the simple version adds a second expression, separated from the first (boolean expression) by a colon, that adds a little more information to the stack trace. Both versions throw an immediate AssertionError, but the simple version gives you a little more debugging help while the really simple version simply tells you that your assumption was false. Assertions are typically enabled when an application is being tested and debugged, but disabled when the application is deployed. The assertions are still in the code, although ignored by the JVM, so if you do have a deployed application that starts misbehaving, you can always choose to enable assertions in the field for additional testing. 88 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 73. Which fragment is an example of inappropriate use of assertions? A. B. assert (!(mapntains(x))); map.add(x); if(x>0) { } else { assert (x == 0); } public void aMethod(int x) { assert (x > 0); } assert(invariantCondition()); return retval; switch (x) { case 1: break; case 2: break; default: assert (x == 0); } C. D. E. Answer: C Explanation: Assert shouldn’t be used for preconditions within public methods. Also see the explanation for Q72. Source code file: Exam Objectives: 2.4 and 2.5 89 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 74. Given: 01. 02. 03. 04. 05. 06. 07. public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) : "assertion failed"; System.out.println("finished"); } } What is the result? A. B. C. D. finished Compilation fails. An AssertionError is thrown. An AssertionError is thrown and finished is output Answer: C Explanation: An assertion Error is thrown as normal giving the output “assertion failed”. The word “finished” is not printed (ensure you run with the –ea option) Assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown, and also in this case with the error’s detail message “assertion failed”. The detail message is supplied by the assert statement in line 04. Source code file: Exam Objectives: 2.4 and 2.5 90 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 75. Given: 01. public class Test { 02. public static void main(String[] args) { 03. int x = 0; 04. assert (x > 0) ? "assertion failed" : "assertion passed" ; 05. System.out.println("finished"); 06. } 07. } What is the result? A. B. C. D. E. finished Compiliation fails. An AssertionError is thrown and finished is output. An AssertionError is thrown with the message "assertion failed." An AssertionError is thrown with the message "assertion passed." Answer: B Explanation: Compilation Fails. You can’t use the Assert statement in a similar way to the ternary operator. Don’t confuse. Source code file: Exam Objectives: 2.4 and 2.5 91 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 76. Which three statements are true? (Choose three.) A. B. C. D. E. Assertion checking is typically enabled when a program is deployed. It is never appropriate to write code to handle failure of an assert statement. Assertion checking is typically enabled during program development and testing. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis. Answer: BCE Explanation: A is wrong. It’s just not true. B is correct. You’re never supposed to handle an assertion failure. C is correct. Assertions let you test your assumptions during development, but the assertion code—in effect—evaporates when the program is deployed, leaving behind no overhead or debugging code to track down and remove. D is wrong. See the explanation for E below. E is correct. Assertion checking can be selectively enabled or disabled on a per-package basis. Note that the package default assertion status determines the assertion status for classes initialized in the future that belong to the named package or any of its "subpackages". The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class. See Example of methods used to set assertion status on both a per-class basis, and per package basis j2sdk1.4.0\docs\guide\lang\assert.html Section “Enabling and Disabling Assertions“ Source code file: Exam Objectives: 2.4 and 2.5 92 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 77. Which statement is true about assertions in the Java programming language? A. B. C. D. Assertion expressions should not contain side effects. Assertion expression values can be any primitive type. Assertions should be used for enforcing preconditions on public methods. An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method. Answer: A Explanation: A is correct. Because assertions may be disabled, programs must not assume that the boolean expressions contained in assertions will be evaluated. Thus these expressions should be free of side effects. That is, evaluating such an expression should not affect any state that is visible after the evaluation is complete. Although it is not illegal for a boolean expression contained in an assertion to have a side effect, it is generally inappropriate, as it could cause program behaviour to vary depending on whether assertions are enabled or disabled. Assertion checking may be disabled for increased performance. Typically, assertion checking is enabled during program development and testing and disabled for deployment. B is wrong. Because you assert that something is “true”. True is Boolean. So, an expression must evaluate to Boolean, not int or byte or anything else. Use the same rules for an assertion expression that you would use for a while condition. C is wrong. Usually, enforcing a precondition on a public method is done by conditionchecking code that you write yourself, to give you specific exceptions. Also see the explanation for question 72. j2sdk1.4.0\docs\guide\lang\assert.html : See “Preconditions, Postconditions, and Class Invariants” D is wrong. “You’re never supposed to handle an assertion failure” Not all legal uses of assertions are considered appropriate. As with so much of Java, you can abuse the intended use for assertions, despite the best efforts of Sun’s Java engineers to discourage you. For example, you’re never supposed to handle an assertion failure. That means don’t catch it with a catch clause and attempt to recover. Legally, however, AssertionError is a subclass of Throwable, so it can be caught. But just don’t do it! If you’re going to try to recover from something, it should be an exception. To discourage you from trying to substitute an assertion for an exception, the AssertionError doesn’t provide access to the object that generated it. All you get is the String message. Source code file: Exam Objectives: 2.4 and 2.5 93 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 78. Which statement is true? A. B. C. D. Assertions can be enabled or disabled on a class-by-class basis. Conditional compilation is used to allow tested classes to run at full speed. Assertions are appropriate for checking the validity of arguments in a method. The programmer can choose to execute a return statement or to throw an exception if an assertion fails. Answer: A Explanation: A is correct. The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class. Also see the explanation for Q76. B is wrong. Is there such a thing as conditional compilation in Java? C is wrong. For private methods - yes. But do not use assertions to check the parameters of a public method. An assert is inappropriate in public methods because the method guarantees that it will always enforce the argument checks. A public method must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError. D is wrong. Because you’re never supposed to handle an assertion failure. That means don’t catch it with a catch clause and attempt to recover. Source code file: Exam Objectives: 2.4 and 2.5 94 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 79. Given: 1. 2. 3. 4. 5. public class Test79{ public static void main( String[] argv){ insert statement here } } Which statement, inserted at line 3, produces the following output? Exception in thread "main" java.lang.AssertionError: true at Test79.main(Test.java:3) A. B. C. D. E. assert true; assert false; assert false: true; assert false == true; assert false: false; Answer: C Explanation: An assertion will raise an exception if its condition returns false. In the answers given above A returns true while B, C, D and E all return false. My conclusion is that the question must be rephrased to: Which statement, inserted at line 3 does not raise an exception? Answer: A A Compiles and runs without exception B. Compiles but fails at runtime with the exception: Exception in thread "main" java.lang.AssertionError: true at Test79.main(Test79.java:3) C Compiles but fails at runtime with the exception: Exception in thread "main" java.lang.AssertionError: true at Test79.main(Test79.java:3) D Compiles but fails at runtime with the exception: Exception in thread "main" java.lang.AssertionError at Test79.main(Test79.java:3) E Compiles but fails at runtime with the exception: Exception in thread "main" java.lang.AssertionError: false at Test79.main(Test79.java:3) Source code file: Test79.java Compile with: Run with: 2.4 and 2.5 javac -source 1.4 Test79.java java -ea Test79 Exam Objectives: 95 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) 80. Given: 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. public class Test { public void foo() { assert false; assert false; } public void bar(){ while(true){ assert false; } assert false; } } What causes compilation to fail? A. B. C. D. Line 13 Line 14 Line 18 Line 20 Answer: D Explanation: D is correct. Compilation fails because of an unreachable statement at line 20. It is a compile-time error if a statement cannot be executed because it is unreachable. The question is now, why is line 20 unreachable? If it is because of the assert then surely line 14 would also be unreachable. The answer must be something other than assert. Examine the following: A while statement can complete normally if and only if at least one of the following is true: - The while statement is reachable and the condition expression is not a constant expression with value true. -There is a reachable break statement that exits the while statement. The while statement at line 17 is infinite and there is no break statement therefore line 20 is unreachable. You can test this with the following code: public class Test80 { public void foo() { assert false; assert false; } public void bar(){ while(true){ assert false; break; } assert false; } } Source code file: Exam Objectives: Test80.java Compile with: 2.4 and 2.5 javac -source 1.4 Test80.java 96 of 256 Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035) General Notes On Garbage Collection: The heap is that part of memory where Java objects live, and it’s the one and only part of memory that is in any way involved in the garbage collection process. So, all of garbage collection revolves around making sure that the heap has as much free space as possible. This boils down to deleting any objects that are eligible for garbage collection. An object is eligible for garbage collection when no live thread can access it – in other words when an object on the heap is no longer reachable by the Java program running. In other, other words, if an object can be accessed from a live thread, it can’t be garbage collected. About the only thing you can guarantee is that if you are running very low on memory, the garbage collector will run before the JVM throws an OutOfMemoryException. Some points about the finalize() method that you need to remember: For any given object, finalize() will be called only once by the garbage collector. Calling finalize() can actually result in saving an object from deletion (for more information, research memory leaks). The following program lets you see the effects of garbage collection. It lets us know how much total memory the JVM has available to it and how much free memory it has. It then creates 10,000 Date objects. After this, it tells you how much memory is left and then calls the garbage collector (which, if it decides to run, should halt the program until all unused objects are removed). The final free memory result should indicate whether it has run. 01. import java.util.Date; 02. public class CheckGC { 03. public static void main(String [] args) { 04. Runtime rt = Runtime.getRuntime(); 05. System.out.println("Total JVM memory: " + rt.totalMemory()); 06. System.out.println("Before Memory = " + rt.freeMemory()); 07. Date d = null; 08. for(int i = 0;i

Article by ArticleForge

New Certification Courses

New Certification Courses Sun Certified Programmer for the Java Platform, Standard Edition 6-- The Sun Certified Programmer for Java Platform, Standard Edition 6 certification exam is for programmers experienced using the Java programming language. Achieving this certification provides clear evidence that a programmer understands the basic syntax and structure of the Java programming language and can create Java technology applications that run on server and desktop systems using Java SE 6.

Sun Certified Programmer for the Java Platform, Standard Edition 6 Upgrade Exam (CX-">310-066)-- The Sun Certified Programmer for Java Platform, Standard Edition 6 certification exam is for programmers experienced using the Java programming language. Achieving this certification provides evidence that a programmer understands the basic syntax and structure of the Java programming language and can create Java technology applications that run on server and desktop systems using Java Platform, Standard Edition 6 Sun strongly recommends that all new candidates interested in becoming a Sun Certified Java Programmer (SCJP) take this new version of the certification exam, rather than a previous version. In this way, candidates can demonstrate that they are knowledgeable in the latest technology. Sun also recommends that those certified on a previous version of SCJP update their credentials by taking the SCJP 6 Upgrade exam.

******

Sun Microsystems Developer Playground

Join Dana Nourie December 18th, Tues 9 AM PST and 7 PM PST, in Second Life at the Sun Microsystems Developer Playground to discover how easy it is to learn the Java platform through web programming. Watch a demo and have your questions answered.

******

Article by ArticleForge

Whizlabs launches SCJP 6.0 CX 310-666 Certification Exam Preparation Kit

Whizlabs, a leading IT certification exam preparation company with 800000+ users since 2000, launched its Preparation Kit for the SCJP 6.0 CX ">310-066 exam:

April 30, 2008 - PRLog -- Whizlabs recently announced the launching of SCJP 6.0 Certification Exam Preparation Kit. The launch of this kit once again shows the leadership of Whizlabs in Sun Java Certification Exam Preparation.

SCJP 6.0 certification exam is essential for the current Java professionals to upgrade their credentials to the latest SCJP 6.0 certification.

A free evaluation version of the new SCJP 6.0 Preparation Kit with 15 free questions is now available to download.SCJP 6.0 Certification Exam Preparation Kit. For trial download visit (visit.whizlabsm

End



References:


Pass4sure Exam Study Notes
Pass4sure Certification Exam Study Notes
Pass4sure Certification Exam Study Notes
Pass4sure Certification Exam Study Notes
Pass4sure Certification Exam Study Notes
Pass4sure Study Guides and Exam Simulator - shadowNET
Killexams Study Guides and Exam Simulator - simepe.com.br
Download Hottest Pass4sure Certification Exams - CSCPK
Complete Pass4Sure Collection of Exams - BDlisting
Latest Exam Questions and Answers - Ewerton.me
Here you will find Real Exam Questions and Answers of every exam - dinhvihaiphong.net
Practice questions and Cheat Sheets for Certification Exams at linuselfberg
Study Guides, Practice questions and Cheat Sheets for Certification Exams at brondby
Study Guides, Study Tools and Cheat Sheets for Certification Exams at assilksel.com
Study Guides, Study Tools and Cheat Sheets for Certification Exams at brainsandgames
Study notes to cover complete exam syllabus - crazycatladies
Study notes, boot camp and real exam Q&A to cover complete exam syllabus - brothelowner.com
Study notes to cover complete exam syllabus - Killexams.com
Study Guides, Practice Exams, Questions and Answers - cederfeldt
Study Guides, Practice Exams, Questions and Answers - chewtoysforpets
Study Guides, Practice Exams, Questions and Answers - Cogo
Study Guides, Practice Exams, Questions and Answers - cozashop
Study Guides, Study Notes, Practice Test, Questions and Answers - cscentral
Study Notes, Practice Test, Questions and Answers - diamondlabeling
Syllabus, Study Notes, Practice Test, Questions and Answers - diamondfp
Updated Syllabus, Study Notes, Practice Test, Questions and Answers - freshfilter.cl
New Syllabus, Study Notes, Practice Test, Questions and Answers - ganeshdelvescovo.eu
Syllabus, Study Notes, Practice Test, Questions and Answers - ganowebdesign.com
Study Guides, Practice Exams, Questions and Answers - Gimlab
Latest Study Guides, Practice Exams, Real Questions and Answers - GisPakistan
Latest Study Guides, Practice Exams, Real Questions and Answers - Health.medicbob
Killexams Certification Training, Q&A, Dumps - kamerainstallation.se
Killexams Syllabus, Killexams Study Notes, Killexams Practice Test, Questions and Answers - komsilanbeagle.info
Pass4sure Brain Dump, Study Notes, Pass4sure Practice Test, Killexams Questions and Answers - levantoupoeira
Pass4sure Braindumps, Study Notes, Pass4sure Practice Test, Killexams Questions and Answers - mad-exploits.net
Pass4sure Braindumps, Study Notes, Pass4sure Practice Test, Killexams Questions and Answers - manderije.nl
Pass4sure study guides, Braindumps, Study Notes, Pass4sure Practice Test, Killexams Questions and Answers - manderije.nl
Pass4sure Exams List - mida12.com.br
Braindumps and Pass4sure Exams Download Links - milehighmattress
Exams Study Guides Download Links - morganstudioonline
Study Guides Download Links - n1estudios.com
Pass4sure Study Guides Download Links - netclique.pt
Killexams Exams Download Links - nrnireland.org
Study Guides Download Links - partillerocken.com
Certification Exams Download Links - pixelcoding
Certificaiton Exam Braindumps Download Links - porumbeinunta
Brain Dumps and Study Guides Links - prematurisinasce.it
Pass4sure Brain Dumps - nicksmagic.com
Quesitons and Answers - recuperacion-disco-duro.com
Exam Questions and Answers with Simulator - redwest.se
Study Guides and Exam Simulator - sarkic.com
Pass4sure Study Guides and Exam Simulator - shadowNET
Killexams Study Guides and Exam Simulator - simepe.com.br
Killexams Study Guides and Exam Simulator - skinlove.nl
Pass4Sure Study Guides and Exam Simulator - marinedubai.com/
Pass4Sure QA and Exam Simulator - brandtsleeper/
Pass4Sure Q&A and Exam Simulator - risingeagleproductions/
VCE examcollection and Exam Simulator - starvinmarv/
Collection of Certification Exam Study Guides - studyguidecourses


Speed Marketing India (c) 2017