Java 1 Flashcards

1
Q

Method Overloading

A
  • Used for reusability and readability
  • Same name with different parameters
  • In this way you can have the same name of method do different and specific tasks and return different values

Parts of Method:

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

What does continue keyword perform ?

A

In a loop if you put continue it will start the iterator again from the next index

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

Word.length

A

length starts at 1 not 0 index.
Lawn has a length of 4
The index does start at 0

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

5/2

A

2

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

An ArrayList collection ..

A

Can only store objects and not primitives

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

Name Standards

A

packages: always lowercase
ClassNames: start with capital and then do camel case
methods: start with lower case and then camelCase
CONSTANTS: ARE ALL CAPS

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

What is Stack Memory and what is stored

A
  • primitive values or the object variables

Ex: Car toyota

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

What is HEAP memory and how is it used ?

A
  • HEAP is larger than STACK
  • String Pool is inside HEAP
  • Objects are placed into HEAP
    Ex: new Toyota
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is String pool ?
Explain what word.toUpperCase does to String value?
Why is a String immutable ?

A

Special space in HEAP java memory where String objects are places and reused.
- If a string is already exists in String pool java will reuse it. Ex: if “java word” already exists and you do String value = “java word”, the word will not be created in String pool it will simple let value variable name point to “java word”

  • To always create a new String Object even if it already exists in the String pool do:
  • String word = new String(“New Java Word”)
  • it will create a new string object in HEAP outside the String pool

String name = null

  • since it is null does not point to any object in Heap String pool
  • the name is just sitting in stack

wordNew = word.toUpperCase
- Will go to HEAP and it will point to the uppercase version of the word without changing the word itself. Therefore it is immutable

String word 2 = “hello” + “world”;

  • String pool will create hello, world and then hello world
  • word2 will point to hello world and the other hello and word will go to garbage collector
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

String builder vs String Buffer

A

Both allow us to use other methods to manipulate strings
However, they have different implementations
String input = “Java is good”;

StringBuilding word1 = new StringBuilder();
word1.append(input)

StringBuffer word2 = new StringBuffer(input);

Now you can do word1.reverse();

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

Encapsulation Principles?

A
  • Used for data hiding/ information hiding
  • Make instance variables Private and provide public getter and setter methods to have access
  • Private variables can only be accessed/changed in the same class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Encapsulation Benefits?

A
  • Protects instance variables from invalid values
  • We can use java code conditions in setter methods to provide criteria on how a value can be assigned. If criteria is not being met, then value is not assigned.
  • Customize getter methods to return data in certain ways
  • toString method is a special method that exists in each and every object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In Encapsulation what is immutable class ?

What is Write-only class?

A
  • We only provide getters and no setters
    • This means that the constructor must have the “this name = name” to store values
      Ex: Person person = new Person(“Johnny”, 25);
      person.getName();
      person.getAge();

Write only object? Provide only setters and no getters

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

Constructors facts:

A
  • Must have for every class, if not provide then java will provide a no arg constructor when the class is executed at run time.
  • No return type
  • no-arg constructor is known as default constructor
  • Constructor is called every time we use the “new” keyword
  • If you have various constructor with different args, the constructor that will be used will depend on which parameters are being sent.
Person person1 = new Person("Johnny", 20);
Person person1 = new Person("Johnny");
Person person1 = new Person(20);
Person person1 = new Person();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is composition ?

A
When a class has instance variables of type of another class
- HAS-A relationship

Bus:
Properties: Driver, Engine
Bus HAS-A Driver
Bus HAS-A Engine

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

Static - method related

A
  • Used to call methods from other classes without having to instantiate class.
  • Static methods can only access call of refer to other static variables or other static methods.
  • Instance methods can do both, static and non-static variables and methods
17
Q

Static - Variable

A
  • it is a shared variable, only single copy of that variable for the entire class.
  • Therefore, many other object of that class can change the values of this static keyword and it affect the other classes as well.
18
Q

Static Block

A
  • Code of block that runs BEFORE anything else when a class is loaded to memory.
  • Runs BEFORE constructor
  • Can be used to initialize and assign data to static variables
  • RUNS ONLY ONCE WHEN IT THAT CLASS IS CALLED FOR THE FIRST TIME
19
Q

Static Inner Class

A

In java, we can create a class inside another class

20
Q

IQ: Why is main method static ? Related to psvm

A
  • JRE can call the main method without creating object of the class.
  • Static makes the values to be objects so they go into HEAP.
Ex: 
public class HelloWorld {
	    public static void main(String[] args) {
	        System.out.println("Hello World!");
			    }
			}

So that JRE can call it using. HelloWorld.main(null);