Questions You Should Be Able To Answer Already (Reference: Data
Structures and Abstractions with Java, 5th Edition, by Carrano and
Henry) See code from this text here and
Power Points from the text here
and here.
The topics covered by this "Q and A" page is material with which you are
assumed to be already familiar from prior experience. For many, this
prior experence will have been the CSCI 1226/1228 course sequence here
at Saint Mary's but you may have received equivalent experience elsewhere.
A summary of the prerequisite material as presented in the Savitch text
used in CSCI 1226/1228 is given under the Savitch link on the course
home page menu. This page contains much of the same material, but with
appropriate references to the Carrano and Henry text used in CSCI 2341.
In addtion to knowing this material, it will be important that you
become familar, as soon as possible, with our style rules and guidelines,
which we will discuss and emphasize in class and lab, and which will be
available on the course website as well as via a convenient handout.
Text Supplement 1 (on-line): Java Basics
Miscellaneous
- What is the difference between a Java identifier and
a Java reserved word?
- How do you use each of the following commands at the command
line:
java, javac, javadoc?
Simple Data Values and Data Types
- What is meant by a literal value, such as an
integer literal value, a character literal value, or a string literal
value, for example?
- What are the eight simple (or primitive) data types in Java
(give a literal value example of each type), what are their
corresponding wrapper class types, and what are
automatic boxing and unboxing?
- What is meant by type casting (also called
just casting), what happens when we cast between
char and int values, and how does
casting vs. assignment compatibility
relate to the following "diagram"?
byte -> short -> int -> long -> float -> double
Variables, Expressions and Operators
- What is the difference between a variable
declaration, a variable initialization,
and a variable assignment?
- How does a variable of a class type differ from a variable of a
primitive type?
- Are you aware that when we use the term
expression we can actually be referring to a
single constant, a single variable, or something more complicated
involving constants, variables, operators and even functions?
- Do you know how to use (properly) all of the arithmetic operators
(
+, -, *, /
and %), the increment and decrement operators
(++ and --),
and the special assignment operators (+=, -+,
*=, and so on)?
- What is a boolean expression and what are its
possible values?
- Do you know how to use (properly) the six relational
operators (
==, !=,
<,
<=, >, >=) and the three
logical operators (!,
&&, ||, also called
boolean operators)?
- What is meant by short-circuit evaluation,
and can you give two examples: one using the
&&
operator and another using the || operator?
- Why should you not use the
== or !=
operators when comparing floating-point values or strings?
- How does the conditional operator (
? :) work?
- What is the precedence of the various operators mentioned above
(arithmetic, relational, logical, conditional, assignment), and what
are the two uses of parentheses in this context?
Simple Output to the Standard Output Stream (the screen) and
Simple
Input from the Standard Input Stream (the keyboard)
- How do you use
System.out.print(),
System.out.println(), and
System.out.printf()?
- How do you use a Scanner object (usually called
keyboard) to read input from the standard input?
- How would you set up an object called
screen to write
output to the standard output in a manner analogous to the way
keyboard reads input? [Hint: Make screen an
object of the class java.io.PrintWriter.]
Decision-Making Control Structures
- How are boolean expressions used in decision-making control
structures?
- What is the syntax of an
if-statement, an
if..else-statement, a multi-way if-statement,
a switch-statement; and what are the typical scenarios where
each is used?
- What is a nested decision-making control structure, and when
would you use one?
Loop Control Structures
- How are boolean expressions used in loop control structures?
- What is the syntax of a
while-loop, a
do..while-loop, a for-loop (including the
"range for-loop", also called a "for-each
loop") and what are the typical scenarios where each is used?
- What is a nested loop control structure, and when would you use
one?
- Are you aware of the typical "gotchas" when using loops
(infinite loops, empty loops, off-by-one errors, declaring a
for-loop control variable in the wrong place)?
- How do the
break-statement and the
continue-statement work?
Arrays
- When and why would you use an array rather than just a bunch of
"ordinary" variables?
- Can you draw and properly label a "picture" of an array?
- How do you declare a variable of an array type?
- How do you create an array object of a particular size?
- How do you initialize an array object when you declare it?
- How do you find the length of an array?
- What happens if you try to use an invalid array index value?
- What are the two kinds of for-loops you can use to process an
array, and can you use either one in any situation where you want
to process some or all of the elements in an array?
- Which, if any, of the following can be an array: a class data
member, a function parameter, a function return-value?
- What do you, as a programmer, have to keep track of if you are
dealing with a partially filled array?
- What is the easiest way to sort all or part of an array in
Java?
- How do you define, and access the elements of, a multidimensional
array in Java?
Strings
- Why can you use the String class in a Java program without an
import statement?
- What are two ways of initializing a String variable?
- What are the Java escape characters, which
ones are most often used, and for what purpose?
- What is the relationship between Java and Unicode, and between
ASCII and Unicode?
- What does it mean that the value of a Java String variable is
immutable?
- What is the simplest way to concatenate (put together) two Java
strings (two literals, two variables, or a literal and a variable),
and can we use the same way to concatenate a string and a number?
- How do you compare strings for equality or inequality?
- Do you know how to use the following basic Java String class methods:
charAt(), compareTo(), concat(),
equals(), equalsIgnoreCase(),
indexOf(), lastIndexOf(),
length(), toLower(), toUpper(),
replace(), substring() (two versions),
trim()?
Command-Line Parameters
- How is the array
String[] args that is always the
sole parameter in the main() method of any class used?
- If you want to use a value input at the command line as a
number (integer or real), what do you have to do and how do you do it?
Text Supplement 2 (on-line): File Input and Output
- What is a stream in Java (in the context of
file I/O and/or standard I/O)?
- What are the two types of files, and how would you describe
each?
- Are you clear about the meanings of input and
output in the context of files and your program?
- How do you set up a textfile as an output stream and write to
it?
- What can you say about exception handling in this context?
- What are the useful methods of the PrintWriter class?
- What do we mean when we say that "a file has two names" in a
program?
- What happens if there is already an output file with the same
name as the one we want to use?
- How do we append to an already existing file?
- How do you set up a textfile as an input stream and read from
it?
- What can you say about exception handling in this context?
- What are some Scanner methods you can use to determine if there
is more data of a specific type available to be read?
- Can you explain why there is a one-parameter constructor for
the PrintWriter class that takes a string as its parameter and sets
up the file whose name is in that string as an output stream, but
there is no corresponding one-parameter constructor for the Scanner
class that takes a string as its parameter and sets up the file
whose name is in that string as an input stream?
- And so ... what class do we have to use as an "intermediate"
class with the Scanner class when setting up an input stream?
- What are some useful methods of the File class?
- Can you explain the difference between a "file" and a "File
object"?
- Do you understand why we have to be careful when using Windows
path names to specify the location of a file in our Java programs?
- Do you know what the acronym CSV stands for, and how to process
the kind of data referred to by this acronym?
- How do you set up a binary file as an output stream and write
to it?
- What can you say about exception handling in this context?
- How is the FileOutputStream class used?
- What are some useful methods of the ObjectOutputStream class?
- What does UTF stand for, and what is perhaps surprising about
its use in the context of binary file output?
- How do you set up a binary file as an input stream and read
from it?
- What can you say about exception handling in this context?
- How is the FileInputStream class used?
- What are some useful methods of the ObjectInputStream class?
- How is the EOFException class used?
- What interface must a class implement if we are to write out
objects of that class to a binary file?
- What must we remember to do when reading in objects from a
binary file?
Text Appendix A: Documentation and Programming Style
- What are the three kinds of comments you will find in Java,
and how is each used?
- What is the Java tool used to produce "automatic documentation"
based on certain types of comments, and which tags are
we likely to use most often with this tool??
- Are you familiar with the conventions you should be observing, in
your particular situation, for each of the following?
- What naming and capitalization style conventions do we use for
variables, constants, methods, classes?
- What indentation level will we use?
- What are method pre-conditions and
method post-conditions and how will we document them?
- How will we comment a class?
- How will we comment a method?
Text Appendix B: Java Classes
- Could you explain (briefly) to someone what the following terms
mean and how the concepts they represent relate to one another, in
the context of Java classes: encapsulation,
information hiding, implementation
details, class interface
(API), abstract data type (ADT)?
- If you compile a Java source code file in a directory and in
that same directory there are other Java source code files that
contain classes that the class in the first file uses, what happens
to those other source code files?
- What is the syntax for a typical Java class definition?
- Typically, in a class definition, what should be
private and what should be public, and
what is the syntax for using these modifiers, or
access qualifiers?
- What does the Java keyword
this refer to?
- What are two uses of the Java keyword
this?
Variables in a Class
- What is an instance variable (or data
member)?
- What is a local variable?
- What is a static variable?
Methods in a Class
- What are two differences (with respect to return-type, and choice
of name) between a value-returning method and one that does not return
a value?
- What is a static method?
- How is any class method called from within its own class?
- How must a (public) non-static class method be called from outside
its own class?
- What are the two ways a (public) static class method may be called
from outside its own class?
- What is a block (of code), and what is the
scope of a variable declared inside such a block?
Method Parameters
- What is the name of the default parameter passing
mechanism in Java?
- So ... what happens when you pass a value of any type to a Java
method?
- But ... what is the difference between passing a variable of
primitive type and passing a variable of class type to a method?
- What is the distinction between the terms
parameter and argument (or, as
these are sometimes called as well, formal
parameter and actual parameter)?
- What happens to parameter values and local variable values when
a method finishes executing?
- How does "automatic type casting" work in the context of
parameter passing?
- What are accessor methods and mutator
methods (also called getters and
setters) for a class?
Testing Your Classes
- What are you doing when you compare two variables of class type
with the
== operator or the != operator?
- If you intend to compare two objects of your class to see if
they are equal in the sense of "having the same content", what
method should you define for your class?
- What are unit testing and regression
testing?
- How do you use Java's
assert statement to help you
with debugging?
- Under what circumstances might you want to use
exit(0)?
- In Java every class can have a
main() method, so
can you explain why this is very useful, and also why Java does not
get "confused" about which main() to use when you run
your program?
Class Constructors
- What is a class constructor?
- What is the syntax of a default class constructor?
- What is the syntax of a non-default class constructor?
- How do you create a new instance of a class (that is, a new
object of that class) using a class constructor?
- What value do you give to a variable of class type to indicate
explicitly that it does not refer to any class object, and how do
you test if the variable contains that value?
- When does Java provide a class constructor for you?
- What happens to class instance variables that you do not
explicitly initialize when the class is created?
- How would you describe any similarities, differences and/or
connections between class constructors and class setters?
- A constructor is a class method, so can you use a class object
to call a constructor like you would call any other class method?
- If you call another class method from a constructor (a "helper"
method to help with the construction, for example) what should the
access modifier of that other method be, and why? (Hint: Both a
constructor and a setter might want to call the same method to do
some setup, so think what potential problems inheritance might open
up if both used the same (public) helper method.)
- Why would you call one constructor from another, and how is it
done?
More on Static vs. Non-Static Variables and Methods
- What are static variables/constants (also called class
variables/constants) and static methods (also, but perhaps less
frequently, called class methods)?
- How would you explain the distinction between a class
variable (also called a static variable)
and a variable of class type?
- How do we access a public static variable or or public static
method of a class if we don't have an object of that class? What if
we do have an object of that class?
- Can you think of a class variable that you have been using
since your first Java program?
- Can you explain the difference between instance variables,
local variables, and class (static) variables?
- Can a static method reference an instance variable of its
class?
- Can a static method call any method of its class?
- Can a non-static method reference an instance variable of its
class?
- Can a non-static method call any method of its class?
Method Overloading and Method Signatures
- What is meant by method overloading?
- What is a method signature, and why is a
method's return-type not part of its signature?
- Can you fill in the blanks in the following statement?
When one method overloads another, both must have the same
_______________ , but they must have different ____________________ .
Packages
- What is a package, and how should a package
be named?
- What is the only package from the Java standard library that is
automatically imported into every Java program?
- Where must a
package statement appear?
- What is the connection between package names, directory paths,
and the Java classpath?
- What do you do so that you can use in your program a class from
a particular package?
Some Particular Useful Classes or Kinds of Classes
- Do you know what
Math.E and Math.PI are,
even if they're not often useful for computing folks?
- Do you know how to use the following static methods of the Math
class:
pow(), sqrt(), cbrt(),
hypot()?
- Do you know how to use the following static methods of the Math
class:
exp(), log(), log10()?
- Do you know how to use the following static methods of the Math
class:
ceil(), floor()?
- Do you know how to use the static method round() of the Math class?
[Note:
float -> int, but double
-> long.]
- Do you know how to use the following static methods of the Math
class:
abs(), max(), min()?
[Note: Argument and return-value have the
same type: int, long, float or
double.]
- Do you know how to use the static method
random() of
the Math class? [Note: Returned value is >= 0 and < 1.]
- What are two useful named static constants found in each of the
wrapper classes
Integer, Long,
Float, and Double?
- What are the names of the static class methods that can be used
to convert a String value that contains a number into the actual
corresponding number (found in
Integer, Long,
Float, Double)?
- Do you know how to use the following methods of the
Character
wrapper class: toUpperCase(), toLowerCase(),
isUpperCase(), isLowerCase(),
isLetter(), isDigit(),
isLetterOrDigit(), isWhiteSpace()?
- What is an enumeration, and do you know how to use each of the
following methods of any enumeration:
toString(),
equals(), compareTo(), ordinal(),
and valueOf()?
Text Appendix C: Creating Classes from Other Classes
- What is inheritance?
- What is a base/parent/super class?
- What is a derived/child/sub class?
- What is the syntax for class inheritance?
- What is meant by the is-a relationship (also
called the may-be-replaced-by-a relationship)?
- What is meant by the has-a relationship (also
called the composition relationship)?
- Why do you suppose the Java language prevents you from
accessing the private data members and/or the private methods in a
base class from a derived class?
- What is meant by method overriding (not to be
confused with method overloading)?
- Can you fill in the blanks in the following statement? When
one method overrides another, both must have the same _______________ ,
the same ____________________ and the same ____________________ .
- What does it mean if a method or a class has the
final modifier?
- What is the keyword
super used for in a derived
class constructor, and what happens if you don't use it?
- Can you explain the use of the keywords
this and
super for calling constructors?
- If you're in a derived class, and there is a method in that
class that has been overridden, but you want to call the version of
that method that is in the base class, how do you do it?
- Is it possible to use "repeated supers", as in
super.super.foo()?
- Can an object of a derived class serve as an object of its base
class?
- Can an object of a base class serve as an object of one of its
derived classes?
- What is the
Object class?
- Can a Java class extend two or more classes? In other words,
does Java have multiple inheritance?
- What methods of the
Object class do you think we
should "always" override in our own classes?
- How do you use the
instanceof operator to help you
write a "good" equals() method for one of your
classes?
- What is polymorphism?
- What is dynamic binding (also called
late binding or runtime binding)?
- What is the relationship between dynamic binding, inheritance
and polymorphism? [Hint: Think of this in the context of an array
of some base object type that may contain both base objects and
derived objects, and a method that has been overridden is called,
using a loop, on each object in the array.]
Text Prelude: Designing Classes [and using interfaces]
- What are the three main "pillars" of Object Oriented
Programming (OOP), and can you briefly
describe each?
- What is an interface?
- What are three things an interface does not
have? [How might this question be answered before, and after, Java 8?]
- How is an interface used, and what is its general syntax?
- What is the syntax that indicates a class is going make use of
a single interface?
- What is the syntax that indicates a class is going make use of
two or more interfaces?
- If two or more classes implement the same interface, do they
both have to implement it in the same way?
- How do we use an interface as a type?
- Do you understand the following statement: A variable's type
determines what methods can be used, but the actual type referenced
by the variable determines which definition of any particular
method will be used.
- Can interfaces be extended like classes?
- Can one interface extend two or more other interfaces?
- How is the built-in Comparable interface used? [In this
context, note that the Java API shows interfaces in italic text,
classes in normal text.]
- What is an abstract method?
- What is an abstract class?
- Can you create an object of an abstract class type?
- How is an abstract class generally used?
- Can an abstract class be a type?
- What does the acronym UML stand for?
- How do you draw a simple UML class diagram,
and for what are the symbols
+ and - used
in such a diagram?
- Should you show constructors in a UML class diagram?
- What does a UML inheritance diagram look like?
Text Java Interlude 2: Exceptions
- What is the "old-fashioned" way of dealing with run-time errors
that occur in our programs, and can you give a reason why this
approach has fallen out of favor over the years?
- What is meant by a Java exception?
- How many specifically exception-related keywords does Java
have, and what are they?
- What is meant by the
try-throw-catch keyword
sequence that we use to deal with Java exceptions, and can you
explain the ideas of throwing,
catching, and handling an
exception?
- What should you always supply when you create an Exception
object, and how should this be retrieved later and used?
- What is a "
catch-block parameter", and why is this
terminology potentially misleading?
- Can you explain the typical "flow of control" in a simple
program when an exception is thrown, as well as when it's not
thrown?
- If you define your own exception class (which you can do), how
does inheritance come into play?
- Typically, what is minimum number of constructors should you have
in an exception class that you define yourself?
- What does it mean to "declare" an exception (informally known
as "passing the buck"), and what is the syntax for doing so?
- What happens if an exception is thrown but not caught and
handled?
- Where do the various exception classes live (that is, in what
packages are they located)?
- Could you draw a (small) UML diagram illustrating how Java
exceptions fit into the Java class hierarchy?
- What is the difference between an error and an
exception in Java?
- What is the difference between a checked
exception and an unchecked exception?
- If you have more than one
catch-block (which you can
have), what do you have to remember about the order of those
catch-blocks?
- Can you offer some good advice about when and where to use
exceptions?
- Can you nest
try-catch blocks? Should you?
- Can you re-throw an exception? Should you?
- What is the purpose of a
finally-block?
Text Chapter 9: Recursion
- What is recursion?
- What is the more general problem-solving approach of which
recursion is a special case?
- What are the main features of a recursive solution?
- What must every recursive method call?
- What happens if a recursive method does not include a "stopping
condition"?
- Which one of the standard control structures is often used as
the basic structure of a recursive solution?
- Can you compare and contrast a recursive solution and an
iterative solution of the same problem?
- Can a recursive method be either
void or
value-returning?
- Do you understand how recursive binary search works?
(discussed in Savitch)
- Do you understand how recursive merge sort works?
(discussed in Savitch)
Some Generics and Containers (discussed briefly in Savitch and in
the previous course)
- What is meant by a dynamic data structure?
- What is an ArrayList and how does it differ
from an "ordinary" array?
- What can you not put into an ArrayList?
- How does a constructor for a parameterized class type differ
from a constructor for a non-parameterized class type?
- What are two different constructors for the ArrayList?
- What is meant by type inference, and how would you use it in
the context of the ArrayList?
- What are some useful methods of the ArrayList class?
- What is the most convenient kind of loop to use if you wish to
process all the elements in an ArrayList object?
- What is mean by saying the ArrayList is a "parameterized" type?
- What is a HashSet and what are some of its methods?
- What is a HashMap and what are some of its methods?