Chapter 1: Java Building Blocks Flashcards

1
Q

Which value type and size for an int?

A

32 bit integral value

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

Which value type for a double?

A

64 floating point value

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

Is this allowed? double d1, double d2;

A

No. If you want to declare multiple values of the same type you cannot repeat the type in front of each variable name. This would work however if coded like this: double d1; double d2;

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

What’s the default value for a boolean?

A

false

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

What values are possible for a boolean?

A

true or false

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

what’s the default value for a char?

A

‘\u0000’ (NUL)

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

Read/Write/Get/Set example

A

String hi = “hi”;//set/write String by = “by”;//set/write String both = hi + by;//set/write and get/read System.out.println(both); //get/read

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

How many primitive types come built in to java?

A

8

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

What can happen if you have a number with many digits (non floating point)? How can you prevent a compiler error for a number like 213123123123?

A

It means a number out of range for an int, even if it is called primitive type long, won’t be registered as a long unless it has an l at the end of it. long max = 312345231233; //does not compile long max - 123312341234L; //now java knows it is a long

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

What does it mean to ‘set’ a variable?

A

To write it

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

Does this work (comparing the two Date classes): import java.util.*; import java.sql.*;

A

no. Compiler error because which Date class to be used is ambiguous. You can fix this by telling it to pick a precedent: import java.util.Date; import java.sql.*; if you really need to use both Date classes you can be explicit with the full name for the class in the application. like table names in sql

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

What is a pointer?

A

A reference type; pointers (references) point to a place in memory where an object is stored.

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

Which of these are valid identifiers? identifier $identifier _identifier __indetifier$$ 3DPointClass hollywood@vine *$Coffee public

A

$identifier - ok _identifier - ok __indetifier$$ - ok 3DPointClass - not ok, identifiers can’t start with a number hollywood@vine - not ok, @ symbol is not a letter, digit, $|_ *$Coffee - not ok, *, not a letter digit $ or _ public - not ok, this is a keyword

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

When do constructors run?

A

After all the fields and instance initializer blocks have run

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

Which value type and size a short?

A

16 bit integral value

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

Does this compile? public int method(){ int y=10; int x; int reply = x+y; return reply; }

A

No, local variables must be initialized or they throw a compile time error.

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

Do objects get garbage collected or references?

A

Objects

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

Which can be assigned null: reference types or primitive types?

A

Reference types

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

When are field and instance initializer blocks run?

A

In the order in which they appear in the file. Fields and instance initializer blocks run before the constructor.

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

does main() always run first?

A

yes

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

What could be consider members of a class?

A

methods and fields

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

Must instance/class variables be initialized by me?

A

no. They are given a default value. Remember, the compiler always wants to keep things as simple as possible : null for an object and 0/false for primitives

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

Can you put two classes in one .java file?

A

Yes, but only one of the classes is allowed to be public.

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

Do local variables have a default value?

A

No.

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

What does it mean to initialize a field?

A

It means to give the variable its first value.

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

What is a reference type?

A

A type of data that refers to an object (which is an instance of a class)

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

What is made when you compile a .java file?

A

Bytecode under the name of .class

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

Why do you need an f on the end of a float?

A

So that Java knows you are using a float instead of a double. You need to do this for longs as well

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

What is a method?

A

An operation that can be called to do something to the state of a program.

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

Where do method declarations go?

A

any where inside a class

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

Is java an interpreted language?

A

Yes, because it compiles into bytecode

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

What’s a key advantage of java over C++?

A

Memory management, C++ requires you to tell it when to collect garbage so it is prone to mistakes

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

What does java do with bits?

A

figure out how much memory it needs to reserve

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

What does the acronym PIC stand for?

A

Package, imports, class. It’s to help with remember the order.

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

How can all references be the same size?

A

References are not the object that they point to, so they will always be the same size.

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

What is a variable?

A

A name for a piece of memory that stores data

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

Can objects be passed to a method or returned from a method?

A

No. Only an object’s references can be passed around

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

How often could the finalize() method run (if put on the class one time

A

0 or 1 times. Never 2

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

What do variables do?

A

Hold the state of the program/make the class unique compared to another instance of the class

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

What does variable scope mean?

A

It refers to the fact that variables only contain data to be passed around for however long the method/class is around. If you are outside of scope you cannot call the method.

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

What is a parameter?

A

It’s a piece of a method. If you call a method that requires a parameter you must pass in a parameter matching the type of that named in the method declaration.

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

What is a method signature? (Three things)

A

The full declaration of a method which includes: 1. Method permeability (private? public? protected?) 2. return type (void? String? Integer?) 3. Parameter list or requirements.

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

What three rules do you have to remember for legal identifiers?

A
  1. The name must start with a letter or the symbol $ or _ 2. Subsequent characters may be numbers 3. You cannot use a java reserved word
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What does platform independent refer too?

A

The fact that java is ‘write once, run everywhere’. The same code gets compiled into the same byte code on every machine.

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

Describe the key words: public void getName()

A

public means it can be called by other classes. void means when the method is called it will not return a value. After the method runs it returns control to whatever class called it.

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

What does the finalize() method do?

A

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. Simply put, this is called before the garbage collection for a particular object. This method only runs if the garbage collector gets called.

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

Explain what’s happening here: String greeting = “How are you?”

A

We’ve created a reference called ‘greeting’ that points to an address where a String object with the data, “How are you?” is stored.

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

Do constructors have return types?

A

no, for example you would never see public void MyClass(){}

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

What are the two ways a value can be assigned to a reference?

A

A reference can be assigned to another object of the same type and a reference can be assigned to a new object using the new keyword

50
Q

What do methods do?

A

They do stuff to the state of the program

51
Q

Where are all java objects stored?

A

In memory aka the heap

52
Q

Does the * slow down your program when importing packages?

A

No, the compiler figures out what it needs. Whether you use this is not is personal preference

53
Q

‘public’ in public class means what?

A

This means the class can be used by other classes.

54
Q

What are instance variables ?

A

these are variables (fields) which has a scope of the entire life of the instantiated class (until garbage collection)

55
Q

What does encapsulation mean?

A

This is about the fact that java has access modifiers which protect data from unintended access and modification.

56
Q

If a file has two classes, can both be something other than public?

A

Yes. The problem would be if both classes were public, this is not allowed.

57
Q

Where do class declarations go?

A

Immediately after the import statement(s)

58
Q

What does System.gc() do?

A

System.gc() kindly asks the sytem to perform a garbage collection. Javadoc says: You can not control how “hard” the garbage collector will work. How the garbage collector work internally is VM-specific and a research topic on its own. But there are usually “full” garbage collection and some smaller “incremental” collection going on. So consider System.gc as a request, but there’s not guaranteed that garbage collection of your object will happen.

59
Q

Where do field declarations go?

A

Anywhere inside a class

60
Q

Which value type and size for a long?

A

64 bit integral value

61
Q

Can primitive types be assigned null?

A

No. You will get a compiler error if you try to assign null to a primitive type. Like an int.

62
Q

Is it Ok to have classes of the same name in the same package?

A

No. If you want to have classes that are named the same in an application they must be in different packages.

63
Q

Do you have to declare a constructor for every class?

A

No, the compiler supplies a ‘do nothing’ default constructor for you.

64
Q

What’s the default value for an object?

A

null

65
Q

What are java classes grouped in?

A

packages

66
Q

What is the scope for class variables (which are always static)?

A

In scope from declaration until the program ends

67
Q

Does Java have to run garbage collection after invoking System.gc()?

A

No. It’s free to ignore the request. this method is more like a suggestion than a command.

68
Q

What’s the purpose of a constructor?

A

To create an object of a class in memory creating a state with initialized fields

69
Q

Are there any primitives that start with a capital letter?

A

No.

70
Q

True or False: Each block (indicated by {}) has it’s own scope

A

True

71
Q

Do objects have names, like public class App? Is this how the object is stored and called?

A

No. They sit on the heap. They are identifiable by their reference.

72
Q

Does this compile? String s1, s2; String s3 = “yes”, s4 = “no”;

A

Yes. You can declare many variables in the same declaration as long as they are all of the same type. You can also initialize any or all of those variables in line.

73
Q

What does garbage collection refer to?

A

The process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program.

74
Q

What does a reference do?

A

references point to an object by storing the memory address where the object is located

75
Q

What needs to happen to a local variable before use?

A

It needs to be initialized

76
Q

What does it mean to ‘get’ a variable?

A

To read it from on place into another

77
Q

what’s the default value for bytes, shorts, ints, and longs?

A

0 (in the bit length of each type)

78
Q

What do you need to compile java into byte code?

A

the jdk

79
Q

What is a local variable?

A

A variable defined within a method

80
Q

what’s the default for a float or a double?

A

0.0 (in the bit length of each type)

81
Q

What can String greeting; point to?

A

This can only point to a reference of type String because of the type.

82
Q

What is an object?

A

an instance of a class created in memory at runtime

83
Q

Which value type for a char?

A

16-bit Unicode value

84
Q

What’s special about java.lang?

A

It’s automatically imported under the hood. Not included in the import list.

85
Q

Before you can use a variable, it needs a ____

A

value

86
Q

What’s one way to know a variable is a class variable?

A

Because these will be static. These are available from the time to the program starts until it ends

87
Q

Does java allow you to learn the physical memory address for an object?

A

no

88
Q

What does the keyword static do?

A

It binds a method/field to the class so it doesn’t require an object to be called/used

89
Q

How long will an object remain on the memory heap?

A

Until it is no longer reachable.

90
Q

Which of these work? import java.nio.*; or import java.nio.file*?

A

import java.nio.* because the wild card only matches class names. Remember, too, you cannot import methods, only class names.

91
Q

Do primitives have methods that can be called?

A

No. Only reference types can have methods

92
Q

Where do import statements go?

A

Immediately after the package declaration

93
Q

What are the two primary elements of a Java class?

A

methods (aka functions) and fields (aka variables)

94
Q

What does it mean to initialize a variable?

A

To give it a value.

95
Q

What is a String?

A

A field/variable we can put text into. String is also a class built into Java for use. It is not a primitive as it is an class that is made into an object in memory at runtime.

96
Q

What are code blocks outside of a method called?

A

instance initializers. if the code block is inside of a method it is not an instance initializer

97
Q

What are the 8 primitive types?

A

boolean byte short int long float double char

98
Q

What’s the scope of method parameters?

A

Their scope extends throughout the entire method

99
Q

Where does a Java program begin execution?

A

At the main() method.

100
Q

Which value type and size for float?

A

32 floating point value

101
Q

What is an identifier in java?

A

The name of a method, variable, class, etc…

102
Q

Which character set does java support?

A

unicode

103
Q

Ho many possible values for a bit?

A

0 or 1

104
Q

What do you need to run java code?

A

just the JRE

105
Q

What do access modifiers do?

A

They declare the class or field method’s level of exposure to potential callers.

106
Q

What is the value range for a byte?

A

-128 to 127

107
Q

Does this compile? double number = 1_000_000;

A

Yes. You are allowed to use underscores except at the beginning, then end, or right before the decimal point. As long as they aren’t there you can put the underscores anywhere you want

108
Q

When are code blocks within a method ran?

A

when the method is called

109
Q

What makes Java secure or safe to run on a machine?

A

The JVM. It creates a sandbox as a place for the code to run keeping it from doing bad things to your machine.

110
Q

What is a reserved word in java?

A

A word that java has reserved so that you can’t use it for something else. like class

111
Q

What’s the scope for instance variables (aka fields)?

A

in scope from the declaration until object garbage collected

112
Q

What does it mean for java to be object oriented?

A

All code is defined in classes and most of them can be instantiated into objects.

113
Q

Can multiple classes be defined in one file?

A

Yes, but only one of the classes can be public and one of the class names (the public one) has to match the name of the file.

114
Q

What two types of data do Java applications have?

A

primitive types and reference types

115
Q

what is the code between {} called

A

code block

116
Q

Does this compile? Why or why not? int num, String value;

A

No. You can declare multiple variables inline but they must be of the same type.

117
Q

What is a constructor?

A

A special type of method called to create an object.

118
Q

Where does the Package declaration go?

A

On the first line of the file. It will fail to compile without it there

119
Q

Which value type and size for a byte?

A

8 bit integral value

120
Q

Under what two conditions is an object no longer reachable by a program?

A
  1. The object no longer has any references pointing to it. 2. All references to the object have gone out of scope.