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 2 of 2.
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;
}
}
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
Ashok said:
1 decade ago
Even it is declared as boolean in fix(),it is defined as an ordinary variable.so it couldn't reflect the value in start(). Got it guys
Parthiban said:
1 decade ago
Anybody have a solution?
Priya said:
1 decade ago
Priya: the variable after start() is assigned to be false.
Here many b1s here & there makes confusion. b1 which is the parametre of fix()is entirley different from previous one.
Here many b1s here & there makes confusion. b1 which is the parametre of fix()is entirley different from previous one.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers