This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

to import or not to import, that is the question...

 
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers!
I got a doubt about a a pair of questions I tackled recently:

This one is taken from Oracle Free Sample Questions
5. Given the code fragment:

What is the result?
A) Pen Pencil Box
B) Pen Pencil
C) Box
D) Compilation fails.

-------------------------

This second is taken from the Practice Test of OCA Java SE 8 Guide by Kathy Sierra & Bert Bates (in attach)


For the first one I answered D beacause there was no java.util.* import for List<E> and ArrayList<E> types; but the correct answer was C.
For the second one I answered B; but the correct answer was D because the java.util.* import misses!!



So I'm puzzled. What's the correct position to keep about imports during the exam?
OCA8_sample1.png
[Thumbnail for OCA8_sample1.png]
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Daniele.

The first question you have slightly incorrect code. The code in the sample question is this:

public class TestClass {
   public static void main(String[] args) {
       List<String> items = new ArrayList<>();
       items.add("Pen");
       items.add("Pencil");
       items.add("Box");
       for (String i : items) {
           if (i.indexOf("P") == 0) {
               continue;
           } else {
               System.out.print(i+" ");
           }
       }
   }
}
Note to moderators: I did not use the code tags because that adds line numbers to the code.

The key to this one is noticing that the line numbers where not added to the question.
You can assume that the imports have been taken care of if:
  • they do not add line numbers to the code in the question
  • add line numbers, but they start some time after the first line then you can assume that the imports have been taken care of.

  • For the second question, which is shown in the image, they started with the import of java.lang.
    However this is another way to show that you are at the beginning of the file because you see the import statement(s).
    Usually they will show you all the imports for the code snippets at the same time and you can then assume that is the beginning of the file

    You can find my other response to this here with some different samples
    https://coderanch.com/t/686700#3223446

    Daniele Barell wrote:So I'm puzzled. What's the correct position to keep about imports during the exam?


    I remember in Jeanne and Scott's book they used PIC to explain this:
  • P = Package if present
  • I = Imports if present
  • C = Class/Code
  • So you simply have to remember the word PIC and that is the order they things must appear in.
     
    Daniele Barell
    Ranch Hand
    Posts: 94
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Pete.
    Thank you very much. It's really useful and pleasent talking with people like you!

    By the way I looked at the exam preparation page at Oracle,- Review Exam Topics - Code fragments.
    It's states:


    A code fragment is a small section of source code that is presented without its context. Assume that all necessary supporting code exists and that the supporting environment fully supports the correct compilation and execution of the code shown and its omitted environment.

     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniele Barell wrote:Thank you very much. It's really useful and pleasent talking with people like you!


    You are welcome and I just doing what I can to help out.
    I simply don't see the point in being offensive or rude and not helpful when it only takes a few moments to be kind help someone out.

    Most likely there will be one or more questions on the exam where you will need to know PIC as explained in my previous post.
    For instance the exam may have the package line after the imports or imports at the end of the code/file etc.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!