Sample Data Structures Questions Chapter 2

advertisement
Sample Data Structures Questions
Chapter 2
Abstract Data Types and Java Classes
Data Structures and Other Objects Using Java (Third Edition)
by Michael Main
ISBN 0-321-37525-4
The Purpose of These Questions
These are typical exam questions from Chapter 2 of the textbook. These exact
questions might not be on your exam, but if you research and find the right answers to
these questions, that should be good preparation for a real exam. (It's also possible
that some of this material was not covered in your class.) At the moment there
are 13 short answer questions and 12 multiple choice questions in this file.
Short Answers
1. Here is part of the Throttle declaration:
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
public class Throttle
{
public Throttle(int size)
...
Short Answers
Section 2.1 - 2.2
Introduction to Classes
public double getFlow()
...
public void shift(int amount)
...
}
Write several lines of Java code to create a Throttle called quiz with 100
positions, activate the method that shifts quiz's flow to the halfway point, and
then print the current flow from quiz.
14. Part of the Throttle implementation from Chapter 2 is shown below. Mark each
method method as follows: Mark C for any constructor; mark M for any
modification method. Mark A for any accessor method.
15.
16.
17.
18.
19.
public class Throttle
{
public Throttle(int size)...
20.
21.
22.
23.
24.
25.
26.
27.
public double getFlow( )...
public boolean isOn( )...
public void shift(int amount)...
public void shutOff( )...
}
28. What is a no-arguments constructor? In what situation does Java automatically
provide a no-arguments constructor? What does the automatic no-arguments
constructor do?
29. How many Throttles are created by this code, and what is the output of the
printlns?
30.
31.
32.
33.
34.
35.
36.
37.
38.
Throttle x;
Throttle y;
x = new Throttle(100);
x.shift(25);
y = x;
y.shift(25);
System.out.println(x.getFlow( ));
System.out.println(y.getFlow( ));
39. Suppose that the Throttle class is implemented in a
Short Answers
Section 2.3
package called edu.colorado.simulations. Write the import
Packages
statment that is needed for a program to use the Throttle
class.
40. Where should you place the .class file for the Throttle class from the previous
question?
41. When is it appropriate to implement a static
Short Answers
Section 2.4
method? Give a small example as part of your
Parameters, Equals, Clones
answer.
42. Here is a method with a parameter named spot, which is a reference to a
Location object:
43.
44.
45.
46.
public static foo(Location spot)
{
spot.shift(2,0);
}
Now, suppose that s is a reference to a Location with s.getX( ) equal to 40.
Then the method foo(s) is activated. What is the value of s.getX( ) after this
activation?
47. Here is a method with a parameter named spot, which is an integer:
48.
49.
50.
51.
public static foo(int spot)
{
spot += 2;
}
Now, suppose that s is an integer with a value of 40. Then the method foo(s) is
activated. What is the value of s after this activation?
52. Consider this code that creates some Location objects with coordinates x=10
and y=20:
53.
54.
55.
56.
57.
Location a, b, c;
a = new Location(10,20);
b = new Location(10,20);
c = b;
...
After this code executes, what are the values of these boolean expressions?
a==b
a.equals(b)
a==c
a.equals(c)
b==c
b.equals(c)
Also, write two clear sentences telling me the difference between == and the
equals method for the Location class.
58. Consider this code that creates some Location objects:
59.
60.
61.
62.
63.
Location a, b, c;
a = new Location(10,20);
b = (Location) a.clone( );
b.shift(3,0);
...
After this code executes, what are the x-coordinates of a and b? Also, write two
clear sentences telling me the difference between = and the clone method for
the Location class.
64. Suppose I implement a class with a clone method. How should I modify the
class head to inform the Java compiler that you plan to implement the clone
method.
65. How does the implementation begin for a typical clone method?
Multiple Choice
1. Suppose I create two Throttles using the class from
Chapter 2:
2.
3.
4.
Throttle mower = new Throttle(...);
Throttle copter = new Throttle(...);
Multiple Choice
Section 2.1 - 2.2
Introduction to Classes
Which statement best describes the instance variable called top for these two
Throttles:
A. mower.top and copter.top will always be two separate instance
variables.
o B. mower.top and copter.top will only be separate if the two throttles are
created with a different number of positions.
o C. mower.top and copter.top will never be two separate instance
variables.
Is it possible for a method of a class to activate another method of the same
class?
o A. No.
o B. Yes, but only public methods.
o C. Yes, but only private methodss.
o D. Yes, both public and private methods can be activated within another
method.
Can two different classes contain methods with the same name?
o A. No.
o B. Yes, but only if the two classes have the same name.
o C. Yes, but only if the main program does not create objects of both
kinds.
o D. Yes, this is always allowed.
What is the common pattern of class definitions that is used in Chapter 2?
o A. Methods and instance variables are both private.
o B. Methods are private, and instance variables are public.
o C. Methods are public, and instance variables are private.
o D. Methods and instance variables are both public.
What is the primary purpose of a constructor?
o A. To allow multiple classes to be used in a single program.
o B. To copy an actual argument to a method's parameter.
o C. To initialize each object as it is declared.
o D. To maintain a count of how many objects of a class have been
created.
o
5.
6.
7.
8.
9. Suppose that a package edu.colorado.foo has two classes
Goo and Hoo. Which statement will import both these
classes?
o A. import edu.colorado.*
o B. import edu.colorado.all
o C. import edu.colorado.Goo-Hoo
o D. import edu.colorado.Goo;Hoo
Multiple Choice
Section 2.3
Packages
o
E. None of the above.
10. Suppose that the Foo class does not have a
Multiple Choice
Section 2.4
clone method. What happens when an
Parameters, Equals, Clones
assignment x=y; is given for two Foo objects?
o A. x is set to refer to a copy of y's object
o B. x is set to refer to the same object that y refers to
o C. Compiler error
o D. Run-time error
11. Suppose that the Foo class has a clone method. What typically happens when
an call x=(Foo) y.clone( ); is given for two Foo objects?
o A. x is set to refer to a copy of y's object
o B. x is set to refer to the same object that y refers to
o C. Compiler error
o D. Run-time error
12. Consider this class definition:
13.
14.
15.
16.
17.
18.
19.
20.
public class Quiz
{
private double score;
public int f( )...
public static int g( )...
}
Where could the assignment score=1.0; appear for the private instance
variable score?
A. Both f and g can carry out the assignment.
o B. f can carry out the assignment, but not g.
o C. g can carry out the assignment, but not f.
o D. Neither f nor g can carry out the assignment.
21. Here is a small method implementation, using the Location type from Chapter
2:
o
22.
23.
24.
25.
26.
27.
public static void f(int i, Location k)
{
i += 1;
k.shift(2, 0);
}
Suppose that a main program has an integer variables a (equal to zero), and a
Location object b (with b.getX( ) equal to zero). Then the main program
calls f(a,b); What are the values of a and b.getX( ) after the method f finishes?
A. Both a and b.getX( ) are still 0.
B. a is now 1, but b.getX( ) is still 0.
o C. a is still 0, but b.getX( ) is now 2.
o D. a is now 1, and b.getX( ) is now 2.
28. Suppose that the Foo class does not have an equals method. What happens
when an expression x==y; is evaluated for two Foo objects?
o A. The expression is true if x and y refer to the same object
o B. The expression is true if x and y refer to objects with the same values
o C. Compiler error
o D. Run-time error
29. Suppose that the Foo class has a typical equals method. What happens when an
expression x.equals(y); is evaluated for two Foo objects?
o A. The expression is true if x and y refer to the same object
o B. The expression is true if x and y refer to objects with the same values
o C. Compiler error
o D. Run-time error
o
o
Michael Main (main@colorado.edu)
Download