Java Flashcards

1
Q

What is java?

A

Java is a programming language and computing platform. Java is secure and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet,

Java is everywhere!

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

Why java

A

Object-oriented. Automatic memory management. Rich API. It’s portable. Easy to learn. It has simple syntax based on C. Java is FREE.
Supported by Oracle Corporation

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

What is the JVM

A

java virtual machine

allows java bytecode (.class files) to be executed on your machine (binary). Loads, verifies, and executes code.

The JVM is abstract in nature

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

What is the JRE

A

java runtime environment

JRE is [JVM + the set of necesary libraries]. Minimum requirement to run java code.

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

What is the JDK

A

Java development kit
is [JRE + development tools]. Dev tools include: the compiler + debugger + javadocs + etc.

The compiler turns source code (.java files) into bytecode (.class files).

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

What is a bit?

A

a binary value (0 or 1 aka ON or OFF)

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

What is a byte?

A

is 8 bits (256 different conbinations of 0s and 1s. 2^8)

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

What is a nibble?

A

is 4 bits (16 different combinations 0s and 1s. 2^4)

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

What are the naming conventions in java?

A

variable names: camel case. e.g. myFirstName,myLastName

class names (nouns): title case. e.g. Animal, UserStory, ButtonColors

interface names (adjectives): title case. e.g. Runnable, Comparable

method names (verbs): camel case. e.g. drawRectangle, run

package names: lowercase. e.g. java, lang, sql, util, etc

constants: uppercase. e.g. RED, YELLOW, MAX_PRIORITY, etc

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

What are the primitive datatypes in java?

A

● boolean - a true or false value (1 bit in size)
● byte - a smaller space efficient integer representation (1 byte in size)
● char - a single character value (2 bytes in size)
● int - a integer numeric value (4 bytes in size)
● double - a decimal numeric value (8 bytes in size)
● float - a floating point value. Holds big decimals with less precision (4 bytes in size)
● short - a smaller space efficient integer representation (2 bytes in size)
● long - a large integer representation (8 bytes in size)

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

What is flow control?

A

statements that break up the normal flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.

Types of flow control statements include:
● if, else if, and else blocks 
● ternary statements 
● switch cases 
● while loops
● do while loops
● for loops
● enhanced for loops (aka for each loops)
● try, catch, and finally blocks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the keyword “break” do?

A

tells java to cease the logic of the current code block. Java will then begin processing the logic just after the code block’s ending curly brace.

may be used in if statements, while loops, etc.

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

What does the keyword “continue” do?

A

tells java to cease the logic of the current code block THEN go back to the top of the loop’s code block. Java will then begin processing the logic as if it has reached a new iteration of the loop.

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

What is short circuiting?

A

deals with && and || operators.

While computing a true/false condition, if the short circuiting operators determine that the result of the condition is apparent even without computing the rest of the statement then it will stop short and proceed with the answer is already knows.

For example, if the left side of && is
already false then it is impossible for the statement to be true; so java will not compute the right side.

Another example, if the left side of || is true then java will stop computing because the condition will be true regardless of the right side’s results.

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

What datatypes does a switch case allow?

A
Switch cases only allow the following datatypes in its condition declaration:
● int (and Integer, the wrapper class)
● short (and Short)
● byte (and Byte)
● char (and Character)
● String 
● enum
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an array?

A

an object which contains elements of a similar datatype Can only store fixed set of elements and is index based.

We can create arrays in two ways:
● int[] arrayOne = {15, 88, 99};
● String[] arrayTwo = new String[200];
each element starts at the datatypes default value,
btw.
We can access each element of an array using square brackets [ ]. If you attempt to access an element that doesn’t exist then you’ll get a
“ArrayIndexOutOfBounds” exception

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

2 What method do you call to find the size of an array?

A

You can find the size of an array (number of element) by using: myArray.length;
BUT “.length” is NOT a method; it’s a property of an array

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

What is a method?

A

A modularized block of code; created by putting a series of statements between two curly braces. This special block of code will be given a name so that it can be referred to later.

(collection of statements that perform some specific task and return the result to the caller.)

The method signature in java is:
[any modifiers] [the return data type] [the method name] ( [the parameter list] )
{
 // my logic
}

EX: static void myMethod() {
System.out.println(“yay!”);
}

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

What is a function vs a method?

A

A method is simply a function that is attached to an object.

It’s truly as simple as that. Java is almost entirely object oriented so it strictly uses methods, not functions.

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

What is method overloading

A

Overloading is when you have multiple methods with the same name, but different parameter lists.

There are three ways to change the parameter list:
● change the data types
● change the number of parameters
● change the order of the datatypes in the parameter list

This concept does not need inheritance to exist; it happens all within the same class.

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

What is a class?

A

A blueprint for an object. A class is abstract in nature, you can not have a physical instance of a class; it simply defines the structure of an object.

Think of a class like a recipe to create a food dish; it isn’t the food itself…just the instructions to create the food.

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

What is an object?

A

An instance of a class. An object has state (variables) and behavior (methods).
States are things like “size”, “weight”, “height”, “color”, “length”, etc.

Behaviors are things like “bounce”, “makeNoise”, changeColor”, etc.

Think of an object like the food dish created from a recipe. The dish is an INSTANCE of the recipe.

23
Q

What are the four pillars of object oriented

programming?

A

Abstraction
Polymorphism
Inheritance
Encapsulation

24
Q

Abstraction

A

OOP concept that displays what something is and what it does but not how it does it. Showing purpose but hiding implementation.

25
Q

Polymorphism

A

OOP concept that allows one task to be performed in different ways. Variables and methods to take multiple forms. The ability of an object to take on many forms. The parent child relationship is a good example.

In java, we achieve this using method overloading, method overriding, and casting

26
Q

Inheritance

A

OOP concept that allows one class to inherit the properties of another (variables, methods, and “maiden name”). It allows code to be reused.

In java, we achieve this using the “extends” and “implements” keywords.

EX:
Subclass-name extends Superclass-name
Class Animal
Class Dog extends Animal

27
Q

Encapsulation

A

OOP concept that is data hiding. Restricts direct access to data.
In java, we achieve this by creating public getters & setters that access private variables.

28
Q

What is a constructor?

A
A constructor is a special method that constructs an instance of the class (aka an object) and initializes the object's state.
The name of the constructor is the same name as the class itself, and it does NOT have a return type.
29
Q

What does the keyword “new” do in java?

A

The new keyword is a Java operator that creates the object; it is followed by a call to an object’s constructor.

Example: MyObject myObj = new MyObject();

30
Q

Can you name 3 types of constructors?

A

● the default constructor
● the no args constructor
● args constructor (can have any number of overloading variations)

31
Q

Is the default constructor and the no args

constructor the same thing?

A

NO!
The no args constructor is a constructor that has an empty parameter list.

The default constructor is the constructor that the compiler will automatically write for you if you compile the code without having written your own constructor. It happens to be a no args constructor; but just because you have a no args constructor does not mean you have a default
constructor.

The no args constructor is ONLY consider default if the compiler automatically gave it to you.

32
Q

What is the first implicit line of any constructor?

A

super();

33
Q

What is the difference between “super” and

“this”?

A

The keyword “this” is used to refer to the CURRENT object/instance. Example “this.myVariable” would refer to an instance variable called “myVariable”.

Using the “this” keyword from inside a constructor with parenthesis (ex: “this()”) will call a different constructor from the current constructor.

The keyword “super” is used to refer to the current object/instance’s PARENT. Example “super.parentVariable” would refer to an inherited variable called “parentVariable”.

Using the “super” keyword from inside a constructor with parenthesis (ex: “super()”) will call a parent’s constructor from the current constructor.

34
Q

What is the difference between initializing

and instantiating?

A

Initializing is when you give an initial value to a variable: char c = ‘x’;

Instantiating is when you create an object instance. Root word “instance” like when you, ya know, create an INSTANCE of a class (aka an object).

 Like so:
Thread th = new Thread();
If you're saying to yourself "Thread th = new Thread()" looks like it is giving a variable an initial value...then you would be correct. Initializing
could POSSIBLY include instantiation; but just because you see initialization doesn't mean something is being instantiated.
35
Q

8 types of primitive

A
● int Integer
 ● char Character 
● boolean Boolean 
● double Double 
● float Float 
● byte Byte 
● short Short 
● Long Long
36
Q

What is autoboxing?

A

if the compiler is looking for a wrapper class and you give it a primitive type then the compiler will automatically turn the primitive into its wrapper class variation. This is called autoboxing.

37
Q

What is unboxing?

A

If at any point in your program, the compiler is looking for a primitive type and it is given its wrapper class variation then the compiler will automatically turn the wrapper class into the primitive variation. This is called unboxing.

38
Q

What is a String?

A
A class that is implemented using an array of chars; one could think of it as a wrapper class for an array of chars.
A String is immutable meaning it cannot be changed. This
39
Q

What is pass by value?

A

is where Caller passes a copy in memory of its values to the callee function. Changes to the copied parameters (the callee parameters) do not affect the caller values.

Java is pass by value

40
Q

What is pass by reference?

A

is to pass the whole reference of a caller parameter as an argument in the callee parameter. If a change is made by the callee, this affects the value in the caller

41
Q

What is a Java Bean?

A

All JavaBeans are a special type POJO. they are Serializable i.e. they should implement Serializable interface.
Fields should be private.
Fields should have getters or setters or both and a no-arg constructor.

42
Q

How do you make a class Immutable

A
The class must be declared as final (So that child classes can’t be created).
Data members in the class must be declared as final (So that we can’t change the value of it after object creation). 
It must have parameterized constructor(its variables are parameters for the constructor, and the parameters are assigned in the constructor). 
Getter method for all the variables in it. 
No setters(To not have the option to change the value of the instance variable)
43
Q

What is the difference between == and . equals()

A

they both by default compare the hash code memory address for the primitive or object, but the the .equals() can be overridden to provide specific implementation to properly compare the values of an object.

44
Q

What is a Datastructure?

A

A data organization, managment and storage format that enables efficient access and modification through convenient operations

45
Q

What is an Array?

A

Basic Data Structure, often used in the implementation of other data structures.
Fixed number of elements, accessed by their index.
Homogeneous (of the same data type), and sequential in memory.
int[] x = {1,2,3,4,5};
int[] x = new int[5];

46
Q

What are the scopes of a variable in java?

A

● Static (aka class)
The variable/method belongs to the class itself. The class has one copy of the variable that all instances share.
● Instance (aka object)
Each object has its own copy of each variable/method and each object. So changing one object’s variables doesn’t change another object’s variables.
● Method
The variable only exists in the method; outside of the method it stops existing.
● Block
The variable only exists within a block of code (denoted by curly braces { } ). Outside of the block the variable stops existing.

47
Q

What are the access modifiers in java?

A

● public
What has access? The class itself & the current package & any subclasses (children classes) & any other classes you can think of.
● protected
What has access? The class itself & the current package & any subclasses (children classes)
● (default)
What has access? The class itself & the current package
● private
What has access? The class itself ONLY.

48
Q

What is shadowing?

A

Shadowing is when a parent class and a child class have declare a variable with the same exact name. The parent’s version of the variable still exists BUT it can now only be accessed using the “super” keyword.

The child’s version of the variable can be accessed with or without the “this” keyword.

If a parent method has the same name as a child method then the parent method will be overriden NOT shadowed.

Keep this in mind.
Shadowing is for variables, Overriding is for method.

49
Q

What is the difference between overloading

and overriding?

A
Overloading is when you create multiple methods with the EXACT same name BUT different implementations. The parameter list in each
method is slightly different, so that there is a distinct version of the method that is called each time. To overload the method you must change
the parameter list of the new signature in one of the following ways: change the data types, change the number of parameter, OR change the
order of the parameter datatypes. This happens within a single class and has nothing to do with inheritance.

Overriding is when you create a NEW implementation for an inherited method. To override just make sure the create a method with the same
name and parameter list as a method in the parent class; if you’ve done that then the parent method’s logic will be altered to the child logic
(assuming you’ve create an object of the child class). Overriding is only able to happen if we have inheritance.

50
Q

What is runtime polymorphism?

A

is method overriding AND type casting. It is runtime because the compiler cannot definitely say what type of object will be in the heap when the operation triggers; it has to wait until a heap exists to check a heap. The heap doesn’t exist until the code is run

51
Q

What is compile time polymorphism?

A

is method overloading. It is compile time because the compiler can tell which method you’re calling before the code is ever run; and if you’re calling an overloaded version of the method that doesn’t exist the compiler can give you an error.
This is possible because it only needs to check the method signatures, which all exist even before the code is run.

52
Q

What is type casting?

A

Type casting is when you tell java to change one data type of another.
For example, you could tell java to change an int into a double:
int myInt = 8;
double myDoub = (double) myInt;

53
Q

What is the difference between up and down

casting?

A

Downcasting turns the reference variable of an object into a reference to a child, or grandchild, or great grandchild, or etc.

Upcasting turns the reference variable of an object into a reference to a parent, or grandparent, or etc

54
Q

What is the difference betwen String,

StringBuilder, and StringBuffer?

A

String is a class implemented using an immutable array of chars.

StringBuilder and StringBuffer are mutable (they CAN be changed). Unlike StringBuilder, StringBuffer is thread safe