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

  • SEARCH
  • Home
  • 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 957517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:42:56+00:00 2026-05-16T00:42:56+00:00

Give the following code: class A { Boolean b; A easyMethod(A a){ a =

  • 0

Give the following code:

class A {
    Boolean b;
    A easyMethod(A a){
        a = null;
        return a;
    }
    public static void main(String [] args){
        A a1 = new A();
        A a2 = new A();
        A a3 = new A();
        a3 = a1.easyMethod(a2);
        a1 = null;
        // Some other code 
    }
}

The question is how many objects are eligible for garbage collection right before // Some other code.

Then correct answer is (at least that’s the interviewer answer): 2 – the Boolean b because it’s a wrapper and a1 .

Can you please me explain me why a2 and a3 aren’t being garbage collected ?

LATER EDIT:

  • Ok, I think I get it now. It was a bit confusing at first, but now i am sure the interviewer was wrong. My initial mistake was that at first I didn’t consider that Java is pass by value only, so it’s impossible to make a2 null from inside a function that take “a2” as a parameter, because that a2 is actually a copy of a2.
  • The part with the Boolean b was indeed quite obvious.

Thanks for an answer, I will send some interview feedback after that :).

  • 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-16T00:42:57+00:00Added an answer on May 16, 2026 at 12:42 am

    Assuming go is supposed to be easyMethod it works like this

    class A {
        Boolean b;
        A easyMethod(A a){
            a = null; // the reference to a2 was passed in, but is set to null
                      // a2 is not set to null - this copy of a reference is!
            return a; // null is returned
        }
        public static void main(String [] args){
            A a1 = new A(); // 1 obj
            A a2 = new A(); // 2 obj
            A a3 = new A(); // 3 obj
            a3 = a1.go(a2); // a3 set to null and flagged for GC - see above for why
            a1 = null; // so far, a1 and a3 have been set to null and flagged
            // Some other code 
        }
    }
    

    Two objects are eligible for garbage collection (a1 and a3). b is not because it’s only a reference to null. No Boolean was ever made.

    To get around the inane subtleties of what // Some other code might be, I instead posit the question be reworded into the following:

    Prdict and explain the following output:

    class A {
        int i;
        A(int i) { this.i = i; }
        public String toString() { return ""+i; }
        A go(A a){
            a = null; // the reference to a2 was passed in, but is set to null
                      // a2 is not set to null - this copy of a reference is!
            return a; // null is returned
        }
        public static void main(String [] args){
            A a1 = new A(1); // 1 obj
            A a2 = new A(2); // 2 obj
            A a3 = new A(3); // 3 obj
            a3 = a1.go(a2); // a3 set to null and flagged for GC - see above for why
            a1 = null; // so far, a1 and a3 have been set to null and flagged
    
            test(a1);
            test(a2);
            test(a3);
    
        }
        static void test(A a) {
            try { System.out.println(a); } 
            catch(Exception e) { System.out.println((String)null); }
        }
    }
    

    And output:

    c:\files\j>javac A.java
    
    c:\files\j>java A
    null
    2
    null
    

    And the followup is that at that point, a1 and a3 were eligible for GC, and a2 was not.

    The lesson from this question is that “Passing an object reference to a method and setting that reference to null does not cause the original reference to be nulled”. That’s the piece of knowledge the interviewer was attempting to test.

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

Sidebar

Related Questions

Should the following code give a warning? class Foo { public void Do() {
why following code doesn't compile ? class aa1 <String> { public void fun(){ String
I have following code: class TR_AgentInfo : public tuple< long long, //AgentId string, //AgentIp
I have the following piece of code: class Student { public: Student(){} void display()
HI, I have the following code: class Libr { public: Libr(); std::string book; class
I have the following code public class Test { Lock lock = new ReentrantLock();
Is the following legal C++ code: class C { static public int x; };
copy paste the following code in new C# console app. class Program { static
The following code: class Base1 { public: void go() {} }; class Base2 {
I have a problem with the following code : class Program { public static

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.