What will be printed by this program public class Question4

What will be printed by this program?

public class Question4 {

static String s1,s2,s3,s4;

public static void main (String[] args){

s1 = new String(\"CAT\");

s2 = new String(\"DOG\");

s3 = new String(\"RAT\");

animal(s1,s2);

System.out.println(mixThem(s2,s3));

} //main

private static void animal (String a1, String a2) {

if(a1.equals(a2))

System.out.println(a1);

else

System.out.println(a2);

}

private static String mixThem (String b1, String b2){

String c1 = b1;

b1 = b2;

b2 = new String (\"HAT\");

System.out.println(b1);

System.out.println(b2);

System.out.println(c1);

System.out.println(s2);

return b2;

}

} //Question4

***Please give the output for the java file

Solution

public class Question4 {
static String s1,s2,s3,s4;
public static void main (String[] args){
s1 = new String(\"CAT\");   //Assigns CAT to s1.
s2 = new String(\"DOG\");   //Assigns DOG to s2.
s3 = new String(\"RAT\");   //Assigns RAT to s3.
animal(s1,s2);           //Calls the method animal with parameters s1, s2, and will print DOG to output.
System.out.println(mixThem(s2,s3));   //Calls the method mixThem with parameters s2, s3, and will print the word HAT.
} //main
private static void animal (String a1, String a2) {
if(a1.equals(a2))   //If both the parameter are equal
System.out.println(a1);   //Prints first string.
else
System.out.println(a2);   //If not, prints second string.
}
private static String mixThem (String b1, String b2){
String c1 = b1;               //Assigns the first string to c1.
b1 = b2;                   //Assigns the second string to first string.
b2 = new String (\"HAT\");   //Assigns the string HAT to second string.
System.out.println(b1);       //Prints b1, i.e., RAT
System.out.println(b2);       //Prints b2, i.e., HAT
System.out.println(c1);       //Prints c1, i.e., DOG
System.out.println(s2);       //Prints s2, i.e., DOG
return b2;                   //Returns b2, i.e., HAT
}
} //Question4

//Therefore, the final output is:
//DOG
//RAT
//HAT
//DOG
//DOG
//HAT

What will be printed by this program? public class Question4 { static String s1,s2,s3,s4; public static void main (String[] args){ s1 = new String(\
What will be printed by this program? public class Question4 { static String s1,s2,s3,s4; public static void main (String[] args){ s1 = new String(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site