5.11 - Writing Classes Quiz
13 Questions
100 Views

5.11 - Writing Classes Quiz

Created by
@SkilledAzalea

Questions and Answers

What will this code print: Person myPerson = new Person("Bob"); myPerson.changeName("Joe"); myPerson.name = "John"; myPerson.printName();?

John

What is missing in the Athlete class code snippet?

Need to declare the name instance variable

What is wrong with the Pokemon class definition?

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

What will the static method bar() output after creating three Main objects?

<p>3</p> Signup and view all the answers

What would be the output of the program if bar() is changed to m3.foo(); after m1.setN(5);?

<p>5</p> Signup and view all the answers

What will the getString() method return in the given Main class?

<p>bar</p> Signup and view all the answers

Why can't you set the jersey in the Athlete class?

<p>You cannot set the jersey, since jersey is private and there is no setter method.</p> Signup and view all the answers

How do you obtain the jersey number of an Athlete object?

<p>athlete.getJersey()</p> Signup and view all the answers

What method would you implement to allow setting a jersey number in the Athlete class?

<p>public void setJersey(int jersey) { this.jersey = jersey; }</p> Signup and view all the answers

What is a valid instantiation of the Athlete class?

<p>Athlete athlete = new Athlete(&quot;Dirk&quot;, &quot;Nowitzki&quot;, 41);</p> Signup and view all the answers

What is the output when instantiating Foo like this: Foo fee; fee = new Foo();

<p>Foo fee; fee = new Foo();</p> Signup and view all the answers

What is the return type for the getName method in the TvShow class?

<p>String</p> Signup and view all the answers

Which methods are available in the Foo class?

<p>foo()</p> Signup and view all the answers

Study Notes

Person Class

  • Code initializes a Person object with a name.
  • The changeName method updates the person's name.
  • The final output after multiple name changes is "John".

Athlete Class Initialization

  • An instance variable for name must be declared in the Athlete class for proper functionality.

Pokemon Class Constructor

  • Constructors must use this keyword when parameter names match instance variable names to avoid shadowing.

Counter in Main Class

  • Each time a Main object is instantiated, a counter n increments.
  • The output of the method foo() after creating three Main instances is 3.

Modified Output in Main Class

  • If bar() method sets n to 5 and calls foo() on instance m3, the output is 5.

Main Class String Handling

  • The constructor’s parameter does not change the instance variable str due to lack of this keyword.
  • The output remains "bar" when calling getString().

Athlete Class Properties

  • The jersey variable is private, preventing direct modification without a setter method.
  • getJersey() method can access the private jersey variable.

Jersey Getter in Athlete Class

  • To retrieve the value of jersey instance variable, call athlete.getJersey().

Setting Jersey in Athlete Class

  • A setter method like public void setJersey(int jersey) allows control over the private jersey variable.

Athlete Object Instantiation

  • To create an Athlete object, use the syntax: athlete = new Athlete("Dirk", "Nowitzki", 41);.

Foo Class Constructors

  • Foo class has overloaded constructors initializing bar and stoo attributes.
  • Example of instantiation: Foo fee = new Foo(); initializes default values.

Valid Instantiations of Athlete Class

  • Valid instances of Athlete should include both first and last names, with the last parameter being an integer jersey number.
  • Only I and III are valid instantiations based on specified constructors.

Static and Instance Methods in Foo Class

  • Static methods in Java, like foo(), can be called without creating an instance of the class.
  • Non-static methods (bar() and baz()) require an object of the class.

TvShow Class Method Return Type

  • The getName() method lacks a return type declaration; it should return a String.

Storm Class Overloaded Constructors

  • The Storm class features overloaded constructors to initialize with different parameters: type only, type with total precipitation, and type with lightning count, ensuring flexibility in object creation.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

Test your understanding of writing classes in Java with this quiz. Analyze code snippets and predict outcomes based on object-oriented programming principles. Perfect for beginners looking to solidify their knowledge of class functionalities.

Use Quizgecko on...
Browser
Browser