Back to marketplace

Module 05: Writing Classes

No description yet. This curated deck is ready for daily study.

Total cards: 24•Content version: v4•Public deck•Updated: 8/1/2026

Card preview (24)

#1
Front (Question)

what will this code print? Person myPerson = new Person(“Bob”); myPerson.changeName(“Joe”); myPerson.name = “John”; myPerson.printName();

Card front image
Back (Answer)

John

#2
Front (Question)

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

Back (Answer)

• Need to declare the name instance variable

#3
Front (Question)

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;

Card front image
Back (Answer)

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

#4
Front (Question)

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

Card front image
Back (Answer)

3 The instance variable n Is declared static, so it is shared by all instances of class Main. Since n is incremented by each constructor, the value of n after three instantiations is 3. It does not matter on which instance foo() is called.

#5
Front (Question)

Refer to this code snippet.

Card front image
Back (Answer)

5

#6
Front (Question)

What is the output of the following program? • The program does not compile. • The program does not produce any output. • foo • bar

Card front image
Back (Answer)

• bar In Main(String str), the parameter name string shadows the instance variable name string. The statement string = string assigns the parameter string to itself, and so the assignment has no effect outside of Main(String str). Therefore, getString() will return the static variable bar.

#7
Front (Question)

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? • 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.

Card front image
Back (Answer)

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

#8
Front (Question)

Given an instance of the Athlete class called athlete, what is the proper way to get the value of the jersey number? • athlete.jersey • athlete.jersey() • athlete.getJersey • athlete.getJersey() • None of the above

Card front image
Back (Answer)

• athlete.getJersey()

#9
Front (Question)

Mark the valid way to create an instance of Athlete given the following Athlete class definition: • 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”);

Card front image
Back (Answer)

• Athlete athlete = new Athlete(“Dirk”, “Nowitzki”, 41);

#10
Front (Question)

Mark the valid way to create an instance of Foo given the following code: • Foo fee = Foo(32); • Foo fee; new fee = Foo(); • Foo fee = new Foo(10, “You.”) • Foo fee; fee = new Foo();

Card front image
Back (Answer)

4. Foo fee; ​fee = new Foo();

#11
Front (Question)

Given the following definition for the class Athlete: 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

Card front image
Back (Answer)

I and III

#12
Front (Question)

Which methods of class Foo can be called without an actual instance of the class Foo? • 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()

Card front image
Back (Answer)

• foo() foo() is declared static, so it can be invoked on the class, without an instance.

#13
Front (Question)

Which of the following explains why this code will not compile? • The constructor is missing a return type and it should be set to void. • The constructor is missing a return type and it should be set to boolean. • The instance variables name and channel should be public. • The return type for the getName method should be set to String. • The return type for the setName method should be set to String.

Card front image
Back (Answer)

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

#14
Front (Question)

Which statement best describes this code? • The code will not compile because there are two constructors with the same signature. • The code will not compile because the constructors do not have a return type. • The code will compile, but will not run because there are multiple constructors. • The code will compile, but will not run because the variables are not initialized. • The code will compile and work as intended.

Card front image
Back (Answer)

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

#15
Front (Question)

Which of the following would be the best example of how the Internet has impacted the economy? • Social networking has allowed friends to keep in touch even when they move apart. • People rely on email and texting to keep in touch instead of phone calls. • Email has reduced the amount of mail that is produced and shipped. • Superstores such as Wal-Mart and Target have caused a decline in the smaller family owned stores.

Back (Answer)

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

#16
Front (Question)

Which of the following is not part of the ACM Code of Ethics and Professional Conduct’s General Ethical Principles? • Respect privacy. • Credit original creator when using other’s work. • Be honest and trustworthy. • Be fair and take action not to discriminate.

Back (Answer)

• Credit original creator when using other’s work

#17
Front (Question)

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? • Precondition: i >= 0, i < word.length, i <= j. • Precondition: i >= 0, i <= word.length, i <= j. • Precondition: i > 0, i < word.length, i < j. • Precondition: i >= 0, i < word.length, i > j. • Precondition: i > 0, i <= word.length.

Back (Answer)

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

#18
Front (Question)

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? • Precondition: j > i and j <= word.length. • Precondition: j >= i and j <= word.length. • Precondition: j >= i and j < word.length. • Precondition: j > 0 and j <= word.length. • Precondition: j < i and j <= word.length.

Back (Answer)

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

#19
Front (Question)

What is the difference between a public and private method/variable?

Back (Answer)

Public: allow access to data and methods from classes outside the declaring class

#20
Front (Question)

What is Encapsulation?

Back (Answer)

The process of hiding the implementation details of a class from the user

#21
Front (Question)

What are accessor methods?

Back (Answer)

Methods used to access instance variables and object data. Also referred to as getter methods

#22
Front (Question)

What are mutator methods?

Back (Answer)

Methods used to change or manipulate instance variables or object data. Also referred to as setter methods.

#23
Front (Question)

What are access specifiers?

Back (Answer)

Access Specifiers: Determine whether classes, data, constructors, and methods can be accessed outside of the declaring class Public: allows access from classes outside the class Private: restricts access to the declaring class • Accessors and Mutators: Allow private data to be accessed outside of the class and be safely modified.

Card back image
#24
Front (Question)

What is public access?

Back (Answer)

public Access: • Designed as public so that they can be accessed outside of the class file • Class members should be public: • Constructors • Accessors/Mutators • Methods that user needs to manipulate the object of the class public class Rectangle { public Rectangle() }