Flat Preloader Icon

Interview Questions

1. What is the difference between checked and unchecked exceptions in Java?

Answer. Checked exceptions are checked at compile time, and you must handle them using ‘try-catch’ or declare them in the method signature with the ‘throws’ keyword. Unchecked exceptions, on the other hand, are not checked at compile time and include runtime exceptions like ‘NullPointerException’ and ‘ArrayIndexOutOfBoundsException’


2. Explain the Java Memory Model (JMM).

Answer: The Java Memory Model defines the rules and constraints for how threads interact with memory. It guarantees that memory operations performed by one thread are visible to other threads in a predictable manner. JMM is crucial for understanding and preventing issues like data races and thread interference in multi-threaded Java applications.


3. What is the purpose of the volatile keyword in Java?

Answer: The volatile keyword is used to declare a variable as “volatile,” which means that it can be accessed and modified by multiple threads. When a variable is declared as volatile, it ensures that any read or write operation on that variable is atomic and that changes made by one thread are immediately visible to other threads.


4. What is the Java Collections Framework, and why is it important?

Answer: The Java Collections Framework is a set of classes and interfaces that provide data structures for managing and manipulating collections of objects. It’s important because it simplifies data manipulation, offers a common set of interfaces and implementations, and enhances code reusability and maintainability.


5. Explain the difference between ArrayList and LinkedList in Java.

Answer: ArrayList is implemented as a dynamic array, making it efficient for random access but less efficient for insertions and deletions. LinkedList is implemented as a linked list, making it efficient for insertions and deletions but less efficient for random access.


6. What is the purpose of the java.lang.ThreadLocal class?

Answer: ‘ThreadLocal’ provides thread-local variables, allowing each thread to have its own independent instance of a variable. It’s often used to store data that should be isolated and specific to a particular thread, such as session data in web applications.


7. Explain the concept of Generics in Java.

Answer: Generics in Java allow you to create classes, interfaces, and methods that operate on typed parameters. They provide type safety by ensuring that you work with the right data types at compile time and eliminate the need for casting.


8. What are Java annotations, and how are they used?

Answer: Annotations are metadata added to Java code to provide information about the code to the compiler, JVM, or other tools. They are used for various purposes, such as marking code for processing, specifying configuration, and generating documentation.


9.What is Java’s Reflection API, and when is it used?

Answer: The Reflection API allows you to inspect and manipulate classes, methods, fields, and other class-related information at runtime. It’s often used for tasks like creating instances of classes dynamically, accessing private members, and building tools like serialization and dependency injection frameworks.


10. Explain the concept of the Java Native Interface (JNI).

Answer: JNI is a programming framework that allows Java code to call native code (written in C/C++) and vice versa. It’s used when you need to access platform-specific features, interact with existing native libraries, or achieve high-performance tasks not easily attainable in pure Java.


11. What is the difference between the JVM, JRE, and JDK?

Answer:
  • The Java Virtual Machine (JVM) executes Java bytecode.
  • The Java Runtime Environment (JRE) contains the JVM and necessary libraries for running Java applications.
  • The Java Development Kit (JDK) includes the JRE and additional development tools, such as compilers and debuggers.

12. Explain the concept of multithreading in Java.

Answer: Multithreading in Java allows concurrent execution of multiple threads within a single Java program. It enables better utilization of CPU resources and can improve the responsiveness of applications. In Java, you can create and manage threads using the ‘Thread’ class or by implementing the ‘Runnable’ interface. It’s important to be aware of thread synchronization to prevent race conditions and ensure thread safety.


13. Explain the significance of the ‘final’ keyword in Java.

Answer:
  • ‘final’ can be applied to classes, methods, and variables.
  • A final class cannot be subclassed.
  • A final method cannot be overridden.
  • A final variable cannot be reassigned after initialization.
14. Explain the concept of Java Reflection.

Answer: Reflection is a feature that allows Java code to inspect and manipulate the properties of classes, methods, fields, and objects at runtime.
It can be used for tasks like dynamic class loading, invoking methods, accessing fields, and creating new objects.


15. What are design patterns, and can you name some Java-specific design patterns?

Answer: Design patterns are reusable solutions to common software design problems.
Java-specific design patterns include Singleton, Factory, Builder, Observer, and others.


16.Explain the purpose of the try-with-resources statement in Java.

Answer: The try-with-resources statement is used to automatically close resources like files, sockets, or database connections when they are no longer needed. It simplifies resource management and ensures that resources are properly closed, even if an exception is thrown within the try block. This is achieved by implementing the AutoCloseable interface and is especially important for preventing resource leaks.


17. What are lambdas in Java, and how do they work?

Answer: Lambdas in Java are a feature introduced in Java 8, allowing the representation of functional interfaces (interfaces with a single abstract method) as concise, inline code. They are essentially anonymous functions that can be passed as arguments to methods or assigned to variables. Lambdas reduce the need for creating separate classes for simple tasks, making code more readable and concise.


18. Explain the purpose of the java.util.stream package in Java 8 and later.

Answer: The java.util.stream package in Java provides a powerful and functional programming-style approach to working with sequences of data, such as collections or arrays. It offers operations like map, filter, reduce, and more to process data in a declarative and efficient manner. Streams are useful for parallel processing and simplifying complex data transformations.


19. What is the difference between composition and inheritance in Java?

Answer: Inheritance is an “is-a” relationship, where a class inherits properties and behaviors from another class. Composition is a “has-a” relationship, where a class contains an instance of another class as a member variable. Composition is typically preferred over inheritance because it promotes code reuse, is more flexible, and avoids some of the issues associated with deep inheritance hierarchies.


20. Explain the purpose of the Executor framework in Java.

Answer: The Executor framework is a higher-level replacement for manually managing threads in Java. It provides a simple and flexible way to execute tasks asynchronously. It decouples task submission from the specifics of how each task will be executed, allowing for better control and resource management of concurrent tasks in applications.


21. Explain the differences between the final, finally, and finalize keywords in Java.

Answer:
  • final is used to indicate that a variable, method, or class cannot be extended, overridden, or reassigned.
  • finally is a block used in exception handling to ensure a piece of code is executed regardless of whether an exception is thrown or not.
  • finalize is a method called by the garbage collector when it’s about to collect an object to allow it to release any resources.



22. What is the Java Memory Model, and why is it important in multithreaded programming?

Answer: The Java Memory Model defines how threads interact with the memory when reading and writing shared variables. It’s important in multithreaded programming to ensure proper synchronization and avoid data races and inconsistencies.


23. Explain the differences between synchronized and volatile in Java.

  • synchronized is used to create a block or method that can be accessed by only one thread at a time, providing mutual exclusion.
  • volatile is used to indicate that a variable’s value may be changed by multiple threads, ensuring visibility and preventing certain compiler optimizations.

24. What is the purpose of the transient keyword in Java?

Answer: The “transient” keyword is used to indicate that a field should not be serialized when an object is transformed into a byte stream. It’s often used for non-essential fields or fields that can’t be serialized.


25. What are the differences between an abstract class and an interface in Java?

Answer:
  • An abstract class can have instance variables and method implementations, while an interface only defines method signatures and constants.
  • A class can extend only one abstract class but implement multiple interfaces.
  • Abstract classes are meant to be extended, while interfaces are used to define a contract that multiple classes can implement.
26. Explain the concept of Generics in Java and provide an example of its use.

Answer: Generics allow you to create classes, interfaces, and methods that operate on type parameters. They provide compile-time type safety. An example is the List interface or the ArrayList class, where T represents the type of elements in the list.


27. What is the purpose of the java.util.concurrent package, and what are some classes it contains?

The java.util.concurrent package provides classes for concurrent programming. It includes classes for managing thread execution, thread pools, synchronization, and atomic operations. Some classes are Executor, ThreadPoolExecutor, Semaphore, and ConcurrentHashMap.


28. Explain the differences between the equals() method and the hashCode() method in Java.

  • The equals() method is used to compare the contents of two objects for equality.
  • The hashCode() method returns an integer that represents the value of an object. It’s used in data structures like hash tables for fast retrieval.

29. What is the purpose of the ClassLoader in Java, and how does it work?

Answer: The ClassLoader is responsible for loading classes into the JVM dynamically. It works by delegating the class loading to parent class loaders and then to its own class loader, searching for the class in the classpath.


30. Explain the concept of reflection in Java and provide an example of its use.

Answer: Reflection allows you to inspect and manipulate classes, methods, fields, and objects at runtime. You can use it to access private members, invoke methods dynamically, or create instances of classes. For example, you can use reflection to load a class by name, inspect its methods, and invoke them.


31. What is the purpose of the ‘assert’ statement in Java?

Answer: ‘assert’ is used for debugging purposes. It allows you to test assumptions about the program’s state and halt the program if the assertion fails.


32. What is the difference between ‘Error’ and ‘Exception’ in Java?

Answer:
  • Errors are typically unrecoverable and are not meant to be caught by the application (e.g., OutOfMemoryError).
  • Exceptions are recoverable and are meant to be handled by the application (e.g., NullPointerException).

33. Explain the concept of Java Serialization.

Answer: Serialization is the process of converting objects into a byte stream. Deserialization is the reverse process of recreating objects from the byte stream. It is used for data persistence and network communication.


34. What is the difference between ‘String’, ‘StringBuffer’, and ‘StringBuilder’ in Java?

Answer:
  • String’ is immutable, meaning its content cannot be changed after creation.
  • ‘StringBuffer’ is mutable and is designed for use in multi-threaded environments (synchronized).
  • ‘StringBuilder’ is mutable and is not thread-safe, providing better performance in single-threaded scenarios.

35. Which containers use a border Layout as their default layout?

Answer: The ‘Window’ , ‘Frame’ and ‘Dialog’ classes use a border layout as their default layout.


36. How are Observer and Observable used?

Answer: 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.


37. What method is used to specify a container’s layout?

Answer: The setLayout() method is used to specify a container’s layout.


38. What state does a thread enter when it terminates its processing?

Answer: When a thread terminates its processing, it enters the dead state.


39. What is the Collections API?

Answer: The Collections API is a set of classes and interfaces that support operations on collections of objects.


40. What is the List interface?

Answer: The List interface provides support for ordered collections of objects.


41. How does Java handle integer overflows and underflows?

Answer: It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.


42.What is the difference between the >> and >>> operators?

Answer: The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.


43. What is the difference between yielding and sleeping?

Answer: When a task invokes its yield() method, it returns to the ready state.
When a task invokes its sleep() method, it returns to the waiting state.


44. Does garbage collection guarantee that a program will not run out of memory?

Answer: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.


45. Can an object’s finalize() method be invoked while it is reachable?

Answer: An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.


46. What is the difference between preemptive scheduling and time slicing?

Answer: Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks.
The scheduler then determines which task should execute next, based on priority and other factors.


47. What are order of precedence and associativity, and how are they used?

Answer: 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.


48. What is the catch or declare rule for method declarations?

Answer: If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.


49. What is the difference between a MenuItem and a CheckboxMenuItem?

Answer: The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.


50. What is a task’s priority and how is it used in scheduling?

Answer: A task’s priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.
41. How does Java handle integer overflows and underflows?

Answer: It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.


42.What is the difference between the >> and >>> operators?

Answer: The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.


43. What is the difference between yielding and sleeping?

Answer: When a task invokes its yield() method, it returns to the ready state.
When a task invokes its sleep() method, it returns to the waiting state.


44. Does garbage collection guarantee that a program will not run out of memory?

Answer: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.


45. Can an object’s finalize() method be invoked while it is reachable?

Answer: An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.


46. What is the difference between preemptive scheduling and time slicing?

Answer: Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks.
The scheduler then determines which task should execute next, based on priority and other factors.


47. What are order of precedence and associativity, and how are they used?

Answer: 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.


48. What is the catch or declare rule for method declarations?

Answer: If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.


49. What is the difference between a MenuItem and a CheckboxMenuItem?

Answer: The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.


50. What is a task’s priority and how is it used in scheduling?

Answer: A task’s priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.


51. What is the purpose of finalization?

Answer: The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.


52.What is the difference between the Boolean & operator and the && operator?

Answer: If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the& operator is applied to the operand. When an expression involving the && operator is evaluated, the first operand is evaluated.
If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.


53. What is the purpose of the wait(), notify(), and notifyAll() methods?

Answer: The wait(), notify(), and notifyAll() methods are used to provide an efficient way for threads to wait for a shared resource. When a thread executes an object’s wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object’s notify() or notifyAll() methods..


54. How are Java source code files named?

Answer: 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 must take on a name that is different than its classes and interfaces. Source code files use the .java extension.


55. What is an object’s lock and which object’s have locks?

Answer: An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock.
All objects and classes have locks. A class’s lock is acquired on the class’s Class object.


56. What happens when a thread cannot acquire a lock on an object?

Answer: If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object’s lock, it enters the waiting state until the lock becomes available.


57. If a class is declared without any access modifiers, where may the class be accessed?

Answer: A class that is declared without any access modifiers is said to have package access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.


58. What is the relationship between clipping and repainting?

Answer: When a window is repainted by the AWT painting thread, it sets the clipping regions to the area of the window that requires repainting.


59. What is the relationship between an event-listener interface and an event-adapter class?

Answer: An event-listener interface defines the methods that must be implemented by an event handler for a particular kind of event. An event adapter provides a default implementation of an event-listener interface.


60. What restrictions are placed on the values of each case of a switch statement?

Answer: During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.


61. How are the elements of a GridBagLayout organized?

Answer: The elements of a GridBagLayout are organized according to a grid. However, the elements are of different sizes and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes.


62. What advantage do Java’s layout managers provide over traditional windowing systems?

Answer: Java uses layout managers to lay out components in a consistent manner across all windowing platforms. Since Java’s layout managers aren’t tied to absolute sizing and positioning, they are able to accomodate platform-specific differences among windowing systems.


63. What is the Collection interface?

Answer: The Collection interface provides support for the implementation of a mathematical bag – an unordered collection of objects that may contain duplicates.


64. How does multithreading take place on a computer with a single CPU?

Answer: The operating system’s task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.


65.When is the finally clause of a try-catch-finally statement executed?

Answer: The finally clause of the try-catch-finally statement is always executed unless the thread of execution terminates or an exception occurs within the execution of the finally clause.


66. What is casting?

Answer: There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.


67. What is the difference between a Choice and a List?

Answer: A Choice is displayed in a compact form that requires you to pull it down to see the list of available choices. Only one item may be selected from a Choice. A List may be displayed in such a way that several List items are visible. A List supports the selection of one or more List items.


68. How is it possible for two String objects with identical values not to be equal under the == operator?

Answer: The == operator compares two objects to determine if they are the same object in memory. It is possible for two String objects to have the same value, but located indifferent areas of memory.


69.What is the Set interface?

Answer: The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements.


70. What is the purpose of the enableEvents() method?

Answer: The enableEvents() method is used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event.
The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods.


71. What is the difference between the File and RandomAccessFile classes?

Answer: 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.


72. What interface must an object implement before it can be written to a stream as an object?

Answer: An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.


73. How are this and super used?

Answer: this is used to refer to the current object instance. super is used to refer to the variables and methods of the superclass of the current object instance.


74. What restrictions are placed on method overriding?

Answer: 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. The overriding method may not throw any exceptions that may not be thrown by the overridden method.


75. What happens if an exception is not caught?

Answer: An uncaught exception results in the uncaughtException() method of the thread’s ThreadGroup being invoked, which eventually results in the termination of the program in which it is thrown.


76. What are three ways in which a thread can enter the waiting state?

Answer: A thread can enter the waiting state by invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object’s lock, or by invoking an object’s wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.


77. What is the ResourceBundle class?

Answer: 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.


78. What is numeric promotion?

Answer: 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.


79. What is the difference between a Scrollbar and a ScrollPane?

Answer: A Scrollbar is a Component, but not a Container. A ScrollPane is a Container.
A ScrollPane handles its own events and performs its own scrolling.


80. What is the difference between the prefix and postfix forms of the ++ operator?

Answer: The prefix form performs the increment operation and returns the value of the increment operation.
The postfix form returns the current value all of the expression and then performs the increment operation on that value.


81. What is a Java package and how is it used?

Answer: A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.


82.What are the Object and Class classes used for?

Answer: The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program..


83. How does a try statement determine which catch clause should be used to handle an exception?

Answer: When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.


84. Can an unreachable object become reachable again?

Answer: An unreachable object may become reachable again. This can happen when the object’s finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects.


85. What are synchronized methods and synchronized statements?

Answer: Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method’s object or class.
Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.


86. What are the problems faced by Java programmers who don’t use layout managers?

Answer: Without layout managers, Java programmers are faced with determining how their GUI will be displayed across multiple windowing systems and finding a common sizing and positioning that will work within the constraints imposed by each windowing system.


87. What are the two basic ways in which classes that can be run as threads may be defined?

Answer: A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface.


88. What method must be implemented by all threads?

Answer: All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.


89. How does a try statement determine which catch clause should be used to handle an exception?

Answer: When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.


90. Can an unreachable object become reachable again?

Answer: An unreachable object may become reachable again. This can happen when the object’s finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects.


91. What classes of exceptions may be thrown by a throw statement?

Answer: A throw statement may throw any expression that may be assigned to the Throwable type.


92. What an I/O filter?

Answer: 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.


93. What is the difference between the JDK 1.02 event model and the event-delegation model introduced with JDK 1.1?

Answer: The JDK 1.02 event model uses an event inheritance or bubbling approach. In this model, components are required to handle their own events. If they do not handle a particular event, the event is inherited by (or bubbled up to) the component’s container. The container then either handles the event or it is bubbled up to its container and so on, until the highest-level container has been tried.
In the event-delegation model, specific objects are designated as event handlers for GUI components. These objects implement event-listener interfaces. The event-delegation model is more efficient than the event-inheritance model because it eliminates the processing required to support the bubbling of unhandled events.


94. What class of exceptions are generated by the Java run-time system?

Answer: The Java runtime system generates RuntimeException and Error exceptions.


95.What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?

Answer: When a task’s interrupt() method is executed, the task enters the ready state. The next time the task enters the running state, an InterruptedException is thrown.


96. Which non-Unicode letter characters may be used as the first character of an identifier?

Answer: The non-Unicode letter characters $ and _ may appear as the first character of an identifier


97.What is the Collection interface?

Answer: The Collection interface provides support for the implementation of a mathematical bag – an unordered collection of objects that may contain duplicates.


98. What is the highest-level event class of the event-delegation model?

Answer: The java.util.EventObject class is the highest-level class in the event-delegation class hierarchy.


99. What modifiers may be used with an interface declaration?

Answer: An interface may be declared as public or abstract.


100. Name the eight primitive Java types.

Answer: The eight primitive types are byte, char, short, int, long, float, double, and boolean.



101. Why do threads block on I/O?

Answer: Threads block on I/O (that is enters the waiting state) so that other threads may execute while the I/O Operation is performed.


102. What’s new with the stop(), suspend() and resume() methods in JDK 1.2?

Answer: The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.


103. Which containers use a FlowLayout as their default layout?

Answer: The Panel and Applet classes use the FlowLayout as their default layout.


104. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?

Answer: 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.


105.What is the Vector class?

Answer: The Vector class provides the capability to implement a growable array of objects

106.Name three Component subclasses that support painting.

Answer: The Canvas, Frame, Panel, and Applet classes support painting.

107. What value does readLine() return when it has reached the end of a file?

Answer: The readLine() method returns null when it has reached the end of a file.

108. What is the immediate superclass of the Dialog class?

Answer: Window.

109. What is clipping?

Answer: Clipping is the process of confining paint operations to a limited area or shape.

110. What is a native method?
 

Answer: A native method is a method that is implemented in a language other than Java.

111. Which method of the Component class is used to set the position and size of a component?

Answer: setBounds() method is used to set the position and size of a component.


112. Which java.util classes and interfaces support event handling?

Answer: The EventObject class and the EventListener interface support event processing.


113. Can a for statement loop indefinitely?

Answer: Yes, a for statement can loop indefinitely. For example, consider the following:
for(;;) ;


114. To what value is a variable of the String type automatically initialized?

Answer: The default value of an String type is null.


115. What class is the top of the AWT event hierarchy?

Answer: The java.awt.AWTEvent class is the highest-level class in the AWT event-class hierarchy.
116. What is the GregorianCalendar class?

Answer: The GregorianCalendar class provides support for traditional Western calendars.


117. Which Container method is used to cause a container to be laid out and redisplayed?

Answer: validate() method is used to cause a container to be laid out and redisplayed.


118. What is the purpose of the Runtime class?

Answer: The purpose of the Runtime class is to provide access to the Java runtime system.


119. What is the purpose of the finally clause of a try-catch-finally statement?

Answer: The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught.


120. What is the argument type of a program’s main() method?

Answer: A program’s main() method takes an argument of the String[] type.
121. What is an abstract method?

Answer: An abstract method is a method whose implementation is deferred to a subclass.


122. What is the relationship between the Canvas class and the Graphics class?

Answer: A Canvas object provides access to a Graphics object via its paint() method.


123. What is the relationship between the Canvas class and the Graphics class?

Answer: String objects are constants. StringBuffer objects are not constants.


124.What is the Dictionary class?

Answer: The Dictionary class provides the capability to store key-value pairs.


125. What is the % operator?

Answer: It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.


126. What value does read() return when it has reached the end of a file?


Answer:
The read() method returns -1 when it has reached the end of a file.



127. If a variable is declared as private, where may the variable be accessed?


Answer:
A private variable may only be accessed within the class in which it is declared.



128. What is an object’s lock and which object’s have locks?


Answer:
An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock.

All objects and classes have locks. A class’s lock is acquired on the class’s Class object.



129. When can an object reference be cast to an interface reference?


Answer:
An object reference be cast to an interface reference when the object implements the referenced interface.



130. What is the difference between the Font and FontMetrics classes?

Answer: The FontMetrics class is used to define implementation-specific properties, such as ascent and descent, of a Font object.

131. Name two subclasses of the TextComponent class.

Answer: TextField and TextArea.


132. Can a Byte object be cast to a double value?

Answer: No. An object cannot be cast to a primitive value.


133. How are the elements of a BorderLayout organized?

Answer: The elements of a BorderLayout are organized at the borders (North, South, East, and West) and the center of a container.

134. Which class is extended by all other classes?

Answer: The Object class is extended by all other classes.

135. How is rounding performed under integer division?

Answer: The fractional part of the result is truncated. This is known as rounding toward zero.
136. What is the SimpleTimeZone class?

Answer: The SimpleTimeZone class provides support for a Gregorian calendar.


137. What is the Map interface?

Answer: The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values.


138. What is the purpose of the System class?

Answer: The purpose of the System class is to provide access to system resources.


139. How are the elements of a CardLayout organized?

Answer: The elements of a CardLayout are stacked, one on top of the other, like a deck of cards.


140. Which class should you use to obtain design information about an object?

Answer: The Class class is used to obtain information about an object’s design.
141. What classes of exceptions may be caught by a catch clause?

Answer: A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types.


142. For which statements does it make sense to use a label?

Answer: The only statements for which it makes sense to use a label are those statements that can enclose a break or continue statement.


143. What restrictions are placed on the values of each case of a switch statement?

Answer: During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.


144. What event results from the clicking of a button?

Answer: The ActionEvent event is generated as the result of the clicking of a button.


145. How can a GUI component handle its own events?

Answer: A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.