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
Q

A code block is any code surrounded with what?

A

{ }

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

What is the order of initialization?

A
  • static variables and static initializers in order
  • instance variables and instance initializers in order
  • constructors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Name the eight primitive types

A
byte
short
int
long
float
double
char
boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

When a number is present in the code, what is it called?

A

it is called a literal.

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

Why won’t this compile:

long max = 3123456789;

A

Need to include the “L” char at the end of the number.

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

When including the “L” char to indicate a long, does it have to be upper or lower case?

A

It can be either.

31
Q

What does an octal number use as a prefix?

A
  1. For example: 017
32
Q

What does a hexidecimal number use as a prefix?

A

the number 0 followed by x or X as a prefix, for example 0xFF

33
Q

What does a binary number use as a prefix?

A

the number 0 followed by b or B as a prefix, for example 0b10

34
Q

Does this compile:

double d = _1000.00

A

no

35
Q

Does this compile:

double d = 1000.00_;

A

no

36
Q

Does this compile:

double d = 1000_.00;

A

no

37
Q

Does this compile:

double d = 1_00_0.0_0;

A

yes

38
Q

Can you assign null to a primitive type?

A

no

39
Q

Can you assign null to a reference type?

A

yes

40
Q

A reference can be assigned to a new object using the ? keyword.

A

new

41
Q

Reference types can be used to call methods when they do not point to ?

A

null

42
Q

Do primitives have methods declared on them?

A

no

43
Q

All primitive types have what case names?

A

lowercase

44
Q

All reference type classes that come with Java begin with an ? case letter.

A

upper

45
Q

Is this declaring or initializing a variable:

String zooName;

A

declaring

46
Q

Is this declaring or initializing a variable:

int numberAnimals;

A

declaring

47
Q

Is this declaring or initializing a variable:

zooName = “The Best Zoo”;

A

initializing

48
Q

Is this declaring or initializing a variable:

numberAnimals = 100;

A

initializing

49
Q

true/false: this code compiles:

String s1, s2;

A

true

50
Q

true/false: this code compiles:

String s3 = “yes”, s4 = “no”;

A

true

51
Q

true/false: this code compiles:

int num, String value;

A

false

52
Q

true/false, this code compiles:

double d1, double d2;

A

false

53
Q

You can declare many variables in the same declaration as long as they are all …

A

of the same type

54
Q

true/false, this code compiles:

int i1; i2;

A

false

55
Q

Name the rules for identifiers

A
  • 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
Q
true/false, this code compiles:
public int mymethod() {
	int y = 10;
	int x;
	int reply = x + y;
	return reply
}
A

false. int x isn’t initialized.

57
Q

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);
}
A

false. int onlyOneBranch has not been initialized.

58
Q

Variables that are not local variables are known as …

A

instance or class variables

59
Q

You can tell a variable is a class variable because it has what keyword before it?

A

static

60
Q

Which variable(s) have default values:
instance
local
class

A

instance and class

61
Q
Give the default initialization values for the given variable types:
boolean
byte, short, int, long
float, double
all object references
A
false
0 (in the type's bit length)
0.0 (in the type's bit length)
null
62
Q

When are instance variables in scope?

A

instance variables are in scope as soon as they are defined and last for the entire lifetime of the object itself.

63
Q

When are static variables in scope?

A

static variables go into scope when declared, and they stay in scope for the entire lifetime of the program.

64
Q

When are local variables in scope?

A

in scope from declaration to end of block

65
Q

Name the ordering of elements in a class

A
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
Q

What is the heap?

A

The heap, which is also known as the free store, represents a large pool of unused memory allocated to your Java application.

67
Q

Is a reference on the heap?

A

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
Q

Are objects on the heap?

A

Objects are always on the heap.

69
Q

What is garbage collection?

A

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
Q

What is the purpose of System.gc()?

A

It suggests that now might be a good time for Java to kick off a garbage collection run.

71
Q

true/false: System.gc() guarantees that garbage collection will be run

A

false

72
Q

true/false: finalize() will get called and could get called more than once

A

false, it might not get called and it definitely will not get called twice

73
Q

Name some of the benefits of java

A
  • Object Oriented
  • Encapsulation
  • Platform Independent - if you get asked on the exam, all classes run everywhere.
  • Robust
  • Simple
  • Secure