Ch 1 - Java Building Blocks Flashcards

1
Q

Java classes have two primary building blocks, what are they?

A

methods and fields

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Methods and fields are the ? of the class

A

members

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The full declaration of a method is called a what?

A

method signature

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three types of comments?

A
1. single line:
// comment
2. multi line:
/*
*  comment
*/
3. java doc:
/**
*	java doc comment
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

You can put multiple classes in the same file, but at most ? can be public

A

one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The public class must match what?

A

…match the file name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

true/false: a file can contain multiple classes where none of them is public

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the lack of any access modifier for a class?

A

The default package private modifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the default package private modifier indicate?

A

indicates the class can be accessed only by a class within the same package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give the class signature for the default package private modifer

A

class Defaultclass {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many access modifiers are there for classes?

A

two: public and default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

To compile java code, the file must have an extension of what?

A

.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The result of compiling is a bytecode file with a ? extension.

A

.class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the name of the one special package in java?

A

java.lang

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why is java.lang special?

A

it is special because it is automatically imported.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many wildcards can you have in an import statement?

A

one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

true/false: In an import, a wildcard matches class names

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

true/false: In an import, a wildcard matches sub package class names?

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

true/false: In an import, you can import methods

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

An object is a ? of a class.

A

an instance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

the name of the ? matches the name of the class and there’s no return type.

A

constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Is this a constructor:

public void Chick() { }

A

no, because it has a return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What happens if you do not provide a constructor?

A

The compiler will supply a do nothing default constructor for you.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is an instance initializer block?

A

a code block outside of a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
A code block is any code surrounded with what?
{ }
26
What is the order of initialization?
- static variables and static initializers in order - instance variables and instance initializers in order - constructors
27
Name the eight primitive types
``` byte short int long float double char boolean ```
28
When a number is present in the code, what is it called?
it is called a literal.
29
Why won't this compile: | long max = 3123456789;
Need to include the "L" char at the end of the number.
30
When including the "L" char to indicate a long, does it have to be upper or lower case?
It can be either.
31
What does an octal number use as a prefix?
0. For example: 017
32
What does a hexidecimal number use as a prefix?
the number 0 followed by x or X as a prefix, for example 0xFF
33
What does a binary number use as a prefix?
the number 0 followed by b or B as a prefix, for example 0b10
34
Does this compile: | double d = _1000.00
no
35
Does this compile: | double d = 1000.00_;
no
36
Does this compile: | double d = 1000_.00;
no
37
Does this compile: | double d = 1_00_0.0_0;
yes
38
Can you assign null to a primitive type?
no
39
Can you assign null to a reference type?
yes
40
A reference can be assigned to a new object using the ? keyword.
new
41
Reference types can be used to call methods when they do not point to ?
null
42
Do primitives have methods declared on them?
no
43
All primitive types have what case names?
lowercase
44
All reference type classes that come with Java begin with an ? case letter.
upper
45
Is this declaring or initializing a variable: | String zooName;
declaring
46
Is this declaring or initializing a variable: | int numberAnimals;
declaring
47
Is this declaring or initializing a variable: | zooName = "The Best Zoo";
initializing
48
Is this declaring or initializing a variable: | numberAnimals = 100;
initializing
49
true/false: this code compiles: | String s1, s2;
true
50
true/false: this code compiles: | String s3 = "yes", s4 = "no";
true
51
true/false: this code compiles: | int num, String value;
false
52
true/false, this code compiles: | double d1, double d2;
false
53
You can declare many variables in the same declaration as long as they are all ...
of the same type
54
true/false, this code compiles: | int i1; i2;
false
55
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.
56
``` 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.
57
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.
58
Variables that are not local variables are known as ...
instance or class variables
59
You can tell a variable is a class variable because it has what keyword before it?
static
60
Which variable(s) have default values: instance local class
instance and class
61
``` 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 ```
62
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.
63
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.
64
When are local variables in scope?
in scope from declaration to end of block
65
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 ```
66
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.
67
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.
68
Are objects on the heap?
Objects are always on the heap.
69
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.
70
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.
71
true/false: System.gc() guarantees that garbage collection will be run
false
72
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
73
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