Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6824149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:52:03+00:00 2026-05-26T21:52:03+00:00

I am studying the SCJP, and while studying I have found an exercise that

  • 0

I am studying the SCJP, and while studying I have found an exercise that seemed very simple at first, but I failed resolving it, and I do not understand the answer. The exercise (taken from OCP Java SE 6 Programmer Practice Exams, Bert Bates and Kathy Sierra), says the following:

Given:

import java.util.*;
public class MyPancake implements Pancake {
  public static void main(String[] args) {
    List<String> x = new ArrayList<String>();
    x.add("3");x.add("7");x.add("5");
    List<String> y = new MyPancake().doStuff(x);
    y.add("1");
    System.out.println(x);
  }

  List<String> doStuff(List<String> z) {
    z.add("9");
    return z;
  }
}

interface Pancake {
  List<String> doStuff(List<String> s);
}


What is the most likely result?

A. [3, 7, 5]

B. [3, 7, 5, 9]

C. [3, 7, 5, 9, 1]

D. Compilation fails.

E. An exception is thrown at runtime

And the answer is:

D is correct. MyPancake.doStuff() must be marked public. If it is, then C would be
correct.

A, B, C, and E are incorrect based on the above.

My guess was C, because the doStuff method is inside the class MyPancake, so the main method should have access to it.

Reconsidering the question, when calling to new from the static context, it may not have access to the private methods, if doStuff were private. Is this true? I am not sure of this.

But anyway, I still think it would have access to the package-private doStuff method.
I guess I am wrong, but I do not know why.

Could you help me?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:52:04+00:00Added an answer on May 26, 2026 at 9:52 pm

    It’s very sad that it doesn’t give you an answer as to why it would fail to compile – but fortunately when you’ve got a compiler, you can find out for yourself:

    Test.java:11: error: doStuff(List<String>) in MyPancake cannot implement doStuff
    (List<String>) in Pancake
      List<String> doStuff(List<String> z) {
                   ^
      attempting to assign weaker access privileges; was public
    2 errors
    

    Basically interface members are always public, so you have to implement interfaces with public methods. It’s not a problem of calling the method – it’s a problem in implementing the interface. If you took off the “implements” part it would work fine.

    From section 9.1.5 of the Java Language Specification:

    All interface members are implicitly public. They are accessible outside the package where the interface is declared if the interface is also declared public or protected, in accordance with the rules of §6.6.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While studying about JMX, I have seen one of the important feature of it
While studying the Collection API, we find that some methods ( add , remove
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
In my adventures studying the boost libraries, I've come across function signatures that have
While studying for the SCJP 6 exam, I ran into this question in a
Studying STL I have tried to negate a functor with not2 but encontered problems.
While studying Java tutorials, Reflection and Late Binding have confused me. In some tutorials,
After studying the docs for a while, I came up with my first jQuery
While studying the book Introduction to Algorithms by Cormen, I found a strange thing.
While studying the Qt framework, I've noticed that it's littered with hundreds of overloaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.