Week 2 Flashcards
Array
a contiguous block of memory storing a group of sequentially stored elements of the same type.
What is int []
int array
int array declaration
int []
int [] count;
What is this set to if not initialized?
null.
When an array is declared, you can resize as often as you’d like. T/F?
False. Once an array is declared it cannot be resized.
How do you declare a double array?
double []
what is the difference between length and length()
length is for an array.
length() is for string objects.
What is a Package?
A method to encapsulate a group of classes or sub packages, and interfaces.
Why do we use packages?
prevent naming conflicts.
location and usage of classes, interfaces, enums, and annotations easier.
Packages can be considered as data encapsulation (or data-hiding)
explain package college.staff.cse
There are 3 directories total in this package.
cse is inside staff which is inside college.
how are packages written?
In the reverse way you would specify a domain name.
How can I pull in other classes?
Import statement after our package declaration.
What is the Scanner class used for?
to read user input from the command line.
What is an import statement?
An import statement improves the readability of the program by taking a class or all classes visible for a program under a package, using just a single statement.
Explain the following:
import package1[.package2].(*);
The top level package is package 1.
The subordiante-level package is package 2, under package 1.
the (*) is to import all classes from package 1 into package 2.
Explain the following:
import java.util.ArrayList;
This is an import statement that takes an Array list from the package util which is the subpackage of the package java.
Where is the Scanner class found?
At the java.util package.
What class is used to get user input? And where can you find the package to import it?
Scanner class.
java.util
What does the following code do?
java.util.Scanner
Imports the scanner class.
What is the following code? Explain each part.
public static void main(String[] args) {
}
The main method.
public = access modifier.
static means the method can be accessed straight from the class, we don’t have to instantiate an object to have a reference and use it.
void = The data type. this method doesn’t return a value.
main = the name of the method. This is the identifier java looks for when executing a Java program.
Explain the following code, define each part:
Scanner myObj = new Scanner(System.in); System.out.println("Enter username");
This creates a scanner object so the user can input their username.
Scanner is the class.
myObj is the object name.
new is telling java to create a new object.
Scanner is instantiating the object as a Scanner.
System.in is allowing the user to input from device.
Explain the following code, define each part:
String userName = myObj.nextLine();
System.out.println(“Username is: “ + userName);
Read user input.
String class is creating a string object called userName which is being set to equal the myObj user input. Then it is printing out the userName.
The three steps when creating an object from a class.
Declaration (creating the name, the variable part.).
Instantiation (‘new’ keyword which is used to create an object.)
Initialization (following ‘new’ keyword is a call to a constructor. This call initializes the new object)
What is a java constructor?
a special method that is used to initialize objects.
When is a constructor called?
When an object of a class is created.
explain keyword ‘this’
used to access methods of the current class.
What is Data Abstraction?
A process in which only the essential details are displayed to the user.
A car is viewed as a car rather than its individual components. What is this example referring to?
Abstraction
In java, abstraction is achieved by ______ and _______.
Interfaces.
abstract classes.
What is an abstract class?
a class that is declared with an abstract keyword.
what is the keyword for an abstract class?
abstract
What is an abstract method?
a method that is declared without implementation.
What does making a class ‘abstract’ do? 2 things.
Allows you to use abstract methods.
Prevents you from creating instances of that abstract type directly.
What is an interface?
an abstract type used to specify the behavior of a class. (blueprint of a class)
What does an interface contain?
Static constants and abstract methods.
Java interface can have a method body. T/F?
False.
If a class implements an interface and doesn not provide method bodies for all functions specified in the interface, then the class must be ___________.
declared abstract.
How to declare an interface?
use ‘interface’ keyword
what keyword can let us generate multiple inheritances in the case of class?
interface.
Delimiter define.
sequence of one or more characters for specifying the boundary between separate, independent regions in text, math, etc.
Ex. comma
What is garbage collection in Java?
the process by which Java programs perform automatic memory management. The garbage collector finds unused objects and deletes them to free up memory.
Another way to say in-use object in Java.
Referenced object.
Where is the garbage collection implementation in Java?
In the JVM (Java Virtual Machine)
Two types of garbage collection in Java? Describe each.
Minor/Incremental - when unreachable objects in the young gen heap memory are removed.
Major/Full - Objects that survived minor are copied into the old gen or perm gen heap are removed. Major/Full is less frequent.
List all 7 non-access modifiers.
static. final. abstract. synchronized. transient. volatile. native.