Packages and interface
Interview Questions With Answers
Packages and interface
Q:
1) What are packages ? what is use of packages ?
Ans :The package statement defines a name space in which classes are stored.If you omit the package, the classes are put into the default
package.
Signature/java/. package pkg;
Use: * It specifies to which package the classes defined in a file belongs
to. * Package is both naming and a visibility control mechanism.
Q:
2) What is difference between importing "java.applet.Applet" and "java.applet.*;"
?
Ans :"java.applet.Applet" will import only the class Applet from the
package java.applet
Where as "java.applet.*" will import all the classes from java.applet
package.
Q:
3) What do you understand by package access specifier?
Ans : public: Anything declared as public can be accessed from anywhere
private: Anything declared in the private can’t be seen outside of its
class.
default: It is visible to subclasses as well as to other classes in the
same package.
Q:
4) What is interface? What is use of interface?
Ans : It is similar to class which may contain method’s signature only but
not bodies.
Methods declared in interface are abstract methods. We can implement many
interfaces on a class which support the multiple inheritance.
Q:
5) Is it is necessary to implement all methods in an interface?
Ans : Yes. All the methods have to be implemented.
Q:
6) Which is the default access modifier for an interface method?
Ans : public.
Q:
7) Can we define a variable in an interface ?and what type it should be ?
Ans : Yes we can define a variable in an interface. They are implicitly
final and static.
Q:
8) What is difference between interface and an abstract class?
Ans : All the methods declared inside an Interface are abstract. Where as
abstract class must have at least one abstract method and others may be
concrete or abstract.
In Interface we need not use the keyword abstract for the methods.
Q:
9) By default, all program import the java.lang package.
True/False
Ans : True
Q:
10) Java compiler stores the .class files in the path specified in
CLASSPATH
environmental variable.
True/False
Ans : False
Q:
11) User-defined package can also be imported just like the standard
packages.
True/False
Ans : True
Q:
12) When a program does not want to handle exception, the ______class is
used.
Ans : Throws
Q:
13) The main subclass of the Exception class is _______ class.
Ans : RuntimeException
Q:
14) Only subclasses of ______class may be caught or thrown.
Ans : Throwable
Q:
15) Any user-defined exception class is a subclass of the _____ class.
Ans : Exception
Q:
16) The catch clause of the user-defined exception class should ______ its
Base class catch clause.
Ans : Exception
17) A _______ is used to separate the hierarchy of the class while
declaring an Import statement.
Ans : Package
Q:
18) All standard classes of Java are included within a package called
_____.
Ans : java.lang
Q:
19) All the classes in a package can be simultaneously imported using
____.
Ans : *
Q:
20) Can you define a variable inside an Interface. If no, why? If yes,
how?
Ans .: YES. final and static
Q:
21) How many concrete classes can you have inside an interface?
Ans .: None
Q:
22) Can you extend an interface?
Ans .: Yes
Q:
23) Is it necessary to implement all the methods of an interface while
implementing the interface?
Ans .: No
Q:
24) If you do not implement all the methods of an interface while
implementing , what specifier should you use for the class ?
Ans .: abstract
Q:
25) How do you achieve multiple inheritance in Java?
Ans : Using interfaces.
Q:
26) How to declare an interface example?
Ans : access class classname implements interface.
Q:
27) Can you achieve multiple interface through interface?
a)True
b) false
Ans : a.
Q:
28) Can variables be declared in an interface ? If so, what are the
modifiers?
Ans : Yes. final and static are the modifiers can be declared in an
interface.
Q:
29) What are the possible access modifiers when implementing interface
methods?
Ans : public.
Q:
30) Can anonymous classes be implemented an interface?
Ans : Yes.
Q:
31) Interfaces can’t be extended.
a)True
b)False
Ans : b.
Q:
32) Name interfaces without a method?
Ans : Serializable, Cloneble & Remote.
Q:
33) Is it possible to use few methods of an interface in a class ? If so,
how?
Ans : Yes. Declare the class as abstract.
Packages and interface
Interview Questions With Answers
Tolal:0 Click:
Show All Comments