OCA Java Certification Flashcards
Package statement. What is it, and what are its requirements?
Defines which package a class is in. Must come first, there can only be one. (A comment could theoretically come before)
When is a variable’s scope determined?
Compile time, not runtime.
What is the scope of a class variable?
Anywhere within the class, and from the outside if the access variable allows.
What is the scope of a method/local variable?
Just the method until the method returns. Cannot be accessed from outside.
When does a method parameter get initialized?
When the method is invoked.
What is a block level scope?
Defined by { } and only accessible within the block.
Describe a class declaration
modifier “class” ClassName (extends OneParent) implements Interface1, Interface2 { //body }
What are the required components of a class declaration?
“class” ClassName { //body }
Describe a field declaration
modifier, type, name (E.g. private String stringName)
Describe a method declaration
modifier returnType methodName(DataType inputParameter, DataType inputParameter2) throws ExceptionType { //body }
Describe a constructor declaration
Constructors are used to create an object from the blueprint. They look exactly like a method declaration except there is no return type
What is the access modifier of the default constructor of a class?
The same as the class.
Can a field and a method have the same name?
Yes, but it’s not a good idea.
What are the requirements for a class to be executable?
main class must be public in form: public static void main(String[] args) { //code } OR public static void main(String... args) { //code } File name must match public class name
How do you get from source code to running application?
Save file as .java
Compile it to get the .class file
then run
What does the javac compiler consume and produce?
Consumes source code,
produces bytecode
Main method
public static void main (String[] args) { }
parameter name can be anything. e.g.
public static void main (String[] whatever) { }
What happens if you name your file something different from the declared class name (and this class has a valid main method)?
Compilation failure
When you launch a program from the command line, what must you pass as a first argument?
The name of the main class without any extension (e.g. .java or .class)
If you don’t import a package, how do you get access to a package?
Use a fully qualified name. E.g.
java.util.List
What are the requirements for an import statement?
import statements must be at the beginning of a file just after the package statement (if there is one)
import all with *
java.lang is automatically imported