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
Q

in order to put primitives in ArrayList

A
wrap using primitive wrapper class
unwrapping happens automatically when taking out the primitive from ArrayList
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

………… are faster to hold primitives

A

arrays

ArrayList has some extra load due to wrapping and unwrapping

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

new String[2];

A

new ArrayList();

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

short circuit operators

A

&&, ||

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

non short circuit operators

A

&, |

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

classes are grouped into

A

packages

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

packages importan

A

help the overall organization of a project or library
give you a name scoping
provide a level of security

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

java.lang

A

need not to be imported

preimported

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

import is not the same as ……….. in C

A

include

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

…………. extends …………….

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

add more stuff to super class method

A

public void method_name(){
super.method_name();
…………….
}

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

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

A

private - not inherited
default
protected
public - inheritied

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

inherited methods can be overriden

A

instance variables cannot be overriden

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

by using inheritance

A

get rid of duplicate code by abstracting out the behavior common to a group of classes, and sticking that code in a superclass

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

with polymorphism, the reference type can be a ………

A

superclass of the actual object type

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

polymorphism makes use of

A

inheritance

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

with polymorphism

A

can write code that doesn’t have to change when you introduce new subclass
polymorphic arrays
polymorphic arguments
polymorphic return types

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

non-public class can be subclassed only by

A

classes in the same package

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

what if i don’t declare a class as public

A

then it is a non-public class

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

final modifier

A

end of the inheritance line

can’t extend a final class

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

can’t extend a class which has only ……….. constructor

A

private

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

if you want to protect a specific method from being overridden, mark the method with final modifier

A

mark the whole class as final if you want to guarantee that none of the methods in that class will ever be overridden

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

in order to overide

A

arguments must be same
return types must be compatible
the methods can’t be less accessible

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

method overloading

A

having two methods with the same name but different argument list

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

in order to overload

A

return types can be different
can’t change only the return type
can vary access levels in any directions

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

overloaded method has nothing to do with ……..

A

inheritance

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

abstract class

A

a class that can’t be instantiated

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

interface

A

100% abstract class

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

making a class abstract

A

abstract class Class_name{…..}

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

abstract class means

A

it must be extended

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

abstract method means

A

it must be overridden

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

creating an abstract method

A

public abstract void method_name();

no curly braces

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

if you declare an abstract method

A

you must mark the class abstract well

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

you must implement all ………. methods

A
abstract
the first concrete class in the inheritance tree must implement all the abstract methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

every class we write extends

A
Object
any class that doesn't explicitly extend another class, implicitly extends Object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

Object class

A
boolean equlas()
Class getClass()
int hashCode()
String toString()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

is class Object abstrac

A

no
got method implementation code that all classes can inherit and use out-of-the-box, without having to overriide the methods

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

you are encouraged to override these methods in Object class

A

hashCode()
equals()
toString()

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

most common use of an instance of type Object

A

thread synchronization

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

mulitple inheritance problem

A

deadly diamond of death

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

to define an interface

A

public interface Interface_name{…..}

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

to implement an abstract class

A

public class Child_class extends Super_class{…..}

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

to implement an interface

A

public class Child_class extends Super_class implements Interface{…………..}

68
Q

save object’s state to a file

A

implement Serializable

69
Q

make methods run in separate thread of execution

A

implement Runnable

70
Q
a java class can have only one parent (super class - extends)
parent class define who you are
A

can implement multiple interfaces

interfaces define roles you can play

71
Q

subclass

A

more specific version of a class and need to override or add new behaviors

72
Q

use abstract class

A

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
Q

use an interface

A

when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree

74
Q

all interface methods are implicitly ………. and ……….

A

public

abstract

75
Q

stack frame

A

holds the state of the method including which line of code is executing, and the values of all local variables

76
Q

if the local variable is a reference to an object

A

only the variable (the reference) goes on the stack

77
Q

java has two areas of memory we care about

A

Stack

Heap

78
Q

instance variable lives in

A

heap

inside the object

79
Q

constructor

A

has code that runs when you instantiate an object

80
Q

java lets you declare a method with the same name as your class

A

if it doesn’t have a return type then it is a constructor

81
Q

pirvate constructors are inherited

A

false

82
Q

if you write a constructor that takes arguments, and you still want a no-arg constructor,

A

you’ll have to build the no-arg constructor yourself

83
Q

to compile each constructor must have a ………. argument list

A

different

84
Q

a constructor is the code that runs when you say ….. on a class type

A

new

85
Q

you can use constructor to initialize the state of the object being constructed

A

i.e the instance variables

86
Q

always provide a no-arg constructor if you can, to make it easy for programmers to make a working object

A

supply default values

87
Q

subclass’ constructor is invoked first

A

but it is the superclass constructor that finishes first

88
Q

…… calls the super constructor

A

super()

89
Q

compiler puts in a call to super() if you don’t

A

by default it calls no arg constructor of the super class

90
Q

the superclass parts of an object have to be fully-formed (completely built) before the subclass parts can be constructed

A

subclass object might depend n things it inherits from the superclass, so it’s important that those inherited things be finished

91
Q

the call to …………. must be the first statement in each constructor

A

super()

92
Q

a reference to the current object

A

this

93
Q

you can say this() only within a ……………….

and it must be the first statement

A

constructor

94
Q

every constructor can have a call to ………. or ………….. but never both

A

super()

this()

95
Q

use ………. to call a constructor from another overloaded constructor in the same class

A

this()

96
Q

an object is alive as long as

A

there are live references to it

97
Q

when an object’s last reference disappears

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

if you use the dot operator on a null reference you’ll get a ………. at run time

A

NullPointerException

99
Q

static class never affected by …………. variables

A

instance

static methods never use instance variable values

100
Q

Math constructor is marked as ………….

A

private

101
Q

lets a method run without any instance of the class

A

static

102
Q

call a static method using ………… name

A

class

103
Q

call a non-static method using ………….. ………. name

A

reference variable

104
Q

the only way to get new object

A
new
or deserialization (or something called the Java Reflection API)
105
Q

static methods ………….. use non-static (instance) variables

A

CAN’T

106
Q

static methods ………….. use non-static methods

A

CAN’T

107
Q

static variable

A

value is the same for all instances of the class

108
Q

static variable is initialized

A
only when the class is first loaded
not each time a new instance is made
109
Q

static variables are ………….

A
shared
all instances of the same class share a single copy of the static variable
110
Q

static variables in a class are initialized before any object of that class can be created

A

static variables in a class are initialized before any static method of the class runs

111
Q

static final variables are

A

constants

112
Q

a variable marked final means

A

once initialized it can never change

113
Q

…………. 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

A
static initializer
clas Foo{
final static int x;
static{x=42;}
}
114
Q

if you don’t give a value to a final variable

A

the compiler will catch it

115
Q

final variable means

A

you can’t change its value

116
Q

final method means

A

you can’t override the method

117
Q

final class means

A

you can’t extend the class

118
Q

if the class is final

A

you don’t need to mark the methods final

119
Q

static method is good for

A

utility method that does not (and will never) depend on a particular instance variable value

120
Q

when you need to treat a primitive like an object

A

wrap it

121
Q

put integer in ArrayList

A
ArrayList list = new ArrayLIst();
list.add(3);
int num = list.get(0);
122
Q

converting a string to a primitive value

A

Integer.parseInt(string);

123
Q

out in System.out is

A

a static variable

124
Q

inner class can use all the methods and variables of the outer class

A

even the private ones

125
Q

a …………. is an object that represents a network connection between two machines

A

socket

126
Q

what is a connection

A

a relationship between two machines, where two pieces of software know about each other.

127
Q

to make a socket connection you need to know

A

ip address

tcp port number

128
Q

a socket connection means

A

the two machines have information about each other including ip address and tcp port

129
Q

a tcp port is just a number

A

a 16-bit number that identifies a specific program on the server

130
Q

without port number

A

the server would have no way of knowing which application a client wanted to connect to

131
Q

a server can have up to ………………… different server apps running, one per port

A

65536

132
Q

the tcp port numbers from 0 to 1023 are reserved for well-known services

A

don’t use them for your own server programs

133
Q

if you try to bind a program to a port that is already in use

A

you’ll get a BindException

134
Q

to communicate over a socket connection we use

A

streams

135
Q

ip address of localhost

A

127.0.0.1

136
Q

…………… is a bridge between low level byte stream (like the one coming from the socket) and a high level character stream (like the BufferedReader)

A

InputStreamReader

137
Q

client and server communicate over a …………. connection

A

socket

138
Q

unless you have multiple processors on your computer, each new Java thread is not actually a separate process running on the OS

A

but it almost feels as though it is

139
Q

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

A
Thread t = new Thread();
t.start();
140
Q

multiple threading in Java means we have to look at both ……….. and ……. that’s run by the thread

A

thread

job

141
Q

every java application starts up a ………. thread

A

main

the thread that puts main method at the bottom of the stack

142
Q

Thread class

A
void join()
void start()
static void sleep()
143
Q

thread’s job

A

Runnable object

144
Q

thread object - worker

A

runnable - thread job

145
Q

to go from having just a Thread instance to having a new thread of execution you must call

A

thread.start();

146
Q

all the methods in an interface are

A

public

147
Q

once the thread becomes runnable, it can move back and forth between

A

runnable
running
blocked

148
Q

blocked

A

sleeping
waiting for another thread to finish
waiting for an object’s lock

149
Q

scheduler implementations are different for different JVMs

A

even running the same program on the same machine can give you different results

150
Q

every thread in java has its own ………….

A

call stack

151
Q

sleep method throws an InterruptedException

so all calls to sleep must be wrapped in a try/catch

A

try{Thread.sleep(2000);
}catch(InterruptException ex){
ex.printStackTrace();
}

152
Q

when the thread wakes up, it always goes back to …………….. state

A

runnable

153
Q

threads can lead to concurrency issues

A

concurrency issues lead to race conditions

race conditions lead to data corruption

154
Q

the ……………. keyword means that a thread needs a key in order to access the synchronized code

A

synchronized

155
Q

when code hits a synchronized method

A

there is going to be a performance hit
synchronization restricts concurrency
synchronized methods can lead to deadlock

156
Q

names used for classes, variables and methods are called

A

identifiers

157
Q

access modifiers

A

default
public
protected
private

158
Q

non-access modifires

A

final
abstract
strictfp

159
Q

enum

A

restrict a variable to have one of only a few predefined values
enum FreshJuiceSize{SMALL, MEDIUM, LARGE}

160
Q

visible to the package

A

default - no modifiers are needed

161
Q

visible to the class only

A

private

162
Q

visible to the world

A

public

163
Q

visible to the package and all subclasses

A

protected

164
Q

final

A

is used to apply restrictions on class, method and variable

165
Q

finally

A

used to place important code, it will be executed whether exception is handled or not
it is a block

166
Q

finalize

A

is a method

used to perform clean up processing just before object is garbage collected