This is a quick list of Java interview questions and answers to revise and get acquainted with before attending your next interview as a Java developer. Here is a link to help guide beginners.

1. Is Java dynamic? why?
Ans. yes, It is designed to adapt to an evolving environment. Java programs can carry the extensive amount of run-time information that can be used to verify and resolve accesses to objects at run-time.


2. Define a class?

Ans. A class is a model from which individual objects are created. A class can contain fields and methods to explain the behavior of an object.

 


3. List some Java IDEs you know

Ans. Netbeans and Eclipse


4. List types of variables in a class?

Ans.Local variables, instance variables and class variables.


5. Define Local Variable?

Ans. Variables defined inside methods, constructors or blocks.


6. Define a Instance Variable?

Ans. Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.


7. Define a Class Variable?

Ans. These are variables declared with in a class, outside any method, with the static keyword.


8. Name three Component sub-classes that support painting.

Ans. The Canvas, Frame, Panel, and Applet classes support painting.


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

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


10. Define the immediate superclass of the Dialog class?

Ans. Window.


11. Define clipping?

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


12. Define a native method?

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


13. Can a for statement loop indefinitely?

Ans. Yes, a for statement can loop indefinitely.


14. Explain order of precedence and associativity, and how are they used?

Ans. 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.


15. When a thread blocks on I/O, what state does it enter?

Ans. A thread enters the waiting state when it blocks on I/O.


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

Ans. The default value of a String type is null.


17. Define the catch or declare rule for method declarations?

Ans. 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.


18. Define the difference between a MenuItem and a CheckboxMenuItem?

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


19. Define a task’s priority and how is it used in scheduling?

Ans. 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.


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

Ans. The java.awt.AWTEvent class is the highest-level class in the AWT event-class hierarchy.


21. When a thread is created and started, Define its initial state?

Ans. A thread is in the ready state after it has been created and started.


22. Can an anonymous class be declared as implementing an interface and extending a class?

Ans. An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.


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

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


24. Define the purpose of the System class?

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


25. Which TextComponent method is used to set a TextComponent to the read-only state

Ans. setEditable().


26. How are the elements of a CardLayout organized?

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


27. Is &&= a valid Java operator?

Ans. No.


28. Name the eight primitive Java types.

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


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

Ans. The Class class is used to obtain information about an object’s design.


30. Define the relationship between clipping and repainting?

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


31. Is “abc” a primitive value?

Ans. The String literal “abc” is not a primitive value. It is a String object.


32. Define the relationship between an event-listener interface and an event-adapter class?

Ans. 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.


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

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


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

Ans. An interface may be declared as public or abstract.


35. Is a class a subclass of itself?

Ans. A class is a subclass of itself.


36. Define the highest-level event class of the event-delegation model?

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


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

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


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

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


39. Define the difference between a while statement and a do statement?

Ans. 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.


40. How are the elements of a GridBagLayout organized?

Ans. 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.


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

Ans. 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 accommodate platform-specific differences among windowing systems.


42. Define the Collection interface?

Ans. The Collection interface provides support for the implementation of a mathematical bag – an un-ordered collection of objects that may contain duplicates.


43. What modifiers can be used with a local inner class?

Ans. A local inner class may be final or abstract.


44. Define the difference between static and non-static variables?

Ans. A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.


45. Define the difference between the paint() and repaint() methods?

Ans. The paint() method supports painting via a Graphics object. The repaint() method is used to cause paint() to be invoked by the AWT painting thread.


46. Define the purpose of the File class?

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


47. Can an exception be rethrown?

Ans. Yes, an exception can be rethrown.


48. Which Math method is used to calculate the absolute value of a number?

Ans. The abs() method is used to calculate absolute values.


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

Ans. 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.


50. When does the compiler supply a default constructor for a class?

Ans. The compiler supplies a default constructor for a class if no other constructors are provided.


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

Ans. 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.


52. Which class is the immediate superclass of the Container class?

Ans. Component.


53. If a method is declared as protected, where may the method be accessed?

Ans. A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.


54. How can the Checkbox class be used to create a radio button?

Ans. By associating Checkbox objects with a CheckboxGroup.


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

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


56. What restrictions are placed on method overloading?

Ans. Two methods may not have the same name and argument list but different return types.


57. What is the result of invoking a thread’s interrupt method while it is sleeping or waiting?

Ans. 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.


58. Define casting?

Ans. Two types of casting exist, casting between primitive numeric types and casting between object references. The former is used to convert larger values, such as double values, to smaller values, such as byte values. The later is used to refer to an object by a compatible class, interface, or array type reference.


59. Define the return type of a program’s main() method?

Ans.Void return type.


60. List four Container classes.

Ans. Dialog, FileDialog, Panel,Window, Frame, Applet, or ScrollPane


61. Explian the difference between a Choice and a List?

Ans. 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 can be displayed in such a way that several List items are visible. A List supports the selection of one or more List items.


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

Ans. The Java runtime system generates RuntimeException and Error exceptions.


63. What class allows you to read objects directly from a stream?

Ans. The ObjectInputStream class supports the reading of objects from input streams.


64. Difference between a field variable and a local variable?

Ans. A field variable is a variable that is declared as a member of a class. A local variable is a variable that is declared local to a method.


65. Under what conditions is an object’s finalize() method invoked by the garbage collector?

Ans. The garbage collector invokes an object’s finalize() method when it detects that the object has become unreachable.


66. How are this() and super() used with constructors?

Ans. this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.


67. Define the relationship between a method’s throws clause and the exceptions that can be
thrown during the method’s execution?

Ans. A method’s throws clause must declare any checked exceptions that are not caught within the body of the method.


68. Who designed Java?

Ans. James Gosling.


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

Ans. 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.


70. Why are the methods of the Math class static?

Ans. So they can be invoked as if they are a mathematical code library.


71. What Checkbox method allows you to tell if a Checkbox is checked?

Ans. getState()


72. What state is a thread in when it is executing?

Ans. An executing thread is in the running state.


73. Explain the legal operands of the instanceof operator?

Ans. The left operand is an object reference or null value and the right operand is a class, interface, or array type.


74. How are the elements of a GridLayout organized?

Ans. All elements of a GridBad layout are of equal size and are laid out using the squares of a grid.


75. What is an I/O filter?

Ans. 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.


76. Define Inheritance?

Ans. It is the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.


77. When super keyword is used?

Ans. If the method overrides one of its superclass’s methods, overridden method can be invoked
through the use of the keyword super. It can be also used to refer to a hidden field.


78. Define Polymorphism?

Ans. Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.


79. Define Abstraction?

Ans. It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.


80. Define Abstract class?

Ans. These classes cannot be instantiated and are either partially implemented or not at all
implemented. This class contains one or more abstract methods which are simply method
declarations without a body.


81. When Abstract methods are used?

Ans. If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.


82. Define Encapsulation?

Ans. It is the technique of making the fields in a class private and providing access to the fields through
public methods. If a field is declared private, it cannot be accessed by anyone outside the class, resulting to hiding the fields within the class. Thus, encapsulation is also referred to as data hiding.


83. Define the primary benefit of Encapsulation?

Ans. The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this Encapsulation gives maintainability, flexibility and makes the code extensible.


84. Define an Interface?

Ans. An interface is a collection of abstract methods. A class implements an interface, thereby
inheriting the abstract methods of the interface.


85. Give some features of Interface?

Ans. It includes − Interface cannot be instantiated, an interface does not contain any constructors, all of the methods in an interface are abstract.


86. Define Packages in Java?

Ans. A Package can be defined as a grouping of related types, classes, interfaces, enumerations and annotations providing access protection and name space management.


87. Why Packages are used?

Ans. Packages are used in Java in-order to prevent naming conflicts, to control access, to make
searching/locating and usage of classes, interfaces, enumerations and annotations, easier.


88. What do you mean by Multithreaded program?

Ans. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.


89. Explain the two ways in which Thread can be created?

Ans. Thread can be created by: implementing Runnable interface, extending the Thread class.


90. Define an applet?

Ans. An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.


91. An applet extend which class?

Ans. An applet extends java.applet.Applet class.


92. Explain garbage collection in Java?

Ans. It uses garbage collection to free the memory. By cleaning those objects that is no longer
reference by any of the program.


93. Define immutable object?

Ans. An immutable object can’t be changed once it is created.


94. Explain the usage of this with constructors?

Ans. It is used with variables or methods and used to call constructer of same class.


95. Explain Set Interface?

Ans. It is a collection of element which cannot contain duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.


96. Explain TreeSet?

Ans. It is a Set implemented when we want elements in a sorted order.


97. Define Comparable Interface?

Ans. It is used to sort collections and arrays of objects using the collections.sort and java.utils. The
objects of the class implementing the Comparable interface can be ordered.


98. Can we have more than one package statement in source file?

Ans. We can’t have more than one package statement in source file. In any java program there can be  only 1 package statement other


99. Can we define package statement after import statement in java?

Ans. We can’t define package statement after import statement in java. package statement must be the first statement in source file. We can have comments before the package statement.


100. Can we have try block without catch block?

Ans. Each try block requires at least one catch block or finally block. A try block without catch or finally will result in compiler error. We can skip either of catch or finally block but not both.


These would help you do a quick revision, always remember to answer any question thrown at you with confidence. You can view Java documentation here. Good luck in your interview.

Advertisements