C482 Software 1: Oracle Certified Associate Java SE 8
What does this line of code do? int num, String value;
Does not compile. You can declare two different types on the same line.
What does the following statement do? import java.nio.file.Paths.*;
Doesn't compile. Wildcards are used for class names NOT method names. In fact, you cannot import methods only classes.
What does the following statement do? import java.nio.*.*;
Doesn't compile. You can only have one wildcard and it must be at the end of the statement.
During the compilation phase Java compiles the .java file into what?
During compilation the .java file is compiled into a .class file that contains the program converted into Java Bytecode.
Where do you put import statements in a file and are they required?
Import statements are not required in a file, but if they are put in, they are put in after the package declaration (if it exists) and if it doesn't exist they are the first lines in the file.
What does the following statements do? import java.util.*; import java.sql.*;
This doesn't compile. It will throw a type Date is ambiguous error because java.util.Date and java.sql.Date both exist. It is important that no conflicting types exist between packages when using the wildcard. You fix this by explicitly sating which one you want to use. For example: import java.util.Date; import java.sql.*;
What is this called? String str;
This is a variable declaration.
What is this called? str = "Hi";
This is a variable initialization.
Which package is automatically imported into Java?
The lang package is automatically imported into Java. Think of it as if the following statement is invisible in every class you write: import java.lang.*
What is the main method?
The main method is a gateway between the startup of a Java process which is managed by the Java Virtual Machine (JVM) and a programmer's code.
What are the members of the class?
The members of a class consist of methods and fields.
What is the octal format and how do you specify numbers in that format?
The octal format is a base 8 format. That is it uses digits 0-7 to represent a number. Numbers in the octal format must be preceded with a 0. For example, 010 would bet the octal form of 8.
If multiple classes are defined in a file which one must be the name of the file?
The public class must share the name of the file it is saved in.
What does the public keyword mean?
The public keyword means that the code in question can be used by another class. You can have public classes and public methods.
What is the purpose of a constructor?
The purpose of a constructor is to initialize fields in an object.
How long are local variables in scope?
Local variables stay in scope from declaration to the end of the block that they were declared in.
What are methods?
Methods (also known as functions or procedures in other programming languages) are sets of code that are named. Those named sets of code can be called by invoking the name. Methods operate on the state of the program.
Where do you place methods and are they required?
Methods go inside of a class, and they are not required.
What is the heap?
The heap is sometimes referred to as the free store and it is a large area of memory allocated to your program for use. There is a limit to the size regardless of how much is available to you.
What is the hexadecimal form and how do you specify numbers in that format?
The hexadecimal format is a base 16 format. It uses numbers 0-9 and letters A-F to represent numbers. Java requires numbers written in this format to have an 0x prefix. For example, 0xA is 10 in decimal and 0x1F is 31 in decimal.
What does the keyword public declare?
The keyword public is what's called an access modifier. It defines the levels of exposure to potential callers of the method. Naturally public means it can be called anywhere in the program.
What does the keyword static mean?
The keyword static binds a method to its class so it can be called just by the class name.
What is the default initialization value of an object reference instance variable?
The default initialization value of an object reference instance variable is null.
How do you write a multiple line comment?
/* multiple * line comment here */
How do you write a javadoc comment?
/** * javadoc multiple line comment * @author Jen and Scott */
How do you write a single line comment?
// comment here
What is a constructor?
A constructor is a special type of method that creates a new object. It's important to note that constructors match the name of the class and DOES NOT have a return type. For example: public class Chick{ public Chick(){ System.out.println("Chirp!"); } } Is how a constructor looks in code.
Where does a Java program begin execution?
A Java program begins execution at the main method.
What is a javadoc comment used for?
A Javadoc comment has a specific structure that the Javadoc tool knows how to read. This structure allows the Javadoc tool to read and generate API documentation for the code.
What is a String?
A String is a value that you can but text into.
What are the ranges for a byte?
A byte is the range from -128 to 127.
Where do you put a class declaration in a file and is it required?
A class declaration is required. It comes after the package statement and the import statements if they are present otherwise it will be the first line of the file.
What is a class?
A class is a program code template used to produce objects. Classes are the building blocks of Java.
What is a code block?
A code block is the code between the braces {}.
What is a comment?
A comment is a line of non-executable code that is used to make code easier to read or to provide valuable information to the programmer.
What is a local variable?
A local variable is a variable defined within a method.
What is a method signature?
A method signature is the full declaration of a method. For example: public int numberOfVisitors(int month) is a method signature for the method numberOfVisitors. It has a return type of int and it takes one parameter, month.
What is a parameter?
A parameter is information that needs to be supplied to the calling method. For example: public void setName(String newName) has the parameter newName, setName is the name of the method, and void is the return type because this method doesn't return anything when called.
What is a reference type?
A reference type refers to an object. Unlike primitive types that hold their values in the memory where the variable is allocated, references do not hold the value of the object they refer to. Instead, a reference "points" to an object by storing the memory address where the object is located, a concept referred to as a pointer.
What is a return type?
A return type tells what a method returns after execution. A return type of int for example would indicate that you should expect an integer to be returned. The return type void indicates that there will be nothing returned by this method.
What is a variable?
A variable is a name for a piece of memory that stores data. For example: int i = 5; The variable in this case is i.
What is API?
API (or application programming interface) is a set of clearly defined methods of communication between software components.
Where are all Java objects stored?
All Java objects are stored in your program's memory heap.
What is an identifier?
An identifier is a sequence of characters that is used to identify a program or an element of that program.
What is an object?
An object is a runtime instance of a class in memory.
Which of the primitive types are used to represent true or false values?
Boolean represents true or false values in Java.
Which data types are used for numbers without decimal points?
Byte, short, int, and long are used for numbers without decimal points.
How long are class variables in scope?
Class variables are in scope until the program ends.
Where do you place field declarations and are they required?
Field declarations go anywhere inside of a class, and they are not required.
What are fields?
Fields are also known as variables and they are used to hold the state of the program.
Which of the primitive types are used to represent decimal values?
Float and double are the primitive types used to represent decimal values.
What is garbage collection?
Garbage collection is the process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program.
What are instance initializers?
Instance initializers are code blocks that appear outside of a method.
How long are instance variables in scope?
Instance variables are in scope until garbage is collected.
What are instance variables (class variables)?
Instance variables are variables that are not defined within a method. This is to say they are not local variables. Instance variables have the keyword static in the declaration.
What happens if you type the following line of code? long max = 3123456789L;
It compiled successfully.
What does javac filename do?
It compiles the file from the command line.
What does this line of code do? double d1; double d2;
It compiles! Remember lines in Java are not determined by whitespace. Java doesn't care about whitespace. The semicolon denotes the end of the line of declaration.
What happens if you type the following line of code? long max = 3123456789;
It does not compile. The number must be followed by an L for the compiler to recognize it as a long.
What does this line of code do? double d1, double d2;
It doesn't compile because you can't declare two different types on the same line, but wait, you might be thinking they are both of type double and you would be right, but you still can't do this as double cannot be explicitly stated twice in the same line as Java still considers it two different types.
What does the following statement do? import java.nio.*;
It doesn't compile. The wildcard can only matches class names not file names.
What does this statement do? import java.util.Random;
It imports the Random class from the util package.
What does this statement do? import java.util.*;
It is a shortcut that imports all the classes in the util package.
What does the following mean? java.lang.ArrayIndexOutOfBoundsException: 1 at Zoo.main(Zoo.java:4)
It means that at line 4 of the Zoo class main argument you have attempted to access an index of an array that doesn't exist.
What does java filename do?
It runs the file*. *It assumes the file has been compiled already.
What are the primitive types?
Java has 8 built in data types referred to as primitive types. They are byte, short, int, long, float, double, char, and boolean.
What is Java compiled into?
Java is compiled into Java bytecode. This allows Java to be platform independent.
Is this a valid identifier? hollywood@vine
No! @ is not a letter, digit, a dollar sign ($), or an underscore (_).
Does this compile? int i = null;
No! Primitive types cannot be assigned a null value.
Is this a valid identifier? 3D
No, identifiers cannot begin with a number.
Do instance variables require that you initialize them?
No, instance variables are by default issued a value when they are declared.
Is this a valid identifier? public
No, it is a reserved word!
Is this a constructor? public void Chick(){}
No, this is not a constructor. Note the void return type. There should be no return type!
Does this compile? int i = 1_000_000.0_;
No, underscores cannot be placed at the end of a literal.
Does this compile? int i = 1_000_000._0;
No, underscores cannot be placed right after a decimal point.
Does this compile? int i = 1_000_000_.0;
No, underscores cannot be placed right before a decimal point.
Does this compile? int i = _1_000_000.0;
No, underscores cannot be places at the start of a literal.
If multiple classes are defined in a file how many can be public?
Only one class in a file can be public.
Where do you put the package declaration in a file and is it required?
Package declarations are not required, but if you put a package declaration in the file then it must be the first line of the file.
What are packages?
Packages are logical groupings of classes.
What are the Java identifier naming rules?
The Java identifier naming rules are that the name must begin with either a letter, a dollar sign ($), or an underscore (_). The subsequent characters can contain numbers and you cannot use the same name as a Java reserved word.
What do the Java identifier rules apply to?
The Java identifier rules apply to everything that you can name! Variables, , methods, classes, and fields are all subjected to the same rules so you don't have to worry about remembering different rules for different things that you can name!
What does String[] args indicate in the main method?
The String[] args statement is a parameter of the main method. It can be written as String[] args, String args[], and String... args. These are all equivalent statements to Java, and it basically means you can read in an array of arguments of type string.
What is the binary form and how do you specify numbers in that format?
The binary format is in base 2. It represents numbers as either a 0 or a 1. Java requires the prefix of 0b to indicate that a number is in binary format. For example, 0b1 is 1 in binary.
What is the default initialization value of the boolean instance variable?
The default initialization value of a boolean instance variable is false.
What is the default initialization value of a byte, short, int, or long instance variable?
The default initialization value of a byte, short, int, or long instance variable is 0.
What is the default initialization value of a char instance variable?
The default initialization value of a char instance variable is '\u0000' (NUL).
What is the default initialization value of a float or double instance variable?
The default initialization value of a float or double instance variable is 0.0.
What does this line of code do? int i1, i2, i3 = 0;
This line of code declares three int values (i1, i2, and i3), but it only initializes one of those values (i3).
How can underscores be used to make numbers more readable as of Java 7?
Underscores can be used in a similar way as commas to separate numbers and make them more legible. For example, int i = 1_000;
Does this compile? String s = null;
Yes! Reference types can be assigned a null value in Java because they are pointers to objects.
Does this compile? int i = 1_000_000.0;
Yes, this compiles.
Can you initialize a variable in the same line as the declaration?
Yes, variable initialization and declaration are allowed in the same line. For example: int i =5; is perfectly valid code.
Is this a valid identifier? _357
Yes.
Where are you not allowed to add underscores to make a number more readable?
You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.
How do you declare a long?
You declare a long by typing long variableName = longNumberL; Note that the L is needed at the end of a long or the compiler will complain. You can use a lowercase l but it is recommended that you always use uppercase because the lowercase l looks too much like the number 1. For example: long max = 3123456789L;
How do you create a new instance of a class?
You put the word new in front of it. For example: Random r = new Random(); This creates a new instance of a Random class and store the location in variable r.
How do you write a basic main method?
You write a basic main method by writing the following line of code: public static void main(String[] args){ // main method goes here! }
How do you declare a package name?
package packageName; declares a package with the name packageName that you can put your package in. If you don't declare a package your code is automatically put in the default package. It is best practice to name your packages to avoid conflicts. You MUST save the class in a file of the same package name.
What is the format for writing a simple Java class?
public class ClassName {}