Introduction to Objects Flashcards

1
Q

In which memory does the object is allocated on JVM?

A
  • In the memory managed by the JVM behind the scenes, object references and the objects assigned to them are managed by Stacks and heap.
  • Heap is a large memory where all the objects are going to live, whereas Stack is a short term memory where we have reference variables and so on.
  • Stacks are a list of references defined within a given block of code(also known as scope).
  • Heap is where all objects are actually stored in memory.
  • References in different stacks can point to the same objects in Heap.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Object-Oriented Programming?

A
  • Object-oriented programming represents a way of creating code.
  • The created code mimics real-world entities or “objects”.
  • Objects are software bundles of data and related procedures.
  • Objects contain state and/or behavior.
  • Java refers to behavior as Methods, they are called function, procedures, or subroutines in other languages.
  • For example, a bank account would contain data such as its balance, account number, and owner information.
  • Refer to more details in the image below:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the advantages of OOP?

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

Objects vs Primitives?

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

Provide examples of creating objects in Java.

A
  • The technical term for creating an object is called Instantiation.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How is an object created, explain in detail?

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

What happens when an object is Instantiated more than once?

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

How are methods in an object are invoked?

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

What are constructors and its uses?

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

What happens behind the scene when we use ‘new’ for invoking a constructor?

A
  1. “new” dynamically allocates space on the heap
  2. The object is created and its instance variables are initialized with default values. For string objects ‘null’ is the default value.
  3. Explicit initialization is executed with the values assigned during object instantiation.
  4. The final step is to call the constructor, thereby initializing the instance variables based on what the constructor indicates.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the default values for all the variables in Java?

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

Is it valid to use an instance variable inside a class to use before initialized?

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

What is a default constructor?

A

The default constructor is a no-argument constructor that is provided by Java if you define a class without explicitly defining any constructors.

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

Can we instantiate an object with the default constructor when it is not defined in the class?

A
  • To fix the code below add a no-argument constructor inside the class Car. Car() { }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an initialization block?

A

It is a block of code that is run every time an object is instantiated.

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

Does the placement of initialization blocks inside a class matter? Explain with example.

A

In the below code as the initialization block is placed before the definition “Green”, it is overridden and it gets value “Green”.

Note: Irrespective of placement of initialization block, if a value is defined inside a constructor like in below “Blue”, that will be executed last and it gets “Blue”.

public class Car {
    {
        color = "Red";
    }
    String color = "Green";
Car() {
    //color = "Blue";
} } ~~~ ```

In the below code as the initialization block is placed after the definition “Green”, it gets value “Red”.

public class Car {

    String color = "Green";
Car() {
    //color = "Blue";
} ~~~

{
    color = "Red";
} } ~~~

In below code color variable gets “Blue” as initial value, as always constructor execution is done at the end.

public class Car {

    String color = "Green";

    Car() {
        color = "Blue";
    }

    {
        color = "Red";
    }
}