Quiz 2 Flashcards

(57 cards)

1
Q

8 Primitive wrapper class

A
Byte
Short
Integer
Long
Float
Character
Double
Boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Java provides an object-based wrapper class for each primitive data type and it will…

A

“box” or “unbox” primitives to/from wrapper-based objects as needed without an explicit request

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

StringBuffer key methods

A
  • apprehend
  • insert
  • delete
  • replace
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Objects

A

the bundles of (related):

  • data (“state”) –> instance variables
  • operations (“behavior”) –> methods
  • invoking operations can change state (values stored in instance variables)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Program

A

collection of interacting objects

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

Class

A
  • Blueprint/”Recipe” for objects
  • include:
    • instance variables(including types, etc.) to include in objects
    • implementations of methods to include in objects
  • seen in static methods, public/private methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Objects are created by…

A
in java: using new
example: Scanner sc = new Scanner(System.in);

invoking new:

  • creates an object in a memory area called “heap”
  • space is created for instance variables
  • returns the address/reference where the object lives
  • if you lose the reference you cannot reach the object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Reference type

A

Objects always stored in heap (including all data)

Reference to objects are another type, and hold one memory address (typically one word)

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

Heap holds allocated memory (i.e., with “new”)

A

e.g.

Scanner sc = new Scanner (System.in);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
is 
String name = "Batman"; 
&
String name = new String ("Batman");
the same?
A

Yes, Java provides it!

String is special because it is used often and Java automatically “fills in” new.

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

Garbage Collection

A
  • heap/memory management
  • Java invokes garbage collector to reclaim unused, but still-allocated heap space
  • Garbage collector reclaims memory in allocated heap and returns it to free heap
e.g.
String s;
s = new String("cat");
s = new String("dog");
s = new String("cow");

“cat” and “dog” are garbage collected

-In C++, you need to take care of reclaiming memory, otherwise you will have memory leaks

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

Memory Leak

A

type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released.

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

Are these Strings equal?

A

String a = new String (“abc”);
String b = new String (“abc”);

if (a == b) {
    println("Equal");
} else {
    println("Not Equal");
}

–> not equal. The variables are pointing at the different objects,although the content is the same

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

Are these Strings equal?

A

String a = new String (“abc”);
String b = a;

if (a == b) {
    println("Equal")
} else {
    println("Not Equal");
}
--> equal. This is called ALIASING --> Two variables refer to the same object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Aliasing

A

two variables refer to the same object

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

equals

A
  • (==) checks if two reference variables refer to the same object
  • methods like str.equals() check if two different objects have the same “content”
  • other classes will have an equals methods also
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Passing Values to Methods

A
  1. Values can be passed to methods through a parameter list.
  2. Actual values provided –> argument list
  3. Process that takes when a method is called:
    i. Arguments are used to initialize the parameters
    a. Matching is one to one
    ii. After the values have been assigned, we transfer control to the first statement in the method
    iii. After the method is done, we return to the point after the method call.
  4. The process is to pass values is called pass-by-value
    i. The parameter is a “photocopy” of the argument
  5. We pass copies to the parameter list
  6. Notice that parameters are like local variables
    i. Created when method is called
    ii. Destroyed when method is over
  7. Notice that local variables cannot be seen from other methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Returning Values from Methods

A
  1. Methods that return values must specify the type of the value to be returned.
  2. The bodies of these methods use “return” to indicate when a value is to be returned.
  3. The value being returned must have the same type as the return type
  4. Notice that return can be used anywhere in the method
  5. Return always ENDS a method returning to the call point
  6. You can have multiple return statements in a method
  7. For a method with no return type “return;” will end the method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

If you don’t specify any modifier, then it is considered…

A

package

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

default value

A

Boolean –> false
Number –> 0
Reference –> null

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

Object Creation

A

-Once a class (Person) is defined, objects based on that class can be created using new: new Person()
-We are “creating an instance of class Person”
-To assign an object to a variable, the VARIABLE’S TYPE MUST BE THE CLASS OF THE OBJECT
Person p = new Person();
-Each object has its own copies of all the instance variables in the class
-Instance variables and methods in an object can be accessed using “.” or using setter(mutator) methods

p. age = 12;
p. setAge(12);

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

Static Method

A

-can be called by using the name of the class, followed by a period

ClassName.staticMethodName()

  • in a class, a static method can call another static method without having to specify the class name
  • static can only call static methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Non-Static methods

A
  • methods that need an object in order for them to be called
    • designed to use data associated with an object
  • cannot call non-static method via class name, need object reference

objectRef.nonStaticMethodName()
-nonstatic can only call nonstatic methods

24
Q

When to define a method as static?

A

When it makes no reference to object data (instance data!)

25
Constructors
- methods that specify how objects are initialized - THEY HAVE THE SAME NAME OF THE CLASS - THEY DON'T HAVE A RETURN TYPE e.g. ``` public Student (String nameIn, double gpaIn) { name = nameIn; gpa = gpaIn; } ```
26
Constructors with no parameters are called...
default constructor. Student() { ... }
27
If you don't define a constructor
you get the default constructor!!!! ****
28
IF you define any constructor
you don't get the default constructor, you will need to define it yourself.
29
toString method
- Every class has a default toString method - toString converts objects into Strings - System.out.println calls this method to print an object - Default: OBJECT TYPE AND ADDRESS * toString can be overridden!! //the method for converting Students to strings public String toString() { return (name: + ": " + id) }
30
getter method
getName() 1. public 2. return whatever private type it is.
31
setter method
1. public 2. no return value setName()
32
instance variables can be...
initialized when they are declared initialized in the constructor **the constructor will override
33
Static Variables
1. We have seen static methods i. Methods that do not require an object in order to be called ii. They don't refer to any instance variables iii. We called them using the name of the class
34
When is a static variable initialized?
At class load time
35
Scope
What is the scope (visibility) of: - instance variables - static variables - local variables - parameters
36
Instance variables
- Belong to the object and created when an object is created - Space for them exists in the heap - Need an object to access them - In a non-static method, we can refer to them directly - have default values
37
Static variables
- Belong to the class - Shared by all instances of the class - Refer them by using the class name (no need for class name if reference by methods of the class)
38
Local variables
- Defined in a method - Created when method is called and destroy on exit - Don't have default values (Eclipse will know if you are using uninitialized variables and force you to initialize them)
39
Parameters
like local variables but initialized when a method is called
40
Debugging
- Process of finding and fixing software errors | - after TESTING detects error
41
Goal of debugging
Determine cause of run-time & logical errors | To correct errors without introducing new ones
42
Three types of information inspected during debugging are...
1. Code 2. Values of variables 3. Program behavior
43
Two debugging approaches
Classic | Modern
44
Classic debugging approach
- insert degugging statements - trace program control flow - display value of variables
45
Modern debugging approach
- IDE (integrated development environment) | - Interactive debugger
46
Interactive debugger
Capabilities: 1. Provides trace of program execution 2. Shows location in code where error encountered
47
(Interactive debugger) Interactive program execution
1. SINGLE STEP through code 2. Run into BREAKPOINTS 3. Displays values of variables i. for current state of program
48
Break Point
1. Drop a marker into the code so when it runs, the execution will stop at that point 2. Allows you to not have to go step by step through things you believe are correct
49
Step Over
1. Takes one step in the current method | 2. If that step is a method call, it performs that whole method call and steps to the next line in the current method
50
Step Into
1. Takes one step in the current method 2. If that step is a method call, it steps into that method so that you can then step through it before getting to the next line in the method you were in.
51
Testing (JUnit)
``` JUnit --> Testing framework for Java With JUnit, define a class where each method represents a tests ``` @Test annotation @Test public void checkingEvenValues() { ... }
52
assertTrue (JUnit)
verifies that the argument expression is true. If the argument expression is false the test fails; otherwise, execution is successful
53
assertEquals
takes two arguments (expected value and actual value). If the values are not equal, the test fails; otherwise, execution is successful
54
If you don't provide an assertion in JUnit test....
a test is NOT considered to fail
55
Copy constructor
``` Constructor that takes as parameter an instance of the same class Uses the data from the parameter object to initialize the data of the new object ```
56
this Reference
1. Current Object i. Represents the object a non-static method operates on 2. this i. Represents a reference to the current object ii. It is a special reference initialized for you iii. It does NOT make sense in a STATIC method
57
the use of this Reference
1. to tell parameters from instance variables this. 2. To call constructors from another constructors this must be the first statement in the constructor 3. In equals method implementation to identify a self-comparison 4. To return a reference to the current object Cascading of method calls 5. To define non-static methods based on static ones Eclipse capitalizes on the use of this when automatically defining code