Podcast
Questions and Answers
To print the last element in the array named ar, you can write:
To print the last element in the array named ar, you can write:
- System.out.println(ar[length()-1])
- System.out.println(ar[ar.length-1]) (correct)
- System.out.println(ar[length-1])
- System.out.println(ar[ar.length])
To print the last element in the array named ar, you can write:
To print the last element in the array named ar, you can write:
- System.out.println(ar[ar.length])
- System.out.println(ar.length)
- System.out.println(ar[length])
- None of these (correct)
A method that has the signature void m(int[] ar):
A method that has the signature void m(int[] ar):
- can only change copies of each of the elements in the array ar
- cannot change the values in the array ar
- can change the values in the array ar (correct)
- None of these
The ____________ relationship occurs when members of one class form a subset of another class.
The ____________ relationship occurs when members of one class form a subset of another class.
Inheritance gives your programs the ability to express _______________ between classes.
Inheritance gives your programs the ability to express _______________ between classes.
Flashcards are hidden until you start studying
Study Notes
Array Access
- Use
System.out.println(ar[ar.length - 1]);
to print the last element of the arrayar
. - Accessing
ar[ar.length]
is out of bounds and will result in an error.
Method Behavior
- Method signature
void m(int[] ar)
can change values in the arrayar
directly since arrays are reference types.
Array Properties
vals.length
for the 2D arrayvals
is4
, indicating the number of arrays it contains.
Data Types
- The data type of
num
inint[] num = new int;
isint
, indicating an integer array.
Constructors
- Constructors share the same name as the class they belong to.
- A class can have any number of constructors, allowing for different ways to instantiate objects.
Accessor Methods
- Accessor instance methods typically begin with "get" for returning values or "is" depending on the return type.
Class Instantiation
- To instantiate a
Rectangle
object, the correct syntax isRectangle myRectangle = new Rectangle(10, 12);
.
Default Constructor
- A valid default constructor for
Clock
ispublic Clock() { setTime(0, 0, 0); }
.
Public Methods
- The method
setTime(int, int, int)
is public and does not return anything.
Valid Method Calls
- The valid set of statements to call methods on a
Rectangle
object is:bigRect.area();
bigRect.perimeter();
bigRect.print();
Class Relationships
- The "is-A" relationship occurs when a subclass is a type of its superclass, indicating a hierarchy.
Inheritance
- Inheritance allows for the expression of relationships between classes, promoting code reuse.
Method Overloading
- When overloading a method in a subclass, you must change the number, type, or order of parameters.
Constructor Requirement
- When defining a subclass without a constructor, its superclass must either have no constructors defined or have a default (no-arg) constructor.
UML Diagrams
- The method
toString()
serves to provide a string representation of the object.
Superclass Constructors
- A subclass constructor with
Student(String name, long id)
will compile if the superclassPerson
has a default or no-arg constructor.
Object Reference Output
- The method used to print an object in the format
pete is Person@addbf1
istoString()
.
ArrayList Requirement
- To use
ArrayList
, import the class from thejava.util
package.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.