Object Oriented Programming using Java Questions and Answers – Object Class

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Object Class”.

1. What will be the output of the following Java code?

class StudentHashcode
{ 
    static int num = 60;  
    int temp; 
    Student() 
    { 
        temp = num;
    } 
    public int hashCode() 
    { 
        return temp; 
    } 
    public static void main(String args[]) 
    { 
        Student s = new Student(); 
        System.out.println(s.hashCode()); 
        System.out.println(s);
    } 
}

a)

60
60

b)

advertisement
advertisement
3c
Student@3c

c)

3c
Student@60

d)

60
Student@3c
View Answer
Answer: d
Explanation: In java.lang class, the hashCode() method returns a hash value that is used to search object in a collection. The toString() method returns a string which has the name of the class, the at-sign character ‘@’, and the unsigned hexadecimal representation of the hash code of the object. In this case, the hexadecimal representation of 60 is 3c as 3*(161) + 12*(160).
Output:

$ javac StudentHashcode.java
$ java StudentHashcode
60
Student@3c

advertisement

2. What will be the output of the following Java code?

advertisement
class boolEqual 
{ 
    public static void main(String[] args) 
    {  
        Boolean obj1 = new Boolean(true); 
        Boolean obj2 = new Boolean(false); 
        System.out.println(obj1.equals(obj2)); 
    } 
}

a) false
b) true
c) 0
d) Compilation error
View Answer

Answer: a
Explanation: equals() method is a non-static method of java.lang.Object class that is used to compare two objects. As all classes in Java are a sub-part of Object class, this method is inherited to every class you create in java. In this ‘obj1’ and ‘obj2’ have the values of true and false respectively. So, the equals() method when invoked returns false as output.
Output:

$ javac boolEqual.java
$ java boolEqual
false

3. What will be the output of the following Java code?

class Clock 
{
   public static void main(String[] args) 
   {
      Clock time = new Clock();
      System.out.println("" + time.getClass());
      Integer i = new Integer(5);
      System.out.println("" + i.getClass());
   }
}

a)

class Clock
class java.lang.Integer

b)

class Clock
class java.lang.Datatype

c)

class time
class java.io.Integer

d)

class Clock
class java.io.Integer
View Answer
Answer: a
Explanation: getClass() is the method of Object class. This method returns the runtime class of the object. In the above example, time is an object which belongs to the Clock class whereas ‘i’ belongs to the Integer which is a subset of java.lang class.
Output:

$ javac Clock.java
$ java Clock
class Clock
class java.lang.Integer

4. Which of the following options can be a possible output of the Java code mentioned below?

class FormDetails
{  
    int id;  
    String name;  
    String tag;  
    Details(int id, String name, String tag)
    {  
        this.id=id;  
        this.name=name;  
        this.tag=tag;  
    }  
    public static void main(String args[])
    {  
        Details obj1=new Details(1,"Pawan","NoTag");  
        Details obj2=new Details(2,"Mahesh","NoTag");  
        System.out.println(obj1);  
        System.out.println(obj2);  
    }  
}

a)

1 Pawan NoTag
2 Mahesh NoTag

b)

1,"Pawan","NoTag"
2,"Mahesh","NoTag"

c)

Details@15db9742
Details@6d06d69c

d)

obj1@15db9742
obj2@6d06d69c
View Answer
Answer: c
Explanation: When we try to print the object without using toString(), we get the output which is in the form of classname@HashCode_in_Hexadecimal_form. If we want the proper information about ‘obj1’ and ‘obj2’ objects, then we have to override toString() method of Object class in the Details class, which is missing in the above code.
Output:

$ javac FormDetails.java
$ java FormDetails
Details@15db9742
Details@6d06d69c

5. What will be the output of the following Java code?

class PersonalDetails
{ 
    String name; 
    int age; 
    Person (String name, int age) 
    { 
        this.name = name; 
        this.age = age;
    } 
    public String toString() 
    { 
        return name + " " + age; 
    } 
    public static void main(String[] args) 
    { 
        Person b = new Person("Gautam Nanda", 21);  
        System.out.println(b.toString()); 
    } 
}

a) “Gautam Nanda” 21
b) Gautam Nanda 21
c) Compilation Error
d) “Gautam Nanda 21”
View Answer

Answer: b
Explanation: Java compiler internally invokes the toString() method on the object. So by overriding the toString() method, it returns the desired output. By overriding the toString() method of the Object class, we can return values of the object.
Output:

$ javac PersonalDetails.java
$ java PersonalDetails
Gautam Nanda 21

6. What will be the output of the following Java code?

class FinaliseObject 
{
    public void finalize()
    {
        System.out.println("This is the finalize() method ");
    }
    public static void main(String[] args) {
        String str = new String("Hi, Welcome in Java World");
        str = null;
        System.gc();
        System.out.println("This is finalize class");
    }
}

a) This is the finalise() method
b) Hi, Welcome in Java World
c) This is finalize class
d) Compilation error
View Answer

Answer: c
Explanation: Every class inherits the finalize() method from java.lang.Object. The method is invoked by the garbage collection protocol when it determines that there are no more references to the existing object.
Output:

$ javac FinaliseObject.java
$ java FinaliseObject
This is finalize class

7. What will be the output of the following Java code?

class Cloning implements Cloneable
{  
    int id;  
    String name;  
    Cloning(int id, String name)
    {  
        this.id=id;  
        this.name=name;  
    }  
    public Object clone()throws CloneNotSupportedException
    {  
        return super.clone();  
    }    
    public static void main(String args[])
    {  
        try
        {  
            Cloning obj1=new Cloning(1,"dravid");  
            Cloning obj2=(Cloning)obj1.clone();  
            System.out.println(obj1.id+" "+obj1.name);  
            System.out.println(obj2.id+" "+obj2.name); 
        }
        catch(CloneNotSupportedException c){}  
    }  
}

a)

1 dravid
1 dravid

b) 1 dravid
c)

1 dravid
1 dravid 1 dravid

d) Compilation error
View Answer

Answer: a
Explanation: Both reference variables in this case have the same value. Thus, the clone() replicates the values of one object to another. So we don’t need to write any separate code to copy the value of an object to another. After cloning takes place, both the objects have the exact same values.
Output:

$ javac Cloning.java
$ java Cloning
1 dravid
1 dravid

8. What will be the output of the following Java code?

public class JavaClassExample
{  
    public static void main(String[] args)   
    {   
        Object obj1 = new String("Apple");   
        Class b = obj1.getClass();   
        System.out.println("Class is:" + b.getName());   
    }   
}

a) Class is Apple
b) Class is JavaClassExample
c) Class is java.lang.String
d) Class is String
View Answer

Answer: c
Explanation: getClass() is the method of Object class. This method returns the runtime class of this object. It returns the Class objects that represent the runtime class of this object. In the above case, the object belongs to java.lang.String.
Output:

$ javac JavaClassExample.java
$ java JavaClassExample
Class is java.lang.String

9. What will be the output of the following Java code?

 class Equality 
 {
     public int hashCode() 
     {
        return 199;
     }
     public static void main(String[] args) 
     {
         String a = new String("a");
         String b = new String("a");
         System.out.println(a.equals(b)); 
     }
 }

a) true
b) false
c) Compilation error
d) -1
View Answer

Answer: a
Explanation: The equals method is used when one wants to know if two objects are equivalent by whatever definition the objects find suitable. It returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object. In this case, both the objects are same.
Output:

$ javac Equality.java
$ java Equality
true

10. What will be the output of the following Java code?

class IntegerHashCode 
{  
    public static void main(String[] args)  
    {  
        int hashValue = Integer.hashCode("155");  
        System.out.println("Hash code Value for object is: " + hashValue);  
    }  
}

a) 155
b) “155”
c) Compilation error
d) Hash code Value for object is: 155
View Answer

Answer: c
Explanation: The java.lang.Integer.hashCode() method of Integer class in Java is used to return the hash code for a particular Integer. It is equal to the uncomplicated primitive integer value, represented by this Integer object. But in this case, a string “155” is passed to an Integer which therefore is not executable and error is thrown.
Output:

$ javac IntegerHashCode.java
$ java IntegerHashCode
java.lang.RuntimeException: Uncompilable source code

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.