Powered by Blogger.

Java Interview Questions - 2

Here are some more Questions..

      31.  What is an abstract class?
 An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete.

32. What is the difference between Integer and int?
 a) Integer is a class defined in the java. lang package, whereas int is a primitive data type defined in the  Java language itself. Java does not automatically convert from one to the other. b) Integer can be used as an argument for a method that requires an object, whereas int can be used for calculations. 

33.What is a cloneable interface and how many methods does it contain?
 It is not having any method because it is a TAGGED or MARKER interface. 

34. What is the difference between abstract class and interface?
 a) All the methods declared inside an interface are abstract whereas abstract class must have at least one abstract method and others may be concrete or abstract. 
b) In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods. 
c) Abstract class must have subclasses whereas interface can’t have subclasses. 

35. Can you have an inner class inside a method and what variables can you access?
Yes, we can have an inner class inside a method and final variables can be accessed. 

36. What is the difference between String and String Buffer?-
a) String objects are constants and immutable whereas StringBuffer objects are not. 
b) String class supports constant strings whereas StringBuffer class supports growable and modifiable strings.

37.What is the difference between Array and vector?
Array is a set of related data type and static whereas vector is a growable array of objects and dynamic.

38.What is the difference between exception and error?
 The exception class defines mild error conditions that your program encounters. Exceptions can occur when trying to open the file, which does not exist, the network connection is disrupted, operands being manipulated are out of prescribed ranges, the class file you are interested in loading is missing. The error class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered. 

 39.What is the difference between process and thread?
Process is a program in execution whereas thread is a separate path of execution in a program

40. What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined?
 Multithreading is the mechanism in which more than one thread run independent of each other within the process. wait (), notify () and notifyAll() methods can be used for inter-thread communication and these methods are in Object class. wait() : When a thread executes a call to wait() method, it surrenders the object lock and enters into a waiting state. notify() or notifyAll() : To remove a thread from the waiting state, some other thread must make a call to notify() or notifyAll() method on the same object.

41. What is the class and interface in java to create thread and which is the most advantageous method?
 Thread class and Runnable interface can be used to create threads and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here.

42. What are the states associated in the thread?
 Thread contains ready, running, waiting and dead states.

43. What is synchronization?
 Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.

44. When you will synchronize a piece of your code?
When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption. 

45. What is deadlock?
When two threads are waiting each other and can’t precede the program is said to be deadlock

46. What is daemon thread and which method is used to create the daemon thread?
 Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread. 

47. Are there any global variables in Java, which can be accessed by other part of your program?
 No, it is not the main method in which you define variables. Global variables is not possible because concept of encapsulation is eliminated here.

48. What is an applet?
Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser.

49. What is the difference between applications and applets?
 a)Application must be run on local machine whereas applet needs no explicit installation on local machine. b)Application must be run explicitly within a java-compatible virtual machine whereas applet loads and runs itself automatically in a java-enabled browser. d)Application starts execution with its main method whereas applet starts execution with its init method. e)Application can run with or without graphical user interface whereas applet must run within a graphical user interface. 

50.  How does applet recognize the height and width?
 Using getParameters() method. 

51. When do you use codebase in applet?
 When the applet class file is not in the same directory, codebase is used. 

52. What is the lifecycle of an applet?
 init() method - Can be called when an applet is first loaded start() method - Can be called each time an applet is started. paint() method - Can be called when the applet is minimized or maximized. stop() method - Can be used when the browser moves off the applet’s page. destroy() method - Can be called when the browser is finished with the applet. 

53. How do you set security in applets?
using setSecurityManager() method 

54.What is an event and what are the models available for event handling?
 An event is an event object that describes a state of change in a source. In other words, event occurs when an action is generated, like pressing button, clicking mouse, selecting a list, etc. There are two types of models for handling events and they are: a) event-inheritance model and b) event-delegation model.

55. What are the advantages of the model over the event-inheritance model?
The event-delegation model has two advantages over the event-inheritance model. They are: a)It enables event handling by objects other than the ones that generate the events. This allows a clean separation between a component’s design and its use. b)It performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to be repeatedly process unhandled events as is the case of the event-inheritance. 

56. What is source and listener?
 source : A source is an object that generates an event. This occurs when the internal state of that object changes in some way. listener : A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications. 

57. What is adapter class?
 An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. You can define a new class to act listener by extending one of the adapter classes and implementing only those events in which you are interested. For example, the MouseMotionAdapter class has two methods, mouseDragged()and mouseMoved(). The signatures of these empty are exactly as defined in the MouseMotionListener interface. If you are interested in only mouse drag events, then you could simply extend MouseMotionAdapter and implement mouseDragged() 

58. What is meant by controls and what are different types of controls in AWT?
Controls are components that allow a user to interact with your application and the AWT supports the following types of controls: Labels, Push Buttons, Check Boxes, Choice Lists, Lists, Scrollbars, Text Components. These controls are subclasses of Component. 

59. What is the difference between choice and list?
 A Choice is displayed in a compact form that requires you to pull it down to see the list of available choices and only one item may be selected from a choice. A List may be displayed in such a way that several list items are visible and it supports the selection of one or more list items. 

60. What is the difference between scrollbar and scrollpane?
 A Scrollbar is a Component, but not a Container whereas Scrollpane is a Conatiner and handles its own events and perform its own scrolling. 

61. What is a layout manager and what are different types of layout managers available in java AWT?
 A layout manager is an object that is used to organize components in a container. The different layouts are available are FlowLayout, BorderLayout, CardLayout, GridLayout and GridBagLayout. 

62. How are the elements of different layouts organized?
 FlowLayout: The elements of a FlowLayout are organized in a top to bottom, left to right fashion. BorderLayout: The elements of a BorderLayout are organized at the borders (North, South, East and West) and the center of a container. CardLayout: The elements of a CardLayout are stacked, on top of the other, like a deck of cards. GridLayout: The elements of a GridLayout are of equal size and are laid out using the square of a grid. GridBagLayout: The elements of a GridBagLayout are organized according to a grid. However, the elements are of different size and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes. 

63. Which containers use a Border layout as their default layout?
 Window, Frame and Dialog classes use a BorderLayout as their layout.

64. Which containers use a Flow layout as their default layout?-
Panel and Applet classes use the FlowLayout as their default layout.

Share on Google Plus

About Sahithi Pallavi

Author is a Tech savvy and Web Enthusiast by nature and really love to help users by providing how-to posts and tech tutorials. He is a founder and author of technolamp.co.in and programming9.com. YouTube Channel to SUBSCRIBE:
Available: info@programming9.com
    Blogger Comment
    Facebook Comment


Javin @ Tibco RV Tutorial said...

Nice article.In my opinion wait/notify is best way for inter thread communication but keep in mind if you have more than two threads its always better to use notifyAll(), Its also good to know How to find if a thread holds lock on a particular object in Java .

On a related note incorrect written multi threaded programs often results in deadlock specially if number of shared resources are more and number of accessing threads are also high its worth knowing How to find and fix deadlock in Java ?