Java Flashcards

1
Q

things an object knows about itself

A

instance variable

represents object’s state

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

things an object can do

A

methods

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

blueprint for an object

A

class

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

class describes what an object ………… and what an object ……….

A

knows

does

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

a method uses ……….. A caller passes ……….

A

parameter

arguments

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

java is pass-by-………..

A

value

that means pass-by-copy

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

getters and setters

A

accessors and mutators

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

access modifiers

A

public

private

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

hide data

A

private

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

declared inside class

A

instance variables

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

declared inside method

A

local variable

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

local variables …………. initialized before use

A

must be

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

check if two objects are equal

A

.equals()

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

to compare two primitives

A

==

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

a class can have any number of

A

instance variables

getter setter methods

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

a method can have only one

A

return

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

these help create encapsulation

A

getter
setter
private
public

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

convert String to int

A

Integer.parseInt(string);

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

iterate in array (enhanced for loop)

A

for(int cell : locationCells){……….}

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

get array length

A

array.length

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

Math.random()

A

returns a random number from 0 to just less than 1

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

while loop is good

A

when you don’t know how many times to loop and just want to keep going while some condition is true

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

ArrayList

A

add(Object elem) - adds elem to the list
remove(int index), remove(Object elem)
contains(Object elem) - returns true if there is a match for the object parameter
isEmpty()
indexOf(Object elem) - returns either the index of the object parameter or -1
size()
get(int index)

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

ArrayList over arrays

A

size changes dynamically
can remove elements
ArrayList is less efficient

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
in order to put primitives in ArrayList
``` wrap using primitive wrapper class unwrapping happens automatically when taking out the primitive from ArrayList ```
26
............ are faster to hold primitives
arrays | ArrayList has some extra load due to wrapping and unwrapping
27
new String[2];
new ArrayList();
28
short circuit operators
&&, ||
29
non short circuit operators
&, |
30
classes are grouped into
packages
31
packages importan
help the overall organization of a project or library give you a name scoping provide a level of security
32
java.lang
need not to be imported | preimported
33
import is not the same as ........... in C
include
34
............. extends ................
``` subclass supeclass ```
35
add more stuff to super class method
public void method_name(){ super.method_name(); ................ }
36
superclass can choose whether or not it wants a subclass to inherit a particular member by the level of access the particular member is given
private - not inherited default protected public - inheritied
37
inherited methods can be overriden
instance variables cannot be overriden
38
by using inheritance
get rid of duplicate code by abstracting out the behavior common to a group of classes, and sticking that code in a superclass
39
with polymorphism, the reference type can be a .........
superclass of the actual object type
40
polymorphism makes use of
inheritance
41
with polymorphism
can write code that doesn't have to change when you introduce new subclass polymorphic arrays polymorphic arguments polymorphic return types
42
non-public class can be subclassed only by
classes in the same package
43
what if i don't declare a class as public
then it is a non-public class
44
final modifier
end of the inheritance line | can't extend a final class
45
can't extend a class which has only ........... constructor
private
46
if you want to protect a specific method from being overridden, mark the method with final modifier
mark the whole class as final if you want to guarantee that none of the methods in that class will ever be overridden
47
in order to overide
arguments must be same return types must be compatible the methods can't be less accessible
48
method overloading
having two methods with the same name but different argument list
49
in order to overload
return types can be different can't change only the return type can vary access levels in any directions
50
overloaded method has nothing to do with ........
inheritance
51
abstract class
a class that can't be instantiated
52
interface
100% abstract class
53
making a class abstract
abstract class Class_name{.....}
54
abstract class means
it must be extended
55
abstract method means
it must be overridden
56
creating an abstract method
public abstract void method_name(); | no curly braces
57
if you declare an abstract method
you must mark the class abstract well
58
you must implement all .......... methods
``` abstract the first concrete class in the inheritance tree must implement all the abstract methods ```
59
every class we write extends
``` Object any class that doesn't explicitly extend another class, implicitly extends Object ```
60
Object class
``` boolean equlas() Class getClass() int hashCode() String toString() ```
61
is class Object abstrac
no got method implementation code that all classes can inherit and use out-of-the-box, without having to overriide the methods
62
you are encouraged to override these methods in Object class
hashCode() equals() toString()
63
most common use of an instance of type Object
thread synchronization
64
mulitple inheritance problem
deadly diamond of death
65
to define an interface
public interface Interface_name{.....}
66
to implement an abstract class
public class Child_class extends Super_class{.....}
67
to implement an interface
public class Child_class extends Super_class implements Interface{..............}
68
save object's state to a file
implement Serializable
69
make methods run in separate thread of execution
implement Runnable
70
``` a java class can have only one parent (super class - extends) parent class define who you are ```
can implement multiple interfaces | interfaces define roles you can play
71
subclass
more specific version of a class and need to override or add new behaviors
72
use abstract class
when you want to define a template for a group of subclasses you have at least some implementation code that all subclasses could use when you want to gurantee that nobody can make objects of that type
73
use an interface
when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree
74
all interface methods are implicitly .......... and ..........
public | abstract
75
stack frame
holds the state of the method including which line of code is executing, and the values of all local variables
76
if the local variable is a reference to an object
only the variable (the reference) goes on the stack
77
java has two areas of memory we care about
Stack | Heap
78
instance variable lives in
heap | inside the object
79
constructor
has code that runs when you instantiate an object
80
java lets you declare a method with the same name as your class
if it doesn't have a return type then it is a constructor
81
pirvate constructors are inherited
false
82
if you write a constructor that takes arguments, and you still want a no-arg constructor,
you'll have to build the no-arg constructor yourself
83
to compile each constructor must have a .......... argument list
different
84
a constructor is the code that runs when you say ..... on a class type
new
85
you can use constructor to initialize the state of the object being constructed
i.e the instance variables
86
always provide a no-arg constructor if you can, to make it easy for programmers to make a working object
supply default values
87
subclass' constructor is invoked first
but it is the superclass constructor that finishes first
88
...... calls the super constructor
super()
89
compiler puts in a call to super() if you don't
by default it calls no arg constructor of the super class
90
the superclass parts of an object have to be fully-formed (completely built) before the subclass parts can be constructed
subclass object might depend n things it inherits from the superclass, so it's important that those inherited things be finished
91
the call to ............. must be the first statement in each constructor
super()
92
a reference to the current object
this
93
you can say this() only within a ................... | and it must be the first statement
constructor
94
every constructor can have a call to .......... or .............. but never both
super() | this()
95
use .......... to call a constructor from another overloaded constructor in the same class
this()
96
an object is alive as long as
there are live references to it
97
when an object's last reference disappears
1. the reference goes out of scope, permanently - method ends 2. the reference is assigned to another object 3. the reference is explicitly set to null
98
if you use the dot operator on a null reference you'll get a .......... at run time
NullPointerException
99
static class never affected by ............. variables
instance | static methods never use instance variable values
100
Math constructor is marked as .............
private
101
lets a method run without any instance of the class
static
102
call a static method using ............ name
class
103
call a non-static method using .............. .......... name
reference variable
104
the only way to get new object
``` new or deserialization (or something called the Java Reflection API) ```
105
static methods .............. use non-static (instance) variables
CAN'T
106
static methods .............. use non-static methods
CAN'T
107
static variable
value is the same for all instances of the class
108
static variable is initialized
``` only when the class is first loaded not each time a new instance is made ```
109
static variables are .............
``` shared all instances of the same class share a single copy of the static variable ```
110
static variables in a class are initialized before any object of that class can be created
static variables in a class are initialized before any static method of the class runs
111
static final variables are
constants
112
a variable marked final means
once initialized it can never change
113
............. is a block of code that runs when a class is loaded, before any other code can use the class, so it's a great place to initialize a static final variable
``` static initializer clas Foo{ final static int x; static{x=42;} } ```
114
if you don't give a value to a final variable
the compiler will catch it
115
final variable means
you can't change its value
116
final method means
you can't override the method
117
final class means
you can't extend the class
118
if the class is final
you don't need to mark the methods final
119
static method is good for
utility method that does not (and will never) depend on a particular instance variable value
120
when you need to treat a primitive like an object
wrap it
121
put integer in ArrayList
``` ArrayList list = new ArrayLIst(); list.add(3); int num = list.get(0); ```
122
converting a string to a primitive value
Integer.parseInt(string);
123
out in System.out is
a static variable
124
inner class can use all the methods and variables of the outer class
even the private ones
125
a ............. is an object that represents a network connection between two machines
socket
126
what is a connection
a relationship between two machines, where two pieces of software know about each other.
127
to make a socket connection you need to know
ip address | tcp port number
128
a socket connection means
the two machines have information about each other including ip address and tcp port
129
a tcp port is just a number
a 16-bit number that identifies a specific program on the server
130
without port number
the server would have no way of knowing which application a client wanted to connect to
131
a server can have up to ..................... different server apps running, one per port
65536
132
the tcp port numbers from 0 to 1023 are reserved for well-known services
don't use them for your own server programs
133
if you try to bind a program to a port that is already in use
you'll get a BindException
134
to communicate over a socket connection we use
streams
135
ip address of localhost
127.0.0.1
136
............... is a bridge between low level byte stream (like the one coming from the socket) and a high level character stream (like the BufferedReader)
InputStreamReader
137
client and server communicate over a ............. connection
socket
138
unless you have multiple processors on your computer, each new Java thread is not actually a separate process running on the OS
but it almost feels as though it is
139
by creating a new Thread object, you've launched a separate thread of execution, with its very own call stack but as it has nothing to do virtually it dies as it born
``` Thread t = new Thread(); t.start(); ```
140
multiple threading in Java means we have to look at both ........... and ....... that's run by the thread
thread | job
141
every java application starts up a .......... thread
main | the thread that puts main method at the bottom of the stack
142
Thread class
``` void join() void start() static void sleep() ```
143
thread's job
Runnable object
144
thread object - worker
runnable - thread job
145
to go from having just a Thread instance to having a new thread of execution you must call
thread.start();
146
all the methods in an interface are
public
147
once the thread becomes runnable, it can move back and forth between
runnable running blocked
148
blocked
sleeping waiting for another thread to finish waiting for an object's lock
149
scheduler implementations are different for different JVMs
even running the same program on the same machine can give you different results
150
every thread in java has its own .............
call stack
151
sleep method throws an InterruptedException | so all calls to sleep must be wrapped in a try/catch
try{Thread.sleep(2000); }catch(InterruptException ex){ ex.printStackTrace(); }
152
when the thread wakes up, it always goes back to ................. state
runnable
153
threads can lead to concurrency issues
concurrency issues lead to race conditions | race conditions lead to data corruption
154
the ................ keyword means that a thread needs a key in order to access the synchronized code
synchronized
155
when code hits a synchronized method
there is going to be a performance hit synchronization restricts concurrency synchronized methods can lead to deadlock
156
names used for classes, variables and methods are called
identifiers
157
access modifiers
default public protected private
158
non-access modifires
final abstract strictfp
159
enum
restrict a variable to have one of only a few predefined values enum FreshJuiceSize{SMALL, MEDIUM, LARGE}
160
visible to the package
default - no modifiers are needed
161
visible to the class only
private
162
visible to the world
public
163
visible to the package and all subclasses
protected
164
final
is used to apply restrictions on class, method and variable
165
finally
used to place important code, it will be executed whether exception is handled or not it is a block
166
finalize
is a method | used to perform clean up processing just before object is garbage collected