javaGod

SCJP Practice Questions: “Drag And Drop” Answers

May 18, 2009 · Comments Off

Here are the answers to Friday’s practice questions.  As always, let me know if you find something wrong with my answers or if you’re still having trouble with something.

This wraps up the question types posts.  Look for something completely different on Wednesday.  If you have any requests, questions, suggestions, etc, email me.

answers

Comments OffCategories: Uncategorized
Tagged: , , , , , ,

SCJP Practice Questions: “Drag And Drop”

May 15, 2009 · Comments Off

Here are the practice questions for the “Drag And Drop” pattern.  We are, of course, limited by our medium, so you’ll have to do the actual dragging and dropping by cut and paste (or good old fashioned pencil and paper).  Look for the answers on Monday.  Good luck!

qs

Have something to say about this post?  Let me have it!

Comments OffCategories: Uncategorized
Tagged: , , , , , ,

SCJP Question Type 3: “Drag And Drop”

May 13, 2009 · Comments Off

Introduction

The question type that most people have trouble being consistent with is the “Drag And Drop” pattern.  These problems can vary greatly in difficulty and can cover almost any topic.  There are typically a lot of parts to each question, and since there is no partial credit on the SCJP, you must get every single piece right, otherwise you do not get credit for a correct answer.

There are two major categories of “Drag And Drop” problems.  The first type will ask you to evaluate a short piece of code and assign a result to it.  For instance, a problem may ask you to determine which exception is thrown by this code, or what String is printed to the console.  Aside from the mechanics of actually dragging the appropriate answer into the provided blank, these questions are identical to the “Results” pattern problems, so I will not consider those here.  If you have questions about this specific type of “Drag And Drop” problem, please don’t hesitate to ask.

The more typical “Drag And Drop” problem will present you with a short piece of code and ask you to fill in the blanks, using the code fragments provided.  These questions are similar to the “Complete The Code” pattern, but the fragments will usually be shorter and there will be more blanks to fill in.

The following problem adheres to the “Drag And Drop” pattern as described above:

q0

Answer Choices

In the “Drag And Drop” pattern, the answer choices will always be provided for you as code fragments.  But be careful to read the instructions carefully!  You may have to use each fragment exactly once, you may have code fragments left over, or you may have to re-use code fragments.  Make sure you know what the requirements for the problem are before you start filling in the blanks.

Solving

Every problem type on the SCJP can be solved in at least two obvious ways.  The straightforward (Front Door) method will be presented first – this is the way that the exam designers intend for you to solve the problem.

The Back Door method can be helpful in a number of ways.  If you can’t determine an answer to the problem using the Front Door method, the Back Door method gives you another way to look at it that might make the answer more apparent.  The Back Door method can also be used to save time, to eliminate impossible answer choices to improve your chances of a correct guess, or even to check your work once you complete the Front Door method.

The Front Door:

  1. First, determine what the piece of code that you are given is trying to do.  It should be obvious that you can’t complete the code until you know what the code is supposed to do.
  2. Once you know what the end goal of the completed code is, take each line in turn.  Run the code as if you were a JVM using the scrap paper that is provided, until you hit the first blank.
  3. After you hit the first blank, read the statements immediately preceding and immediately following the statement containing the blank.  Figure out exactly what has to be done in the middle statement to allow the preceding and following statements to compile.
  4. Before looking at the code fragments, figure out what you would put in the blank if you had no answer choices and write it on your scrap paper.
  5. Now look at the code fragments for a match (or a close match) and drag it to the blank.
  6. Repeat steps 2 – 5 until you’ve filled in every blank.  Re-read the entire piece of code one final time to verify that it still does what you determined it should do (in step 2).

The Back Door:

  1. Start by looking at the code fragments.  Frequently there will be fragments that just don’t make any sense – they are completely unrelated, they call a non-existent method, etc.  Eliminate these fragments right away.
  2. Once you’ve reduced the number of fragments, look at each statement containing a blank in turn.  Determine what the goal of that statement is, and determine which fragment would best complete that purpose.
  3. See if there are any similar fragments that might be used instead.  Determine why your answer is correct for this blank and the similar fragment is not.
  4. Fill in the blank with your final choice, and repeat steps 2 – 4 until you’ve filled in every blank.
  5. Read the entire piece of code and determine what it’s trying to do, making sure that your additions make sense in the larger context.

Tips and Tricks

  • Watch out for identifier names.  Frequently the testers will try to trick you by putting a code fragment that should work – except a variable name or a method signature is slightly off.
  • Don’t eliminate code fragments that don’t seem to be complete.  Frequently a semi-colon or right-parenthesis will already be included in the larger piece of code, so inserting the fragment that doesn’t seem complete will actually work.  Vice-versa, don’t choose a fragment that does complete the parentheses or does include the semi-colon when it is already there.  Make sure you fill in exactly the information that is necessary, no more and no less.
  • Use identifier names to help you determine what the code should do.  The name of the class in the example above makes it pretty clear what the code is trying to accomplish.

That’s the basic pattern.  Tomorrow I’ll follow this up with a few more examples so that you can try out both of the strategies I described above.  Until then, see if you can solve the above problem using both The Front Door and The Back Door methods.

Have something to say about this post?  Let me have it!

Comments OffCategories: Uncategorized
Tagged: , , , , , , ,

SCJP Practice Questions: “Complete The Code” Answers

May 11, 2009 · Comments Off

Here are the answers to yesterday’s practice questions.  If you still have problems or you think I made a mistake, let me know.

1.  b, c, d

Answer a does not compile because it violates a fundamental rule of Generics: the generic type defined within the angle brackets must be the same on both sides of the declaration, even if, as in this case, the type on the right passes the IS-A test for the type on the left.  The only exception to this rule is when using the wildcard syntax on the left hand side.  You shouldn’t even have to insert this code into the problem – the fragment itself won’t compile.

Answer e does not compile because C is not a subclass of B, thus you cannot pass a collection of C objects to a method expecting a collection of objects that are subclasses of B.

2.  d

Only I compiles, thus only I can run to completion.  It’s a little tricky to see, but if you have conflicting methods in super-classes or interfaces, try to find a single method signature that will satisfy both.  In this case, I1 and I2 have conflicting methods that differ only in return type.  If you try to implement both (as in IV) you will get a compilation error.  However, notice that String IS-A Object, so choice I satisfies both interfaces.  Object does not pass the IS-A test for String, so none of the other choices will compile

3.  a, c, e

Answer choice a is a proper override of method m1( ).

Answer choice b declares the method as protected.  Protected is more restrictive than public, so it violates one of the rules of overriding (overriding methods can be declared as less restrictive but not more restrictive than the method they are overriding) and thus does not compile.

Answer choice c would be similar to b except that the parameter list is different, thus the method is not an override but an overload.  It compiles without a problem.

Answer choice d would work but it does not compile itself – the method signature declares that it returns a String, but there is no method code.

Answer choice e compiles – you can override a method and change it from non-abstract to abstract as long as the class itself is abstract.

4. b, c

Answer choices a and d do not compile because they both try to instantiate a List.  List is an interface, and thus cannot be instantiated.  Another problem with answer choice d, and the problem with answer choice e, is that it tries to pass a collection of Sub objects while the method is expecting a collection of Super objects.

5. d

Fragment I does not compile.  Even though b IS-A B, it is declared as an object of type A, so it needs an explicit cast in order to be passed to a method expecting an object of type B.  Fragment II compiles but throws a class cast exception when you try to cast object a to type B.  Fragment III is the only one that both compiles and runs successfully.  Fragment IV does not compile – you can’t pass an object of type A to a method expecting an object of type B.  Fragment V does not compile.  Object c does not pass the IS-A test for type B, thus it cannot be cast to type B.

Have something to say about this post?  Let me have it!

Comments OffCategories: Uncategorized
Tagged: , , , , , , , ,

SCJP Practice Questions: “Complete The Code”

May 8, 2009 · Comments Off

As promised, here’s a set of questions you can use to practice the strategies I detailed on Wednesday.   Look for answers and explanations on Monday.

allQs

Have something to say about this post?  Let me have it!

Comments OffCategories: Uncategorized
Tagged: , , , , , , ,

SCJP Question Type 2: The “Complete The Code” Pattern

May 6, 2009 · Comments Off

Introduction

The “Complete the Code” pattern is very similar to the “Results” pattern with one exception: a portion of the code that you are given is missing, and it’s up to you to complete it. These problems are usually on the easier side of the fence, since in most cases the code that you’re given will compile and run without a problem, as long as you insert the correct statement to complete it. This allows you to focus your attention on the code fragments you must insert, rather than spending time verifying that the given code fragment compiles. Unless you’re running short on time, though, I still recommend always making sure any code you’re given compiles.

The following problem adheres to the “Complete the Code” pattern:

q1v3

Answer Choices

There are two variations of answer choices in the “Complete the Code” pattern. The first is shown above, where a number of statements are given and you are asked to specify a letter answer which lists some combination of the statements. In the second variation, the code to insert is actually an answer choice. In this case, you are typically asked to select all code fragments that will allow the code to compile and run.

Solving

Every problem type on the SCJP can be solved in at least two obvious ways. The straightforward (Front Door) method will be presented first – this is the way that the exam designers intend for you to solve the problem.

The Back Door method can be helpful in a number of ways. If you can’t determine an answer to the problem using the Front Door method, the Back Door method gives you another way to look at it that might make the answer more apparent. The Back Door method can also be used to save time, to eliminate impossible answer choices to improve your chances of a correct guess, or even to check your work once you complete the Front Door method.

The Front Door:

  1. If it’s possible that none of the fragments to insert are valid (ie: choice g above) and you have plenty of time, look through the code that’s given and make sure that it will compile. If it doesn’t choose “None” and move on to the next problem.
  2. If the code you are given does compile (as will usually be the case), turn your attention to the code fragments to insert. Insert each one into the given code and determine its effect. At this point, focus only on the affect that the inserted code will have – you already determined in step 1 that the rest of the code compiles.
  3. For each fragment, write down whether or not: 1) it compiles, and 2) it runs to completion.
  4. If the actual answer choices appear below the fragments to insert, compare the choices to your list, and make the selection that matches.
  5. If the fragments to insert are also the answer choices, select those fragments that compile or run to completion (depending on what the question asks for – see the Tips section below).

The Back Door:

  1. Look at each fragment to insert in turn and immediately rule out any fragments that have incorrect syntax. If the fragment to insert does not compile by itself, obviously the whole will not compile either.
  2. If the actual answer choices are below the fragments to insert, eliminate any choices that include fragments that you’ve already ruled out. If the fragments are the answer choices, eliminate those that don’t compile.
  3. Break up each fragment into parts and take each part in turn. For instance, if the code to insert is a method signature, look at the code that calls the method to determine what the access modifier must be. Eliminate any answer choices that don’t comply. Next look at the argument list, etc.
  4. Once you’re left with just a couple of fragments to insert, look at the differences among those fragments. For instance, one fragment might declare a method as static, while one does not. Determine whether these differences will cause the code to fail compilation (or to run successfully) and eliminate the appropriate answer.
  5. By the end of this process, you should have eliminated at least half of the answer choices. Proceed with step 4 or 5 of the Front Door method to complete the problem.

Tips

  • Pay attention to what the question is asking. If it’s asking for all code fragments that compile, then a code fragment that throws an exception is a correct answer.
  • Make sure you don’t miss part of the answer. In the example above, if you pick the first answer that is correct (b) you will miss the fact that IV also compiles – the correct answer is actually f.

That’s the basic pattern. Tomorrow I’ll follow this up with a few more examples so that you can try out both of the strategies I described above.

Have something to say about this post? Let me have it!

Comments OffCategories: Uncategorized
Tagged: , , , , , , ,

An Aside: Work To Live, Don’t Live To Work

May 4, 2009 · Comments Off

Well, it’s been a busy day here at JavaGod HQ, and after a very busy weekend, I haven’t been able to carve out enough time to do justice to the second part of the SCJP Question Types series.  I don’t want to put up something here that’s not my best work, so you’ll have to wait until Wednesday for the “Complete the Code” pattern.  Never fear, though.  I have a little something for you today that is even better: some sage advice.

Some (good) things happened to me over the weekend that really caused me to sit down and think long and hard about where I am now and what I want to accomplish in my life.  Notice, that I said “my life” not “my career.”  That’s really the point of this whole post.  I know most of you tune in here for practical, career-oriented discussion and advice, but today we’re going a little further afield.  But maybe not as far as you might think…

Listen, folks, no matter how wrapped up you get in your work, no matter how much you think you need that next promotion, no matter how much you need to succeed, you must remember that you work to live, you don’t live to work.  These days we are so focused on using our respective salaries to determine our worth to society that we ignore other, more accurate, methods of measures of success.  Money can buy you a big house, a nice car, a boat, and a bunch of other things, but what they say is true: money can never buy happiness.

Don’t ever forget that work is only a part of the whole.  There are other areas of your life you should be working just as hard at improving.  Your family and friends are far more important than your work – these people will stick with you whether you achieve your career goals or not.  Make sure you don’t neglect them or take them for granted!

Having something to do that’s completely different from what you do in your day job can also be a life-saver.  Step away from the computer for a while and get your hands dirty – go for a hike, build something out of wood, play ball with your son, run around the block a few times.  It’s been shown hundreds of times that people with a well-rounded life outside of the office make for some of the best employees.  Why do you think they asked about your “outside interests” in every job interview you’ve ever had?  Let your mind come up for air every once in a while and it will thank you on Monday morning.

Don’t let your job be the be-all-end-all of your life.  If you’re prepping for a release and you need to work some overtime, do it with a smile and your boss will remember you later.  But if you’re always prepping for a release, always working overtime, maybe it’s time to have a talk with your boss, or find a new job.  Your Software Lead won’t be there for you if things take a turn for the worse, your family will.

Comments OffCategories: Uncategorized
Tagged: ,

SCJP Practice Questions: “Results” Pattern Answers

May 1, 2009 · Comments Off

Here are the answers to yesterday’s practice questions.  No cheating!  If you’re still having trouble with something, or I screwed something up, email me.

1. b, c , d

Two concepts to keep in mind here: First, the two Thread objects created in main are created with different Test objects, thus the synchronized keyword doesn’t affect the output at all. If both Thread objects had been passed the same Test object, then the statements in the run method would have to execute as one (atomically). Since they are different objects, though, the statements can be interrupted.

Second, there are only two Thread objects created (besides the main thread), so there can only be two distinct numbers in the output.

2. e.

The sayHi( ) method is called from main, which is a static method. An instance method cannot be called from a static context. At first glance, this problem seems like it’s testing you on command line arguments, possibly exception handling. This is a clear example of how the SCJP will try to get you thinking way, while the right answer actually lies in a different direction.

3. d

On line 1, when t1 is set to null, it is still being referenced by t3.t1, so it is not eligible for garbage collection. On line 2, when t2 is set to null, two objects are lost at the same time – the object referenced by t2 and the object referenced by t2.t1. Thus, never in this program is exactly one object eligible for garbage collection.

This is a pretty straightforward example of the type of garbage collection problems you will encounter on the SCJP. Using the scrap paper that is provided will make this type of problem much easier. Trying to keep track of all the objects in your head is a recipe for disaster.

4. e

Again, two concepts to keep in mind.  One: the compiler will always insert a call to super( ) in a constructor if there is not already a call to super( ) or this( ).  The compiler will never insert a call to any constructor except the default, no-argument constructor.  Two: The compiler will only insert a default constructor if no constructor is defined. Since PreTest defines a constructor, the compiler will not insert a default constructor in PreTest. Thus, when it attempts to insert a call to the default super-constructor in Test, compilation will fail. To fix this compiler error, either insert a call to super(String) as the first line of the Test constructor or insert a no-argument constructor into PreTest.

5. e

Make sure you look at the modifiers! This code looks like it should compile and run without a problem, until you realize that the two code fragments are in different packages. Since the PreTest constructor does not declare an access modifier, it can only be accessed within the same package.

I can’t make this point enough: Always always always look at the modifiers in this type of problem. It will save you time in the long run – this problem should take you less than a minute if you look at the modifiers first.

Well that’s it for now.  Next week we’ll move onto the second pattern, but until then, try out my strategies for the “Results” Pattern on some other practice questions and see how it works for you.

Have something to say about this post?  Let me have it!

Comments OffCategories: Uncategorized
Tagged: , , , , , ,

SCJP Practice Questions: “Results” Pattern

April 29, 2009 · Leave a Comment

As promised, here are a couple practice questions that you can use to try out the problem-solving strategies that I went over on Monday.  Answers and explanations will come in the next post.

1.  Given:

Question 1

What are the possible results? (Select three correct answers)
a) “1a2b3a4b”
b) “1a1b2a2b”
c) “1a2a1b2b”
d) “2a2b1a1b”
e) “1a1b2a3b”
f) “1a1a2b2b”

————————————————————————————————————

2.  Given:

q22

And the command line: java Test 0 0 0
What is the result? (Select one correct answer)
a) Program runs to completion with no output.
b) “Hi!”
c) “Hi!Hi!”
d) “Hi!Hi!Hi!”
e) Compilation fails.
f) Exception thrown at runtime.

————————————————————————————————————

3.  Given the following code fragment:

q31

At what point is exactly one object eligible for garbage collection?
a) After line 1
b) After line 2
c) After line 3
d) Never
e) Compilation fails.
f) Exception thrown at runtime.

————————————————————————————————————

4.  Given the following in a single file:

q4

What is the result? (Select one correct answer)
a) Program runs to completion with no output.
b)
Test
Test constructor
PreTest constructor
c)
Test constructor
PreTest constructor
d)
PreTest constructor
Test constructor
e) Compilation fails.
f) Exception thrown at runtime.

————————————————————————————————————

5.  Given the following:

q5

What is the result? (Select one correct answer)
a) Program runs to completion with no output.
b)
Test
Test constructor
PreTest constructor
c)
Test constructor
PreTest constructor
d)
PreTest constructor
Test constructor
e) Compilation fails.
f) Exception thrown at runtime.

→ Leave a CommentCategories: Uncategorized
Tagged: , , , , , ,

SCJP Question Type 1: The “Results” Pattern

April 27, 2009 · Leave a Comment

Introduction

The following problem adheres to the “Results” pattern:

prob11

The majority of problems on the SCJP exam follow the “Results” pattern. These problems can vary greatly in difficulty, mainly dependant on how complicated the code fragment is. They can cover a variety of topics and frequently try to trick you into assuming that they are testing one thing when they are actually testing another. It pays to be careful and methodical when approaching a “Results” pattern problem.

Answer Choices

The number of answers you must select varies in the “Results” pattern. Typically there will be only one correct answer, but sometimes the question will direct you to choose two or three answers. The question will usually include at least “Compilation Fails” as an answer choice, and frequently “Exception thrown at runtime” is also an option. The rest of the answers choices usually represent possible output produced by the program.

Occasionally you will come across a set of answer choices that points you toward a specific compilation error or exception thrown. For instance, “Compilation fails due to error on line 3” may be one possible choice. These answers are usually easy to rule out since they force you to look at only one line of code instead of the entire fragment.

Solving The “Results” Pattern

Every problem type on the SCJP can be solved in at least two obvious ways. The straightforward (Front Door) method will be presented first – this is the way that the exam designers intend for you to solve the problem.

The Back Door method can be helpful in a number of ways. If you can’t determine an answer to the problem using the Front Door method, the Back Door method gives you another way to look at it that might make the answer more apparent. The Back Door method can also be used to save time, to eliminate impossible answer choices to improve your chances of a correct guess, or even to check your work once you complete the Front Door method.

The Front Door:

  1. If the “Compilation Fails” answer choice is present, the first thing you need to do is verify that the code compiles. As always, check for problems with modifiers (such as a static method that calls an instance method without an object reference), unreachable code, and mismatched method arguments.
  2. Once you’re sure the code compiles, your second step is simply to execute the code as your JVM would do. You will be provided scratch paper during the exam, so make sure you use it if the code is at all confusing. This also forces you to take it slow, one line at a time, the way your JVM would do it. If the answer is constructed in multiple parts (like in the example), make sure you write down each part of the answer as it is calculated. Otherwise, you’re likely to misremember something at some point during the exam and get the problem wrong because of it.
  3. Once you have completed your run through the code, compare your answer to the answer choices. If your answer matches one of the answer choices, mark it and move on.

The Back Door:

  1. Look at the first answer choice and try to prove that it cannot be correct. For instance, in the example provided, an IOException is thrown from within the try block. It should be clear, then, that the exception will always be caught in the first block, and the second block will never execute. Thus, any answer with a “2” in it cannot possibly be correct.
  2. Repeat step 1 for each answer choice. In the end, there will only be one answer choice (or several if there is more than one correct answer) that you cannot prove is incorrect. Ideally, if you were able to solve the problem using the Front Door method, this solution matches the one you came up with earlier.

That’s the basic gist of the “Results” pattern. I’ll follow this up with a few more examples so that you can try out both of the strategies I described above. In the meantime, check out the previous post for some general tips and tricks if you haven’t done so already.

Got something to say about this post?  Let me have it!

→ Leave a CommentCategories: Uncategorized
Tagged: , , , ,