Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!

Clemonte Johnstone

Ranch Hand
+ Follow
since Jan 05, 2021
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Clemonte Johnstone

Dear all

Please could someone tell me why the second value that is returned is true?

/* What will be the result of compiling and executing Test class?
  A) null:true                                                         [CORRECT]
  B) true:true
  C) NullPointerException is thrown at runtime          [WRONG ANSWER]
  D) null:null
  E) true:false

  1) list.remove() returns the element previously at the specified position that was removed.
  2)
*/

public class _09_Question_ArrayList {
  public static void main(String[] args) {
     List<String> list = new ArrayList<>();
     list.add(null);
     list.add(null);
     list.add(null);
     System.out.println(list.remove(0) + ":" + list.remove(null));
  }
}

Scott Selikoff wrote:The reason toString() is called is that the closest matching overloaded version of println() is the version that takes an object, aka System.out.println(Object).  If you look at the source, this method calls String.valueOf(Object), which then calls object.toString().  Since the Counter class overwrites this method, this version is used.



I don't understand this point. Please could you put it in simple terms for me.

I know that overloading is when you have a method signature with different arguments or a different return type

Campbell Ritchie wrote:

Stephan van Hulst wrote:Why did you think it would throw an exception?

If there were to be any problems like that, they would manifest themselves at compile time and the program wouldn't start. I presume you can tell us what poor design features that code shows.



Lack of encapsulation? No private variables?
I know that do while always executes at least once. However, I am particuarly intersted in what happens when there is false in while condition. Please can you explain that, specifically.
Dear All, please could someone explain what the below program is doing?


public class _66_Question_Do_While_Loop {
   public static void main(String[] args) {
       do {
           System.out.println(100);
       } while (false);
       System.out.println("Bye");
   }
}
Google defines it as such:

== should be used during reference comparison. == checks if both references points to same location or not. equals() method should be used for content comparison. equals() method evaluates the content to check the equality.

Please could you kindly explain the program for me in simple terms?
Dear All, please could someone explain to me why true:true is the wrong answer? I thought that equals() evaluates the contents of an object and not its memory location


/* What will be the result of compiling and executing the below program?
  A) Compilation error
  B) true:true [WRONG ANSWER]
  C) false:true
  D) true:false
  E) false:false [CORRECT]

 
*/

public class _62_Question_StringBuilder {
public static void main(String[] args) {
String str = "java";
StringBuilder sb = new StringBuilder("java");

System.out.println(str.equals(sb) + ":" + sb.equals(str));
}
}
Dear All, please could someone talk me through why this program returns Counter-5. Thanks for any help that you can give

import java.util.ArrayList;

/* What will be the result of compiling and executing the below program?
  A) Compilation error
  B) [Counter-10]
  C) [Counter-5]                                   [CORRECT]
  D) An exception is thrown at runtime             [WRONG ANSWER]
*/


class Counter {
   int count;
   Counter(int count) {
       this.count = count;
   }

   public String toString() {
       return "Counter-" + count;
   }
}

public class _49_Question_ArrayList_Of_Type_POJO_With_Constructor {
   public static void main(String[] args) {
       ArrayList<Counter> original = new ArrayList<>();
       original.add(new Counter(10));                          // Counter-10

       // clone() returns a shallow copy of this ArrayList instance (the elements themselves are not copied)
       ArrayList<Counter> cloned = (ArrayList<Counter>) original.clone();
       cloned.get(0).count = 5;

       System.out.println(original);
   }
}

Stephan van Hulst wrote:Every time you call sb.toString(), it creates a new String instance. The reason for this is easy to understand: The string builder doesn't keep a reference to the previous string object it created, so it can't return the same string object when you call toString() a second time.



Many thanks
Dear all,
I keep on getting caught out when it comes to object reference equality. I know that String is immutable and StringBuilder is mutable. Shouldn't s1 and s2 have the same reference on the heap? sb vs (s1 or s2) would be different?

/* What will be the result of compiling and executing the below class?
  A) Compilation error
  B) An exception is thrown at runtime
  C) false                                 [CORRECT]
  D) true                                  [WRONG ANSWER]

*/

public class _40_Question_StringBuilder {

   public static void main(String[] args) {
       StringBuilder sb = new StringBuilder("Java");       // obj ref 1
       String s1 = sb.toString();                          // obj ref 2
       String s2 = sb.toString();                          // obj ref 3

       System.out.println(System.identityHashCode(sb));    // 356573597
       System.out.println(System.identityHashCode(s1));    // 1735600054
       System.out.println(System.identityHashCode(s2));    // 21685669

       System.out.println(s1 == s2);   // false
   }

}
Dear All, I am uncertain and have no way of checking. I know that 3 objects were created.

/* How many objects of Pen class are eligible for Garbage Collection at Line 4?
  a) 0
  b) 1
  c) 2
  d) 3    
*/


class Pen { }

public class _36_Question_Automatic_Garbage_Collection {
   public static void main(String[] args) {
       new Pen();                           // Line 1
       Pen p = new Pen();                   // Line 2
       change(p);                           // Line 3
       System.out.println("About to end."); // Line 4
   }

   public static void change(Pen pen) {    // Line 5
       pen = new Pen();                    // Line 6
   }
}

Stephan van Hulst wrote:The contents don't matter to the == operator. The == operator performs reference equality. If s1 == s2 returns true, it simply means that s1 refers to the same object that s2 does. So the question boils down to: Why does the string literal "OCAJP" yield the same string instance that "OCAJP" + "" does?

The answer is that the expression "OCAJP" + "" is a compile time constant expression. That means that the compiler can evaluate the expression during compilation and replace it with its final value, which is "OCAJP". When the application starts, all string constants that have the same value will all be represented by the same String instance.



Excellent explanation Stephan. Many thanks.
Dear All, Is this a trick question?

/* Considering the below code, what will be the result of compiling and executing the program?
* a) Compilation error
* b) OCAJP
* c) false             [WRONG ANSWER]
* d) true              [CORRECT]                                                                    */

public class _33_Question_String_Object_Equality {

   public static void main(String[] args) {
       String s1 = "OCAJP";
       String s2 = "OCAJP" + "";
       System.out.println(s1 == s2);

       System.out.println("s1 hash: " + System.identityHashCode(s1));
       System.out.println("s2 hash: " + System.identityHashCode(s2));
   }

}

Program output is

true
s1 hash: 356573597
s2 hash: 356573597

Campbell Ritchie wrote:

Dave Tolls wrote:. . . That comment explains . . .

The comment is a bit mild; that code isn't “redundant”, but it hides a logic error.


Thanks Campbell. Please could you elaborate