Top Java Frequently Asked Questions



1. Can you write a Java class that could be used both as an applet as well as an application?
Yes. Just, add a main() method to the applet.

2. Explain the usage of Java packages.
This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

3. If a class is located in a package, what do you need to change in the OS environment to be able to use it?
You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Once the class is available in the CLASSPATH, any other Java program can use it.

4. What's the difference between J2SDK 1.5 and J2SDK 5.0?
There's no difference, Sun Microsystems just re-branded this version.

5. What are the static fields & static Methods ?
If a field or method defined as a static, there is only one copy for entire class, rather than one copy for each instance of class. static method cannot accecss non-static field or call non-static methods

6. How are Observer and Observable used?
Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

7. Is null a keyword?
No, the null value is not a keyword.

8. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.

9. How does Java handle integer overflows and underflows?
It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

10. What is the difference between the >> and >>> operators?
The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.

11. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.

12. Is sizeof a keyword?
No, the sizeof operator is not a keyword.

13. What restrictions are placed on the location of a package statement within a source code file?
A package statement must appear as the first line in a source code file (excluding blank lines and comments). It cannot appear anywhere else in a source code file.

14. What value does readLine() return when it has reached the end of a file?
The readLine() method returns null when it has reached the end of a file.

15. What is a native method?
A native method is a method that is implemented in a language other than Java.

16. Can a for statement loop indefinitely?
Yes, a for statement can loop indefinitely.
Ex:
for(;;) ;

17. What are order of precedence and associativity, and how are they used?
Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left

18. What is the range of the short type?

The range of the short data type is -(2^15) to 2^15 - 1.

19. What is the range of the char type?
The range of the char type is 0 to 2^16 - 1.

20. What is the difference between the Boolean & operator and the && operator?
&& is a short-circuit AND operator - i.e., The second condition will be evaluated only if the first condition is true. If the first condition is true, the system does not waste its time executing the second condition because, the overall output is going to be false because of the one failed condition.

& operator is a regular AND operator - i.e., Both conditions will be evaluated always.

21. What is the GregorianCalendar class?
The GregorianCalendar provides support for traditional Western calendars.

22. What is the purpose of the Runtime class?
The purpose of the Runtime class is to provide access to the Java runtime system.

23. What is the argument type of a program's main() method?
A program's main() method takes an argument of the String[] type. (A String Array)

24. Which Java operator is right associative?
The = operator is right associative.

25. What is the Locale class?
This class is used in conjunction with DateFormat and NumberFormat to format dates, numbers and currency for specific locales. With the help of the Locale class you’ll be able to convert a date like “10/10/2005” to “Segunda-feira, 10 de Outubro de 2005” in no time. If you want to manipulate dates without producing formatted output, you can use the Locale class directly with the Calendar class

26. Can a double value be cast to a byte?
Yes, a double value can be cast to a byte. But, it will result in loss of precision.

27. What is the difference between a break statement and a continue statement?
A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the beginning of the loop.

28. How are commas used in the intialization and iterationparts of a for statement?
Commas are used to separate multiple statements within the initialization and iteration parts of a for statement.

29. How are Java source code files named?
A Java source code file takes the name of a public class or interface that is defined within the file. A source code file may contain at most one public class or interface. If a public class or interface is defined within a source code file, then the source code file must take the name of the public class or interface. If no public class or interface is defined within a source code file, then the file can take on a name that is different than its classes and interfaces. Source code files use the .java extension.

30. What value does read() return when it has reached the end of a file?
The read() method returns -1 when it has reached the end of a file.

31. Can a Byte object be cast to a double value?
No, an object cannot be cast to a primitive value.

32. What is the Dictionary class?
The Dictionary class provides the capability to store key-value pairs. It is the predecessor to the current day HashMap and Hashtable.

33. What is the % operator?
It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.

34. What is the difference between the Font and FontMetrics classes?
The FontMetrics class is used to define implementation-specific properties, such as ascent and descent, of a Font object.

35. How is rounding performed under integer division?

The fractional part of the result is truncated. This is known as rounding toward zero.

36. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.

37. What is the SimpleTimeZone class?

The SimpleTimeZone class provides support for a Gregorian calendar. You can use it to manipulate dates & times.

38. For which statements does it make sense to use a label?
The only statements for which it makes sense to use a label are those statements that can enclose a break or continue statement.

39. What is the purpose of the System class?
The purpose of the System class is to provide access to system resources.

40. Is &&= a valid Java operator?
No, it is not a valid operator.

41. Name the eight primitive Java data types.
The eight primitive types are byte, char, short, int, long, float, double, and boolean.

42. What restrictions are placed on the values of each case of a switch statement?
During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.

43. What is the difference between a while statement and a do statement?

A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.


44. What is the difference between static and non-static variables?
A static variable is associated with the class as a whole rather than with specific instances or objects of a class. Non-static variables take on unique values with each object instance.

45. What is the purpose of the File class?
The File class is used to create objects that provide access to the files and directories of a local file system.

46. Which Math method is used to calculate the absolute value of a number?
The abs() method is used to calculate absolute values.

47. Which non-Unicode letter characters may be used as the first character of an identifier?
The non-Unicode letter characters $ and _ may appear as the first character of an identifier

48. What restrictions are placed on method overloading?
Two methods may not have the same name and argument list but different return types.

49. What is the return type of a program's main() method?
A program's main() method has a void return type. i.e., the main method does not return anything.

50. What an I/O filter?
An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.



No comments:

Post a Comment