core java interview question and answers, core java interview question and answers for freshers
Hi , Today iam sharing some more core java interview question and answers.
Hi , Today iam sharing some more core java interview question and answers.
1. Is it possible for class declared as private be accessed outside its package?
Not possible for class .
2. Can class be declared as protected?
A class cannot be declared as protected.
3. What is access scope of protected method?
A protected method can be accessed by the classes within the same package or can be accessed by subclasses of the class in any package.
4. What is purpose of declaring a variable as final?
A final variables value cant be changed. The final variables should be initialized before using them.
5. What is impact of declaring method as final?
A method declared as final cannot t be overridden. so A subclass cant have same method signature with a different implementation.
6. I dont want my class to be inherited by any other class. What should i do provide solution?
You should declared your class as final. But you cannot t define your class as final, if it is an abstract class. A class declared as final cannot t be extended by any other class.
7. Can you give few examples of final classes ?
java.lang.String, java.lang.Math.
8. How is final different from finally and finalize()?
final is a modifier which can be applied to a class /method /variable. Final class can't be inherited, final method can't be overridden and final variable can't be changed.
finally is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment.
whereas finalize() is method of Object class which will be executed by JVM just before garbage
collecting object to give a final chance for the resource releasing activity.
finally is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment.
whereas finalize() is method of Object class which will be executed by JVM just before garbage
collecting object to give a final chance for the resource releasing activity.
9. Can class be declared as static explain ?
We cannot declare top level class as static, but only the inner class can be declared static.
for example :
for example :
public class Test
{
static class InnerClass
{
public static void InnerMethod()
{ System.out.println("Static Inner Class!"); }
}
public static void main(String args[])
{
Test.InnerClass.InnerMethod();
}
}
//output: Static Inner Class!
10. When will you define method as static?
When a method needs to be accessed even before the creation of the object of the class then we should declare method as static.
11. What are restriction imposed on a static method or static block of code?
A
static method should not refer to instance variables without creating instance and cannot use "this" operator to refer instance
12. If i want to print "Hello" even before main() is executed. How to acheive this ?
Print
the statement inside a static block of code. Static blocks get executed
when class gets loaded into memory and even before creation
of an object. Hence it will be executed before the main() method. and this will be executed only once
ConversionConversion EmoticonEmoticon