Exam 2019, questions and answers
Computer Programming (CSC 104)
1 of 1
Chapter 1
34 questions
11 fill in the blank
8 true/false
15 multiple choice
Fill in the Blank Questions
1. The __________ is the part of a
computer that follows
instructions.
[processor]
2. A(n) __________ is a program that
translates programs to a simpler
language that the computer
can execute.
[compiler]
3. The Java interpreter translates a
Java program from __________ to
machine language.
[byte code]
4. A(n) __________ is an action that a
Java object can perform.
[method]
5. Another term for __________ is
information hiding.
[encapsulation]
6. A(n) __________ is a set of
instructions that tells the basic
strategery for solving a problem.
[algorithm]
7. A(n) __________ is a program
meant to be sent from one
computer to another across the
world-
wide web.
[applet]
8. A(n) __________ is a piece of
information passed to a method.
[argument]
9. A(n) __________ is a digit that can
only have the value 1 or 0.
[bit]
10. A(n) __________ is part of a
program that has data associated
with it and can perform certain
actions.
[object]
Optional Graphics Supplement
Questions:
11. The __________ method of an
applet is used to draw graphics
and is invoked automatically when
the applet runs.
[paint]
True/False Questions
1. Main memory refers to the
amount of storage space available
on a computer’s disk drive.
TRUE FALSE
[FALSE]
2. Java is a high-level language.
TRUE FALSE
2 of 2
[TRUE]
3. The Java compiler translates
Java programs into machine
language programs.
TRUE FALSE
[FALSE]
4. Pseudocode is a mixture of
English and a programming
language.
TRUE FALSE
[TRUE]
5. The computer will not display
an error message when a logic
error occurs.
TRUE FALSE
[TRUE]
6. System.out is an example of a
method.
TRUE FALSE
[FALSE]
7. In Java, the double equal sign
means "make equal to".
TRUE FALSE
[FALSE]
8. Polymorphism is a classification
system for organizing classes in
Java.
TRUE FALSE
[FALSE]
Multiple-Choice Questions
1. A bit can have __________ different
values.
A. 2 B. 10 C. 100 D. 256
[A]
2. Bytes in auxiliary memory are
grouped into __________.
A. bits B. files C. directories D.
programs
[B]
3. The kind of language that is
easiest for people to use is
A. assembly language B. machine
language
C. low-level language D. high-level
language
[D]
4. Input to a compiler is called
A. a byte-code file B. source code
C. an executable file D. object code
[B]
5. The term __________ refers to a
way of organizing classes that
share properties.
A. object-oriented B.
encapsulation C. polymorphism D.
inheritance
[D]
6. __________ means that two or
more methods can have different
names in the same way that an
English word can have two or
more meanings.
A. object-oriented B.
encapsulation C. polymorphism D.
inheritance
[C]
3 of 3
7. A(n) __________ occurs when a
program has a grammatical
mistake like missing punctuation.
A. syntax error B. run-time error C.
logic error D. hidden error
[A]
8. If the computer displays an
error message while executing
your program, the program
contains
a(n) __________.
A. syntax error B. run-time error C.
logic error D. hidden error
[B]
9. Invoking a method means
A. checking the method for errors
B. designing the method
C. asking the method to do its job
D. revising the method
[C]
10. In Java, a library of classes is
called
A. a folder B. a package C. a
directory D. an application
[B]
11. The __________ is a program that
supervises operation of the entire
computer.
A. operating system B. compiler C.
interpreter D. virtual machine
[A]
12. A(n) __________ is a unit of
memory that contains eight binary
digits.
A. bit B. byte C. object D. class
[B]
13. Which of these is not a high-
level language?
A. Java B. BASIC C. C++ D.
assembly language
[D]
Optional Graphics Supplement
Questions:
14. The point (0, 0) is in the
__________ of the screen in the Java
coordinate system.
A. upper left-hand corner B. upper
right-hand corner
C. lower left-hand corner D. lower
right-hand corner
E. center
[A]
15. Which of the following is
information passed by
parameter(s) to fillOval?
A. the fill color
B. the line color
C. the width of the bounding
rectangle
D. the coordinates of the lower-
right corner of the bounding
rectangle
[C]
Chapter 2
39 questions
15 fill in the blank
13 true/false
13 multiple choice
Fill in the Blank Questions
8 of 8
[FALSE]
2. What is the value of the
expression (x > 3) || (y < 3) if x has
the value 1 and y has the value 5?
true false
[false]
3. The == operator can be used to
see if two strings have the same
characters.
TRUE FALSE
[FALSE]
4. A multibranch if-else statement
is one kind of nested if-else
statement.
TRUE FALSE
[TRUE]
5. The controlling expression of a
switch statement must have a
value that is an integer or a
character.
TRUE FALSE
[TRUE]
6. The body of any while loop will
be executed at least once.
TRUE FALSE
[FALSE]
7. for loops and while loops use
the same stopping mechanism.
TRUE FALSE
[FALSE]
8. Java uses complete evaluation
for Boolean expressions that use &
instead of && and | instead of
||.
TRUE FALSE
[TRUE]
9. In ASCII, a capital 'Z' comes
before a small 'a'.
TRUE FALSE
[TRUE]
Optional Graphics Supplement
Questions:
10. The drawstring method takes
three parameters: a string and two
numbers.
TRUE FALSE
[TRUE]
Multiple-Choice Questions
1. What will be the value of x after
the following section of code
executes:
int x = 5;
if (x > 3)
x = x – 2;
else
x = x + 2;
A. 1 B. 3 C. 5 D. 7
9 of 9
[B]
2. What will be the value of y after
the following section of code
executes:
int y = 4, z = 3;
if (y > 4)
{
z = 2;
y = y – 1;
}
else
z = 1;
y = z;
A. 1 B. 2 C. 3 D. 4
[A]
3. What will be the value of w after
the following section of code
executes:
int w = 4, q = 3;
if (q > 5)
if (w == 7)
w = 3;
else
w = 2;
else
if (w > 3)
w = 1;
else
w = 0;
A. 0 B. 1 C. 2 D. 3
[B]
4. What will be the value of b after
the following section of code
executes:
int a = 4, b = 0;
if (a < 5)
b = 4;
else if (a < 10)
b = 3;
else if (a > 5)
b = 2;
else
b = 1;
A. 1 B. 2 C. 3 D. 4
[D]
5. What will the following code
print?
10 of 10
int color = 3;
switch (color)
{
case 1:
case 2:
System.out.print("red ");
case 3:
System.out.print("blue");
break;
case 4:
System.out.print("purple");
break;
default:
System.out.print("gray");
}
A. red B. blue C. purple
D. gray E. red blue
[B]
6. What will the following code
print?
int count = 1;
while (count <= 3)
{
System.out.print(count + " ");
count++;
}
A. 1 2 3 B. 1 2 C. 3 D. 1 1 1
[A]
7. What will the following code
print?
int count;
for (count = 4; count > 1; count--)
System.out.print(count + " ");
A. 1 2 3 4 B. 1 2 3 C. 4 3 2 D. 4 3 2 1
[C]
8. A(n) __________ is one kind of
branching statement.
A. compound statement B. for
statement
C. break statement D. switch
statement
[D]
9. A(n) __________ goes in the
parentheses following the word if
in an if-else statement.
A. parameter list B. compound
statement
C. nested if statement D. boolean
expression
[D]
11 of 11
10. What happens when an
expression uses == to compare
two string variables?
A. The value of the expression will
be true if both strings have the
same characters in them.
B. The value of the expression will
be true if both string variables
refer to the same object.
C. The expression will not be
correct syntax and the program
will not compile.
D. A run-time error will occur.
[B]
11. What is the correct way to end
a program’s execution normally?
A. exit(); B. exit(0);
C. System.exit(); D. System.exit(0);
[D]
Optional Graphics Supplement
Questions:
12. How do you set the color of an
oval?
A. By setting the drawColor field of
the Graphics class.
B. By calling the setColor method
of the Graphics class.
C. By calling the set method of the
Color class.
D. By passing a Color object as a
parameter to the drawOval
method.
[B]
Chapter 4
34 questions
13 fill in the blank
12 true/false
9 multiple choice
Fill in the Blank Questions
1. A(n) __________ is an action that
an object can take and is specified
in the class definition.
[method]
2. Compiling a file called
Game.java will produce a file
called __________.
[Game.class]
3. A(n) __________ is a data item that
belongs to an object.
[instance variable]
4. __________ is the word used to say
that there are no restrictions on
the use of a method or data
item.
[public]
5. The reserved word __________ can
be used as a name for the calling
object.
[this]
6. Names of mutator methods
usually begin with __________.
[set]
7. The interface of a class consists
of comments, __________, and
defined constants.
[headings of public methods]
8. To create an object of a class
type, the word __________ is used.
[new]
12 of 12
9. One kind of method returns a
value. The other kind of method is
called a(n) __________.
[void-method]
10. A __________ is the definition of a
kind of object.
[class]
11. The __________ of a method
definition is enclosed between
braces.
[body]
12. A variable declared inside a
method is called a __________
variable.
[local]
Optional Graphics Supplement
Questions:
13. The __________ method of an
applet is called automatically
when the applet is starting up.
[init]
True/False Questions
1. All objects of the same class
have the same data values.
TRUE FALSE
[FALSE]
2. When a method call is executed,
values from the method definition
are substituted for the
arguments in the method call.
TRUE FALSE
[FALSE]
3. Parameters of a primitive type
are passed to methods using the
call-by-value mechanism.
TRUE FALSE
[TRUE]
4. Encapsulation is the process of
defining a class that has no public
methods.
TRUE FALSE
[FALSE]
5. Private methods are part of the
interface of a class.
TRUE FALSE
[FALSE]
6. A variable of class type does not
store an object of that class.
TRUE FALSE
[TRUE]
7. An equals method should return
a boolean value.
TRUE FALSE
[TRUE]
8. A method can change the value
of a variable of a primitive type
that is an argument to the
method.
TRUE FALSE
[FALSE]
13 of 13
9. A method can change the value
of instance variables of an
argument that is of a class type.
TRUE FALSE
[TRUE]
10. A Java method that does not
return a value can have any
number of return statements.
TRUE FALSE
[TRUE]
Optional Graphics Supplement
Questions:
11. drawString is a method of the
Graphics class.
TRUE FALSE
[FALSE]
12. An applet can be successfully
compiled even if it has no init
method and no paint method.
TRUE FALSE
[TRUE]
Multiple-Choice Questions
1. A method is invoked by
A. writing the name of the method
followed by a colon and the name
of the calling object
B. writing the name of the calling
object followed by a dot and a list
of values in parentheses
C. listing the name of the calling
object and the name of the method
inside parentheses
D. writing the name of the calling
object, followed by a dot, followed
by the name of the method
and a list of values in parentheses
[D]
2. What can you tell about the
method surpriseMe in the code
below?
int something;
something = surpriseMe();
A. It has no parameters but has a
return value.
B. It has one parameter but no
return value.
C. It has no parameters and it has
no return value.
D. It has one return value and an
unknown number of parameters.
E. It has one parameter and an
unknown number of return
values.
[A]
3. A data item that can only be
used within a method is called
A. a local variable B. an instance
variable
C. a global variable D. a private
variable
[A]
4. Normally all instance variables
are given the qualifier
A. public B. protected C. private D.
static
[C]
5. One benefit of encapsulation is
A. the implementation of the class
will be smaller
B. the implementation can be
changed without changing
programs that use the class
C. the interface of the class will be
smaller
D. the interface can be changed
without changing programs that
use the class
[B]
14 of 14
6. When comparing two objects to
see if they have the same contents,
one should
A. use the = operator
B. use the == operator
C. define an equals method in the
class and use it
D. use the equals method to see if
they have the same variable name
[C]
7. A __________ method does not
return a value.
A. null B. static C. void D. recursive
[C]
Optional Graphics Supplement
Questions:
8. Where does the Graphics object
of an applet come from?
A. It is usually created in the init
method and stored as an instance
variable of the applet.
B. It is inherited from the JApplet
class.
C. It is a local variable of the
applet’s init method.
D. It is created automatically and
passed in to the applet’s paint
method as a parameter.
[D]
9. Which of the following is a
method of the Graphics class?
A. drawRectangle B. circleFill C.
drawOval D. paint
[C]
Chapter 5
28 questions
10 fill in the blank
8 true/false
10 multiple choice
Fill in the Blank Questions
1. A __________ method is a method
that can be invoked using the class
name instead of an object
name.
[static]
2. The method used to convert
from Character to char is __________.
[charValue]
3. The method used to convert
from String to Float is __________.
[parseFloat]
4. A(n) __________ is a simplified
version of a method that is easily
verified and is used for testing.
[stub]
5. In the technique of __________
design, the problem of designing a
method is divided into
subproblems, which are then
solved using the same technique.
[top-down]
6. A(n) __________ is a special kind of
method used to initialize objects.
[constructor]
7. A(n) __________ is a collection of
related classes that serves as a
class library.
15 of 15
[package]
8. The __________ class provides the
methods pow and round, among
others.
[Math]
9. Integer and Double are __________
classes.
[wrapper]
10. A constructor with no
parameters is called a __________
constructor.
[default]
True/False Questions
1. The object and dot can be
omitted in a non-static method call
if the calling object is the this
parameter.
TRUE FALSE
[TRUE]
2. Instance variables cannot be
referenced in the definition of any
static method unless an object
(other than this) and dot precede
the variable name.
TRUE FALSE
[TRUE]
3. Java will use automatic type
conversion before it will use
overloading.
TRUE FALSE
[FALSE]
4. It is possible to have two
methods in the same class that
have the same name, the same
number and types of arguments,
but different return types.
TRUE FALSE
[FALSE]
5. Constructors have return type
void.
TRUE FALSE
[FALSE]
6. A class may have more than one
constructor.
TRUE FALSE
[TRUE]
7. If you add at least one
constructor to a class, no
constructors will be automatically
created for
the class.
TRUE FALSE
[TRUE]
Optional Graphics Supplement
Questions:
8. It is possible to make a
component in an applet invisible
after the applet has started
executing.
TRUE FALSE
[TRUE]
Multiple-Choice Questions
1. The method __________ in the
Math class returns the nearest
whole number that is equal to or
16 of 16
less than its argument.
A. abs B. min C. round D. floor
[D]
2. Which of these words is the
name of a wrapper class?
A. int B. float C. Double D. String
[C]
3. The term overloaded refers to
A. an error where more than one
method matches a method call
B. two or more methods in the
same class that have the same
name
C. method calls where there are
two many actual parameters
D. two or more methods in
different classes that have the
same name
[B]
4. Java will automatically define a
constructor
A. if the programmer does not
define a default constructor for a
class
B. if the programmer does not
define any constructors for a class
C. if the program refers to a
constructor with no parameters
D. for every class defined in a
program
[B]
5. A private instance variable
A. can never be modified by a
method outside the class
B. can be modified by a method
outside the class if it is returned
by a method
C. can be modified by a method
outside the class if it is a class
type, using a mutator method
(assuming one exits)
D. can be modified by using an
object name followed by a dot and
then the name of the
instance variable
[C]
6. The name of a constructor
A. can be any legal identifier
B. must include the name of the
package
C. will always be the word new
D. will always be the same as the
name of the class
[D]
7. __________ is a special constant
that is not an object but can be
used to give a value to any
variable of class type.
A. void B. this C. null D. false
[C]
8. A(n) __________ allows you to
have a class object that
corresponds to a value of a
primitive type.
A. wrapper class B. type cast C.
constructor D. static variable
[A]
Optional Graphics Supplement
Questions:
9. When the user clicks a button,
the event will be handled by an
object of type __________.
A. ActionListener B. EventHandler
C. ButtonListener D.
ActionHandler
[A]
17 of 17
10. In Java, an icon is __________ that
can be added to a component.
A. a menu B. a button C. a sound D.
a picture
[D]
Chapter 6
28 questions
8 fill in the blank
11 true/false
9 multiple choice
Fill in the Blank Questions
1. The type of elements in an array
is called the __________.
[base type]
2. Every array object has an
instance variable called __________
that tells how many elements are
in
the array.
[length]
3. An array can be initialized by
enclosing a list of values in
__________.
[braces]
4. When a program looks at array
elements in order from first to last
to find a value, it is using the
__________ algorithm.
[sequential search]
5. An array that uses more than
one index is called a(n) __________
array.
[multidimensional]
6. The parameter to main is a(n)
__________.
[array of Strings]
7. A two-dimensional array is
implemented as a(n) __________.
[array of arrays]
Optional Graphics Supplement
Questions:
8. The __________ method is the
same as the drawPolygon method,
except that it does not draw a
line from the last point back to the
first point.
[drawPolyline]
True/False Questions
1. All data stored in an array must
be the same type.
TRUE FALSE
[TRUE]
2. The index to use with an array
can be an expression whose value
is computed when the program
runs.
TRUE FALSE
[TRUE]
3. If one element from an array is
passed as a parameter to a
method, then the called method
can
18 of 18
modify any element in the array.
TRUE FALSE
[FALSE]
4. Every array has a method called
equals that can be used to
compare it to other arrays.
TRUE FALSE
[FALSE]
5. An accessor method that
returns an array should return a
copy of an instance variable array
rather than the instance variable
itself.
TRUE FALSE
[TRUE]
6. In a two-dimensional array, all
of the rows must have the same
number of elements.
TRUE FALSE
[FALSE]
7. The size of an array in Java is
determined when the program is
compiled.
TRUE FALSE
[FALSE]
8. You can change the size of an
array by assigning a new value to
the length instance variable.
TRUE FALSE
[FALSE]
9. The first element in an array is
accessed using zero as an index.
TRUE FALSE
[TRUE]
10. Arrays in Java are objects.
TRUE FALSE
[TRUE]
11. Selection sort is not one of the
easier to understand sorting
algorithms, but it is one of the
most
efficient.
TRUE FALSE
[FALSE]
Multiple-Choice Questions
1. The lowest index that can be
used with an array
A. is zero
B. is one
C. is determined by the array
declaration
D. changes as the program runs
[A]
2. The highest index that can be
used with an array
A. is the number of elements in the
array
B. is the number of elements in the
array minus one
C. is determined by the array
declaration
D. changes as the program runs
[B]
19 of 19
3. What will happen if a program
uses an array index that is out of
bounds?
A. The compiler will give an error
message and will not compile the
program.
B. The compiler will give a
warning but will still compile the
program.
C. The program will compile but
Java will give an error message
when the program runs.
D. The program will compile and
run with no error messages but
might give incorrect results.
[C]
4. Which statement best describes
an array of 10 elements where the
elements are of class String?
A. 10 String objects will be created
when the array is created.
B. 10 String objects must be
created before the array is
created.
C. The array will be created with
the elements equal to null.
D. Elements of an array cannot be
a class type.
[C]
5. Selection sort works by
repeatedly
A. using the position of elements
to split the list into two sublists of
equal size
B. finding the smallest element in
the remainder of the list and
moving it forward
C. comparing adjacent elements in
the list and swapping them if they
are out of order
D. using a threshold value to split
the list into two sublists where
one sublist's elements are
greater than the threshold and the
other sublist's elements are less
than the threshold
[B]
6. Which of the following is correct
Java code?
A. int[10] list;
B. int list[10];
C. int list = new int[10];
D. int[] list = new int[10];
[D]
7. The main method of a program
has
A. no parameters
B. one String parameter
C. one parameter which is an array
of String values
D. an array of integers as the first
parameter and an array of strings
as the second parameter
[C]
Optional Graphics Supplement
Questions:
8. What is the main difference
between a text field and a text
area?
A. A text area can display more
than one line and a text field can
only display one line of text.
B. A text field can be changed by
the user and a text area cannot.
C. A text area has one or more text
fields inside it.
D. A text area has a setText
method but a text field does not.
[A]
9. How are the points of a polygon
specified?
A. By repeated calls to the
drawPolygon method, with each
call specifying one x coordinate
and
one y coordinate.
B. By passing an array of numbers
that contains alternating x and y
coordinates.
C. By passing two arrays of
numbers, one containing x
coordinates and the other
containing y
coordinates.
20 of 20
D. By passing in a two-
dimensional array of numbers that
contains both x and y coordinates.
[C]
Chapter 7
29 questions
12 fill in the blank
12 true/false
5 multiple choice
Fill in the Blank Questions
1. A(n) __________ is defined by
adding instance variables and
methods to an existing class.
[derived class or child class]
2. Inheritance can be used to
create a new class by adding to an
existing __________ class.
[base or parent]
3. When a class has a method that
has the same name and the same
number and types of
parameters as an inherited
method, then the method __________
the inherited method.
[overrides]
4. The constructor of a derived
class can access the constructor of
its base class by using the
reserved word __________.
[super]
5. The reserved word __________ can
be used as a name for another
constructor in the same class.
[this]
6. The reserved word __________
between two class names at the
beginning of a class definition
indicates that the first class
inherits from the second.
[extends]
7. In Java, every class is a
descendant of the class __________.
[Object]
8. __________ binding is when the
meaning of a method invocation is
not bound to it until the
program is run.
[dynamic or late]
9. __________ means "many forms".
In programming, it allows
different objects to use different
method actions for the same
method name.
[polymorphism]
10. A(n) __________ method is one
which has no body and is meant to
be overridden in every derived
class.
[abstract]
11. Adding the __________ modifier
to a method heading prevents
derived classes from overriding
the
method.
[final]
12. A(n) __________ specifies
headings for methods that must be
defined by an implementing class.
[interface]
21 of 21
True/False Questions
1. Instance variables that are
private in a base class can be
accessed in the definition of a
method
in a derived class.
TRUE FALSE
[FALSE]
2. Private methods are not
inherited.
TRUE FALSE
[TRUE]
3. If a constructor includes a call to
the constructor of the base class,
then the call must be the first
action in the constructor.
TRUE FALSE
[TRUE]
4. An object of a derived class has
the type of the derived class and
also the type of every ancestor
class.
TRUE FALSE
[TRUE]
5. A method in a derived class may
not re-use any method name used
in an ancestor class.
TRUE FALSE
[FALSE]
6. If class Able inherits from class
Baker, and class Baker inherits
from class Charlie, then class
Able inherits from class Charlie.
TRUE FALSE
[TRUE]
7. The keyword this can be used as
a method name that refers to a
constructor.
TRUE FALSE
[TRUE]
8. The expression
super.super.foo() is legal as long
as the foo method is defined in the
appropriate
class.
TRUE FALSE
[FALSE]
9. You can assign an object of an
ancestor class to a variable of a
derived class type.
TRUE FALSE
[FALSE]
Optional Graphics Supplement
Questions:
10. Every class derived from
JFrame has a paint method.
TRUE FALSE
[TRUE]
11. The close-window button of a
JFrame GUI generates an event
which is handled by an action
listener.
TRUE FALSE
[FALSE]
22 of 22
12. The setSize method is
commonly used to change the size
of an applet.
TRUE FALSE
[FALSE]
Multiple-Choice Questions
1. The expression super.foo() in a
method
A. is a call to a method named
super.foo in the same class
B. is a call to the foo method in the
base class
C. is a call to the constructor of the
class named foo
D. is a syntax error
[B]
2. Suppose that class Able inherits
from class Baker, with a1 and b1
referring to objects of class
Able and Baker respectively.
Which statement best describes
how objects of classes Able and
Baker could be assigned to
variables?
A. an object of class Able could be
assigned to b1, and an object of
class Baker could be
assigned to a1
B. an object of class Able could be
assigned to b1, but an object of
class Baker could not be
assigned to a1
C. an object of class Baker could be
assigned to a1, but an object of
class Able could not be
assigned to b1
D. an object of class Able could not
be assigned to b1, and an object of
class Baker could not
be assigned to a1
[B]
3. What determines whether a
method definition in a derived
class or an ancestor class will be
used?
A. the return type of the method
B. the type of the variable that
names the object
C. the object's place in the
inheritance chain
D. the derived class's descendants
[C]
4. An abstract class
A. is a class which cannot be
instantiated
B. is a class which has no methods
C. is a class which has only
abstract methods
D. is a class which has only
overridden methods
[A]
5. Which statement best describes
overriding a method?
A. Methods in a base class and
derived class have the same name
but different visibility
modifiers.
B. Methods in a base class and
derived class have the same name
and have the same number
and types of parameters.
C. Methods in a base class and
derived class have the same name
but have different return
types.
D. Methods in a base class and
derived class have the same name
but different number or
types of parameters.
[B]
23 of 23
Chapter 8
25 questions
7 fill in the blank
9 true/false
9 multiple choice
Fill in the Blank Questions
1. The part of the code that tells
what the program will do in
normal circumstances is called the
__________.
[try-block]
2. The part of the code that tells
what to do when an exception
occurs is called the __________.
[catch-block]
3. An exception is a(n) __________.
[object]
4. Exception-handling code can get
more information about the
exception by using the __________
method.
[getMessage]
5. A method that includes a throw
statement but does have a
corresponding catch must include
a(n) __________ in its heading.
[throws-clause]
6. Statements in a(n) __________
block will be executed regardless
of whether or not an exception is
thrown.
[finally]
7. __________ are often the only
methods in an exception class,
other than inherited methods.
[constructors]
True/False Questions
1. A throw statement is only
allowed inside a catch-block.
TRUE FALSE
[FALSE]
2. A program is not allowed to
have more throw statements than
catch-blocks.
TRUE FALSE
[FALSE]
3. Programmer-defined exception
classes must be derived from a
previously defined exception
class.
TRUE FALSE
[TRUE]
4.
ArrayIndexOutOfBoundsException
is an exception that does not need
to be caught or declared in
the method heading.
TRUE FALSE
[TRUE]
5. catch-blocks should be in order
from most general to most
specific.
24 of 24
TRUE FALSE
[FALSE]
6. catch-blocks may have any
number of parameters.
TRUE FALSE
[FALSE]
7. Every exception class is an
ancestor of the class Exception.
TRUE FALSE
[FALSE]
8. Any method that includes a
throw statement must also include
a catch-block.
TRUE FALSE
[FALSE]
9. Exceptions that are descendants
of the class RuntimeException do
not have to be caught or
declared.
TRUE FALSE
[TRUE]
Multiple-Choice Questions
1. A catch-block
A. is a special method used in
exception handling
B. allows the user to specify what
will happen next in the program
C. is not allowed in the same
method as a try-block
D. contains code that is executed
when an exception occurs
[D]
2. The purpose of the exception-
handling mechanism in Java can
best be described as
A. to simplify the code for normal
circumstances
B. to provide an alternative for if-
else statements
C. to reduce running time of
programs
D. to reduce debugging time of
programs
[A]
3. When an object is thrown
A. it is deleted and can no longer
be used
B. execution resumes with the
next executable statement
C. control passes to a catch-block
D. the object is unavailable until
the catch-block finishes execution
[C]
4. When a catch-block finishes
execution
A. the program continues in the
try-block that was previously
executing
B. the program returns to the
beginning of the try-block to re-
execute it.
C. the program continues with the
next statement after the try-block
and catch-blocks
D. the program stops execution
[C]
5. A programmer-defined
exception class
A. can be derived from any
existing class
B. should provide two or more
constructors that set up the
message for the exception
25 of 25
C. should not have more than one
instance variable
D. should contain at least one int
variable to store the exception
code number
[B]
6. Overuse of exceptions
A. is not an issue because
exceptions may be used freely
B. is not an issue because the
compiler restricts programmer-
defined exceptions
C. is a problem because it causes
an excessive number of class
definitions
D. is a problem because it results
in unrestricted flow of control
[D]
7. catch-blocks are located
A. at the beginning of the method
B. at the end of the method
C. before a try-block
D. after a try-block
[D]
8. The parameter to the Exception
class constructor
A. is a String that will be the
message for the exception
B. is an int that will be the
exception code number
C. tells the statement where
execution should resume
D. is a boolean telling whether or
not the program should terminate
[A]
9. What happens if the catch-block
of a more general exception comes
before the catch-block of a
more specific exception?
A. Both catch-blocks will execute
when the more general exception
occurs.
B. Both catch-blocks will execute
when the more specific exception
occurs.
C. The more general catch-block
will never execute.
D. The more specific catch-block
will never execute.
[D]
Chapter 9
28 questions
11 fill in the blank
10 true/false
7 multiple choice
Fill in the Blank Questions
1. A(n) __________ is an object that
takes data from a source or
delivers data to its destination.
[stream]
2. Files that are considered to be a
string of characters and can easily
be opened with an editor are
called __________ files.
[text]
3. The preferred class for output
to a text file is __________.
[PrintWriter]
4. __________ to a file means to add
data to the end of the file.
[appending]
26 of 26
5. The method __________ in the
BufferedReader class can be used
to read a line of input from a
file.
[readLine]
6. The class __________ can be used
to break a string up into individual
words.
[StringTokenizer]
7. All output streams have a
method called __________ that writes
all buffered data to the file.
[flush]
8. The class __________ provides a
method to find out if a given file
already exists.
[File]
9. When a program tries to read
beyond the end of a file, the read
method returns the value
__________.
[-1]
10. The print method of
PrintWriter will automatically call
the __________ method of objects
passed
to it as parameters.
[toString]
11. A(n) __________ name gives the
name of a file and also tells what
directory (or folder) the file is
in.
[path]
True/False Questions
1. A disadvantage of binary files is
that are less efficient to process
than other files.
TRUE FALSE
[FALSE]
2. A file variable declared inside a
try-block will not be accessible
outside of the try-block.
TRUE FALSE
[TRUE]
3. Careless use of the
FileOutputStream constructor can
result in lost files.
TRUE FALSE
[TRUE]
4. When a program will be
appending to a file rather than
creating a new file, the string
"append"
should be added as a second
parameter to the constructor for
FileOutputStream.
TRUE FALSE
[FALSE]
5. The preferred way to open a
text file for input is to pass the file
name to the constructor for
BufferedReader.
TRUE FALSE
[FALSE]
6. The way to find out if a
tokenizer has more tokens is to
use the boolean instance variable
more.
TRUE FALSE
[FALSE]
27 of 27
7. The readLine method in
BufferedReader returns the value
null if there is no more data to
read
from the file.
TRUE FALSE
[TRUE]
8. ObjectInputStream methods
that try to read past the end of a
file will cause an EOFException.
TRUE FALSE
[TRUE]
9. If data in a stream flows out of a
file then the stream is an output
stream.
TRUE FALSE
[FALSE]
10. Your program should call the
close method when it is finished
with a file because otherwise the
next program that needs to use
the file will not be able to open it.
TRUE FALSE
[FALSE]
Multiple-Choice Questions
1. Every program that does file I/O
A. must include the line include
io.java somewhere in the program
B. can do input from a file or
output to a file but not both
C. must be designed specifically
for the operating system in which
it will run
D. must include the line import
java.io.* near the beginning
[D]
2. Opening a text file for writing
A. requires use of the PrintWriter
constructor
B. requires use of the
FileOutputStream constructor
C. requires use of the PrintWriter
constructor and the
FileOutputStream constructor
D. does not require use of any
constructors
[C]
3. If a new output text file cannot
be created
A. a FileNotFoundException will
be thrown
B. a FileNotCreatedException will
be thrown
C. the program will stop executing
with an error message
D. the program will continue
executing and might produce
incorrect results
[A]
4. If an open file is not closed
when the program finishes
executing
A. a FileNotClosed exception will
be thrown
B. the contents of the file will be
lost
C. Java will close the file but it is
better to explicitly close the file
D. no other program will be able to
open the file
[C]
5. The classes used to open a
binary file for writing are
A. PrintWriter and
FileOutputStream
B. ObjectOutputStream and
FileOutputStream
C. PrintWriter and
ObjectOutputStream
28 of 28
D. BufferedWriter and
ObjectOutputStream
[B]
6. A good programming practice
for I/O in Java is
A. have objects do their own I/O
B. design a single class to do all
I/O for the program
C. avoid file I/O since it is
inefficient
D. never append to a file
[A]
7. If the second parameter to the
FileOutputStream constructor is
__________, a program will be able
to append to a file.
A. the boolean value true
B. the character 'a' or 'A'
C. the character '+'
D. the string "append"
[A]
Chapter 10
26 questions
12 fill in the blank
10 true/false
8 multiple choice
Fill in the Blank Questions
1. Vectors can be thought of as
__________ that can grow and shrink.
[arrays]
2. The vector method that takes an
index as a parameter and returns
an element of the vector is
__________.
[elementAt]
3. The vector method that tells
how many elements are currently
in the vector is __________.
[size]
4. Usually the __________ node is the
only node in the list for which
there is a named variable.
[head]
5. A(n) __________ is an object that
allows you to step through a list
and perform some action for
each element of the list.
[iterator]
6. To make a linked list that can
contain different kinds of
elements, the data instance
variable
should be defined as __________.
[Object]
7. A(n) __________ is a linked list
where a program can easily move
backward in the list as well as
forward.
[doubly-linked list]
8. The vector method __________ can
be used to reduce the amount of
memory used by the vector
to the minimum necessary.
29 of 29
[trimToSize]
9. Each node in a linked list has
some data and a(n) __________ to
another node.
[link or reference]
10. The process of recycling the
memory used by deleted nodes is
called automatic
__________.
[garbage collection]
11. A class defined within another
class is called a(n) __________.
[inner class]
12. The __________ of a vector is the
type of elements that can be added
to the vector.
[base type]
True/False Questions
1. A program that uses vectors
must include the line: import
java.vector.*;
TRUE FALSE
[FALSE]
2. If the current size of a vector is
10, the method call
v.setElementAt(newEl, 10) will
add newEl as
the 11
th
element of the vector.
TRUE FALSE
[FALSE]
3. Any vector that is not
parameterized (has no type
parameter) will have a base type
of Object.
TRUE FALSE
[TRUE]
4. A tree is a linked data structure
where links form loops among the
elements.
TRUE FALSE
[FALSE]
5. A vector created with the
default constructor will have 10
elements and will add 10 elements
whenever it needs to increase its
capacity.
TRUE FALSE
[FALSE]
6. A vector can grow and shrink
during execution of a program.
TRUE FALSE
[TRUE]
7. The square bracket notation
used with arrays can also be used
with vectors.
TRUE FALSE
[FALSE]
8. The base type of a vector is
specified in angular brackets after
the class name, Vector.
TRUE FALSE
[TRUE]
9. A primitive type cannot be the
base type of a vector.
TRUE FALSE
30 of 30
[TRUE]
10. The Vector method elementAt
always returns a result of type
Object.
TRUE FALSE
[FALSE]
Multiple-Choice Questions
1. One disadvantage of vectors is
A. they have a limited capacity
B. they can only contain primitive
types
C. they can only contain objects
D. they often cause exceptions
[C]
2. A number passed as an
argument to the Vector
constructor
A. causes a compiler error
B. indicates the type of elements
that will be stored in the vector
C. determines the initial capacity
of the vector
D. makes the number the first
element of the vector
[C]
3. A program can make a copy of a
vector by
A. using the assignment operator
B. using the clone method of
Vector
C. using the copy method of Vector
D. writing special-purpose code
[B]
4. Links in a linked list are usually
A. references B. integers C. arrays
D. vectors
[A]
5. In the textbook's
implementation of a linked list, the
statement used to go from one
node to the
next was a statement like:
A. position.next();
B. position++;
C. position.addElement(1);
D. position = position.getLink();
[D]
6. What happens to nodes that are
deleted from a linked list?
A. The memory they used is
reclaimed for other uses
automatically.
B. The memory they use is
reclaimed when the method
System.garbageCollect is called.
C. The memory they use is not
reclaimed until the head node is
deleted.
D. The memory they use is not
available for other uses until the
program terminates.
[A]
7. Where is the easiest place to
add new nodes to a linked list?
A. At the beginning of the list.
B. At the end of the list.
C. Wherever the current position
is.
D. Wherever there is an empty
position.
31 of 31
[A]
8. The elementAt method
A. has two parameters
B. only returns values of primitive
types
C. is typically used with a type cast
D. returns null if the requested
element does not exist
[C]
Chapter 11
25 questions
5 fill in the blank
11 true/false
9 multiple choice
Fill in the Blank Questions
1. When a method definition
contains an invocation of itself, the
method is said to be __________.
[recursive]
2. A __________ is a case with no
recursive calls.
[stopping case or base case]
3. If every case of a recursive
method includes calls to itself, the
result will be __________.
[infinite recursion]
4. A rewritten recursive method
that uses a loop instead of
recursion is called a(n) __________
version of the method.
[iterative]
5. Binary search cannot be used
with an array that is not __________.
[sorted]
True/False Questions
1. The main reason for using
recursion is to make a program
easier to understand.
TRUE FALSE
[TRUE]
2. A recursive method needs to
have an if-else or some other
branching statement that leads to
different cases.
TRUE FALSE
[TRUE]
3. A recursive method should only
contain one call to itself.
TRUE FALSE
[FALSE]
4. A recursive method must
contain at least one case where it
does not call itself.
TRUE FALSE
[TRUE]
5. The error that results from
having too many stopping cases is
called stack overflow.
TRUE FALSE
Saved