Você está na página 1de 11

Print Page - Java Interview Questions-2

CareerEnclave.Com

Knowledge Base ( Powered By TechSlash.Com ) => J2EE Technology => Topic


started by: Swapna_S on June 21, 2006, 06:48:02 PM

Title: Java Interview Questions-2


Post by: Swapna_S on June 21, 2006, 06:48:02 PM

For Technology & CAREER News Alerts & Updates, Join Our Group now
http://finance.groups.yahoo.com/group/careerenclave/ (http://finance.
groups.yahoo.com/group/careerenclave/)

What is the difference between an Interface and an Abstract class?

An Abstract class declares have at least one instance method that is declared
abstract which will be implemented by the subclasses. An abstract class can
have instance methods that implement a default behavior. An Interface can only
declare constants and instance methods, but cannot implement default
behavior.

What is the purpose of garbage collection in Java, and when is it used?

The purpose of garbage collection is to identify and discard objects that are no
longer needed by a program so that their resources can be reclaimed and
reused. A Java object is subject to garbage collection when it becomes
unreachable to the program in which it is used.

Describe synchronization in respect to multithreading.?

With respect to multithreading, synchronization is the capability to control the


access of multiple threads to shared resources. Without synchonization, it is
possible for one thread to modify a shared variable while another thread is in
the process of using or updating same shared variable. This usually leads to
significant errors.

Explain different way of using thread?

The thread could be implemented by using runnable interface or by inheriting


from the Thread class. The former is more advantageous, 'cause when you are
going for multiple inheritance..the only interface can help.

What are pass by reference and passby value?

Pass By Reference means the passing the address itself rather than passing the

http://www.careerenclave.com/jobs/index.php?action=printpage;topic=1858.0 (1 of 3)6/23/2006 10:40:02 PM


Print Page - Java Interview Questions-2

value. Passby Value means passing a copy of the value to be passed.

What is HashMap and Map?

Map is Interface and Hashmap is class that implements that.

Difference between HashMap and HashTable?

The HashMap class is roughly equivalent to Hashtable, except that it is


unsynchronized and permits nulls. (HashMap allows null values as key and value
whereas Hashtable doesnt allow). HashMap does not guarantee that the order of
the map will remain constant over time. HashMap is non synchronized and
Hashtable is synchronized.

Difference between Vector and ArrayList?

Vector is synchronized whereas arraylist is not.

Difference between Swing and Awt?

AWT are heavy-weight componenets. Swings are light-weight components.


Hence swing works faster than AWT.

What is the difference between a constructor and a method?

A constructor is a member function of a class that is used to create objects of


that class. It has the same name as the class itself, has no return type, and is
invoked using the new operator. A method is an ordinary member function of a
class. It has its own name, a return type (which may be void), and is invoked
using the dot operator.

What is an Iterators?

Some of the collection classes provide traversal of their contents via a java.util.
Iterator interface. This interface allows you to walk a collection of objects,
operating on each object in turn. Remember when using Iterators that they
contain a snapshot of the collection at the time the Iterator was obtained;
generally it is not advisable to modify the collection itself while traversing an
Iterator.

State the significance of public, private, protected, default modifiers


both singly and in combination and state the effect of package
relationships on declared items qualified by these modifiers.?

public : Public class is visible in other packages, field is visible everywhere (class
must be public too) private : Private variables or methods may be used only by
an instance of the same class that declares the variable or method, A private
feature may only be accessed by the class that owns the feature. protected : Is
available to all classes in the same package and also available to all subclasses

http://www.careerenclave.com/jobs/index.php?action=printpage;topic=1858.0 (2 of 3)6/23/2006 10:40:02 PM


Print Page - Java Interview Questions-2

of the class that owns the protected feature.This access is provided even to
subclasses that reside in a different package from the class that owns the
protected feature. default :What you get by default ie, without any access
modifier (ie, public private or protected).It means that it is visible to all within a
particular package.

What is an abstract class?

Abstract class must be extended/subclassed (to be useful). It serves as a


template. A class that is abstract may not be instantiated (ie, you may not call
its constructor), abstract class may contain static data. Any class with an
abstract method is automatically abstract itself, and must be declared as such. A
class may be declared abstract even if it has no abstract methods. This prevents
it from being instantiated.

What is static in java?

Static means one per class, not one for each object no matter how many
instance of a class might exist. This means that you can use them without
creating an instance of a class.Static methods are implicitly final, because
overriding is done based on the type of the object, and static methods are
attached to a class, not an object. A static method in a superclass can be
shadowed by another static method in a subclass, as long as the original method
was not declared final. However, you can't override a static method with a
nonstatic method. In other words, you can't change a static method into an
instance method in a subclass.

CareerEnclave.Com | Powered by SMF 1.0.7.


© 2001-2005, Lewis Media. All Rights Reserved.

http://www.careerenclave.com/jobs/index.php?action=printpage;topic=1858.0 (3 of 3)6/23/2006 10:40:02 PM


http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQOeybyuLoGAn...xVqeSMuH8e1C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm

Quick Revision Tips

1. An identifier in java must begin with a letter , a dollar sign($), or an underscore (-); subsequent
characters may be letters, dollar signs, underscores, or digits.

2. There are three top-level elements that may appear in a file. None of these elements is required. If
they are present, then they must appear in the following order:
-package declaration
-import statements
-class definitions

3. A java source file (.java file) can't have more than one public class , interface or combination of
both.
4. "goto" is a keyword in Java.

5. NULL is not a keyword, but null is a keyword in Java.


6. A final class can't be subclassed.

7. A final method can't be overridden but a non final method can be overridden to final method.

8. Abstract classes can't be instantiated and should be subclassed.

9. Primitive Wrapper classes are immutable.


10. A static method can't refer to "this" or "super".

11. A static method can't be overridden to non-static and vice versa.

12. The variables in java can have the same name as method or class.
13. All the static variables are initialized when the class is loaded.
14. An interface can extend more than one interface, while a class can extend only one class.
15. The variables in an interface are implicitly final and static. If the interface , itself, is declared as
public the methods and variables are implicitly public.

16. Variables cannot be synchronized.

17. Instance variables of a class are automatically initialized, but local variables of a function need to
be initialized explicitly.
18. An abstract method cannot be static because the static methods can't be overridden.

19. The lifecycle methods of an Applet are init(), start(), stop() and destroy().

http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQ...C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm (1 of 8)6/23/2006 10:55:29 PM


http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQOeybyuLoGAn...xVqeSMuH8e1C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm

20. The transient keyword is applicable to variables only.

21. The native keyword is applicable to methods only.

22. The final keyword is applicable to methods , variables and classes.

23. The abstract keyword is applicable to methods and classes.

24. The static keyword is applicable to variables, methods or a block of code called static initializes.

25. A native method can't be abstract but it can throw exception(s).

26. A final class cannot have abstract methods.

27. A class can't be both abstract and final.

28. A transient variable may not be serialized.

29. All methods of a final class are automatically final.

30. While casting one class to another subclass to superclass is allowed without any type casting.
e.g.. A extends B , B b = new A(); is valid but not the reverse.

31. The String class in java is immutable. Once an instance is created, the string it contains cannot be
changed.
e.g. String s1 = new String("test"); s1.concat("test1"); Even after calling concat() method on s1,
the value of s1 will remain to be "test". What actually happens is a new instance is created.
But the StringBuffer class is mutable.

32. The + and += operators are the only cases of operator overloading in java and is applicable to
Strings.

33. Bit-wise operators - &, ^ and | operate on numeric and boolean operands.
34. The short circuit logical operators && and || provide logical AND and OR operations on boolean
types and unlike & and | , these are not applicable to integral types. The valuable additional
feature provided by these operators is the right operand is not evaluated if the result of the
operation can be determined after evaluating only the left operand.

35. The difference between x = ++y; and x = y++;


In the first case y will be incremented first and then assigned to x. In second case first y will be
assigned to x then it will be incremented.

http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQ...C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm (2 of 8)6/23/2006 10:55:29 PM


http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQOeybyuLoGAn...xVqeSMuH8e1C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm

36. Please make sure you know the difference between << , >> and >>>(unsigned rightshift)
operators.

37. The initialization values for different data types in java is as follows
byte = 0, int = 0, short = 0, char = '\u0000', long = 0L, float = 0.0f, double = 0.0d, boolean =
false,
object referenece (of any object) = null.

38. Setting the object reference to null makes it a candidate for garbage collection.

39. An overriding method may not throw a checked exception unless the overridden method also
throws that exception or a superclass of that exception.

40. Interface methods can't be native, static, synchronized, final, private, protected or abstract.

41. Abstract classes can have constructors , and it can be called by super() when its subclassed.

42. A final variable is a constant and a static variable is like a global variable.

43. The String class is a final class, it can't be subclassed.

44. The Math class has a private constructor, it can't be instantiated.

45. The random() method of Math class in java returns a random number , a double , between 0.0 and
1.0.

46. The java.lang.Throwable class has two subclasses : Exception and Error.

47. An Error indicates serious problems that a reasonable application should not try to catch. Most
such errors are abnormal conditions.

48. The two kinds of exceptions in java are : Compile time (Checked ) and Run time (Unchecked)
exceptions. All subclasses of Exception except the RunTimeException and its subclasses are
checked exceptions.
Examples of Checked exception : IOException, ClassNotFoundException.
Examples of Runtime exception :ArrayIndexOutOfBoundsException,NullPointerException,
ClassCastException, ArithmeticException, NumberFormatException.

49. The unchecked exceptions do not have to be caught.

http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQ...C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm (3 of 8)6/23/2006 10:55:29 PM


http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQOeybyuLoGAn...xVqeSMuH8e1C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm

50. A try block may not be followed by a catch but in that case, finally block must follow try.

51. While using multiple catch blocks, the type of exception caught must progress from the most
specific exception to catch to the superclass(es) of these exceptions.

52. More than one exception can be listed in the throws clause of a method using commas. e.g. public
void myMethod() throws IOException, ArithmeticException.

53. On dividing an integer by 0 in java will throw ArithmeticException.

54. The various methods of Java.lang.Object are


clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString and wait.

55. The Java.lang.System is a final class and can't be subclassed.

56. Garbage collection in java cannot be forced. The methods used to call garbage collection thread
are System.gc() and Runtime.gc()

57. To perform some task when an object is about to be garbage collected, you can override the
finalize() method of the Object class. The JVM only invokes finalize() method once per object.
The signature of finalize() method of Object class is : protected void finalize() throws Throwable.

58. The parent classes of input and output streams : InputStream and OutputStream class are abstract
classes.

59. File class can't be used to create a new file. For that any other output stream class such as
FileOutputStream or filter streams like PrintWriter should be used. Please note that java will
create a file only when the file is not available.

60. For the RandomAccessFile constructor, the mode argument must either be equal to "r" or "rw".
61. In java, console input is accomplished by reading from System.in .
62. System.in is an object of type InputStream; System.out and System.err are objects of type
PrintStream.
63. Both FileInputStream and FileOutputStream classes throws FileNotFoundException. In earlier
versions of java, FileOutputStream() threw an IOException when an output could not be created.
64. System.in is an object of type InputStream; System.out and System.err are objects of type
PrintStream.
65. A method can't be overridden to be more private. e.g.. a public method can only be overridden to
be public.

66. Both primitives and object references can be cast.

http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQ...C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm (4 of 8)6/23/2006 10:55:29 PM


http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQOeybyuLoGAn...xVqeSMuH8e1C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm

67. If inner class is declared in a method then it can access only final variables of the particular
method but can access all variables of the enclosing class.

68. To refer to a field or method in the outer class instance from within the inner class, use Outer.this.
fieldname .
69. Inner classes may not declare static initializers or static members unless they are compile time
constants i.e. static final var = value;
70. A nested class cannot have the same name as any of its enclosing classes.
71. Inner class may be private, protected, final, abstract or static.

72. An example of creation of instance of an inner class from some other class:
class Outer
{
public class Inner{}
}

class Another
{
public void amethod()
{
Outer.Inner i = new Outer().new Inner();
}
}

73. Classes defined in methods can be anonymous, in which case they must be instantiated at the
same point they are defined. These classes can't have explicit constructor and may implement
interface or extend other classes.

74. The Thread class resides in java.lang package and need not be imported.

75. The sleep and yield methods of Thread class are static methods.

76. The range of Thread priority in java is 1-10. The minimum priority is 1 and the maximum is 10.
The default priority of any thread in java is 5.

77. There are two ways to provide the behavior of a thread in java: extending the Thread class or
implementing the Runnable interface.

78. The only method of Runnable interface is "public void run();".

79. New thread take on the priority of the thread that spawned it.

http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQ...C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm (5 of 8)6/23/2006 10:55:29 PM


http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQOeybyuLoGAn...xVqeSMuH8e1C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm

80. Using the synchronized keyword in the method declaration, requires a thread obtain the lock for
this object before it can execute the method.
81. A synchronized method can be overridden to be not synchronized and vice versa.
82. In java terminology, a monitor is any object that has some synchronized code.

83. Both wait() and notify() methods must be called in synchronized code.

84. The notify() mthod moves one thread, that is waiting on this object's monitor, into the Ready
state. This could be any of the waiting threads.
85. The notifyAll() method moves all threads, waiting on this object's monitor into the Ready state.
86. Every object has a lock and at any moment that lock is controlled by, at most, one single thread.

87. There are two ways to mark code as synchronized:


a.) Synchronize an entire method by putting the synchronized modifier in the method's
declaration.
b.) Synchronize a subset of a method by surrounding the desired lines of code with curly brackets
({}).

88. The argument to switch can be either byte, short , char or int.

89. The expression for an if and while statement in java must be a boolean.

90. Breaking to a label (using break <labelname>;) means that the loop at the label will be terminated
and any outer loop will keep iterating. While a continue to a label (using continue <lablename>;)
continues execution with the next iteration of the labeled loop.

91. A static method can only call static variables or other static methods, without using the instance
of the class. e.g. main() method can't directly access any non static method or variable, but using
the instance of the class it can.

92. instanceof is a java keyword not instanceOf.

93. The if() statement in java takes only boolean as an argument. Please note that if (a=true){},
provided a is of type boolean is a valid statement and the code inside the if block will be
executed.

94. The (-0.0 == 0.0) will return true, while (5.0==-5.0) will return false.

95. An abstract class may not have even a single abstract method but if a class has an abstract method
it has to be declared as abstract.

96. Collection is an Interface where as Collections is a helper class.

http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQ...C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm (6 of 8)6/23/2006 10:55:29 PM


http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQOeybyuLoGAn...xVqeSMuH8e1C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm

97. The default Layout Manager for Panel and Applet is Flow. For Frame and Window its
BorderLayout.

98. The FlowLayout always honors the a component's preferred size.

99. BorderLayout honors the preferred width of components on the East and West, while the
preferred height is honored for the components in the North and South.
100. The java.awt.event package provides seven adapter classes, one for each listener interface. All
these adapter classes have do-nothing methods (empty bodies) , implementing listener interface.

101. A componenet subclass may handle its own events by calling enableEvents(), passing in an even
mask.
102. The Listener interfaces inherit directly from java.util.EventListener interface.

103. A Container in java is a Component (Container extends Component) that can contain other
components.

104. Graphics class is abstract, hence cannot be instantiated.


105. The repaint() method invokes a component's update() method() which in turn invokes paint()
method.

106. The Applet class extends Panel, Frame class extends Window.

107. A String in java is initialized to null, not empty string and an empty string is not same as a null
string.

108. The statement float f = 5.0; will give compilation error as default type for floating values is
double and double can't be directly assigned to float without casting.

109. The equals() method in String class compares the values of two Strings while == compares the
memory address of the objects being compared.
e.g. String s = new String("test"); String s1 = new String("test");
s.equals(s1) will return true while s==s1 will return false.

110. The valueOf() method converts data from its internal format into a human-readable form. It is a
static method that is overloaded within String class for all of Java's built-in types, so that each
type can be converted properly into a string.
111. The main difference between Vector and ArrayList is that Vector is synchronized while the
ArrayList is not.
112. A Set is a collection, which cannot contain any duplicate elements and has no explicit order to its
elements.

http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQ...C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm (7 of 8)6/23/2006 10:55:29 PM


http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQOeybyuLoGAn...xVqeSMuH8e1C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm

113. A List is a collection, which can contain duplicate elements, and the elements are ordered.
114. A Map does not implement the Collection interface.
115. The following definition of main method is valid : static public void main(String[] args).

116. The main() method can be declared final.

117. The example of array declaration along with initialization - int k[] = new int[]{1,2,3,4,9};

118. The size of an array is given by arrayname.length.


119. The local variables (variables declared inside method) are not initialized by default. But the array
elements are always initialized wherever they are defined be it class level or method level.

120. In an array , the first element is at index 0 and the last at length-1. Please note that length is a
special array variable and not a method.

121. The octal number in java is preceded by 0 while the hexadecimal by 0x (x may be in small case
or upper case)
e.g.
octal :022
hexadecimal :0x12

122. A constructor cannot be native, abstract, static, synchronized or final.

123. Constructors are not inherited so not possible to override them.

124. A constructor body can include a return statement providing no value is returned .
125. A constructor never return a value. If you specify a return value, the JVM (java virtual machine)
will interpret it as a method.

126. A call to this() or super() in a constructor must be placed at the first line.

127. If a class contains no constructor declarations, then a default constructor that takes no arguments
is supplied. This default constructor invokes the no-argument constructor of the super class via
super() call (if there is any super class).

128. Be careful for static and abstract key word in the program.

129. Also be careful for private keyword in front of a class.

130. The UTF characters in java are as big as they need to be while Unicode characters are all 16 bits.

http://f1.grp.yahoofs.com/v1/EB6cRAOLysvCKfh5KGYnQ...C6_zMLo0CVciaRsQ7958udz1Ffw/Tutors/java%20tips.htm (8 of 8)6/23/2006 10:55:29 PM

Você também pode gostar