Interview Questions

Q:

I want to print "Hello" even before main() is executed. How will you acheive that?

Answer Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main() method. And it will be executed only once.
Report Error

View answer Workspace Report Error Discuss

Subject: Java

6 7740
Q:

I do not want my class to be inherited by any other class. What should i do?

Answer You should declared your class as final. But you can't define your class as final, if it is an abstract class. A class declared as final can't be extended by any other class.
Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 2064
Q:

What is the impact of declaring a method as final?

Answer A method declared as final can't be overridden. A sub-class can't have the same method signature with a different implementation.
Report Error

View answer Workspace Report Error Discuss

Subject: Java

1 2849
Q:

What is the purpose of declaring a variable as final?

Answer A final variable's value can't be changed. final variables should be initialized before using them.
Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 2227
Q:

What is the access scope of a protected method?

Answer A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.
Report Error

View answer Workspace Report Error Discuss

Subject: Java

1 5364
Q:

Can a class be declared as protected?

Answer A class can't be declared as protected. only methods can be declared as protected.
Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 2109
Q:

Which package is imported by default?

Answer java.lang package is imported by default even without a package declaration.
Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1898
Q:

Can a source file contain more than one class declaration?

Answer Yes a single source file can contain any number of Class declarations but only one of the class can be declared as public.
Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 2459