Review Flashcards

1
Q
public class MyClass2 
{
  //...
  public void method1(int a)
  {
  }
}

Given the class above, which method signature(s) below would be valid for method overloading (in other words, which methods could you add without a compiler error)? More than one answer may be correct.

public boolean method1(int a)

public void method1(int a, int b)

public void method1(String a)

A

public boolean method1(int a)

This should not be selected
This is not valid for overloading. You can only overload by changing the parameter list. Changing the return type alone is not valid for method overloading as the return type is not part of the method signature (just the method name and parameter list are in the method signature).

public void method1(int a, int b)

Correct
This is valid for overloading. Having a different parameter list and the same method name is method overloading.

public void method1(String a)

Correct
This is valid for overloading. Having a different parameter list and the same method name is method overloading.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Given the following definition of the SimpleLocation class, what does the main method in LocationTester print?

public class SimpleLocation
{
    public double lat;
    public double lon;
    public SimpleLocation(double latIn, double lonIn)
    {
        this.lat = latIn;
        this.lon = lonIn;
    }
}
public class LocationTester
{
    public static void main(String[] args)
    {
        SimpleLocation loc1 = new SimpleLocation(39.9, 116.4);
        SimpleLocation loc2 = new SimpleLocation(55.8, 37.6);
        loc1 = loc2;
        loc1.lat = -8.3;
        System.out.println(loc2.lat + ", " + loc2.lon);
    }
}
  1. 8, 37.6
    - 8.3, 37.6
    - 8.3, 116.4
  2. 9, 116.4
A

-8.3, 37.6

Правильно
This is correct. loc2’s lat value changes via the change we make through loc1 because loc1 and loc2 now refer to the same object (the one that was created second).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly