Java Frequently Asked Questions


 

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.

 

Top Java Frequently Asked Questions- 2

51. Are true and false keywords? 
The values true and false are not keywords. 

52. What is a void return type? 
A void return type indicates that a method does not return a value after its execution. 

53. What is the difference between the File and RandomAccessFile classes? 
The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods needed to directly access data contained in any part of a file. 

54. Which package is always imported by default? 
The java.lang package is always imported by default in all Java Classes. 

55. What restrictions are placed on method overriding? 
Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides but it can expand it. The overriding method may not throw any exceptions that are not thrown by the overridden method. 

56. Which arithmetic operations can result in the throwing of an ArithmeticException? 
Integer / and % can result in the throwing of an ArithmeticException. 

57. What is the ResourceBundle class? 
The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program's appearance to the particular locale in which it is being run. 

58. What is numeric promotion? 
Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required. 

59. To what value is a variable of the boolean type automatically initialized? 
The default value of the boolean type is false. 

60. What is the difference between the prefix and postfix forms of the ++ operator? 
The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value to the expression and then performs the increment operation on that value. 

61. What is the purpose of a statement block? 
A statement block is used to organize a sequence of statements as a single statement group. 

62. What is the difference between an if statement and a switch statement? 
The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed. 
A Switch statement with 5 Case blocks can be compared to an if statement with 5 else-if blocks. 

63. What do you mean by object oreiented programming 
In object oreinted programming the emphasis is more on data than on the procedure and the program is divided into objects. Some concepts in OO Programming are: 
* The data fields are hidden and they cant be accessed by external functions. 
* The design approach is bottom up. 
* The Methods operate on data that is tied together in data structure 

64. What are 4 pillars of object oreinted programming 
1.
Abstraction - It means hiding the details and only exposing the essentioal parts 

2.
Polymorphism - Polymorphism means having many forms. In java you can see polymorphism when you have multiple methods with the same name 

3.
Inheritance - Inheritance means the child class inherits the non private properties of the parent class 

4.
Encapsulation - It means data hiding. In java with encapsulate the data by making it private and even we want some other class to work on that data then the setter and getter methods are provided 

65. Difference between procedural and object oreinted language 
In procedural programming the instructions are executed one after another and the data is exposed to the whole program 
In Object Oriented programming the unit of program is an object which is nothing but combination of data and code and the data is not exposed outside the object 

66. What is the difference between parameters and arguments 
While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments. 

67. What is reflection in java 
Reflection allows Java code to discover information about the fields, methods and constructors of loaded classes and to dynamically invoke them. The Java Reflection API covers the Reflection features. 

68. What is a cloneable interface and how many methods does it contain 
The cloneable interface is used to identify objects that can be cloned using the Object.clone() method. IT is a Tagged or a Marker Interface and hence it does not have any methods. 

69. What is the difference between Java Bean and Java Class
Basically a Bean is a java class but it has getter and setter method and it does not have any logic in it, it is used for holding data. 
On the other hand the Java class can have what a java bean has and also has some logic inside it 

70. What are null or Marker interfaces in Java ?
The null interfaces or marker interfaces or Tagged Interfaces, do not have method declarations in them. They are empty interfaces, this is to convey the compiler that they have to be treated differently 

71. Does java Support multiple inheritance ?
Java does not support multiple inheritance directly like C++, because then it is prone to ambiguity, example if a class extends 2 other classes and these 2 parent classes have same method names then there is ambiguity. Hence in Partial Java Multiple inheritance is supported using Interfaces 

72. What are virtual function ?
In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class functions overridden by the derived class, a problem then arises when a derived object has been cast as the base class type. When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous. 

The distinction between virtual and not virtual is provided to solve this issue. If the function in question is designated "virtual" then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called. 

73. Does java support virtual functions ?
No java does not support virtual functions direclty like in C++, but it supports using Abstract class and interfaces 

74. What is JVM ?
When we install a java package. It contains 2 things 
* The Java Runtime Environment (JRE) 
* The Java Development Kit (JDK) 

The JRE provides runtime support for Java applications. The JDK provides the Java compiler and other development tools. The JDK includes the JRE. 

Both the JRE and the JDK include a Java Virtual Machine (JVM). This is the application that executes a Java program. A Java program requires a JVM to run on a particular platform 

75. What is the difference between Authentication and Authorization ?
Authentication is a process for verifying that an individual is who they say they are. Authorization is an additional level of security, and it means that a particular user (usually authenticated), may have access to a particular resource say record, file, directory or script. 

76. What types of values does boolean variables take ?
It only takes values true and false. 

77. Which primitive datatypes are signed ?
All primitive datatypes are signed except char and Boolean

78. Is char type signed or unsigned ?
char type is integral but unsigned. It range is 0 to 2^7-1

79. What forms an integral literal can be ?
decimal, octal and hexadecimal, hence example it can be 28, 034 and 0x1c respectively 

80. Why is the main method static ?
So that it can be invoked without creating an instance of that class 

81. What is the difference between class variable, member variable and automatic(local) variable 
class variable is a static variable and does not belong to instance of class but rather shared across all the instances of the Class. 

member variable belongs to a particular instance of class and can be called from any method of the class 

automatic or local variable is created on entry to a method and is alive only when the method is executed

82. When are static and non static variables of the class initialized ?
The static variables are initialized when the class is loaded

Non static variables are initialized just before the constructor is called 

83. How is an argument passed in java, is it by copy or by reference? 
If the variable is primitive datatype then it is passed by copy. 
If the variable is an object then it is passed by reference 

84. How does bitwise (~) operator work ?
It converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s, e.g 11110000 gets coverted to 00001111 

85. Can shift operators be applied to float types ?
No, shift operators can be applied only to integer or long types (whole numbers)

86. What happens to the bits that fall off after shifting ?
They are discarded (ignored)

87. What are the rules for overriding ?
The rules for Overriding are: Private method can be overridden by private, protected or public methods Friendly method can be overridden by protected or public methods Protected method can be overridden by protected or public methods Public method can be overridden by public method 

88. Explain the final Modifier ?
Final can be applied to classes, methods and variables and the features cannot be changed. Final class cannot be subclassed, methods cannot be overridden 

89. Can you change the reference of the final object ?
No the reference cannot be changed, but the data in that object can be changed 

90. Can abstract modifier be applied to a variable ?
No it can be applied only to class and methods 

91. Where can static modifiers be used ?
They can be applied to variables, methods and even a block of code, static methods and variables are not associated with any instance of class 

92. When are the static variables loaded into the memory 
During the class load time 

93. When are the non static variables loaded into the memory ?
They are loaded just before the constructor is called 

94. How can you reference static variables ?
You can refer to static variables directly using the class name and you dont need any object instance for it. 

Ex: ClassTest.execMethod(); can be used to access the execMethod() method of the class ClassTest

95. Can static method use non static features of there class ?
No they are not allowed to use non static features of the class, they can only call static methods and can use static data 

96. What is static initializer code ?
A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g. 

public class Test{ 
static int =10; 
static{ 
System.out.println("Hiiiiiii"); 



And this code is executed exactly once at the time of class load 

97. Where is native modifier used? 
It can refer only to methods and it indicates that the body of the method is to be found else where and it is usually written in language other than Java. 

98. When do you use continue and when do you use break statements ?
When continue statement is applied it prematurely completes the iteration of a loop. 
When break statement is applied it causes the entire loop to be abandoned. 

99. What do you understand by late binding or virtual method Invocation ?
When a compiler for a non object oriented language comes across a method invocation, it determines exactly what target code should be called and build machine language to represent that call. In an object oriented language, this is not possible since the proper code to invoke is determined based upon the class if the object being used to make the call, not the type of the variable. Instead code is generated that will allow the decision to be made at run time. This delayed decision making is called as late binding 

100. Can Overridden methods have different return types ?
No they cannot have different return types 

101. If the method to be overridden has access type protected, can subclass have the access type as private 
No, it must have access type as protected or public, since an overriding method must not be less accessible than the method it overrides 

102. What are the Final fields & Final Methods ? 
Fields and methods can also be declared final. A final method cannot be overridden in a subclass. A final field is like a constant: once it has been given a value, it cannot be assigned again.

 

Post a Comment

0 Comments