Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key

image

Play button

image

Play button

image

Progress

1/139

Click to flip

139 Cards in this Set

  • Front
  • Back
How many public classes can you have per source code file?
one
What must the name of the source code file be that contains a public class?
It must match the name of the public class.
How do you make a class a part of a package?
You must have the package statement as the first line in the source code file.
Where do "import" statements go in a source code file?
Between the package statement and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file.
If there are no package or import statements, what must be the first line in the source code file?
the class declaration
Give the 3 access modifiers.
public, protected, private
Give 3 nonaccess modifiers.
strictfp, final, abstract
There are ____ access controls but only ____ access modifiers.
four
three
What is the "default" or "package access"?
What you get when you don't use any of the three access modifiers.
What kind of access can a class have?
"public" or "default" access
How does Sun recommend that you name your packages?
Use reverse domain names, appended with division and/or project names.
What does it mean for a class to have "default" access?
package-level access - a class with default access can be seen only by classes within the same package
What happens when a class in package A tries to access a default class in package B.
You get a compilation error.
What does a class declaration with the "public" keyword do?
Gives all classes from all packages access to the public class.
What do you need to do to access a public class in another package?
You need to import the public class.
Can you use "strictfp" in combination with "abstract" or "final" as class modifiers?
Yes
Can you use both "final" and "abstract" together as class modifiers?
No
What does it mean to mark a class as "strictfp"?
It means that any method code in the class will conform to the IEEE754 standard rules for floating point. Without it, floating points used in the methods might behave in a platform-dependent way.
What does it mean to use the "final" keyword in a class declaration?
It means the class can't be subclassed, i.e. no other class can ever "extend" a "final" class and any attempts to do so will give you a compiler error.
Why would you mark a class as "final"?
You need an absolute guarantee that "none" of the methods in that class will ever be overridden.
Name a class in the Java core libraries that is "final".
the java.lang.String class
A "final" class obliterates a key benefit of OO - _________.
extensibility
Can an "abstract" class be instantiated?
No, you'll get a compiler error
What is an "abstract" class' sole mission in life?
to be extended (subclassed)
How do methods that are marked "abstract" end?
they end in a semicolon rather than curly braces
If a method declaration ends with a semicolon rather than curly braces and it is in a class (as opposed to an interface), how must the method and class be marked?
they must both be marked as "abstract"
If even a single method is "abstract", the whole class must be declared ______.
"abstract"
Can you put nonabstract methods in an abstract class?
Yes
Why can't you mark a class as both "abstact" and "final"?
What happens if you do this?
They have nearly opposite meanings. An abstract class must be subclassed, whereas a final class must not be subclassed.
The code will not compile.
What are the members of a class?
its methods and instance (nonlocal) variables
How many access control levels can the members of a class use?
all four: public, protected, default, and private
If class A itself can't be accessed by class B, can any members of class A be accessed by class B?
no, not even if the member is declared "public".
What does it mean to declare a member of a class "public"?
It means all other classes, regardless of the package they belong to can access the member (assuming the class itself is visible).
If a member of a superclass is declared "public", does a subclass inherit that member?
Yes, regardless of whether both classes are in the same package.
What does the reference "this" refer to?
It always refers to the currently executing object - the object running the code where you see the "this" reference.
What does it mean to see a method invoked (or a variable accessed) without the dot operator (.)?
It means that the method or variable belongs to the class where you see that code. It also means that the method or variable is implicitly being accessed using the "this" reference.
What does it mean for a member of a class to be marked "private"?
It means the member can't be accessed by code in any class other than the class in which the private member was declared.
Can a subclass inherit a private member?
No
Is it good practice to mark an instance variable as "public"?
No, it's nearly always best to keep all instance variables "private" or "protected".
What should a programmer use if an instance variable needs to be changed?
Should use public accessor methods, like "get<propertyName>" or "set<propertyName>"
Can a "private" method be overridden?
No, since a subclass cannot inherit a "private" method.
What is the critical difference between "protected" and "default" members?
A "default" member may be accessed only if the class accessing the member belongs to the same package whereas a "protected" member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
When you think of "default" access, think ______ restriction. But when you think "protected", think _____ + ____.
package
package kids
If a subclass-outside-the-package gets a reference to the superclass, can the subclass use the dot operator on the superclass reference to access a protected member of the superclass?
No, the subclass can only see a "protected" member through inheritance.
Once the subclass-outside-the-package inherits a protected member, that member (as inherited by the subclass) becomes _______ to any code outside the subclass, with the exception of subclasses of the subclass.
private
"default" members are visible only to the subclasses that are in the same ______ as the superclass.
package
Can an access modifier be applied to local variables?
No, you will get a compiler error.
What is the only modifier that can ever be applied to local variables?
final
What does the "final" keyword do for a method?
It prevents a method from being overridden in a subclass, and is often used to enforce the API functionality of a method.
What happens when a subclass tries to override a "final" method of its superclass?
You get a compiler error
Method arguments are essentially the same as _______.
local variables
What must be true of a final argument to a method?
It must keep the same value that the parameter had when it passed into the method.
What is an "abstract" method?
A method that's been declared (as abstract) but not implemented. It has a semicolon instead of curly braces for where the implementation code goes.
What must be true about a class that has an abstract method?
It must be declared "abstract" also.
Why would you mark a method "abstract"?
when you want to force subclasses to provide the implementation
Can you hava an "abstract" class with no "abstract" methods?
Yes
The first concrete subclass of an "abstract" class must do what?
It must implement all abstract methods of the superclass. Any class that extends an abstract class must do this unless the subclass is also abstract.
A method can never be marked as both "abstract" and _____, or "abstract" and _____.
final
private
What must be true about a class that has an abstract method?
It must be declared "abstract" also.
Why would you mark a method "abstract"?
when you want to force subclasses to provide the implementation
Can you hava an "abstract" class with no "abstract" methods?
Yes
The first concrete subclass of an "abstract" class must do what?
It must implement all abstract methods of the superclass. Any class that extends an abstract class must do this unless the subclass is also abstract.
A method can never be marked as both "abstract" and _____, or "abstract" and _____. Why?
final
private
"abstract" methods must be implemented(which essentially means overridden by a subclass) whereas "final" and "private" methods cannot ever be overridden
"abstract" methods say nothing about the implementation, so they cannot be marked as _____, _____, or ______.
synchronized
strictfp
native
Can you combine the "abstract" modifier with the "static" modifier?
No
What does the "synchronized" keyword on a method indicate?
It indicates that a method can be assessed by only one thread at a time.
Can the "synchronized" modifier be applied to variables or classes?
No - only to methods
Can you use "synchronized" with "abstract"?
No, since sychronization is an implementation issue.
Can you combine "synchronized" with "final"?
Yes
What does the "native" modifier indicate about a method?
that a method is implemented in a platform-dependent language, such as C
"native" can never be combined with _____, and "native" can only be applied to _____.
abstract
methods
What is a "native" method's body?
must be a ";" (like "abstract" methods), indicating that the implemtation is omitted
What can "strictfp" modify?
a class or nonabstract methods, a variable can never be declared "strictfp"
What does "strictfp" do?
It forces floating points (and any floating-point operations) to adhere to the IEEE754 standard.
Where are "instance variables" defined and when are they initialized?
They are defined inside the class, but outside of any method, and are only initialzed when the class is instantiated. They are the "fields" that belong to each unique object.
What 2 non-access modifiers can be used with "instance variables"?
"final" and "transient"
What 4 non-access modifiers cannot be used with "instance variables"?
abstract
synchronized
strictfp
native
Where are local variables declared and where do they live?
They are declared within a method and they live on the stack.
If a local variable is an object reference, where will the object be created?
On the heap, there is no such thing as a stack "object", only a stack "variable"
local variables can't use most of the modifiers that can be applied to instance variables but they can be marked ____.
final
What must be done before a local variable can be used?
It must be initialized. local variables don't get default values.
What is it called when a local variable is declared with the same name as an instance variable?
shadowing
What does it mean to declare a variable "final"?
It makes it impossible to reinitialize that variable once it has been initialized with an explicit value.
What does it mean to mark an object reference variable final?
It can't ever be reassigned to refer to a different object. However, the data within the object can be modified.
What does a reference variable hold?
bits that represent, in a platform-dependent format, a way to get to an object
What must be done if you declare a "final" instance variable by the time the constructor completes?
you're obligated to give it an explicit value.
Note: a final variable isn't given a default value.
If you declare an instance variable "final", but don't give it an explicit value at the time you declare it, the variable is considered a _____ final.
blank
It can stay blank only until the constructor completes
Variables declared in an interface are always implicitly _____.
final
Interface variables are always implicitly ____ _____ _____.
public static final
What does it mean to mark an instance variable as "transient"?
you're telling the JVM to skip (ignore) this variable when you attempt to serialize the object declaring it
The keyword "transient" can only be applied to _______.
instance variables
What is serialization?
It lets you save (sometimes called "flatten") an object by writing its state (in other words its instance variables) to a special type of IO stream.
What does the "volatile" modifier do?
it tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory. It can only be applied to instance variables
What do "static" variables and methods belong to?
the class rather than to any particular instance. You can use a static method or variable without having any instances of that class at all.
How many copies are there of a "static" variable?
one, it will be shared by all instances of the class
When is a "static" instance variable set to zero?
When the class it is in is first loaded by the JVM before any instances are created.
Making the method called by the JVM (main()) a static method means what?
the JVM doesn't have to create an instance of your class just to start running code.
Can you access an instance variable (non-static) from the static main() method?
No, main() doesn't know anything about any instances so it can't access the variables. You will get a compile error.
A "static" method of a class can't access a ______ member of
its own class.
non-static (instance) - method or variable
How do we access a "static" method (or "static" variable)?
Use the dot operator on the class name, as opposed to on a reference to an instance.
Note: however, you are allowed to use an object reference variable to access a static member.
What are 3 things you can mark as "static"?
methods, variables, top-level nested classes
What are 6 things you cannot mark as "static"?
constructors
classes
interfaces
inner classes
inner class methods and instance variables
local variables
How many package statements can there be per source code file?
one
If there is no "public" class in your source code file, how can you name your file?
you can name it whatever you like
Does the order of the classes in a source code file matter?
No
When would you group several classes into a single source code file?
when those classes should only be used together as one component
Do package and import declarations apply to all classes in a source code file?
Yes
When you put a class in a package (through the package declaration) you essentially give the class a longer name, which we call the _______________.
fully qualified name
What are the 2 ways you can eliminate confusion about which class you want in your code?
1. Use an "import" statement
2. Use the fully qualified name throughout your code
What is the order in which the compiler resolves imports?
Explicit imports are resolved first, then the classes from the current package, and last - the implicit imports (wildcard package imports)
What do you have to do if you want to use two classes with the same name from two different packages?
you have to use the fully qualified names
List 5 rules that apply to the "main()" method.
1. It must be marked "static"
2. It must have a "void" return type
3. It must have a single String array argument
4. You can name the argument anything you like
5. It should be declared "public" and for the exam, assume it must be "public"
If the JVM can't find you "main()" method, what happens?
You get a runtime error - the NoSuchMethodError
What must the signature of your "main()" method look like if you want the JVM to run your class?
public static void main(String[] args) {}
otherwise you get a runtime error
Note: you can write "static public" instead of "public static" and you can give the argument "args" a different name
What is the signature of the one and only java.lang.Runnable interface method?
public void run(){}
When you create an interface, you're defining a ______ for what a class can do.
contract
Can interfaces be implemented by any class, from any inheritance tree?
Yes - this lets you take radically different classes and give them a common characteristic.
Can an interface have both abstract and nonabstract methods?
No - only abstract methods so they must end with a semicolon rather than curly braces
All interface methods are implicitly ______ and ______.
public
abstract
Can interface methods be static?
No
Do you need to type the "public" or "abstract" modifiers in the method declarations of interface methods?
No
All variables defined in an interface must be _____, _____,
and _____.
public, static, and final - in other words they must be constants, not instance variables
Because interface methods are abstract, they cannot be marked ____, _____, ______, or _____.
final, native, strictfp, synchronized
Can an interface method extend one or more other interfaces?
Yes
What is the only thing that an interface can extend?
another interface
Can an interface implement another interface or class?
No
What keyword must you use to declare an interface?
"interface"
Can interface types be used polymorphically?
Yes
Is the "abstract" modifier required in an interface declaration?
No - interfaces are implicitly abstract
When is the "public" modifier required on a interface declaration?
When you want the interface to have public rather than default access.
Do you need to explicitly declare interface constants as "public", "static", and "final"?
No
Can an interface variable be given a value by the implementing class?
No - they are final
If you create a class that implements the "Runnable" interface, what must you provide?
the "public void run()" method
If a class implements an interface, what must it do?
It must have an implementation for each method in the interface
What 4 things must a nonabstract implementation class do to be a legal implementation class?
1. Provide concrete(nonabstract) implementation for all methods from the declared interface
2. Follow all the rules for legal overrides
3. Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method.
4. Maintain the signature of the interface method and maintain the same return type (but does not have to declare the exceptions declared in the interface method declaration).
Can an implementation class be abstract?
Yes - then it doesn't need to provide the implementation methods but can pass the buck to its first concrete class
You can extend only one _____ but ______ many interfaces.
class
implement
Can an interface extend another interface?
Yes - the subinterface simply adds more requirements to the contract of the superinterface.
Can an interface extend more than one interface?
Yes