Picture ID is REQUIRED for all exams
Use the list of questions below as a
guide when studying for Exam 1.
It is
by no means a comprehensive list of questions. The form of the questions on the
exam may be different from those found here.
You are responsible for all material presented in lecture.
You are also responsible for all associated reading assignments and material covered in lab.
Answering the applicable "self-test" questions in each chapter of the text is also a good way to review.
The answers to the self-test questions are found at the end of each chapter.
The following material from the text was not covered in class and is NOT on the exam.
Any thing not listed here may appear on the exam.
- Chapter 1
- Unicode Character Set
- Chapter 2
- Money Formats Using NumberFormat
- Importing packages and Classes
- The DecimalFormat Class
- Chapter 3
- Algorithms and Pseudocode
- Debugging
- Preventive Coding
- Assertion Checks
- Chapter 4
- The StringTokenizer Class
- Chapter 5
- Packages and Javadoc
- Chapter 6
- Methods with a variable Number of Parameters
- Enumerated Types
For all TRUE/FALSE questions below which are FALSE, explain why.
General Coding
- Write Java code to output the sentence
"I am the Greatest" to the standard output device, one word per line.
- TRUE/FALSE
- Variables can be declared almost anywhere in the code.
- In a
for
loop a variable can be declared at the same time it's initialized
as in for (int i = 0; i < 42; i++)
.
- Java supports the
boolean
data type.
- Describe the Java "garbage collection" process
- Why is method overloading possible in Java?
- What is a method's "signature"?
- Why is a method's return type NOT part of its signature?
- Describe the potential problem when using both
nextInt( )
and
next()
when performing user input. How is this problem addressed?
- You have been asked to write a set of four methods that find the smallest
element in an array of ints, a array of doubles, a array of chars
and a array of strings. You decide to use method overloading.
Write the method headers for these four overloaded methods.
- Explain how the compiler decides which method to call when given
several methods with the same name.
- Define PreConditon, PostCondition as used in method header
comments required by our coding standards.
- What do we mean when we say that parameters and variables declared inside
a method are "local" variables.
- What is the scope of a variable declared in a 'for' loop control statement?
(i.e. for (int i = 0; i < size; i++)
- Given the declaration
String aString = "the quick brown fox jumped over the lazy dog";
write syntactically correct Java code to
- Output the number of characters in aString to the standard output stream.
- Output the string backwards to the standard output stream.
- Instantiate a String object named
anotherString
as a copy
of aString
.
- Write the variable declaration for a array of four (4) Strings named
sentence
in which each string is initialized to "I love Java".
- Draw a picture of memory that results from the code snippet below. Be sure to
differentiate between stack memory and heap memory
Integer [ ] ints = new Integer[ 5 ];
ints[0] = new Integer( 42 );
ints[1] = new Integer( 57 );
ints[2] = new Integer( 25 );
- In Java, arrays are considered to be objects. In what ways is this true?
- Explain why the equality operator, == cannot be used to test the
equality of objects.
- Explain why the assignment operator, =, cannot be used to assign one
object to another.
- True/False - Arrays may be passed to a method.
- True/False - Methods can return arrays.
- What are the primary issues when programming with a partially filled array?
- Write a code snippet to define and populate a 10 x 10 2-D array of ints with the
values 1 - 100.
- Write a code snippet to define and populate a 10 x 10 2-D array of Integers with the
values 1 - 100.
- Define wrapper classes, auto-boxing, auto-unboxing.
- What is/are the purpose(s) of wrapper classes?
Object Oriented Programming
- Define the following terms
class, object, method, reference variable, primitive type,
instance variable, static method, static instance variable,
public method/instance variable, privata method/instance variable,
constructor, copy constructor, method overrloading, class invariant, visibility modifier,
instance, instantiation, class scope, mutator, accessor, method signature,
immutable class/object, shallow copy, deep copy, partially filled array, privacy leak.
- Explain the difference between a class and an object
Use a "real life" entity as an example.
- A constructor is a "special" kind of method. Explain why this is so.
- Explain the OO design technique of "aggregation" (also known as "composition")
Explain how aggregation promotes code reuse.
- Why is it not a violation of the OOP data hiding principle to provide an accessor and mutator
for all instance variables?
- Why is it a violation of the OOP data hiding principle to provide public instance variables?
- Why is returning a reference to a private instance variable considered a violation of OOP principles?
- What issues must be considered when implementing a copy constructor for a user-defined class?
- Define immutable object. How can you tell if an instance of a class will be immutable?
- The following questions refer to the following class definition
class Car
{
public Car( )
{
// code intentionally missing
}
public Car( int cylinders, int people, String color )
{
// code intentionally missing
}
public void start( ) { isMoving = true; }
public void stop( ) { isMoving = false; }
public String getColor ( ) { return color; }
public int getCylinders( ) { return cylinders; }
public int getNrPassengers( ) { return passengers; }
public void setColor (String color) { this.color = color; }
public void setCylinders (int cylinders ) { this.cylinders = cylinders; }
public void setPassengers (int passengers) { this.passengers = passengers; }
private int cylinders;
private int passengers;
private String color;
private bool isMoving;
}
- Write the code to implement Car's constructors
- In general, returning a reference to an instance variable is ill-advised.
Why is doing so acceptable in the
getColor( )
method?
- Write the declarations and code to instantiate a Car object for
a Black, 6-cylinder car that can hold two people.
- Under what circumstances can a user of Car directly access
color
?
- Identify the use of "aggregation" in the definition of Car (if any).
- Explain how the Java features of private instance variables and
public methods help achieve the OO objectives of encapsulation and data hiding.
- Define class invariant. What method(s) of a class have responsibilty
for maintaining the class invariant?
- What is the usual purpose of a private method?
- Given the following class definitions of a Point and Circle in a coordinate plane
class Point
{
public Point( int x, int y) { this.x = x; this.y = y; }
public int getX ( ) { return x; }
public int getY ( ) { return y; }
public void move (int deltaX, int deltaY) { x += deltax; y += deltaY; }
private int x, y;
}
class Circle
{
public Circle (Point p, int radius) { center = p; this.radius = radius; }
public Point getCenter( ) { /* code missing */ }
public int getRadius( ) { return radius; }
public void alter (Point newCenter) { /* code missing */ }
public void alter( int radius } { this.radius = radius; }
private Point center;
private int radius;
}
and the declarations
Point point = new Point(3, 6);
Circle circle1 = new Circle(point, 5);
Circle circle2 = new Circle(new Point (1, 2), 3);
- Write a class invariant for Circle.
- Write the implementation of Circle's
alter( Point newCenter )
method.
- Explain the relationship of Circle's
alter( )
methods in the
context of aggregation
- Write the implementation of Circle's
getCenter( )
method.
- Write one line of code to output the x- and y- coordinates of
circle2
's center.
- Write one line of code to make
circle2
and circle1
have the same center.
- Find the errors in the following statements (if any)
Point p = point;
Point p2 = new Point(3, 5);
Circle c = new Circle( );
System.out.println( circle1.getX( ) );
circle1 = circle2;
- Write the Point's
toString
method that format's the Point as ( x, y )
.
- True/False - Static instance variables of a class are shared among all objects of that class.
- True/False - Static methods of a class can only access static instance variables and
other static methods of that class.
- True/False - Every method of a class can access the static data members of that class.
- True/False - Static data members are declared and initialized outside the class.
- True/False - Static data members may not be final.
Inheritance
- Explain the differences among public, private, and protected member access specifiers.
Be sure to mention their role in inheritance.
- Explain how inheritance promotes code reuse
- Explain the difference between the "is a" and "uses a" relationships.
- Explain how Java implements "is a" relationship
- Explain how Java implements "has a" relationship
- Describe the order in which constructors are called when using inheritance.
- Give a real life example (other than those used in class) for which inheritance would be appropriate
- For the following questions, assume that class D extends class B, that D has some of its own instance
variables, some of its own instance methods, and overrides some of B's instance methods.
- Which of the two classes is the more general class?
- Which of the two classes is the more specific class?
- Can the toSting() method defined in D call the toString( ) method defined in B?
If not, why not. If so, how?
-
- What does the keyword final mean in relation to inheritance.
- What is the Java class Object?
- What impact does the existance of Object have (if any) when implementing classes
- Explain the statement, "Private methods are effectively not inherited"
- What does the keyword super represent? Give one example of its use