Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Object in java Assignment and Object Passing By Value or Reference

object in java

Table of Contents

  • Object in java
  • object in java Passing by Value or Reference to a Method Explained
    • object in java MCQ
    • 3) State TRUE or FALSE. A Java class provides encapsulation.
    • 5) The value of one Pri-mitive variable is assigned to another Pri-mitive variable by ___ in Java.
    • 7) An object or Pri-mitive value that is passed from one method to another method is called ___ in Java. (Argument / Parameter)
    • 11) Java object assignment happens by ___.
    • 13) In Java Pass by reference ___ is passed even if you are passing a reference to an object.
    • 14) A Java reference is comparable to ___ in C language.
    • 15) ___ is the superclass to all Java classes either user-defined or built-in.

Study and learn Interview MCQ Questions and Answers on object in java . Attend job interviews easily with these Multiple Choice Questions.

Object in java

Objects are always passed by Reference. Reference is nothing but the starting address of a memory location where the actual object lies. You can create any number of references to just One Object. Let us go by an example.

We create a Class “Cloud” with a single Property called “color”. It is a String object. We create 3 objects of the Cloud type.

public Class Cloud
{
  String color;
  public static void main(String[] args)
  {
    Cloud obj1 = new Cloud();
    obj1.color = "RED";

    Cloud obj2, obj3;
    obj2 = obj1; //Assign obj1 to obj2
    obj3 = obj2; //Assign obj2 to obj3
    System.out.println("OBJ2= " + obj2.color + ", OBJ3= " + obj3.color);
    obj2=null; obj3=null;
    System.out.print("Color= " + obj1.color);
  }

}
//OUTPUT
//OBJ2= RED, OBJ3= RED
//Color= RED

Notice that all three references obj1, obj2 and obj3 are pointing to the same Cloud object. Thus the above example outputs the same color-property value “RED“. Later, we made the references obj2 and obj3 to point to the null location. Still the original object pointed or referenced by obj1 holds the value. So the final PRINT statement outputs “RED” as usual.

Note: Reference is also called a Variable whether Pri-mitive or Object.

This concludes that a Java object assignment happens by Reference. But the actual value of memory location pointed by References are copied by Value only. So we are actually copying memory locations from one Reference variable to another Reference variable by Value. Interviewers ask this famous Java question just to test your knowledge. If someone asks in C language point of view, say that it is by Reference. If they ask in Java point of view, say that it is by Value. 

object in java Passing by Value or Reference to a Method Explained

You already learnt that Object assignment in Java happens by Reference. Let us try to pass Objects to another method as a parameter or argument.

class Tractor
{
  int height;

  void change(Tractor obj)
  {
    obj.height = 200;
  }
  public static void main(String[] args)
  {
    Tractor obj1 = new Tractor();
    obj1.height = 100;
    System.out.println("Before Height= " + obj1.height);
    obj1.change(obj1);
    System.out.println("After Height= " + obj1.height);
  }
}
//OUTPUT
//Before Height= 100
//After Height= 200;

In the above example, we have created an object “obj1” of type Tractor. We assigned value of 100 to the property “height”. We printed the value before passing the object to another method. Now, we passed the object to another method called “change” from the calling-method “main“. We modified the height property to 200 in the called-method. Now, we tried to print the value of the property in the Calling method.

Similarly, we can pass Java objects to the Constructor using Pass by Reference or simply Reference. 

Here also, we pass the memory location pointed by one Reference variable to another Reference variable of a Called method as a Value only. So, you are actually passing Values (memory locations represented by integer or long data type). Interviewers take advantage of this tricky logic to confuse you. If someone asks in C language point of view, say that it is by Reference. If they ask in Java point of view, say that it is by Value.

[WpProQuiz 157]

object in java MCQ

1) What is the output of the below Java program with two classes?

//Testing1.java
public class Example
{
	
}
public class Testing1
{
  public static void main(String[] args)
  {
    System.out.println("Hello Boss.!");
  }
}

A) Hello Boss.!

B) No Output

C) Compiler error

D) None of the above

Answer [=] C

Explanation:

There can not be more than one public class declared inside a single java file.

2) What is the output of the below Java program?

//bingo.java file
public class Hello
{
  public static void main(String[] args)
  {
    System.out.println("BINGO");
  }
}

A) bingo

B) BINGO

C) Compiler error

D) None

Answer [=] C

Explanation:

The class name and the java file name should be the same. So, change either file name or class name to match.

3) State TRUE or FALSE. A Java class provides encapsulation.

A) TRUE

B) FALSE

Answer [=] A

4) What is the output of the below java class?

class Fox
{
  int legs = 2;
}
class Testing2
{
  public static void main(String[] args)
  {
    Fox t1 = new Fox();
    System.out.println("T1 before: " + t1.legs);
    t1.legs = 4;
    System.out.println("T1 After: " + t1.legs);
  }
}

A)

T1 before: 4
T1 After: 4

B)

T1 before: 2
T1 After: 2

C)

T1 before: 2
T1 After: 4

D) Compiler error

Answer [=] C

Explanation:

There can be any number of classes in a single .java file.

5) The value of one Pri-mitive variable is assigned to another Pri-mitive variable by ___ in Java.

A) Pass by value

B) Pass by reference

Answer [=] A

6) A Pri-mitive variable is passed from one method to another method by ___ in Java.

A) Pass by value

B) Pass by reference

Answer [=] A

7) An object or Pri-mitive value that is passed from one method to another method is called ___ in Java. (Argument / Parameter)

A) Argument

B) Parameter

Answer [=] B

8) An object or a Pri-mitive value that is received in a method from another method is called ___ in Java. (Argument / Parameter)

A) Argument

B) Parameter

Answer [=] A

9) What is the output of the below Java program that passes an object to another method?

class Food
{
  int items;
  int show()
  {return items;}
}

class Testing9
{
  public static void main(String[] args)
  {
    Food f = new Food();
    f.items = 5;
    System.out.println("Items Before = " + f.show());
    change(f);
    System.out.println("Items After = " + f.show());
  }
  static void change(Food foo)
  { foo.items = 10; }
}

A)

Items Before = 10
Items After = 10

B)

Items Before = 5
Items After = 5

C)

Items Before = 5
Items After = 10

D)

Items Before = 10
Items After = 5

Answer [=] C

10) What is the output of the below Java program that passes pri-mitive values?

class Testing10
{
  int rats = 5;

  public static void main(String[] args)
  {
    Testing10 t1 = new Testing10();
    System.out.println("Rats Before = " + t1.rats);
    modify(t1.rats);
    System.out.println("Rats After = " + t1.rats);
  }
  static void modify(int r)
  { r = 20; }
}

A)

Rats Before = 5
Rats After = 5

B)

Rats Before = 20
Rats After = 20

C)

Rats Before = 5
Rats After = 20

D)

Rats Before = 20
Rats After = 5

Answer [=] A

Explanation:

The Pri-mitive values are passed by value only. So, changes in the method modify does not change the original value.

11) Java object assignment happens by ___.

A) Pass by Value

B) Pass by Reference

Answer [=] B

Explanation:

Yes. That is the reason why you can change the values of variables of the object using another reference.

12) Java object passing from one method to another method happens by ___.

A) Pass by Value

B) Pass by Reference

Answer [=] B

Explanation:

References point to the original objects. So they can change the state of the objects.

13) In Java Pass by reference ___ is passed even if you are passing a reference to an object.

A) Address value

B) Variable value

C) Hash code

D) None of the above

Answer [=] A

Explanation:

Yes. The address is passed automatically by Java. So, Java pundits argue that it is passing a value (Address).

14) A Java reference is comparable to ___ in C language.

A) Enum

B) Structure

C) Pointer

D) None

Answer [=] C

15) ___ is the superclass to all Java classes either user-defined or built-in.

A) Class

B) Object

C) Superclass

D) Null

Answer [=] B

Explanation:

Yes. java.lang.Object is the superclass to all Java classes.

16) State TRUE of FALSE. Java objects have built-in methods to handle threads.

A) TRUE

B) FALSE

Answer [=] A

Explanation:

Yes. The methods are wait(), notify() and notifyAll().

17) State TRUE or FALSE. Java Object’s hashcode() method is mainly used with Collection objects.

A) TRUE

B) FALSE

Answer [=] A

Explanation:

Java collection classes use the hashcode() method to determine the equality of two objects.

18) What is the output of the below Java program using toString() method?

class College
{
  public String toString()
  { return "College Object"; }
}
class Testing18
{
  public static void main(String[] args)
  {
    College col = new College();
    System.out.println("Printing Object=" + col);
  }
}

A) Printing Object=

B) Printing Object=null

C) Printing Object=College Object

D) Compiler error

Answer [=] C

Explanation:

print() and println() methods call toString() method of objects automatically.

19) What is the output of the below Java program?

class Cricket
{ int runs; }

class Testing19
{
  public static void main(String[] args)
  {
    Cricket c1 = new Cricket();
    c1.runs = 250;
    Cricket c2;
    c2 = c1;
    c2.runs = 300;
    System.out.println("Runs= " + c1.runs);
  }
}

A) Runs= 0

B) Runs= 250

C) Runs= 300

D) Compiler error

Answer [=] C

Explanation:

The reference C2 also points to the same object pointed by reference C1.

20) What is the output of the below Java program?

class WordPress
{ int posts; }
class Testing20
{
  public static void main(String[] args)
  {
    WordPress wp1 = new WordPress();
    wp1.posts = 25;
    WordPress wp2 = wp1;
    wp1 = null;
    System.out.println("Posts=" + wp2.posts);
  }
}

A) Posts=25

B) Posts=0

C) Posts=null

D) Runtime exception occurs

Answer [=] A

Explanation:

Even if one REFERENCE to the same object is alive, it can be used to access the object. So, wp2 still works even if wp1 is set to null.

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

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.