Given this code snippet, public class Person { public String name; public Person(String…
- User
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?
- Assistant
- John
- Need to declare the name instance variable
- Must use this in constructor when the constructor parameters have the same name as instance variables. ie: this.name = name; this.health = health;
- 3
- 5
- bar
- You cannot set the jersey, since jersey is private and there is no setter method.
- athlete.getJersey()
- public void setJersey(int jersey) { this.jersey = jersey; }
- Athlete athlete = new Athlete("Dirk", "Nowitzki", 41);
- Foo fee; fee = new Foo();
- I and III
- foo()
- User
- Assistant
- John
- Need to declare the name instance variable
- Must use this in constructor when the constructor parameters have the same name as instance variables ie: this.name = name; this.health = health;
- 3
- 5
- bar
- You cannot set the jersey, since jersey is private and there is no setter method.
- athlete.getJersey()
- public void setJersey(int jersey) { this.jersey = jersey; }
- Athlete athlete = new Athlete("Dirk", "Nowitzki", 41);
- Foo fee; fee = new Foo();
- I and III
- foo()
I am confident in these answers.
- User
- Assistant