ISYS 303 - FINAL REVIEW Flashcards

1
Q

Scope & Life Time of Variables in Block

A

-Java allows variables to be declared w/in any block
-Block: begins w/ curly brace and ends with curly brace, defines a scope, each time you start a new block you are creating a new scope
-Scope: determines what objects are visible to other parts of the program & determines the lifetime of those objects
-Method Scope: if the method has parameters it is included in the method’s scope, variables declared (type var-name) inside a scope are not visible to code that is defined outside that scope
In nested scopes, variables declared in the outside are visible to the nested scopes (inner scopes) but not vice versa

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

Scope (Public, Private, Protected)

A
Public - Visible within class, subclasses, same/different packages, and entire project
Private - Visible only within same class
Protected - Visible within class, subclasses, and same package
Default - visible within class, subclasses, and same package (Except different package by subclass)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a class?

A

Class is a template that defines the form of an object. It specifies both the data and the code that will operate on that data. Java uses a class specifications to construct objects. Objects are instances of a class. Thus, a class is a set of plans that specify how to build an object. A class is a logical abstraction; it is not until an object of that class has been created that a physical representation of that class exists in memory.

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

How to define a class?

A
A class defintion creates a new data type. In this case, the new data type is called Vehicle. You will use this name to declare objects of type Vehicle. Remember that a class declaration is only a type of description; it does not create an actual object. To actually create a Vehicle object, you will: 
Vehicle minivan = new Vehicle();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are methods?

A

Methods are subroutines that manipulate that data defined by the class and, in many cases, provide access to that data. A method contains one or more statements. Usually performs only one task. Each method has a name, and it is this name that is used to called the method.

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

What is a constructor?

A

Initializes an object when it is created, same name as class, no explicit return type

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

.this

A

when a methods is called, it is automatically passed an important argument that is referenced to the invoking object (that is, the object on which the method is called)

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

bubble sort

A
for (a = 1; a = a; b--) {
     if (nums[b - 1] > nums[b]) {
       t = nums[b-1];
       nums[b-1] = nums[b];
       nums[b] = t;
}
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Static

A

When a member is declared static, it can be accessed before any object of its class are created and without reference to any object. You can declare both methods and variables to be static. Outside the class, to use a static member, you need only specify the name of its class followed by the dot operator. Variables declared as static are, essentially, global variables. When an object is declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

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

super

A

super(var-name, var-name)

***var-name = variable names from the parent class

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

Method Overriding

A

When a method in a subclass has the same return type and signature as a method in its parent class, then the method in the subclass is said to OVERRIDE the method in the parent class. When an overriden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the parent class will be hidden. If you want to access the parent class version, you can by using super: super.NameOfMethod

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

Using final

A
Prevents method overriding and inheritance. 
-Declaring a class as final implicitly declares all of its methods as final too
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Packages

A

Packages are groups of related classes. Packages help organize your code and provide another layer of encapsulation. Package serves two purpose.

  1. Provides a mechanism by which related pieces of a program can be organized as a unit, Classes defined within a package must be accessed through their package name. Thus, a package provides a way to name a collection of classes
  2. Participates in Java’s access control mechanism. Classes defined within a package can be made private to that package and not accessible by code outside the package. Thus, a package provides a means by which classes can be encapsulated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Importing Packages

A

import pkg.classname; //just to import the particular class
***pkg = package name

import pkg.* //import all of the classes in the package

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

Exception Handling

A

An exception is an error that occurs at run-time. Exception handling streamlines error handling by allowing your program to define a block of code, called exception handler, that is executed automatically when an error occurs.

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

Throwable

A
In Java, all exceptions are represented by classes. All exception classes are derived from a class called Throwable. Thus, when an exception occurs in a program, an object of some type of exception class is generated. There are two direct subclasses of Throwable: Exception and Error. Exceptions of type Error are related to errors that occur in the JVM, and in the program. Errors that result from program activity are represented by subclasses of Exception. EX: divid by zero, array boundry, and file errors 
An important subclass of Exception is RunTimeException which is used to represent various common types of run-time errors
17
Q

Exception Handling Fundamentals

A

Java exception handling is managed via five words: try, catch, throw, throws, and finally. They form an interrelated subsystem in which the use of one implies the use of another.
Program statements that you want to monitor for exceptions are contained within a TRY block. If an exception occurs within the TRY block, it is THROWN. You code can catch this exception using CATCH and handle it in some manner. System generated exceptions are automatically thrown by the Java run-time system. To manually THROW an exception, use the keyword THROW. In some cases, an exception that is thrown out of a method must be specified as such by a THROWS clause. Any code that absolutely must by executed upon exiting from a TRY block is put in a FINALLY block.

18
Q

Try & Catch

A

-At the core of exception handling, keywords work together can’t have a catch without a try and vice versa

try{
//block of code to monitor for errors 
}
catch (exceptionType Var-Name) {
//handler for ExceptionType 
}
19
Q

Try & Catch Example

A

int nums[] = new int[4]

try{
nums[7] = 10;
}
catch (ArrayIndexOutOfBoundsException exc) {
sout("Index-out-of-bounds!")
}
20
Q

How Try & Catch Works

A
  1. code you want monitored is contained in a try block
  2. when an exception occurs, the exception is thrown out of the try block and caught by the catch statement. Control passes to the catch, and the try block is terminated. Catch is NOT called rather the program execution is called to it.
  3. After the catch statement executes program control continues with the statements following the catch
  4. If no exception is thrown by a try block, no catch statements will be executed and program control resumes after the catch statement
21
Q

Throwing an Exception

A
-Manually throwing an exception by using the THROW statement: 
throw exceptOb;
***exceptOB = must be an object of an exception class derived from Throwable
22
Q

Throwing an Exception Example:

A
try{
throw new ArithmeticException();
}
catch (ArithmeticException exc) {
}
23
Q

Finally

A

the finally block will be executed whenever execution leaves a try/catch block, no matter what conditions cause it. That is, whether the try block ends normally, or because of an exception, the last code executed is that defined by finally. The finally block is also executed if any code within the try block or any of its catch statements return from the method.

24
Q

Throws

A

In some cases, if a method generates an exception that it does not handle, it must declare that exception in a throws clause. Exceptions that are subclasses of Error or RuntimeException don’t need to be specified in a throws list. All other type of exceptions do need to be declared. Failure to do so causes a compile-time error

ret-type methName(param-list) throws except-list {
//body
}
***except-list = comma separated list of exceptions that the method might throw outside of itself