Enterprise: Week 1 Flashcards
What are environment variables?
Environment variables are values that are accessible in an entire working environment. In Unix, these values are set in the shell when it is started.
For example, your home directory is an environment variable called $HOME. If you wish to see the value of a particular environment variable, you can use the echo command like so: echo $HOME.
What are package managers?
In Unix, if you wish to install software, you generally use a package manager. There are many package managers available and we will talk about a few.
What are the basic linux commands?
The top-level directory is known as the root directory and it is the folder that contains all of the other folders on the drive partition you are currently accessing. In Unix, your root directory is represented by the / character. For example, if you wished to change directory to the root directory from anywhere on the partition, you would issue the following command: cd /
The . character represents the current directory. Furthermore, .. represents the parent directory, so if you wanted to copy everything from the current directory up to the directory immediately above it, you would issue the following command: cp -r . ..
In Unix, we also have a directory called the home directory. This directory is usually the one that our terminal starts in and it is where our personal files are generally stored. This directory is represented by the ~ character.
What are Flags Commands?
Flags are special arguments given to a command. There are two kinds of flags in Unix, short-hand or character flags, a single character (or group of characters), prefixed by a single dash -c, and full flags, the full name of the flag, prefixed by a double dash –flag.
What is the most important Linux Command?
man - The manual command will print to the terminal the manual for using a particular command. If you are unsure what flags or arguments a command takes, you simply type man command.
For example, if you wished to see the manual for the copy command, you would issue the command: man cp
What are the directory commands?
cd - The change directory command allows us to navigate to a different directory on the drive.
- go to root directory: cd /
- go to home directory: cd or cd ~
- navigate one directory up: cd ..
- navigate into the hi directory, which is inside the bye directory: cd ./bye/hi
- change to the previous directory: cd -
ls - The list directory command allows us to see the contents of a particular directory. When given no arguments, it lists the contents of the current directory. The -a flag allows you to see hidden items in the directory.
- list the contents of the current directory: ls
- list the contents of the hi directory: ls hi or ls ./hi
- list the contents of the directory including the “hidden” contents: ls -a
mkdir - The make directory command allows us to create a new directory. mkdir takes an argument representing the name of the directory you wish to create.
- create a directory named hi: mkdir hi
pwd - The print working directory command prints the full name of the directory you are currently working in. For example, if you were working in the home directory inside of the root directory the output of pwd might be /home.
What are the General Purpose Commands?
su - The substitute user command allows you to switch users. With no argument, this defaults to the root user, which has higher priveleges. This can be useful if you need to perform multiple commands with elevated priveleges but is generally considered to be bad practice in preference to sudo, for administrative logging purposes.
sudo - the sudo command allows you to run a particular command as the root user.
clear - the clear command usually prints a number of blank lines such that all previous commands are no longer on the screen. There is a shortcut for this command, ctrl-l
echo - the echo command will print a string or the result of a command to the console.
>
- The > operator will redirect the output of a command to a file. The file will be created or overwritten if it already exists. ex. ls . > log.txt
> >
- The»_space; operator acts the same way as the > operator but appends output to the file instead of overwriting if it exists.
grep - the grep command prints any lines in a file or files that match a given pattern. By default, grep interprets the pattern as a basic regular expression.
- Print all lines in hello.txt that contain the word goodbye: grep goodbye hello.txt
What are the File Commands?
cat - the concatenate command prints the contents of a file to the console. cat hello.txt
head - the head command prints the first ten lines of a file to the console. head hello.txt
tail - the tail command prints the last ten lines of a file to the console. tail hello.txt
touch - the touch command allows you to modify the timestamp of a file. This command is usually used to create empty files, as an empty file is created if touch is given a file name that does not exist. touch hello.txt
cp - the copy command creates a copy of the specified file at the location specified. If the recursive glag is used, it will operate on directories.
- copy a hello.txt to goodbye.txt: cp hello.txt goodby.txt
- copy the hello directory to the goodbye directory: cp -r hello goodbye
mv - the move command will rename or move a file or entire directory with the recursive flag.
- rename a hello.txt to goodbye.txt: mv hello.txt goodbye.txt
- move hello.txt to the goodbye directory: mv hello.txt goodbye/.
- rename the hello directory to goodbye: mv -r hello goodbye
rm - the remove command will delete a file. If you use the recursive flag, it can delete a directory. The force flag will cause the command to delete files without prompting the user if there are warnings. The command rm -rf . is extremely dangerous.
- remove hello.txt: rm hello.txt
- remove the hello directory: rm -r hello
wc - the word count command will print the number of words in a file. This command has several flags available
- -c, –bytes - prints the byte count
- -m, –chars - prints the character count
- -l, –lines - prints the lines
- -w, –words - prints the word count (default)
ln - the link command creates a link between files. This allows you to make a shortcut to a file in one location without copying it over.
What are the 3 File Permissions?
Owner permissions - What the owner of the file is allowed to do to the file.
Group permissions - What the group of users that the file belongs to is allowed to do to the file.
Other (world) permissions - What everyone else is allowed to do to the file.
How do you view permissions?
You can see the permissions of files in a directory by using the -l flag on the ls command to get it to print the “long listing format”
ls -l .
How do you change permissions?
You can change the permissions on a file using the chmod or change file mode bits command.
What is Git?
Git is a distributed version control system. This means that the entire codebase and history of a project is available on every developer’s computer as a local repository , which allows for easy branching and merging
This repository contains all of the information that the remote repository has, based on the last time that you synced those two together.
Even if you don’t have access to the remote repository, you can still view all of the changes that have been made, and contributers can maintain a copy of this record on their own machines.
What is GitHub?
GitHub is a website and cloud-based service that helps developers store and manage their code, as well as track and control changes to their code.
GitHub is a Git repository hosting service, but it adds many of its own features. While Git is a command line tool, GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, such as a wikis and basic task management tools for every project.
What is Git Branching?
Branching is a feature available in most modern version control systems. Instead of copying files from directory to directory, Git stores a branch as a reference to commit. The branch itself represents the HEAD of a series of commits.
The default branch name in Git is master, which commonly represents the official, working version of your project. As you start making commits, the master branch points to the last commit you made. Everytime you commit, the master branch pointer moves forward automatically. Think of a branch as a timeline of versions of a project as it progresses.
Branching is a strategy that allows developers to take a snapshot of the master branch and test a new feature without corrupting the project in production. If the tests are successful, that feature can be merged back to the master branch and pushed to production.
What is a JDK?
The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets.
It includes the Java Runtime Environment (JRE), an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development.
What is a JRE?
The Java Runtime Environment (JRE) is a set of software tools for development of Java applications. It combines the Java Virtual Machine (JVM), platform core classes and supporting libraries.
JRE is part of the Java Development Kit (JDK), but can be downloaded separately. JRE was originally developed by Sun Microsystems Inc., a wholly-owned subsidiary of Oracle Corporation.
Also known as Java runtime.
What is a JVM?
The Java Virtual Machine (JVM) is an abstraction layer between a Java application and the underlying platform. As the name implies, the JVM acts as a “virtual” machine or processor. To the bytecodes comprising the program, they are communicating with a physical machine; however, they are actually interacting with the JVM.
OOP: What is inheritance?
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
OOP: What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
OOP: What is encapsulation?
Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.
We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
Advantages of Encapsulation:
By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.
It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.
It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.
The encapsulate class is easy to test. So, it is better for unit testing.
What are the 4 types of java access modifiers?
- private: within class
- default: within class, package
- protected: within class, package, by subclass
- public: class, package, subclass, outside package
OOP: What is abstraction?
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don’t know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
There are two ways to achieve abstraction in java
Abstract class (0 to 100%) Interface (100%)
Abstract Class
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
Some points to remember :
An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the body of the method.
What is a class?
A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods. Core properties include the data types and methods that may be used by the object
A class can also be defined as a blueprint from which you can create an individual object. Class doesn’t consume any space.
What is an object?
Any entity that has state and behavior is known as an object. For example a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other’s data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
What are the advantages of OOP over Procedural Oriented Programming?
OOPs makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases.
OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
What is the difference between an interface and an abstract class?
Conceptually, interfaces define behaviors and abstract classes are for concepts and inheritance.
You can implement multiple interfaces, but you can extend only one class.
What is an interface?
An interface acts as a contract for behaviors that a class can implement.
Interfaces have implicit modifiers on methods and variables.
- Methods are ‘public’ and ‘abstract’
- Variables are ‘public’, ‘static’, and ‘final’ To inherit interfaces, a class must implement them and they are REQUIRED to implement all methods, unless the class is abstract.
What is Java’s Compilation process?
Compilation and Execution of a Java Program
Java, being a platform independent programming language, doesn’t work on one-step-compilation. Instead, it involves a two-step execution, first through an OS independent compiler; and second, in a virtual machine (JVM) which is custom-built for every operating system. The two principle stages are explained below:
Compilation:
First, the source ‘.java’ file is passed through the compiler, which then encodes the source code into a machine independent encoding, known as Bytecode. The content of each class contained in the source file is stored in a separate ‘.class’ file. While converting the source code into the bytecode, the compiler follows the following steps:
Parse: Reads a set of *.java source files and maps the resulting token sequence into AST (Abstract Syntax Tree)-Nodes.
Enter: Enters symbols for the definitions into the symbol table.
Process annotations: If Requested, processes annotations found in the specified compilation units.
Attribute: Attributes the Syntax trees. This step includes name resolution, type checking and constant folding.
Flow: Performs dataflow analysis on the trees from the previous step. This includes checks for assignments and reachability.
Desugar: Rewrites the AST and translates away some syntactic sugar.
Generate: Generates ‘.Class’ files.
Execution:
The class files generated by the compiler are independent of the machine or the OS, which allows them to be run on any system. To run, the main class file (the class that contains the method main) is passed to the JVM, and then goes through three main stages before the final machine code is executed. These stages are:
What are java’s primitive types?
The eight primitive data types in Java are:
- boolean, the type whose values are either true or false
- char, the character type whose values are 16-bit Unicode characters
The arithmetic types:
- the integral types:
- byte
- short
- int
- long
- the floating-point types:
- float
- double
What are java literals?
Literals are values assigned to various variables, which differ in various forms. Whatever the input-form, the compiler will understand the code and the output will be as expected. We understood literals for Boolean, Integer, Character forms and implemented the understanding along with code. These literals are best applicable when we’re to pass a fixed value in code.
What are Java’s operators?
Java divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
What are Java’s arithmetic operators?
(+) Addition, Adds together two values
(-) Subtraction, Subtracts one value from another
(*) Multiplication, Multiplies two values
(/) Division, Divides one value by another
(%) Modulus, Returns the division remainder
(++) Increment, Increases the value of a variable by 1
(–) Decrement, Decreases the value of a variable by 1
What are java’s assignment operators?
= \+= -= *= /= %= &= example: x & 3 |= example: x | 3 ^= >>= <<=
What are java’s comparison operators?
== != > < >= <=
What are java’s logical operators?
&&
||
!
What are java’s reference data types?
Java provides two types of data types primitive and reference data type.
The primitive data types are predefined in Java that serves as a fundamental building block while the reference data type refers to where data is stored.
In Java, non-primitive data types are known as reference types. In other words, a variable of class type is called reference data type. It contains the address (or reference) of dynamically created objects. For example, if Demo is a class and we have created its object d, then the variable d is known as a reference type.
It refers to objects. It is not pre-defined. It is created by the programmer if required. The reference types hold the references of objects. All reference types are a subclass of type java.lang.Object. It provides access to the objects stored in the memory
Reference Type:
Class- It is a set of instructions. It describes the content of the object.
Array - It provides the fixed-size data structure that stores the elements of the same type.
Annotations - It provides a way to associate metadata with program elements.
Interface - It is implemented by Java classes.
Enumeration - It is a special kind of class that is type-safe. Each element inside the enum is an instance of that enum.
What are packages in Java?
Packages are a way of organizing classes, interfaces, and enums in a hierarchical manner. Packages follow a naming convention of lowercase characters separated by periods in the reverse way you would specify a web domain - thus, com.revature.mypackage instead of mypackage.revature.com.
Also, classes can be referenced anywhere in a program by their “fully qualified class name” - which is the package declaration followed by the class, in order to uniquely identify the class. In our example, the fully qualified class name is com.revature.mypackage.HelloWorld.
But typically we do not want to write out a verbose package and class name together. Instead, we can use an import statement after our package declaration to pull in other classes. We can then just use the class name without the package. By default, everything in the java.lang package is imported (which gives us the System class we used in the example). Other packages and classes must be imported by the programmer explicitly.
What is a Java Constant?
As the name suggests, a constant is an entity in programming that is immutable. In other words, the value that cannot be changed. In this section, we will learn about Java constant and how to declare a constant in Java.
In Java, to declare any variable as constant, we use static and final modifiers. It is also known as non-access modifiers. According to the Java naming convention the identifier name must be in capital letters.
What is the purpose for final and static modifiers?
Static and Final Modifiers:
- The purpose to use the static modifier is to manage the memory.
- It also allows the variable to be available without loading any instance of the class in which it is defined.
- The final modifier represents that the value of the variable cannot be changed. It also makes the primitive data type immutable or unchangeable.
What are Access Modifiers?
Access modifiers are keywords which define the ability of other code to access the given entity. Modifiers can be placed on classes, interfaces, enums, and class members. The access modifiers are listed below:
Modifier Access Level:
- public: Available anywhere
- protected: Within the same package, and this class’ sub-classes
- default: Within the same package
- private: Only within the same class
The default access level requires additional clarification - this access level is “default” because there is no keyword to be used. This access level is also known as “package private”.
Using private modifiers on instance variables - along with public getter and setter methods - helps with encapsulation, which is one of the pillars of object-oriented programming.
What are Non-access modifiers?
Non-Access Modifiers: Java also has non-access modifiers which can be placed on various class members:
static - denotes “class” scope, meaning the member resides on the class itself, not object instances.
- static variables can be accessed through the class, e.g. MyClass.staticVariable
- static methods can be called directly without needing an instance of the class, e.g. MyClass.someMethod()
final
- when applied to a variable, it means the variable cannot be re-assigned
- when applied to a class, it means the class cannot be extended
- when applied to a method, it means the method cannot be overriden
abstract
- when applied to a class, the class cannot be instantiated directly (instead, it should be inherited)
- when applied to a method, only the method signature is defined, not the implementation. Also, the class where the method resides must also be abstract. Concrete subclasses must implement the abstract method.
synchronized - relevant to threads and preventing deadlock phenomena (discussed in a separate module)
transient - marks a variables as non-serializable, meaning it will not be persisted when written to a byte stream (discussed in another module)
- volatile - marks a variable to never be cached thread-locally. Obscure, rarely-used keyword.
- strictfp - restricts floating point calculations for portability. Obscure, rarely-used keyword.
What are the variable scopes?
When a variable is declared in a Java program, it is attached to a specific scope within the program, which determines where the variable resides. The different scopes of a variable in Java are:
- Instance, or object, scope
- Class, or static, scope
- Method scope
- Block scope
Instance scope means that the variable is attached to individual objects created from the class. When an instance-scoped variable is modified, it has no effect on other, distinct objects of the same class.
Class scoped variables reside on the class definition itself. This means that when objects update a class-scoped variable, the change is reflected across all instances of the class. Class scope is declared with the static keyword. Methods can also be declared as class scope. However, static methods cannot invoke instance methods or variables (think about it: which specific object would they reference?). Static methods and variables should be referenced through the class directly, not through an object. For example: MyClass.myStaticMethod() or MyClass.myStaticVariable.
Method scope is the scope of a variable declared within a method block, whether static or instance. Method-scoped variables are only available within the method they are declared; they do not exist after the method finishes execution (the stack frame is popped from the stack and removed from memory after execution).
Block scoped variables only exist within the specific control flow block, of which there are several in Java: for, while, and do-while loops, if/else-if/else blocks, switch cases, or even just regular blocks of code declared via curly braces ({}). After the block ends, variables declared within it are no longer available.
What are Arrays?
An array is a contiguous block of memory storing a group of sequentially stored elements of the same type. Arrays in Java are of a fixed size and cannot be resized after declaration. Arrays are declared with square brackets after the type of the array like so:
int[] myInts = new int[]{1, 2, 3, 4};
String languages[] = {“Java”, “JavaScript”, “SQL”};
Items in an array are referenced via their index in square bracket notation, which begins with 0 for the first element. Arrays also have a length property specifying the length of the array. This is helpful when iterating over arrays with a for loop:
String[] myArr = {“first”, “second”, “third”};
for (int i = 0; i < myArr.length; i++) {
System.out.println(myArr[i]);
}
What is a Varargs?
Instead of writing our main method the standard way, we can use an alternative notation:
public static void main(String… args) { }
Here we are using the varargs construct … which replaces the array notation. varargs stands for “variable arguments”, and allows us to set an argument to a method whose size is determined at runtime. Java will create an array under the hood to fit the arguments provided. You can only ever have 1 varargs parameter in a method, and it MUST be the last parameter defined (otherwise, how would the JVM know the difference between the last value in varargs and the next parameter of the method?). You can omit the vararg value when invoking the method and Java creates an array of size 0.
public class VarargsExample { public static void someMethod(int a, int... manyInts) { System.out.println("First argument: " + a); System.out.println("Next argument: "); for (int i = 0; i < manyInts.length; i++) { System.out.println(manyInts[i]); } }
public static void main(String[] args) { VarargsExample.someMethod(1, 3, 4, 5, 6); // First argument: 1 // Next argument: // 3 // 4 // 5 // 6 } }