Search
Browse
Create
Log in
Sign up
Log in
Sign up
AP Computer Science A
STUDY
Flashcards
Learn
Write
Spell
Test
PLAY
Match
Gravity
Terms in this set (303)
AP CS A concerns _______-__________ programming.
object-oriented
Related classes in java are grouped into...
packages
A Java program must have at least ___ class(es).
1
The java files that comprise your program are called...
source files
A __________ converts source code into a machine readable form.
compiler
A compiler converts source code into a machine readable form, called...
bytecode
All Java methods must be contained in a(n)...
class
All program statements must be placed inside a(n)...
method
The keyword _____ signals that the class or method is usable outside the class.
public
The keyword ____ signals data or methods are not usable outside the class.
private
The keyword ________ is used for methods that will not access any objects of a class.
static
The ________ method must always be static.
main
A(n) ____ is a name for a variable, parameter, constant, user-defined method, or user-defined class
identifier
An identifier may use... (3)
letters, digits, and underscores
Identifiers may not begin with a(n)...
digit
What primitive types are included in the AP Java subset? (4)
int, boolean, double, char
If you try to store a value that is too big in an int variable, you'll get a(n)....
overflow error
When you get an overflow error, does Java give you a warning?
no
Assigning a double to an int will cause a(n)...
compile-time error
A(n) ________ variable is used to name a quantity whose value will not change.
final
True or false: constant identifiers are capitalized.
true
What purpose does using constants serve?
it makes it easier to revise the code
_____________ operators are used in boolean expressions.
relational
Relational operators should generally only be used when comparing...
primitive types
Avoid using boolean operators to compare ______-______ numbers.
floating-point
_________ operators are applied to boolean expressions to form compound boolean expressions.
logical
What logical operator does the following truth table correspond to?
? | T F
--------------
T | T F
F | F F
&&
What logical operator does the following truth table correspond to?
? | T F
--------------
T | T T
F | T F
||
What logical operator does the following truth table correspond to?
? | T
-----------
T | F
F | T
!
Operators such as +=, =, -=, and /= are known as __________ operators.
compound assignment
True or false: when chain assignments like the following are used:
x = y = z = 0;
the statement is evaluated from left to right.
false
Is the following order of operator precedence correct?
1) !, ++, --
2) *, /, %
3) +, -
4) <, >, <=, >=
5) ==, !=
6) &&
7) ||
8) =, +=, -=, *=, /=, %=
yes
What will be the output of the following statement?
System.out.println( 5 + 3 < 6 - 1 );
false
_________ is an object in the System class that allows output to be displayed on the screen.
System.out
The ______ method outputs an item and then goes to a new line.
println
The ________ method outputs an item without going to a new line afterward.
print
A(n) _________ __________ is a backslash followed by a single character.
escape sequence
What are escape sequences used for?
to print special characters
What are the three escape sequences necessary to know for the AP exam?
\n, \", \\
_______ __________ are the mechanism by which you make the statements of a program run in non-sequential order.
control structures
if, if-else, nested if, and extended if statements are all known as ______-______ control structures.
decision-making
Name the two control structures that allow the computer to perform iterative tasks.
for loop and while loop
The following is known as what kind of loop?
for (SomeType element : collection)
{
statements;
}
for-each
A(n) __________ is an error condition that occurs during the execution of a Java program.
exception
If you divide an integer by zero, what kind of error is thrown?
ArithmeticException
If you use a negative array index, what kind of error is thrown?
ArrayIndexOutOfBoundsException
An ________ exception is one where you don't provide code to deal with the error.
unchecked
A(n) __________ exception is thrown to indicate that a parameter does not satisfy a method's precondition
IllegalArgumentException
A(n) ______ is a software blueprint for implementing objects of a given type
class
A(n) ________ is a single instance of a class.
object
The current state of a given object is maintained in its ____ ______ or _______ _________.
data fields; instance variables
The ________ of the class provide behaviors exhibited by an object and the operations that manipulate the object
methods
Combining an object's data and methods into a class is known as...
encapsulation
In the AP Java subset, all classes are...
public
A ____ ___________ contains a value that is shared by all instances of the class
static variable
Give the formula for a method header.
access specifier, return type, method name, parameter list
A(n) _______ creates an objects of the class.
constructor
A constructor's name is always the same as...
the class
A constructor has no...
return type
A(n) _________ constructor has no arguments.
default
Getter methods are also known as...
accessor methods
Setter methods are also known as...
mutator methods
A(n) _______ method accesses a class object without altering the object
accessor
A(n) ________ method changes the state of an object by modifying at least one of its instance variables.
mutator
A method that performs an operation for the entire class, not its individual objects, is called a(n)...
static method
All methods defined in the main program must be...
static
________ methods are two or more methods in the same class that have the same name but different parameter lists.
overloaded
A method's signature consists of...
the method name and parameter types
The compiler figures out which method to call by examining a method's...
signature
True or false: you can't have two methods with identical signatures but different return types.
True
Having more that one constructor in the same class is an example of...
overloading
The _____ of a variable or method is the region in which that variable or method is visible and can be accessed.
scope
A(n) ______ variable is defined inside a method.
local
A ________ is a piece of code enclosed in a {} pair.
block
When a block is exited, the memory for a local variable is...
recycled
True or false: local vars take precedence over instance vars with the same name
true
In the method obj.doSomething(x); "obj" is a(n) ______ parameter and "x" is a(n) ________ parameter.
explicit, implicit
Whereas double, int, char and boolean are primitive data types, all objects are ______ data types.
reference
Having two references for the same object is known as...
aliasing
An uninitialized object variable is called a...
null reference/null pointer
An attempt to invoke an instance method with a null reference may throw a...
NullPointerException
The parameters used in a method heading are known as _____ or ________ parameters.
dummy, formal
The parameters one uses when actually using a method are known as _______ parameters or __________.
actual, arguments
____________ are passed by value
parameters
What does it mean for primitive types if parameters are passed by value?
when a method is called, a new memory slot is allocated for each parameter
In Java, both ___________ _________ and _______ __________ are passed by value.
primitive types; object references
What happens when an object is passed as a parameter?
The address is copied into local memory, but not the values of the instance variables
Define inheritance.
the mechanism by which a new class, called a subclass, is created from an existing class, called a superclass; by absorbing its state and behavior and augmenting these with features unique to the new class.
Which is bigger: a subclass or a superclass?
a subclass; it contains more data and more methods
A subclass can itself be a superclass for another subclass, leading to a(n)...
inheritance hierarchy
The relationship that a subclass has to its superclass is informally referred to as a(n)...
is-a relationship
True or False: a subclass cannot redefine a method that it inherits.
false
If part of the original method from the superclass is retained, we refer to the subclass's rewrite as...
partial overriding
What is the format of implementing a subclass?
public class Subclass extends Superclass
True or false: subclasses inherit the private instance variables and private methods of their superclasses.
false
True or false: subclasses can only access the public methods of their superclasses.
true
How can a subclass override a method from its superclass?
by defining a method with the same return type and signature
What keyword is necessary in order to invoke a method from a superclass?
super
True or false: Private methods from superclasses cannot be overridden in subclasses.
True
Are constructors from superclasses inherited by subclasses?
No
How can a subclass constructor implement its instance variables exactly the same way as the superclass constructor?
by using the super() method
If super is used in the implementation of a subclass constructor, it must be used...
in the first line of the constructor body
What is within the body of the constructor of a subclass is no constructor is provided?
super();
True or false: A subclass may redefine a public method as private.
false
True or false: A subclass may not override static methods of the superclass
true
Assuming GradStudent and UnderGrad are subclasses of Student, is the following code legal?
Student g = new Student();
Student s = new GradStudent();
Student u = new Undergrade();
Yes
Assuming GradStudent and UnderGrad are subclasses of Student, is the following code legal?
GradStudent g = new Student();
Undergrad u = new Student();
No
What does it mean if a method is polymorphic?
It has been overridden in at least one subclass
Define polymorphism.
The mechanism of selecting the appropriate method of a particular object in a class hierarchy.
Method calls are always determined by the type of the ________ _______, not the type of the object reference.
actual object
Making a run-time decision about which instance method to call is known as...
dynamic binding/late binding
The compiler selecting the correct overloaded method at compile time is known as...
static binding/early binding
In polymorphism, what determines the actual method that will be called?
the run-time environment
In polymorphism, the compiler deterimes __ a method can be called, and the run-time environment determines ___ a method can be called
if, how
A subclass can call a method in its superclass by using...
super
If a superclass method calls another method that has been overridden in the subclass, the method that is executed is...
the one in the subclass
Casting a superclass to a subclass type is called a...
downcast
Will this code cause an error? Why or why not?
int x = (GradStudent) s.getID();
Yes; dot operator has higher precedence so s.getID() runs before s can be cast to GradStudent
Will this code cause an error? Why or why not?
int x = (GradStudent s).getID();
No, because s is cast to GradStudent before the method is run
Why is a ClassCastException thrown?
to signal an attempt to cast an object to a class of which it is not an instance
Define abstract class.
A superclass that represents an abstract concept, and is not instantiated
An abstract class may contain...
abstract methods
An abstract method only has a...
header
If a subclass of an abstract class does not provide implementation code for all the abstract methods of its superclass...
it must also be declared as an abstract class
Abstract methods are declared with the keyword...
abstract
Abstract methods have no...
method body
True or false: It's impossible for an abstract class to have no abstract methods.
False
True or false: An abstract class may or may not have constructors.
True
True or false: No instance can be created for an abstract class.
True
Define interface.
a collection of related abstract methods
What is the opposite of an abstract class?
a concrete class
A concrete class that implements an interface must do what?
implement every method in the interface
If a class does not implement all the methods in an interface, what must happen?
it must be declared abstract
An interface is declared with the _________ keyword.
interface
Interfaces are implemented using what keyword?
implements
True or false: A class that extends a superclass can also implement an interface.
true
When a class extends a superclass AND implements an interface, which clause comes first?
the extends clause
True or false: A class can have just one superclass.
True
True or false: A class can only have one interface.
False
An (interface/abstract class) typically does NOT provide implementations for any of its methods, while an (interface/abstract class) can.
interface; abstract class
What methods of Object are important to know? (2)
toString; equals
What does the default toString() return?
the class name, an @, and the memory address of the object
Why should you redefine the toString method for all of your classes?
to help in debugging
Do Array objects have a toString method?
no
How do you print the elements of an array?
traverse the array and explicitly print each element
When will the equals() method return true?
If the objects being compared reference the same memory slot
If you want the equals() method to test the contents of an object rather than its memory location, you must...
override the method
The default implementation of equals() is equivalent to what operator?
==
What do you use to test objects for equality?
equals()
What is a String object defined as?
a sequence of characters
What does a string literal consist of?
zero or more characters, escape sequences, surrounded by double quotes
String objects are immutable. What does this mean?
There are no methods to change them after they've been constructed.
If String objects are immutable, then how do you mutate them?
create a new String
Why are String objects unusual? (think about initialization)
They can be initialized like a primitive type
What is the concatenation operator?
+
What does the concatenation operator do to two Strings?
combines them into a single String
What does the concatenation operator do with a String and a primitive type?
the primitive type is converted to a String, and the two are combined into one String
How does the compareTo method compare Strings?
according to their position in the ASCII chart, or lexicographical order
What does lexicographical order mean?
the order it comes up in the dictionary
How are characters sorted in the ASCII chart? (out of capitals, lowercase, and digits, in what order do they come?)
digits, capitals, and lowercases
Why shouldn't you use == to test Strings?
it tests whether they're the same reference, not if they're the same string
How do you read the results of a String compareTo method?
If it's less than 0, the String it's called on comes first in the dictionary. If it's greater than 0, the String it's called on comes after. If it's equal to 0, the two Strings are identical.
What does a wrapper class do?
it takes either an existing object or a value of primitive type, "wraps" it in an object, and provides new methods for that type.
Why would you use a wrapper class?
Some methods need the parameters to be objects
What happens when you "wrap" a primitive?
it constructs an object from a single value
What happens when you "unwrap" an object?
it retrieves the primitive value
What is another word for wrapping?
boxing
What does compareTo return in the Integer class?
ex: int compareTo(Integer other)
0 if the Integer is equal to other, negative if it's less than other, and positive if it's greater than other
What does the equals method do in the Integer class?
ex: boolean equals(Object obj)
returns true only if the Integer has the same int value as obj.
When you call the equals method in the Integer class, what happens if the object you use isn't an Integer?
it throws a ClassCastException
What does int length() do?
it returns the length of the String
What happens when you use a negative number when calling the String substring method?
an IndexOutOfBoundsException
What does indexOf do?
it returns the index of the first occurrence of the parameter String within the String it's called on
When using indexOf, what happens when the parameter string isn't in the string the method is called on?
the method returns a -1
What does the Double class do?
it wraps a value of type double in an object
What does the Math class do?
it implements standard mathematical functions
What does the method Math.abs(x) do?
it returns the absolute value of x
What does the method Math.pow(base, exp) do?
Returns base^exp.
What four Math methods are necessary to know for the exam?
abs, pow, sqrt, and random
All of the functions and constants in the Math class are...
static
How do you invoke methods in the Math class?
Math, followed by the dot operator
What is the domain of the result when Math.random() is called?
[0.0,1.0)
True or false: Math.random() can return a random real number from 0.0 up to (but NOT including) 1.0.
True
Provide code to find a random real value in the range [0.0,6.0).
6 * Math.random();
Provide code to find a random real value in the range [2.0,3.0).
Math.random() + 2
Provide code to find a random real value in the range [4.0,6.0).
2 * Math.random() + 4
What is the formula to produce a random real value in the range lowValue<=x<highValue?
(highValue - lowValue) * Math.random() + lowValue;
Provide code to find a random integer in the range [0,99].
(int) (Math.random() * 100)
Provide code to find a random integer in the range [1,100].
(int) (Math.random() * 100) + 1
When did the waterfall model of software development come about?
in the 1960s
Name the 5 steps of the waterfall model.
analysis of the specification, program design, implementation, testing & debugging, and maintenance
Define a specification.
a written description of the project
What is the first step in writing a program?
analyze the specification, make sure you understand it, and clarify with the customer if anything is unclear.
Define program design.
a fairly detailed plan for solving the problem outlined in the specification
What should a proper program design include?
all objects that will be used, the data structures that will implement them, and a detailed list of tasks the program will perform
Why is it necessary to select test data?
because not every possible input can be tested
What is test data?
a set of input values to test the program with
When does a compile-time error occur?
during compilation of the program
What happens during a compile-time error?
the compiler cannot translate the program into bytecode and prints an error
What kind of error is a syntax error?
a compile-time error
What causes a syntax error?
violating the rules of the programming language
When does a run-time error occur?
during the execution of the program
When a run-time error occurs, what exactly throws the exception?
the Java run-time environment
What does it mean when an exception is thrown?
execution stops and an error message is printed
What kind of error is an infinite loop?
a run-time error
What is another way to say logic error?
intent error
What is a logic error?
when the program compiles and runs but the result is wrong
What are the three characteristics of a robust program?
won't give inaccurate answers for input data, won't crash if the input data is invalid, and won't allow execution to proceed if invalid data is entered
What is program maintenance?
upgrading the code as circumstances change?
When did object-oriented programming become the dominant programming methodology?
the mid 1990s
What are the five steps in object-oriented program design?
identify classes to be written, identify behaviors, determine relationships between classes, write the interface for each class, and implement the methods.
When designing a program, how do you identify behaviors?
find useful verbs in the program specification
When designing a program, how do you identify class names?
pick out "big-picture" nouns from the program specification
When designing a program, after you identify necessary methods, what do you do?
group the methods into classes
What is encapsulation?
the process of bundling a group of methods and data fields into a class
After encapsulation, what is the next step of designing a program?
decide what data fields each class needs and what data structures should store them
What two kinds of relationships between classes should you consider when designing a program?
inheritance and composition
What is an inheritance relationship?
is-a; involves subclasses and superclasses
What is a composition relationship?
has-a; involves wrapper classes
When implementing classes, what are the two different kinds of development?
bottom-up and top-down
What does it mean when a class is a collaborator?
classes needed to implement a method
If you're using bottom-up development, what is the first thing you need to do?
for each method in a class, list all the other classes needed to implement that method
What does it mean if you use a bottom-up approach?
independent classes are fully implemented and tested before being incorporated into the overall project
What can you use to test a class as you go?
a dummy Tester class
True or false: When writing a class, constructors and methods should be added and tested one at a time.
True
What is a driver class?
a class that contains a main method that can be used to test the program as you go
What is the purpose of a driver class?
to test the class fully before incorporating it as an object in a completely different class
In what order are classes implemented when using the bottom-up method?
independent classes first, then classes that depend on just one other class, and so on
What is an independent class?
a class whose methods don't need any other class to function
How does a top-down design work?
the programmer starts with an overview of the program, selects the highest-level controlling object and the tasks needed.
If several methods in a class require the same task, you should use...
helper methods
Define procedural abstraction.
the use of helper methods within a class
Define helper methods.
methods that break up larger methods into smaller tasks
Procedural abstraction is an example of...
top-down development
The process of breaking a long method into a sequence of smaller tasks is sometimes known as...
stepwise refinement
Define information hiding.
declaring instance variables and helper methods as private so client classes can't use them
Define a stub method.
a dummy method that is called by another method being tested
What is an algorithm?
a precise step-by-step procedure that solves a problem or achieves a goal
Define a UML diagram.
a tree-like representation of relationship between classes
What is a precondition?
a statement of what is true immediately before execution of that code
What is a postcondition?
a statement of what is true immediately after execution of that code
An efficient algorithm is one that is economical in the use of... (2)
CPU time and memory
What is an array?
a data structure used to implement a list object, where the elements in the list are of the same type
In Java, an array is an object, therefore what keyword is needed when initializing?
new
Once an array has been created, its size is...
fixed
When an array with doubles or ints is declared, what are the values automatically initialized to?
0
When an array with boolean values is declared, what are the values automatically initialize to?
false
When an array with object references is declared, what are the elements automatically initialized to?
null
Small arrays whose values are known can be declared with a(n)...
initializer list
When getting the length of an array, do you use parentheses? Why or why not?
no, because it's not a method
What should you use to traverse elements in an array?
a for loop
When passing an array as a parameter, can you modify it? Why or why not?
Yes, because you're passing its object reference and not a copy
Which can grow and shrink as needed in a program: an array or an ArrayList?
ArrayList
In an ArrayList, the last slot is always...
list.size()-1
Which only needs a single statement to insert or delete elements: an array or an ArrayList?
ArrayList
What is a container class?
a class or data structure that holds other objects
What three characteristics do all container classes share?
they're designed to be memory and run-time efficient, they provide methods for insertion and removal of items, and they provide for iteration over the entire collection
The ArrayList class implements what interface?
List
What five methods of List<E> are important to know?
add, size, get, set, and remove
What does the method boolean add(E obj) do?
it appends obj to the end of the list, and always returns true
What does int size() do?
it returns the number of elements in the list
What does E get(int index) do?
returns the element at the specified index in the list
What does E set(int index, E element) do?
it replaces the item at index with the specified element
What does void add(int index, E element) do?
it inserts element at the specified index
What does E remove(int index) do?
it removes and returns the element at the specified index
What does ArrayList() do?
it constructs an empty list
What is a matrix?
a two-dimensional array
In a 2D array, what does arr.length represent?
the number of rows
In a 2D array, what does arr[i].length represent?
the number of columns
What is the format for a for-each loop? (for an array)
for (type [] name : NameOfArray)
When using a for-each loop to traverse an array, what can you NOT do?
replace the value
What are the three methods of traversing a 2D array?
row-column, for-each loop, and row-by-row array processing
What is a recursive method?
A method that calls itself
What happens when a method actually terminates in a recursive loop?
The program returns to the most recent call
What are the two parts of a recursive method?
a base case and a non-base case
What is a base case?
a termination condition that causes the method to end
What is a nonbase case?
actions that move the algorithm towards termination
True or false: A recursive algorithm cannot have more than one base case.
false
Define tail recursion.
When a method has no pending statements after its recursive call
What is infinite recursion?
when a recursive method has no base case
What error will be thrown if infinite recursion occurs?
StackOverflowError
What happens when there are too many recursive calls?
It can cause memory overflow
When should you use recursion?
When it significantly simplifies the code
Name the two steps of Selection Sorting.
Find the smallest element in the unsorted section of the array, then put that element at the front of the unsorted section.
In selection/insertion sort, for an array of n elements, the array is sorted after how many passes?
n-1
How do you do insertion sort?
You move through the array one element at a time, and sort each element as it comes up.
What is the worst case for insertion sort? Why?
If it's already sorted in reverse order; because it will need the maximum number of moves
What is the best case for insertion sort? Why?
If it's already sorted in increasing order, because it will not need to make any moves.
What sorting method is known as a "search-and-swap" algorithm?
Selection Sort
In what case will selection and insertion sort be inefficient?
large arrays
What sorting method is known as a "divide-and-conquer" approach?
mergesort
What are the four steps in Mergesort?
Break the array in two, mergesort the left half, mergesort the right half, then merge the two subarrays into one.
What is the main disadvantage of mergesort?
It uses a temporary array
True or false: The efficiency of mergesort is unaffected by the initial sorting of the methods.
True
How does sequential search work?
Each element in an array is compared until the key is found
What is the worst case in sequential search?
if every element needs to be compared
What is the best case in sequential search?
if the key is the first element
What SEARCH method is known as a divide-and-conquer approach?
binary
During binary search, what happens if the 'key' value isn't in the array?
a -1 is returned
Binary search only works if...
the array is sorted
What are the four steps to Binary Search?
Divide the array in half, check to see if the middle value is the key, search the former half, and search the latter half.
What is the best case for Binary Search?
if the key is right in the middle
What is the worst case for Binary Search?
the key is not in the list or is at the end of a sublist
;