5.11 - Writing Classes Quiz

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

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

John

2
New cards

Given this code snippet,

public class Athlete {

public Athlete(String name) {

this.name = name;

}

}

Need to declare the name instance variable

3
New cards

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.

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

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

}

}

3

5
New cards

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

}

5

6
New cards

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;

}

}

bar

7
New cards

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;

}

}

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

8
New cards

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.getJersey()

9
New cards

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 void setJersey(int jersey) {

this.jersey = jersey;

}

10
New cards

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 athlete = new Athlete("Dirk", "Nowitzki", 41);

11
New cards

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;

fee = new Foo();

12
New cards

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

13
New cards

public class Foo {

public static void foo()

{

...

}

public void bar()

{

...

}

public void baz()

{

...

}

}

foo()

14
New cards

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;

}

}

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

15
New cards

public class Storm {

private int lighteningCount;

private int precipTotal;

private String precipType;

public Storm(String precipType) {

this.precipType = precipType;

precipTotal = 0;

lighteningCount = 0;

}

public Storm(String precipType, int precipTotal) {

this.precipType = precipType;

this.precipTotal = precipTotal;

lighteningCount = 0;

}

public Storm(String precipType, int lighteningCount) {

this.precipType = precipType;

this.lighteningCount = lighteningCount;

precipTotal = 0;

}

}

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?

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?

Credit original creator when using other's work.

18
New cards

public static String mystery(String word, int i, int j) {

String mystery = word.substring(i, i+ 1);

mystery += word.substring(i, j);

return mystery;

}

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

19
New cards

public static String mystery(String word, int i, int j) {

String mystery = word.substring(i, i+ 1);

mystery += word.substring(i, j);

return mystery;

}

Precondition: j >= i and j

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?

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