Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 2)
2.
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
Answer: Option
Explanation:
The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method.
Discussion:
17 comments Page 1 of 2.
Ramu said:
1 decade ago
Hi @Siva,
long [] a1 is a reference array object means al is not a data type (or) primitive data type.
data types and primitive data types will not change values when you modify in fix method but reference object values will change like
void start()
{
StringBuffer b1 = new StringBuffer("Hello, world");
StringBuffer b2 = fix(b1);
System.out.println(b1 + " : " + b2);
}
StringBuffer fix(StringBuffer b1)
{
b1.append(" Hey");
return b1;
}
Out put : Hello, world Hey : Hello, world Hey // here both are same because b1 is reference object.
Hope you got :)
long [] a1 is a reference array object means al is not a data type (or) primitive data type.
data types and primitive data types will not change values when you modify in fix method but reference object values will change like
void start()
{
StringBuffer b1 = new StringBuffer("Hello, world");
StringBuffer b2 = fix(b1);
System.out.println(b1 + " : " + b2);
}
StringBuffer fix(StringBuffer b1)
{
b1.append(" Hey");
return b1;
}
Out put : Hello, world Hey : Hello, world Hey // here both are same because b1 is reference object.
Hope you got :)
Siva said:
1 decade ago
Hi @Durgaprasad,
I got you but I have one more doubt regarding the following fragment,
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
I got you but I have one more doubt regarding the following fragment,
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
Durgaprasad said:
1 decade ago
Executing starting from main().
Created object of Test class and calling of start() of Test class.control goes to start() and in start()asign value of b1=false after calling fix(boolean b1) method see below
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
/// b1 local verible of fix()method and it return b1 .store this value into another boolean variaable b2. control goes to in start()method and print the b1 and b2 values .
out put is b1=false, b2= true
Created object of Test class and calling of start() of Test class.control goes to start() and in start()asign value of b1=false after calling fix(boolean b1) method see below
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
/// b1 local verible of fix()method and it return b1 .store this value into another boolean variaable b2. control goes to in start()method and print the b1 and b2 values .
out put is b1=false, b2= true
Pratik said:
9 years ago
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
Here, b1 is primitive data type, not an object so when passing it through fix its value will not change.
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
Here, b1 is primitive data type, not an object so when passing it through fix its value will not change.
Kalpesh somani said:
8 years ago
The difference between the definition of an array and ordinary variable is the array is always declared, initialized, and accessed using subscript whereas ordinary variable does not have any subscript.
For that reason in the previous example we are referring to object & in this example, we are referring to the ordinary variable.
So that reason. Array is the set of multiple values where as a variable can store a single value at a time.
For that reason in the previous example we are referring to object & in this example, we are referring to the ordinary variable.
So that reason. Array is the set of multiple values where as a variable can store a single value at a time.
Jansi said:
1 decade ago
I have one more doubt regarding the following fragment,
How you find reference object this program please?
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
How you find reference object this program please?
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
Gurpreet Kaur said:
4 years ago
I think the difference is because b1 was a local datatype variable of the start method. So the change of the value of the variable in one method won't affect its value in the other method.
While a1 was an array object and so its value changed.
While a1 was an array object and so its value changed.
Sijos said:
9 years ago
a1 is the reference object of the long array. It will change the value by fix method.
b1 is datatype it will not change the value by fix method.
Look at the difference between the reference object and datatype.
b1 is datatype it will not change the value by fix method.
Look at the difference between the reference object and datatype.
Sidh said:
5 years ago
See, the b1 in the start block is assigned to the b1 (in printing), but in b2=fix (b1) the b1 value of fix block is assigned and so in printing first false is printed and then true is printed.
(1)
Akhilabanala said:
7 years ago
The main difference is b1 in one method is local variable of that method. Similarly the other b1. So the change in local variable of one method will not affect the variable of another method.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers