Questions and Answers
The behaviour of an object is defined by the object's...
methods
The relationship between a class and an object is best described as...
objects are instances of class
Define a class that will represent a car.
public class Car
What reserved words in Java is used to create an instance of a class?
Signup and view all the answers
In order to preserve encapsulation of an object, we would NOT do what?
Signup and view all the answers
If a method does not have a return statement, then...
Signup and view all the answers
A class' constructor usually defines...
Signup and view all the answers
Having multiple class methods of the same name where each method has a different number of or type of parameters is known as...
Signup and view all the answers
Instance data for a Java class...
Signup and view all the answers
An example of passing a message to a String where the message has a String parameter occurs in what?
Signup and view all the answers
What could be used to instantiate a new Student s1?
Signup and view all the answers
A method that will compute and return the student's class rank is defined as...
Signup and view all the answers
A method that updates the Student's number of credit hours by receiving a number of credit hours and adds these to the Student's current hours is defined as...
Signup and view all the answers
"Die d = new Die(10);" results in...
Signup and view all the answers
"Due d = new Die(10, 0);" results in...
Signup and view all the answers
If "Swapper s = new Swapper(0, 'hello', 0);" is followed by "s.toString();", what value is returned from "s.toString()?"
Signup and view all the answers
Which of the following criticisms is valid about the Swapper class?
Signup and view all the answers
If "Swapper r = new Swapper(5, 'no', 10);", then "r.swap()" returns what?
Signup and view all the answers
Consider a method defined with the header: "public void foo(int a, int b)". Which of the following method calls is legal?
Signup and view all the answers
What is a mutator method?
Signup and view all the answers
Java methods can return only primitive types.
Signup and view all the answers
Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
Signup and view all the answers
All Java classes must contain a main method which is the first method executed when the Java class is called upon.
Signup and view all the answers
Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo( ) {...}
Signup and view all the answers
The following method header definition will result in a syntax error: public void aMethod( );
Signup and view all the answers
A method defined in a class can access the class' instance data without needing to pass them as parameters or declare them as local variables.
Signup and view all the answers
The interface of a class is based on those data instances and methods that are declared public.
Signup and view all the answers
Defining formal parameters requires including each parameter's type.
Signup and view all the answers
Every class definition must include a constructor.
Signup and view all the answers
While multiple objects of the same class can exist, there is only one version of the class.
Signup and view all the answers
Study Notes
Object Behavior and Classes
- An object's behavior is determined by its methods.
- Classes serve as blueprints for creating objects, with each object being an instance of a class.
- A typical class definition in Java is formatted as
public class ClassName
.
Object Instantiation and Encapsulation
- The
new
keyword is essential for creating instances of a class. - Encapsulation can be compromised if a class is declared as final, restricting further subclassing.
Method Characteristics
- A method lacking a return statement is classified as a void method.
- Constructors are specialized methods that dictate how objects are initialized.
- Method overloading allows multiple methods with the same name but differing parameter lists.
Instance Data and Method Usage
- Instance data can either be primitive data types or reference other objects.
- A common method for comparing strings is
equals
, which accepts a String parameter. - Objects can be instantiated, e.g.,
Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);
.
Student Class Methods
- Methods like
s1.getClassRank()
are designed to compute and return values based on object data. - Mutator methods, such as
public void updateHours(int moreHours)
, modify instance variables.
Constructors and Syntax
-
Die d = new Die(10);
sets up an object withnumFaces = 10
and a defaultfaceValue = 1
. - Incorrect constructor calls, such as
Due d = new Die(10, 0);
, will lead to syntax errors.
Swapper Class Behavior
- Invoking
s.toString()
onSwapper s = new Swapper(0, "hello", 0);
returns the string "00". - A notable issue with the Swapper class is that its instance data
z
is publicly accessible, which can violate encapsulation. - Calling
r.swap()
onSwapper r = new Swapper(5, "no", 10);
returns "no".
Method Call and Parameter Rules
- Legal method calls include expressions like
foo(0/1, 2*3);
. - Mutator methods are specifically designed to alter values within a class.
True/False Statements Overview
- Java methods are not limited to returning primitive types (False).
- Formal parameters are defined in the method header, while actual parameters appear in method calls (False).
- Not all Java classes require a main method (False).
- The term
continue
is misapplied in discussions about returning multiple values from a method (False). - A method header defining
public void aMethod();
will not cause a syntax error (True). - Methods can access instance data directly without requiring them as parameters (True).
- Public instances and methods constitute the public interface of a class (True).
- Each formal parameter must declare its type in method definitions (True).
- Having a constructor is not mandatory for class definitions (False).
- Multiple objects of the same class can exist while only one class definition is maintained (True).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers key concepts from Chapter 4 of Java Programming, focusing on object behavior, class-object relationships, and class definitions. Test your understanding of essential terminology and reserved words used in Java.