×

Loading...

Java Quiz

What output is produced by compiling and running the following program? Note that the instance variable named x is declared private (as highlighted in boldface in the code).

A. A compiler error
B. A runtime error
C. 15
//File Q001_10.java
class Q001_10{
public static void main(
String args[]){
AClass ref1 = new AClass(5);
AClass ref2 = new AClass(10);
System.out.println(
ref1.add(ref2));
}//end main()
}//end class definition

class AClass{
private int x;

AClass(int x){//constructor
this.x = x;
}// end constructor

int add(AClass ref){
return x + ref.x;
}//end add()

}//end class AClass
Sign in and Reply Report

Replies, comments and Discussions:

  • 枫下家园 / 护照签证 / Java Quiz Java Quiz
    What output is produced by compiling and running the following program? Note that the instance variable named x is declared private (as highlighted in boldface in the code).

    A. A compiler error
    B. A runtime error
    C. 15
    //File Q001_10.java
    class Q001_10{
    public static void main(
    String args[]){
    AClass ref1 = new AClass(5);
    AClass ref2 = new AClass(10);
    System.out.println(
    ref1.add(ref2));
    }//end main()
    }//end class definition

    class AClass{
    private int x;

    AClass(int x){//constructor
    this.x = x;
    }// end constructor

    int add(AClass ref){
    return x + ref.x;
    }//end add()

    }//end class AClass
    • Answering this questions needs one's discretion
      I strongly encourage new Java people try to answer FromZtoA's question. Of course, you had better post your reasoning. I will comment on the quiz problem in a few days.
    • Let me try it. Let me try it.
      A compiler error will be generated.

      In the method "add" in class "AClass", the variable "ref" refers to another object rather than the object who executes this method. Thus a call to "ref.x" is invalid, because x is a private member.

      I admit this program is quite tricky, and it's helpful for us new learner.

      Thanks peterlf.
      • Jabber's comments Jabber's comments
        The answer will be 15.

        This quiz tests our understanding of accessibilities for Java instance variables.

        "x" is private so we cannot directly access it
        by saying a.x in another class, where a is
        an instance name. In the example code,
        teh author used ref.x, but this piece of code is still in the definition of AClass. So the author DID NOT violate the accessibility of private variables. Hence, there won't be compiling-time error, let alone the run-time problem.

        In summary, private means one cannot access a variable from out of the class definition. Say again, in the example code,
        the auther used "a.x" syntax in the definition of the class AClass.

        Hope it helps
    • FromZto A, what's your opinion? I have posted my comments on your Quiz.
      • Thanks jabber. You got 100 in the quiz and Sailor got 0.
        How is your Java school plan? Actually I'm quite interested in a position of Oracle instructor. So if you need one...
        • FromZtoA, you are in Vancouver, aren't you? FromZtoA, you are in Vancouver, aren't you?
          本文发表在 rolia.net 枫下论坛I prefer to be a contractor once I move to Canada. I have no ability to set up a training school at present. I am a serious guy. If 3 guys follow me to learn Java for 180 hrs, I need to have two of them get an Java job in one year. To meet this goal, I need to invest a sum of money.... but right now I need consider buy a house first. Probably I can do training in the spare time.

          By the way, I don't think I can have enough students. Most Chinese guys don't want to spend money and time studying Java though
          many Chinese guys want to become a Java programmer.

          I have ever taught for 3 training centers. One I have taught for set up its EJB course as 18 hrs and charge $400. (You may think 400USD corresponds to 400 CAD in Canada). Actually, this is a complete wasiting of time and money. Most students have no client/server and distributed computing concept. In addition, most students have problems in setting PATH and CLASSPATH...
          In addition, most students have no idea about Java RMI... they told me after classes that they had just gone through 21 hours's Java programming training.. I could not help smiling because I have no other choices.
          I told them.---I had ever wriiten more 1500 small Java exercises before I took Sun's programmer test. I told them--- what ever Java principle you discuss with me, I can create a small example to prove or disprove
          it. I am not showing off ...I just want to say,
          if one wants to get Java programmer's high pay, he need to sit down and do some hard work. Rome is not built in a day.

          It seems to me you are in Vancouver. Why not to move to East Canada?
          I hope we have opportunities to get together once we have stabalize our routine life.更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • Vancouver is a nice place. Vancouver is a nice place.
            Here temperature seldom drops under zero. I used to be in Scandinavia for a long time and I cannot stand with very cold weather for very long winter.

            You are very organized and plan things carefully in advance. It's very important for success. I believe your unselfish helps to other people in this forum will reward someday.

            I thinks someone else also interested in your background as me, such as how old... I think you will be much elder than me. I'm 26 yet.
            • I love sauna, but now I only got a steaming room, so upset.
            • How old am I? How old am I?
              You know it is impolite to ask a lady her age.
              But I can offer you some interesting answers.
              (I do believe you are a gentleman, so just for fun and for helping new immigrant learning English.)

              Q: If you don't mind, can you tell me how old you are?

              A1. I am always 29.99
              A2. I am older some than some, but younger than others
              A3. I have not yet been as old as centuries
              A4. I am twenty this year, but eighteen next year....
              • Is Jabber a lady? I don't believe it. How can a lady be so good at Java?
                • It does not matter whether I am a guy or a lady
                  However, you guy might be thought of as being a sexist. Please don't discriminate females. I can remind you that the Chief Architect for Sun's JINI project is a lady.
                  • U said it