Given this code snippet,

public class Person
{
    public String name;

    public Person(String name)
    {
        this.name = name;
    }

    public void changeName(String name)
    {
        this.name = name;
    }

    public void printName()
    {
        System.out.println(this.name);
    }
}
what will this code print?

Person myPerson = new Person("Bob");
myPerson.changeName("Joe");
myPerson.name = "John";
myPerson.printName();


Bob


Joe


John


This code will error


Bob

Joe

John

Question: 2
Given this code snippet,

public class Athlete
{  
    public Athlete(String name)
    {
        this.name = name;
    }
}
what is missing from the class definition?



Need to declare the name instance variable


Missing void in constructor definition


Class constructors must be private


Java does not use the keyword this, instead it uses self


No constructor is defined for the class

Question: 3
Given the code snippet,

public class Pokemon
{
    private String name;
    private int health;

    public Pokemon(String name, int health)
    {
        name = name;
        health = health;
    }
}
what is wrong with the class definition?

Hint : We expect the instance variables name and health to be initialized in the constructor.



Missing void in constructor definition


Class constructors must be private


The constructor parameters cannot have the same names as the instance variables.


Must use this in constructor when the constructor parameters have the same name as instance variables. ie:

this.name = name;
this.health = health;
Question: 4
What is the output of the following program?

public class Main
{
    private static int n = 0;

    public void run()
    {
        Main m1 = new Main();
        Main m2 = new Main();
        Main m3 = new Main();
        m1.foo();
    }

    public Main()
    {
        n = n + 1;
    }

    public void foo()
    {
        System.out.println(n);
    }
}


0


1


2


3

Question: 5
Refer to the code listing from the previous question. Suppose
the following method is added:

public void setN(int n)
{
    this.n = n;
}
What would be the output of the program if run() were changed to the following:

public void run()
{
    Main m1 = new Main();
    Main m2 = new Main();
    Main m3 = new Main();

    m1.setN(5);
    m3.foo();
}


0


1


3


5

Question: 6
What is the output of the following program?

public class Main
{
    private String str = "bar";

    public void run()
    {
        Main m = new Main("foo");
        System.out.println(m.getString());
    }

    public Main(String str)
    {
        str = str;
    }

    public String getString()
    {
        return str;
    }
}


The program does not compile.


The program does not produce any output.


foo


bar

Question: 7
Given an instance of the Athlete class called athlete, what is the proper way to set the value of the jersey number after it has been instantiated?

public class Athlete
{
  private String first_name;
  private String last_name;
  private int jersey;

  public int getJersey()
  {
    return this.jersey;
  }

  public Athlete(String first_name, String last_name, int jersey)
  {
    this.first_name = first_name;
    this.last_name = last_name;
    this.jersey = jersey;
  }
}


athlete.jersey = 23;

athlete.getJersey() = 23;

athlete.getJersey(23);

athlete.setJersey(23);

You cannot set the jersey, since jersey is private and there is no setter method.

Question: 8
Given an instance of the Athlete class called athlete, what is the proper way to get the value of the jersey number?

public class Athlete
{
  private String first_name;
  private String last_name;
  private int jersey;

  public int getJersey()
  {
    return this.jersey;
  }

  public Athlete(String first_name, String last_name, int jersey)
  {
    this.first_name = first_name;
    this.last_name = last_name;
    this.jersey = jersey;
  }
}


athlete.jersey

athlete.jersey()

athlete.getJersey

athlete.getJersey()

None of the above

Question: 9
Given this Athlete class, which of the following setter methods for the jersey variable is correct?

public class Athlete
{
  private String first_name;
  private String last_name;
  private int jersey;

  public int getJersey()
  {
    return this.jersey;
  }

  public Athlete(String first_name, String last_name, int jersey)
  {
    this.first_name = first_name;
    this.last_name = last_name;
    this.jersey = jersey;
  }
}


public int setJersey(int jersey)
{
    this.jersey = jersey;
}

public void setJersey(int jersey)
{
    this.jersey = jersey;
}

public void setJersey(int jersey)
{
    jersey = jersey;
}

public void setJersey(jersey)
{
    this.jersey = jersey;
}

public void setJersey(int jersey)
{
    this.jersey = jersey;
    return jersey;
}
Question: 10
Mark the valid way to create an instance of Athlete given the following Athlete class definition:

public class Athlete {
  String first_name;
  String last_name;
  int jersey;

  public Athlete(String first_name, String last_name, int jersey) {
    this.first_name = first_name;
    this.last_name = last_name;
    this.jersey = jersey;
  }
}


athlete = new Athlete("Dirk", "Nowitzki", 41);

Athlete athlete = new Athlete("Dirk", "Nowitzki", 41);

Athlete athlete = new Athlete("Dirk" "Nowitzki" 41);

Athlete athlete = new Athlete("Dirk", "Nowitzki", "41");
Question: 11
Mark the valid way to create an instance of Foo given the following code:

public class Foo {
  int bar;
  String stoo;

  public Foo() {
    this.bar = 0;
    this.stoo = "";
  }

  public Foo(int bar) {
    this.bar = bar;
    stoo = "You.";
  }
}


Foo fee = Foo(32);

Foo fee;
new fee = Foo();

Foo fee = new Foo(10, "You.")

Foo fee;
fee = new Foo();
Question: 12
Given the following definition for the class Athlete:

public class Athlete
{
  String first_name;
  String last_name;
  int jersey;

  public Athlete(String first_name, String last_name)
  {
      this.first_name = first_name;
      this.last_name = last_name;
      this.jersey = 0;
  }

  public Athlete(String first_name, String last_name, int jersey)
  {
      this.first_name = first_name;
      this.last_name = last_name;
      this.jersey = jersey;
  }
}
Which of the following are valid instantiations of the class Athlete?
I – Athlete joe = new Athlete("Joe", "Montana");
II – Athlete joe = new Athlete("Joe, "Montana", "16");
III – Athlete joe = new Athlete("Joe", "Montana", 16);



I and III


I only


II only


I II and III


II and III

Question: 13
Which methods of class Foo can be called without an actual instance of the class Foo?

public class Foo
{
    public static void foo()
    {
        ...
    };

    public void bar()
    {
        ...
    };

    public void baz()
    {
        ...
    };
}


All methods require an instance of Foo in order to be called.


No methods require an instance of Foo in order to be called.


bar()


baz()


foo()

Answer all questions.