What is the expected output of this code segment: public static void main(String
ID: 3813555 • Letter: W
Question
What is the expected output of this code segment: public static void main(String [] args) {Car myCar - new Car(); myCar.make = "Mercedes"; myCar.topSpeed = 100; String myString = new String("Monday"); int myValue = 10; System, out.println(myCar.make); System.out.println(myCar.topSpeed); System.out.println(myString); System.out.println(myValue); chanqeValues(myCar, myString, myValue); System.out.println(myCar.make); System.out.println(myCar.topSpeed); System.out.println(myString); System.out.println(myValue);} public static void changeValues(Car myCar, String myString, int myValue) {myCar.make - "Audi"; myCar.topSpeed = 200; myString = "Tuesday"; myValue = 500;} class Car {public int topSpeed; public String make;}Explanation / Answer
Output:
Explaination:
The values of myCar object variables make and topSpeed will be changed by function changeValues. But local variables myString and myValue will not be changed as expected as their scope is inside the function only.
import java.util.*;
class TestCar
{
public static void main (String[] args)
{
Car myCar = new Car();
myCar.make = "Mercedes";
myCar.topSpeed = 100;
String myString = new String("Monday");
int myValue = 10;
System.out.println(myCar.make);
System.out.println(myCar.topSpeed);
System.out.println(myString);
System.out.println(myValue);
changeValues(myCar,myString,myValue);
System.out.println(myCar.make);
System.out.println(myCar.topSpeed);
System.out.println(myString);
System.out.println(myValue);
}
public static void changeValues(Car myCar,String myString,int myValue)
{
myCar.make = "Audi";
myCar.topSpeed = 200;
myString = "Tuesday";
myValue = 500;
}
}
class Car
{
public int topSpeed;
public String make;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.