• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Sybex OCP Study Guide 1Z0-816/817 Chapter 1 Question 11

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I would say that question 11 for chapter 1 (Java fundamentals), page 50 of the book is wrong.

It presents a code having several public interfaces like:
1: public interface CanWalk {
...
4: }
5: public interface CanRun {
...
9: }
10: public interface  CanSprint {
...
16: }

For me, the right answer is 'F': "None of the above", since the code cannot compile: You cannot have more than one public interface in a .class file. I think that there shouldn't be the line numbers (that indicate that these are lines of the same file).

What do you think about it?
 
Saloon Keeper
Posts: 1601
51
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holy CRUD!

First, welcome back to the Ranch!

I have posted/replied recently about one of the nastiest (but fair) tricks on the exams is the tendency to test more than one thing on a question.

So let's say you've recently studied all the rules for combining interfaces with default methods and you see the question and are ready to go.

But of course, because the lines are numbered sequentially, we are to presume they are all typed contiguously in one source file, by the standards of the real and mock exams.

So of course, at most one of those top-level public interfaces will compile successfully, disregarding completely any additional errors in terms of rules about interface hierarchies!

Now, if the numbering began at say, 6, instead of 1, maybe we were just looking at some nested static interfaces that were terribly indented.

As nested interfaces are inherently static members, then their answer would be correct, but then they couldn't show the numbering beginning at 1, which by convention means "no code in source before snippet!"

So, as it is, at most one of the interfaces could compile, provided it had the same name as the source file.  It is also possible that none of them will, depending on the name of the source file.

The authors are very nice and pleasant individuals, who make their mock exam questions tricky so that we will be prepared for the Real Thing.

In this case, I believe the authors got caught in the trap of their own trickiness, along with all of the rest of us, by focusing on the rules for extending interfaces.

Unless they are nested, at most one of them can be marked public, per Java rules for placing classes, interfaces, enums and annotations in source files!  The simplest fix to the question would be to remove the public annotation, since for good or bad the JLS allows you to define any number of default-access classes, enums, interfaces in a single source file, with no requirement as to file name.

It caught myself, the authors and everyone else doing that set of questions, until you came along.

Good eye, or better yet, good job of not getting distracted!  Had they placed three unrelated interfaces in there, I immediately would have noticed it would not compile, because I wouldn't have jumped into thinking about interface hierarchy regulations.

 
Jesse Silverman
Saloon Keeper
Posts: 1601
51
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You cannot have more than one public interface in a .class file.



You can not have more than one class, interface or enum in a .class file, regardless of whether they are public, default, private static, etc. etc. etc. -- as each class or interface of any of those categories generates its own .class file.

You may not declare more than one public interface, class or enum in a .java source file is what you meant to say.

Preparing for these exams always makes me feel like I am defending myself in court because my court-appointed attorney showed up drunk and fell asleep...

I am probably just jealous that you caught an error I missed.  Again, good catch.
 
Daniel Fernandez Boada
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:

You cannot have more than one public interface in a .class file.



You can not have more than one class, interface or enum in a .class file, regardless of whether they are public, default, private static, etc. etc. etc. -- as each class or interface of any of those categories generates its own .class file.

You may not declare more than one public interface, class or enum in a .java source file is what you meant to say.

Preparing for these exams always makes me feel like I am defending myself in court because my court-appointed attorney showed up drunk and fell asleep...

I am probably just jealous that you caught an error I missed.  Again, good catch.



Hi Jesse,
Yes, you are right I was talking about .java files not .class.
I am not sure what are the rules here, should I edit my post?
Cheers,
Daniel.
 
Jesse Silverman
Saloon Keeper
Posts: 1601
51
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We avoid editing posts after someone has replied to them, because the thread then makes no sense to all who read it in the future, "Why is he discussing/correcting an error he says she made that I don't see?"

Like we strive to remember about our code, threads are read far more times than they are written, and use an iterative, rather than a waterfall approach.

So let's leave this thread as it is.
 
author & internet detective
Posts: 40791
828
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Confirmed the line numbers should not be there. I've added it to the errata.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic