21-30 Flashcards

1
Q

Can we declare the main method of our class as private?

A

In java, main method must be public static in order to run any application correctly. If main method is declared as private, developer won’t get any compilation error however, it will not get executed and will give a runtime error.

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

How can we pass argument to a function by reference instead of pass by value?

A

In java, we can pass argument to a function only by value and not by reference.

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

Is it compulsory for a Try Block to be followed by a Catch Block in Java for Exception handling?

A

Try block needs to be followed by either Catch block or Finally block or both. Any exception thrown from try block needs to be either caught in the catch block or else any specific tasks to be performed before code abortion are put in the Finally block.

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

Is there any way to skip Finally block of exception even if some exception occurs in the exception block?

A

If an exception is raised in Try block, control passes to catch block if it exists otherwise to finally block. Finally block is always executed when an exception occurs and the only way to avoid execution of any statements in Finally block is by aborting the code forcibly by writing System.exit(0) at the end of try block.

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

When is the constructor of a class invoked?

A

The constructor of a class is invoked every time an object is created with new keyword.

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

Can a class have multiple constructors?

A

Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects.

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

Can we override static methods of a class?

A

We cannot override static methods. Static methods belong to a class and not to individual objects and are resolved at the time of compilation (not at runtime).Even if we try to override static method,we will not get a compilation error,nor the impact of overriding when running the code.

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