Week 1 Flashcards

1
Q

What are the fundamentals of object oriented programming (OOP)?

A
  • Encapsulation and information hiding
  • Inheritance and polymorphism
  • Decomposition and abstraction
  • Dependency inversion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does write once run everywhere refer to?

A

The java bytecode (.class) file that is generated from the JVM allows any operating system to run any .java file because the JVM accommodates for the OS when compiling the bytecode file.

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

What is a class in Java?

A

The fundamental programming unit of Java. It is a “blueprint” that defines a type, nothing in Java exists outside the class.

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

In general, what is contained in a class file?

A
  • What data it contains (state stored in variables)
  • How to instantiate it (parameters we must pass to the constructor)
  • How we interact with it (public methods aka behaviours)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What makes an instance of a class unique?

A

The attributes in the objects fields at a given moment.

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

What are the different letter cases of class names, variables and methods, and constants?

A

Class names: TitleCase
Variables and methods: camelCase
Constants: UPPER_SNAKE_CASE

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

What does it mean when a programming language is “strongly typed”?

A

Strongly typed means every “thing” has a type and only certain actions can be performed on certain types.

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

What does it mean when a program is “statically typed”?

A

Statically typed means variables need their data type to be declared before assigning anything to it, it also only allows variables to refer/store things of a single data type.

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

What are the two mandatory components of declaring and initializing variables?

A
  1. Data type
  2. Identifier
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are local variables? When do we assign their values?

A

Variables that are inside methods. Local variables must be assigned compatible values before they are used.

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

What are instance variables? Where do we assign their values?

A

Variables that are inside a class but outside of methods. Instance variables must be assigned values inside the constructor.

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

What happens if instance variables are not assigned in the constructor?

A

They will assigned default values depending on their data type.

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

What are primitive data types in Java?

A

Primitives are a data type in Java that do not require an object in the heap to be created for them. They are stored wherever they are used.

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

What are some important properties of primitive data types?

A
  • They can only store data in a specified range
  • The size and nature of primitive data types are immutable over all platforms
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Briefly described underflow and overflow.

A

Underflow occurs when you go out of bounds of the range from the smaller “end” and loop back around to the higher end of the range. Overflow is the exact opposite of underflow.

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

What are the 3 ways to convert data in Java?

A
  1. Assignment
  2. Promotion
  3. Casting
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What keyword is used to instantiate a class?

A

new

18
Q

What is the difference between identity and equality?

A

Identity refers to the address of an object while equality refers to the state of the object.

19
Q

Where are interned strings stored?

A

String constant pool

20
Q

What are the four “integer” primitives (from smallest to biggest)?

A
  1. byte
  2. short
  3. int
  4. long
21
Q

What are the four primitive data types that aren’t “integers”?

A
  • float
  • double
  • boolean
  • char
22
Q

What are the three ways data is converted in Java?

A
  1. Assignment
  2. Promotion
  3. Casting (most dangerous)
23
Q

Why can precision potentially be lost when converting from a long to float? What does it mean for widening conversion to always preserve magnitude? Are widening conversion automatic?

A

Magnitude is preserved in widening because we are converting to a data type that allows for a larger range of numbers. Precision may be lost when converting from a long to a float because of the way floats are represented with a mantissa in computers, if the long is large enough some precision may be lost. Yes widening conversion are automatic.

24
Q

Are narrowing conversion automatic? What can potentially be lost in narrowing conversions?

A

No, narrowing conversions are not automatic and both magnitude and precision may be lost.

25
Q

What is special about the byte to char conversion?

A

It is first a widening conversion (byte to int) then a narrowing conversion (then converted to char).

26
Q

Briefly describe assignment conversion.

A

Assignment conversion automatically when you assign a variable storing a primitive value to one of a wider primitive type.

27
Q

Briefly describe promotion.

A

Promotion is widening that occurs automatically during arithmetic expressions.

28
Q

Briefly describe casting.

A

Narrowing and widening conversion are explicitly accomplished by the developer.

29
Q

What is a Java String?

A

Immutable sequence of primitive data type char SURROUNDED BY DOUBLE QUOTES.

30
Q

What are some useful methods from the String class?

A
  • trim()
  • toUpperCase()
  • equals()

equals() is used when comparing the contents of strings. do not use “==” because this compares the addresses of the two strings rather than the content.

31
Q

How do we concatenate strings?

A

Using the + operator.

Java reads the operand from left to right. Depending on the type of the left side value (String vs Integer) Java will either try to concatenate (String) or add (Integer).

32
Q

What is a variable that does not store a primitive? What is stored in this type of variable?

A

It is an object reference. An object reference stores the address of an object (an instance of a class).

Remember…
- A JAVA VARIABLE WHOSE DATA TYPE IS PRIMITIVE STORES THE VALUE
- A JAVA VARIABLE WHOSE DATA TYPE IS A CLASS STORES AN ADDRESS

33
Q

What is null in Java? Can we assign null to primitive types?

A

It indicates an unset reference (an object reference that isn’t currently storing a memory address). No, we cannot assign null to primitive types.

34
Q

Why should we check for null inside methods?

A

null can be passed as a parameter to a method that expects an object reference therefore we must test for it when appropriate.

35
Q

What are the benefits of the Java Virtual Machine (JVM)?

A
  • JVM permits cross platform distribution of executable files
  • Bytecode that is executed frequently is optimized and compiled directly into machine code
36
Q

What are the two sections of memory in Java? What is contained in these sections?

A
  1. The stack
    - contains primitive values
    - references to objects
  2. The heap of allocated objects
    - objects
37
Q

What does “Java is a pass by value language” mean?

A

Pass by value means when arguments (variables) are passed to methods, a copy of the arguments (variables) value is made and that is what is passed to the method. If the variable stores an object, a copy of that address is what is passed and we can therefore modify the object. If a primitive is stored the value is simply copied and the original remains unchanged.

38
Q

What are the Java equivalents to these common Python collections?
1. List
2. Dictionary
3. Set

A
  1. ArrayList<E> (for all 3, type of data stored goes in between the angle brackets)</E>
  2. HashMap<K, V>
  3. HashSet<E></E>
39
Q

What class is used to determine user input? What java package is it from?

A

The Scanner class from java.util

40
Q

What is the tiny issue with the Scanner class that we should always be mindful of?

A

BEWARE OF THE NEWLINE CHARACTER WHEN GETTING NUMERICAL INPUT! DON’T FORGET TO CLEAR THE BUFFER WITH

scan.nextLine();

41
Q

Syntax for compiling, executing, and passing arguments to Java program in the Command Line?

A

Navigate to program location first*

compile:
javac ClassName.java

execute:
java ClassName

passing arguments:
java ClassName argument 1 argument 2 argument 3

42
Q

Important facts about Java compilation and portability to remember?

A
  • Source file *.java contains a single class definition
  • Class file *.class contains sharable, portable Java bytecode
  • The Java compiler converts .java file into .class file which the Java interpreter can execute
  • During execution, the interpreter in the JVM interprets the .class file line by and line and converts it to instructions for the CPU