Posts tagged Java Interview Question
Packages and interface
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… 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.
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.
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.
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.
5) Is it is necessary to implement all methods in an interface?
Ans : Yes. All the methods have to be implemented.
6) Which is the default access modifier for an interface method?
Ans : public.
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.
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.
9) By default, all program import the java.lang package.
Ans : True hudihudi.com
10) Java compiler stores the .class files in the path specified in CLASSPATH
Ans : False
Ref Link contributor : http://www.hudihudi.com/placement/java/java25.htm
Permanent link to Java Tutorials / Java Interview Questions: http://www.freshersinterviewquestions.com/java-packages-and-interface/
Classes and Methods
50) Which are keywords in Java?
Ans : d and e
51) When must the main class and the file name coincide?
Ans :When class is declared public.
52) What are different modifiers?
Ans : public, private, protected, default, static, trancient, volatile, final, abstract.
53) What are access modifiers?
Ans : public, private, protected, default.
54) What is meant by “Passing by value” and ” Passing by reference”?
Ans : objects – pass by reference
Methods – pass by value
55) Is a class a subclass of itself?
Ans : A class is a subclass itself.
56) What modifiers may be used with top-level class?
Ans : public, abstract, final.
57) What is an example of polymorphism?
Ans : c
Ref Link contributor : http://www.hudihudi.com/placement/java/java24.htm
Permanent link to Java Tutorials / Java Interview Questions: http://www.freshersinterviewquestions.com/java-classes-and-methods-2