CodeHS 5.11: Writing Classes

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

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();

A.
Bob
B.
Joe
C.
John
D.
This code will error
E.
Bob
Joe
John

C.
John

2
New cards

public class Athlete
{
public Athlete(String name)
{
this.name = name;
}
}


What is missing from the class definition?

A. Need to declare the name instance variable
B. Missing void in constructor definition
C. Class constructors must be private
D. Java does not use the keyword this, instead it uses self
E. No constructor is defined for the class

A. Need to declare the name instance variable

3
New cards

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.

A.
Missing void in constructor definition
B.
Class constructors must be private
C.
The constructor parameters cannot have the same names as the instance variables.
D.
Must use this in constructor when the constructor parameters have the same name as instance variables. ie:
this.name = name;
this.health = health;

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

4
New cards

What is the output running Main.bar();?

public class Main
{
private static int n = 0;
public static void bar()
{
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);
}
}

A. 0
B. 1
C. 2
D. 3

D. 3

5
New cards

Refer to this code snippet.

public class Main
{
private static int n = 0;
public static void bar()
{
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);
}
}

Suppose the following method is added:
public void setN(int newValue)
{
n = newValue;
}
What would be the output of the program if bar() were changed to the following:

public static void bar()
{
Main m1 = new Main();
Main m2 = new Main();
Main m3 = new Main();
m1.setN(5);
m3.foo();
}

A. 0
B. 1
C. 3
D. 5

D. 5

6
New cards

What is the output of the following program?

public class Main
{
private String str = "bar";
public static void main(String[] args)
{
Main m = new Main("foo");
System.out.println(m.getString());
}
public Main(String str)
{
str = str;
}
public String getString()
{
return str;
}
}

A. The program does not compile.
B. The program does not produce any output.
C. foo
D. bar

D. bar

7
New cards

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;
}
}

A. athlete.jersey = 23;
B. athlete.getJersey() = 23;
C. athlete.getJersey(23);
D. athlete.setJersey(23);
E. You cannot set the jersey, since jersey is private and there is no setter method.

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

8
New cards

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;
}
}

A. athlete.jersey
B. athlete.jersey()
C. athlete.getJersey
D. athlete.getJersey()
E. None of the above

D. athlete.getJersey()

9
New cards

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;
}
}

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

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

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

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

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

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

10
New cards

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;
}
}

A. athlete = new Athlete("Dirk", "Nowitzki", 41);
B. Athlete athlete = new Athlete("Dirk", "Nowitzki", 41);
C. Athlete athlete = new Athlete("Dirk" "Nowitzki" 41);
D. Athlete athlete = new Athlete("Dirk", "Nowitzki", "41");

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

11
New cards

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.";
}
}

A.
Foo fee = Foo(32);

B.
Foo fee;
new fee = Foo();

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

D.
Foo fee;
fee = new Foo();

D.
Foo fee;
fee = new Foo();

12
New cards

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;
his.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);

A. I and III
B. I only
C. II only
D. I II and III
E. II and III

A. I and III

13
New cards

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()
{
...
}
}

A. All methods require an instance of Foo in order to be called.
B. No methods require an instance of Foo in order to be called.
C. bar()
D. baz()
E. foo()

E. foo()

14
New cards

Given the following code:

public class TvShow
{
private String name;
private int channel;
public TvShow (String name, int channel)
{
this.name = name;
this.channel = channel;
}
public void getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}


Which of the following explains why this code will not compile?

A. The constructor is missing a return type and it should be set to void.
B. The constructor is missing a return type and it should be set to boolean.
C. The instance variables name and channel should be public.
D. The return type for the getName method should be set to String.
E. The return type for the setName method should be set to String.

D. The return type for the getName method should be set to String.

15
New cards

public class Storm
{
private int lightning Count
private int precipTotal
private String precipType
public Storm(String precipType)
{
this.precipType = precipType
precipTotal = 0
lightning Count = 0
}
public Storm(String precipType, int precipTotal)
{
this.precipType = precipType
this.precipTotal = precipTotal
lightningCount = 0
}
public Storm(String precipType, int lightningCount)
{
this.precipType = precipType
this. lightningCount = lightningCount
precipTotal = 0
}
}


Which statement best describes this code?

A. The code will not compile because there are two constructors with the same signature
B. The code will not compile because the constructors do not have a return type
C. The code will compile, but will not run because there are multiple constructors
D. The code will compile, but will not run because the variables are not initialized
E. The code will compile and work as intended

A. The code will not compile because there are two constructors with the same signature.

16
New cards

Which of the following would be the best example of how the Internet has impacted the economy?

A. Social networking has allowed friends to keep in touch even when they move apart.
B. People rely on email and texting to keep in touch instead of phone calls.
C. Email has reduced the amount of mail that is produced and shipped.
D. Brick and mortar superstores such as Wal-Mart and Target have caused a decline in the smaller family owned stores.

C. Email has reduced the amount of mail that is produced and shipped.

17
New cards

Which of the following is not part of the ACM Code of Ethics and Professional Conduct's General Ethical Principles?

A. Respect privacy.
B. Credit original creator when using other's work.
C. Be honest and trustworthy.
D. Be fair and take action not to discriminate.

B. Credit original creator when using other's work.

18
New cards

Consider the following code segment:

public static String mystery(String word, int i, int j)
{
String mystery = word.substring(i, i+ 1);
mystery += word.substring(i, j);
return mystery;
}

Which of the following is the most appropriate precondition for the variable i in mystery so that substring does not throw an exception?

A. Precondition: i >= 0, i < word.length, i <= j.
B. Precondition: i >= 0, i <= word.length, i <= j.
C. Precondition: i > 0, i <= word.length.
D. Precondition: i >= 0, i < word.length, i > j.

A. Precondition: i >= 0, i < word.length, i <= j.

19
New cards

Consider the following code segment:

public static String mystery(String word, int i, int j)
{
String mystery = word.substring(i, i+ 1);
mystery += word.substring(i, j);
return mystery;
}

Which of the following is the most appropriate precondition for the variable j in mystery so that substring does not throw an exception?

A. Precondition: j < i and j <= word.length.
B. Precondition: j >= i and j <= word.length.
C. Precondition: j >= i and j < word.length.
D. Precondition: j > 0 and j <= word.length.

B. Precondition: j >= i and j <= word.length.

20
New cards

The Insect class will contain a String for the insect's name, an int for the number of legs it has, and a boolean for whether or not the insect has wings.

Which of the following is the most appropriate implementations for the Insect Class?

A.
public class Insect
{
String name;
int numLegs;
boolean hasWings;
//constructor and methods not shown
}

B.
public class Insect
{
private String name;
private int numLegs;
private boolean hasWings;
//constructor and methods not shown
}

C.
private class Insect
{
public String name;
public int numLegs;
public boolean hasWings;
//constructor and methods not shown
}

D.
public class Insect
{
public String name;
private int numLegs;
private boolean hasWings;
//constructor and methods not shown
}

E.
private class Insect
{
String name;
int numLegs;
boolean hasWings;
//constructor and methods not shown
}

B.
public class Insect
{
private String name;
private int numLegs;
private boolean hasWings;
//constructor and methods not shown
}