What is the result?

Given:

What is the result?
A. false, false
B. false, true
C. true, false
D. true, true

Download Printable PDF. VALID exam to help you PASS.

6 thoughts on “What is the result?

  1. Answer B:

    It is important to understand that the reference variable (e.g. str1, str2…etc) are NOT the objects, they are actually “integers” under the hood, and these integers represent the memory addresses where the objects reside.

    So, we can say that the value of str1 is an integer (the address in the memory), NOT the object itself. We can also say that the CONTENTS of the object which resides in the memory address held by str1 are “Java”.

    1. Now, we know that, in general, the (==) operator, when used to compare between two reference variables, it compares reference equality (i.e. on whether both the compared reference variables hold the same “memory address” of the compared object instances), or in other words, on whether the compared reference variables point to the same object in memory.
      We also know that the method (.equals()), is a method of the super class Object. And what it does, is it checks the reference equality, just exactly as the (==) does, and returning a boolean value when it is done.

      1. HOWEVER, String objects are a bit special. The String class has its own version of the (.equals()) method, so to say, the String-class (.equals()) method overrides the (.equals()) method of the super class “Object”. And the “String” version of the (.equals()) method actually checks THE CONTENTS of two String objects, NOT the reference equality. Therefore, ( str1.equals(str3) ) returns true.

  2. answer is B
    public static void main(String[] args) {
    String str1=”java”;
    String [] str2={“j”,”a”,”v”,”a”};
    String str3=””;
    for (String str:str2){
    str3=str3+str;
    }
    boolean b1=(str1==str3);
    boolean b2=(str1.equals(str3));
    System.out.println(b1+” “+b2);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.