81-90 Flashcards

1
Q

In Java thread programming, which method is a must implementation for all threads?

A

Run() is a method of Runnable interface that must be implemented by all threads.

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

I want to control database connections in my program and want that only one thread should be able to make database connection at a time. How can I implement this logic?

A

This can be implemented by use of the concept of synchronization. Database related code can be placed in a method which hs synchronized keyword so that only one thread can access it at a time.

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

How can an exception be thrown manually by a programmer?

A

In order to throw an exception in a block of code manually, throw keyword is used. Then this exception is caught and handled in the catch block.

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

I want my class to be developed in such a way that no other class (even derived class) can create its objects. How can I do so?

A

If we declare the constructor of a class as private, it will not be accessible by any other class and hence, no other class will be able to instantiate it and formation of its object will be limited to itself only.

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

How objects are stored in Java?

A

In java, each object when created gets a memory space from a heap. When an object is destroyed by a garbage collector, the space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects.

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

How can we find the actual size of an object on the heap?

A

In java, there is no way to find out the exact size of an object on the heap.

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

What happens if an exception is not handled in a program?

A

If an exception is not handled in a program using try catch blocks, program gets aborted and no statement executes after the statement which caused exception throwing.

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

I have multiple constructors defined in a class. Is it possible to call a constructor from another constructor’s body?

A

If a class has multiple constructors, it’s possible to call one constructor from the body of another one using this().

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

What’s meant by anonymous class?

A

An anonymous class is a class defined without any name in a single line of code using new keyword.

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