Ch 1 - Java Building Blocks Flashcards
Java classes have two primary building blocks, what are they?
methods and fields
Methods and fields are the ? of the class
members
The full declaration of a method is called a what?
method signature
What are the three types of comments?
1. single line: // comment 2. multi line: /* * comment */ 3. java doc: /** * java doc comment */
You can put multiple classes in the same file, but at most ? can be public
one
The public class must match what?
…match the file name
true/false: a file can contain multiple classes where none of them is public
true
What is the lack of any access modifier for a class?
The default package private modifier
What does the default package private modifier indicate?
indicates the class can be accessed only by a class within the same package.
Give the class signature for the default package private modifer
class Defaultclass {}
How many access modifiers are there for classes?
two: public and default
To compile java code, the file must have an extension of what?
.java
The result of compiling is a bytecode file with a ? extension.
.class
What is the name of the one special package in java?
java.lang
Why is java.lang special?
it is special because it is automatically imported.
How many wildcards can you have in an import statement?
one
true/false: In an import, a wildcard matches class names
true
true/false: In an import, a wildcard matches sub package class names?
false
true/false: In an import, you can import methods
false
An object is a ? of a class.
an instance
the name of the ? matches the name of the class and there’s no return type.
constructor
Is this a constructor:
public void Chick() { }
no, because it has a return type
What happens if you do not provide a constructor?
The compiler will supply a do nothing default constructor for you.
What is an instance initializer block?
a code block outside of a method.
A code block is any code surrounded with what?
{ }
What is the order of initialization?
- static variables and static initializers in order
- instance variables and instance initializers in order
- constructors
Name the eight primitive types
byte short int long float double char boolean
When a number is present in the code, what is it called?
it is called a literal.
Why won’t this compile:
long max = 3123456789;
Need to include the “L” char at the end of the number.
When including the “L” char to indicate a long, does it have to be upper or lower case?
It can be either.
What does an octal number use as a prefix?
- For example: 017
What does a hexidecimal number use as a prefix?
the number 0 followed by x or X as a prefix, for example 0xFF
What does a binary number use as a prefix?
the number 0 followed by b or B as a prefix, for example 0b10
Does this compile:
double d = _1000.00
no
Does this compile:
double d = 1000.00_;
no
Does this compile:
double d = 1000_.00;
no
Does this compile:
double d = 1_00_0.0_0;
yes
Can you assign null to a primitive type?
no
Can you assign null to a reference type?
yes
A reference can be assigned to a new object using the ? keyword.
new
Reference types can be used to call methods when they do not point to ?
null
Do primitives have methods declared on them?
no
All primitive types have what case names?
lowercase
All reference type classes that come with Java begin with an ? case letter.
upper
Is this declaring or initializing a variable:
String zooName;
declaring
Is this declaring or initializing a variable:
int numberAnimals;
declaring
Is this declaring or initializing a variable:
zooName = “The Best Zoo”;
initializing
Is this declaring or initializing a variable:
numberAnimals = 100;
initializing
true/false: this code compiles:
String s1, s2;
true
true/false: this code compiles:
String s3 = “yes”, s4 = “no”;
true
true/false: this code compiles:
int num, String value;
false
true/false, this code compiles:
double d1, double d2;
false
You can declare many variables in the same declaration as long as they are all …
of the same type
true/false, this code compiles:
int i1; i2;
false
Name the rules for identifiers
- must begin with a letter or the symbol $ or _
- subsequent characters may also be numbers
- You cannot use the same name as a Java reserved word. Java is case sensitive, so you can use versions of the keywords that only differ in case.
true/false, this code compiles: public int mymethod() { int y = 10; int x; int reply = x + y; return reply }
false. int x isn’t initialized.
true/false, this code compiles:
public void findAnswer(boolean check) {
int answer; int onlyOneBranch; if(check) { onlyOneBranch = 1; answer = 1; } else { answer = 2; } System.out.println(answer); System.out.println(onlyOneBranch); }
false. int onlyOneBranch has not been initialized.
Variables that are not local variables are known as …
instance or class variables
You can tell a variable is a class variable because it has what keyword before it?
static
Which variable(s) have default values:
instance
local
class
instance and class
Give the default initialization values for the given variable types: boolean byte, short, int, long float, double all object references
false 0 (in the type's bit length) 0.0 (in the type's bit length) null
When are instance variables in scope?
instance variables are in scope as soon as they are defined and last for the entire lifetime of the object itself.
When are static variables in scope?
static variables go into scope when declared, and they stay in scope for the entire lifetime of the program.
When are local variables in scope?
in scope from declaration to end of block
Name the ordering of elements in a class
package declaration - first line in a file imports - immediately after package definition class definition - immediately after the imports field declarations - anywhere inside a class method declarations - anywhere inside a class
What is the heap?
The heap, which is also known as the free store, represents a large pool of unused memory allocated to your Java application.
Is a reference on the heap?
A reference may or may not be created on the heap. All references are the same size, no matter what their data type is and are accessed by their variable name.
Are objects on the heap?
Objects are always on the heap.
What is garbage collection?
Garbage collection refers to the process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program.
What is the purpose of System.gc()?
It suggests that now might be a good time for Java to kick off a garbage collection run.
true/false: System.gc() guarantees that garbage collection will be run
false
true/false: finalize() will get called and could get called more than once
false, it might not get called and it definitely will not get called twice
Name some of the benefits of java
- Object Oriented
- Encapsulation
- Platform Independent - if you get asked on the exam, all classes run everywhere.
- Robust
- Simple
- Secure