Java Flashcards

1
Q

In terms of inheritance, what is the effect of keeping a constructor private?

A

This prevents creating subclasses of this class. It also prevent instantiation of this class outside of the class itself.

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

Does the finally block get executed if we insert a return statement inside the try block of a try-catch-finally?

A

Yes. If the try block exits, the finally block is executed.

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

What is the difference between final, finally, and finalize?

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

Explain the difference between templates in C++ and generics in Java.

A
  • Java generics is rooted in an idea of “type erasure”. This technique eliminates the parameterized types when source code is translated to JVM bye code.
  • In C++, the compiler creates a new copy of the template code for each type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain what object reflection is in Java and why it is useful.

A

Object reflection is a feature in Java which provides a way to get reflective information about Java classes and objects, and perform operations such as:

  1. Getting information about the methods and fields present inside the class at runtime.
  2. Creating a new instance of a class.
  3. Getting and setting the object fields directly by getting field reference, regardless of what the access modifier is.

Useful because:

  1. It helps in observing or manipulating the runtime behavior of applications.
  2. It can help in debugging or testsing programs, as we have direct access to methods, constructors and fields.
  3. We can call methods by name when we don’t know the method in advance.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly