Computer Science Ch.5 Exercises

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

Computer Science

Ch. 5 Homework
Jeremiah Simpkins
Exercise 5.1
1. What is the difference between a class and an object? An object is defined by its class, and as
such depends upon a class for its creation.
2. What happens to an object's memory storage when it is no longer referenced by a variable?
When it is not in use, it is purged from memory.
3. List the three important characteristics of an object. Behavior, state and identity.
4. Describe the client-server relationship. The client is limited to sending messages to the server
and has no knowledge of how the server works. The server answers the client's request by
returning data or another message.
5. What is the interface of a class? The list of methods that can be utilized by the class.
Exercise 5.2
1. What are mutators and accessors? Give examples. Mutators change an object's state while
accessors access the object's state. Mutator s1.setName (Bill); Accessor str =
s1.getName()
2. List two visibility modifiers and describe when they are used. The clauses private and public.
Public is used to allow any object or class to access another, while private is used to prevent
other classes/objects from accessing information directly.
3. What is a constructor method? A constructor initializes the instance variables of a newly
instantiated object.
4. Why do we include a toString method with a new user-defined class? It gives an object's string
representation to the object by sending it as a toString message.
5. How can two variables refer to the same object? Give an example. By setting one variable
equal to another after the first has been associated with the same object, both can refer to the
same object. S1 = new Student();
s2 = s1;
6. Explain the difference between a primitive type and a reference type and give an example of
each. Primitive types are those such as ints, doubles, booleans, chars and shorter and longer
versions of those. Reference types are all classes. Primitive = myPI, radius, yourPI Reference
= Student, HerPie, Car
7. What is the null value? When a reference variable previously pointed to another object, but
which has no other variables associated with it, java reclaims the object's memory during
garbage collection and thus the value means that no object could be accessed.
8. What is a null pointer exception? Give an example. An error that occurs when a program
attempts to run a method with an object that is null. String str = null;
System.out.println(str.length());
9. How does a default constructor differ from other constructors? A constructor has a specified
parameter list, while a default constructor has an empty parameter list.
10. How does Java handle the initialization of instance variables if no constructors are provided?
The JVM provides a primitive default constructor to initialize numeric variables to zero and
object variables to null.
Exercise 5.4
1. Explain the difference between formal parameters and actual parameters. Formal parameters
are those that are listed in a method's definition, while actual parameters are values that are
invoked when a method is invoked.
2. How does Java transmit data by means of parameters? Information is passed to methods by

using correct and matching parameters.


3. Define a method sum. This method expects two integers as parameters and returns the sum of
the numbers ranging from the first integer to the second one. Public void sum (int1, int2)
intSum, intSum = int1 + int2, return intSum;
4. What is the purpose of local variables? Local variables serve as temporary working storage for
within a method.
Example 5.5
1. What are the lifetimes of an instance variable, a local variable, and a parameter? They only
exist during a single execution of a method.
2. What is shadowing? Give an example and describe the bad things that shadowing might cause
to happen in a program. Shadowing assigns a value of a parameter to a global variable. It
increases the chances for coding error. This.iAmAVariable = iAmAVariable;
3. Consider the following code segment:
A. Instance variables: ints a and b. Parameters: int x and y. Local variables: int c and d.
B. a and b are global variables, x and y are parameters that are referenced from another
class/object, c and d are local variables that are used just temporarily within the class.
C. Describe the lifetime of each variable or parameter. Parameters and local variables such as
x, y and c and d respectively are no longer accessible after the method stops executing. Instance
variables last for the lifetime of an object.

You might also like