Search this Site


More books @amazon.com

Home- Self Evaluation on Object Equivalence 

Introduction:
Clear understanding of object equivalence is essential for SCJP examination. Note the difference reference identity and reference equivalence. In case of reference identity, == between the two references will always return true. On the other hand, equivalence denotes value equality where in the objects may or may not be identical but
equals() will return true. 
The following self review questions are kept simple and illustrate the above mentioned points. Once you are well versed with the concept underlying these equality methods and operators, you would be in a better position to tackle any question on this subject in the actual examination. 

Self Evaluation Questions: (Total Questions: 16)

Q1. Will the following code print true or false
public class MyClass
{
 public static void main (String args[])
 {
  String strlit = "SCJP";

  String strobj = new String ("SCJP");

  System.out.println(strlit.hashCode() == strobj.hashCode());
 }
}

Will print true
Will print false
SKIP THE QUESTION


Q2. Will the following code print true or false
public class MyClass
{
 public static void main (String args[])
 {
  StringBuffer strbuf = new StringBuffer("SCJP");

  StringBuffer strbuf1 = new StringBuffer ("SCJP");

  System.out.println(strbuf.hashCode() == strbuf1.hashCode() )
}

Will print true
Will print false

SKIP THE QUESTION


Q3. Will the following code print true or false
public class MyClass
{
  public static void main (String args[])
  {
    StringBuffer strbuf = new StringBuffer("SCJP");

    StringBuffer strbuf1 = new StringBuffer("SCJP");

    System.out.println(strbuf.equals(strbuf1) )
  }
}

Will print true
Will print false 
SKIP THE QUESTION



Q4.
Will the following code print true or false
public class MyClass
{
  public static void main (String args[])
  {
    StringBuffer strbuf = new StringBuffer("SCJP");

    StringBuffer strbuf1 = new StringBuffer("SCJP");

    System.out.println(strbuf == strbuf1);
  }
}

Will print true
Will print false 
SKIP THE QUESTION


Q5.
Will the following code print True or False
public class MyClass
{
  public static void main (String args[])
  {
    String str = new String ("SCJP");

    System.out.println((str.toString() == "SCJP" ) );
  }
}

Will print true
Will print false

SKIP THE QUESTION

Q6. What will be the result of executing the following code
public class MyClass
{
  public static void main(String args[])
  {
   
   Object obj1 = "SCJP";

   Object obj2 = new String("SCJP");

   System.out.println(obj1.equals(obj2));
  }
}

Will print true
Will print false
SKIP THE QUESTION


Q7. What will be the result of executing the following code
public class MyClass
{
  public static void main(String args[])
  {
    System.out.println("SCJP" == "scjp".toUpperCase() )
  }
}
Will print true
Will print false
SKIP THE QUESTION


Q8. What will be the result of executing the following code
public class MyClass
{
   public static void main (String args[])
   {
       String str = "SCJP";

       String str2 = ""; //blank string

       System.out.println(str == (str + str2).intern());
   }
}

Will print true
Will print false
SKIP THE QUESTION


Q9. What will be the result of executing the following code
public class MyClass
{
   public static void main (String args[])
   {
       String str = "SCJP";

       String str2 = "";  // There is no space 
                          // between double quotes

       System.out.println(str == str + str2);
   }
}


Will print true
Will print false
SKIP THE QUESTION


Q10. What will be the result of executing the following code
public class MyClass
{
 public static void main (String args[])
 {
   String str = "SCJP";

   String str2 = str.concat(""); // There is no space 
                                 // between double quotes

    System.out.println(str == str2);
 }
}

Will print true
Will print false
SKIP THE QUESTION


Q11. What will be the result of executing the following code
public class MyClass extends Object
{
 public static void main (String args[])
 {
   int arr1[] = {1,2,3,4};
   int arr2[] = {1,2,3,4};

   System.out.println(arr1.equals(arr2));
 }
}
Will print false
Will print true
Will give compile error
SKIP THE QUESTION


Q12. What will be the result of executing the following code
public class MyClass
{
  public static void main (String args[])
  {
    String str1="Java";

    System.out.println(str1 == "Ja"+"va");
  }
}
Will print false
Will print true
SKIP THE QUESTION


Q13. What will be the result of executing the following code
public class MyClass
{
  public static void main (String args[])
  {
    String str1= "Ja";
    String str2= "va";
    String str3= "Java";

    System.out.println(str3 == str1 + str2);
  }
}
Will print false
Will print true
SKIP THE QUESTION


Q14. Which of the following line of code needs to be inserted for the following application at line 5 to compile correctly and print true.
1.public class MyClass{
2. public static void main (String  args[])  {
3.    Object strarr  [] = {"Welcome",null};
4.    Object strarr2 [] = {"Welcome",null};
5.    <<  >>
6.  }
7.}

System.out.println(strarr[1] == strarr2[1] );
System.out.println(strarr[1].equals(strarr2[1]) );
SKIP THE QUESTION


Q15. Assuming the following code compiles correctly and prints true, which of the below mentioned concepts are illustrated.

compilation unit 1:

package MyPackage;
public class MyClass
{
  public static void main (String args[])
  {
    String str1 = "SCJP";
    System.out.println(str1 == AClass.str);
  }
}
class AClass
{
  static String str = "SCJP";
}

compilation unit 2:

package MyPackage2;
public class AClass
{
  static String str = "SCJP";
}


(1) String objects that are values of constant expressions share unique instances.
(2) Literal strings within the same class in the same package represent references to same String object.
(3) Strings computed by constant expressions are resolved at compile time and then treated as if they were constant expressions.
(4) Strings computed at runtime share the same instances and hence are same.


Q16. What will be the output of compiling and running following code
public class MyClass
{
  public static void main (String args[])
  {
    Integer i = new Integer(15);

    Integer j = new Integer(15);

    System.out.println(i == j);

    System.out.println(i.equals(j));
  }
}
The code will print
    
false
  true
The code will print
    
true
  false
The code will print
    
true
  true
The code will print
    
false
  false
SKIP THE QUESTION


  

[ TOP OF PAGE ]

 


If you have any suggestions which will help me to improve the contents, please email me your feedback. My email id is sandeepnachane@yahoo.com

MAIN MENU

BEFORE YOU BEGIN

Certification FAQ
Certification Objectives

TICKLE YOUR GRAY CELLS

Interactive Sample Java Test
Over 6000 students have  tested !

Object Equivalence 

Threads

SCJP STUDY TOOLS

Study Tips

Certification Books

Other Certification Sites

Tutorials

SCJP Revision Tips 

MISCELLANEOUS LINKS

Java Magazines

Java IDE's

Career Related Websites


Practical Java Programming Language Guide

 

Page Last Modified: 15 February, 2001

 

Disclaimer     © 2000-01 Sandeep Nachane Feedback