Core Interview Questions
Core Interview Questions
Core Interview Questions
main() method is called by the JVM even before the instantiation of the class
hence it is declared as static.
18. What is the arguement of main() method?
main() method accepts an array of String object as arguement.
19. Can a main() method be overloaded?
Yes. You can have any number of main() methods with different method
signature and implementation in the class.
20. Can a main() method be declared final?
Yes. Any inheriting class
default main() method.
will
not
be
able
to
have
it's
own
21. Does the order of public and static declaration matter in main()
method?
No. It doesn't matter but void should always come before main().
22. Can a source file contain more than one class declaration?
Yes a single source file can contain any number of Class declarations but
only one of the class can be declared as public.
23. What is a package?
Package is a collection of related classes and interfaces. package declaration
should be first statement in a java class.
24. Which package is imported by default?
java.lang package is imported by default even without a package
declaration.
25. Can a class declared as private be accessed outside it's package?
Not possible.
26. Can a class be declared as protected?
be
should
be
We can not declare top level class as static, but only inner class can be
declared static.
publicclassTest
{
staticclassInnerClass
{
publicstaticvoidInnerMethod()
{System.out.println("StaticInnerClass!");}
}
publicstaticvoidmain(Stringargs[])
{
Test.InnerClass.InnerMethod();
}
}
//output:StaticInnerClass!
34. When will you define a method as static?
When a method needs to be accessed even before the creation of the object
of the class then we should declare the method as static.
35. What are the restriction imposed on a static method or a static
block of code?
A static method should not refer to instance variables without creating an
instance and cannot use "this" operator to refer the instance.
36. I want to print "Hello" even before main() is executed. How will
you acheive that?
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.
37. What is the importance of static variable?
static variables are class level variables where all objects of the class refer to
the same variable. If one object changes the value then the change gets
reflected in all the objects.
38. Can we declare a static variable inside a method?
Static varaibles are class level variables and they can't be declared inside a
method. If declared, the class will not compile.
39. What is an Abstract Class and what is it's purpose?
A Class which doesn't provide complete implementation is defined as an
abstract class. Abstract classes enforce abstraction.
40. Can a abstract class be declared final?
Not possible. An abstract class without being inherited is of no use and
hence will result in compile time error.
41. What is use of a abstract variable?
Variables can't be declared as abstract. only classes and methods can be
declared as abstract.
42. Can you create an object of an abstract class?
Not possible. Abstract classes can't be instantiated.
43. Can a abstract class be defined without any abstract methods?
Yes it's possible. This is basically to avoid instance creation of the class.
44. Class C implements Interface I containing method m1 and m2
declarations. Class C has provided implementation for method m2.
Can i create an object of Class C?
No not possible. Class C should provide implementation for all the methods
in the Interface
I. Since Class
C didn't provide implementation
for m1 method, it has to be declared as abstract. Abstract classes can't be
instantiated.
45. Can a method inside a Interface be declared as final?
No
not
possible.
Doing
so
will
result
in
compilation
error. public and abstract are the only applicable modifiers for method
declaration in an interface.
46. Can an Interface implement another Interface?
hence
interface
cannot