Java_Strings_Encapsulation_Inheritance Flashcards

1
Q

Difference between creating a java string with same values using a “new” keyword and a string literal as below:

String myStr1 = new String(“Mulligan”);

String myStr2 = new String(“Mulligan”);

AND

String myStr1 = “Seagan”;

String myStr2 = “Seagan”;

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

Which java package has the String class?

A

java.lang.String

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

Explain sub String operations with examples.

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

As strings are immutable, explain a situation where they can cause memory leaks?

A

In the example below, as a is now pointing to a new string, the unreferenced heap are will cause a memory leak.

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

What are StringBuilder and StringBuffer in Java?

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

Difference between pass by value and pass by the object?

A
  • Pass by value will not reflect the value changed inside the called method() to the caller, as it creates a new stack for the called method with a local primitive value copied(only) and pops off the stack when returned to the caller.
  • Whereas pass by the object will reflect the change to the caller because the local reference created inside the called method will reference the same object on the heap.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is encapsulation in Java?

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

What is the default access modifier in Java?

A

When an access modifier is not specified in a class, the default access is available within the declaring class and the classes in the same package.

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

What are the different access modifiers in Java?

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

What is inheritance in Java?

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

How many classes can be inherited by a class in Java?

A

Each class in java can only extend one class.

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

Can we inherit the constructor of the parent class?

A

No. Every class should have its own constructor. It cannot inherit a constructor from the parent.

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

What is a chaining constructor?

A
  • When we do not have access to parent class constructor, we mostly try to use the getters and setters of the parent class by making them public or protected. But that breaks the idea of proper encapsulation.
  • Chaining constructor allows accessing parent class constructor using super keyword. And, this super() should be the first line in the child’s constructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens when we do not explicitly use a super() in the child class?

A

Remember when the parent class does not have a default constructor the child class will give a compilation error when super() is not used.

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

Where is the super called?

A

Because a parent should exist before the creation of a child.

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

What is overriding in Java?

A
17
Q

Can we override private and static methods?

A
18
Q

How do we call the parent’s method after we have overridden the method in the child class?

A
19
Q

What is a covariant return for overriding methods?

A

??

20
Q

How is the ‘final’ keyword used in Java?

A
21
Q

What does adding ‘final’ in front of a method do?

A
22
Q

What does ‘final’ in front of a class?

A
23
Q

Where else in Java do we can observe ‘final’ usage?

A