Sample Questions Best site for GRE, LSAT, SAT, GMAT, TOEFL, CCNA, CCSA and interview sample questions  

Technical Sample Questions : C |  C++ |  Oracle |  Java | Unix |  Operating Systems |  Data Structure

Java Sample Questions ›› Java questions

Technical General Java Sample Questions

  1. What is an event loop?

    Answer: An event-driven program doesn't have a main() routine that says what will happen from beginning to end, in a step-by-step fashion.Instead, the program runs in a loop that says:

    while the program is still running:
    Wait for the next event
    Process the event

    This is called an event loop.In Java, the event loop is executed by the system.The system waits for events to happen.When an event occurs, the system calls a routine that has been designated to handle events of that type.

  2. Explain carefully what the repaint() method does.

    Answer: The repaint() method is called to notify the system that the applet (or other component for which it is called) needs to be redrawn.It does not itself do any drawing (neither directly nor by calling a paint() or paintComponent() routine).You should call repaint() when you have made some change to the state of the applet that requires its appearance to change.Sometime shortly after you call it, the system will call the applet's paint() routine or the component's paintComponent() routine.

  3. Suppose that you are writing an applet, and you want the applet to respond in some way when the user clicks the mouse on the applet.What are the four things you need to remember to put into the source code of your applet?

    Answer: The four things are as follows:

    • Since the event and listener classes are defined in the java.awt.event package, you have to put "import java.awt.event.*;" at the beginning of the source code, before the class definition.
    • The applet class must be declared to implement the MouseListener interface, by adding the words "implements MouseListener" to the heading.For example: "public class MyApplet extends JApplet implements MouseListener".(It is also possible for another object besides the applet to listen for the mouse events.)
    • The class must include definitions for each of the five methods specified in the MouseListener interface.Even if a method is not going to do anything, it has to be defined, with an empty body.
    • The applet (or other listening object) must be registered to listen for mouse events by calling addMouseListener().This is usually done in the init() method of the applet.

  4. Java has a standard class called MouseEvent.What is the purpose of this class? What does an object of type MouseEvent do?

    Answer: When an event occurs, the system packages information about the event into an object.That object is passed as a parameter to the event-handling routine.Different classes of objects represent different types of events.An object of type MouseEvent represents a mouse or mouse motion event.It contains information about the location of the mouse cursor and any modifier keys that the user is holding down.This information can be obtained by calling the instance methods of the object.For example, if evt is a MouseEvent object, then evt.getX() is the x-coordinate of the mouse cursor, and evt.isShiftDown() is a boolean value that tells you whether the user was holding down the Shift key.

  5. Explain what is meant by input focus.How is the input focus managed in a Java GUI program?

    Answer: When the user uses the keyboard, the events that are generated are directed to some component.At a given time, there is just one component that can get keyboard events.That component is said to have the input focus.Usually, the appearance of a component changes if it has the input focus and wants to receive user input from the keyboard.For example, there might be a blinking text cursor in the component, or the component might be hilited with a colored border.In order to change its appearance in this way, the component needs to be notified when it gains or loses the focus.In Java, a component gets this notification by listening for focus events.

    Some components, including applets and canvasses, do not get the input focus unless they request it by calling requestFocus().If one of these components needs to process keyboard events, it should also listen for mouse events and call requestFocus() when the user clicks on the component.(Unfortunately, this rule is not enforced uniformly on all platforms.)

  6. Java has a standard class called JPanel.Discuss two ways in which JPanels can be used.

    Answer: A JPanel is a type of component.That is, it is a visible element of a GUI.By itself, a JPanel is simply a blank rectangular region on the screen.However, a JPanel is a "container", which means that other components can be added to it and will then appear on the screen inside the JPanel.A JPanel can also be used as a drawing surface.

    The two ways are as follows:

    • paintComponent()An object belonging to that subclass can then be added to an applet or other component.The
    • paintComponent()This method defines how that object will draw itself on the screen.

  7. What is the FontMetrics class used for?

    Answer: An object that belongs to the class FontMetrics can be used to obtain information about the sizes of characters and strings that are drawn in a specific font.The font is specified when the FontMetrics object is created.If fm is a variable of type FontMetrics, then, for example, fm.stringWidth(str) gives the width of the string str and fm.getHeight() is the usual amount of vertical space allowed for one line of text.This information could be used, for example, for positioning the string is a component.

  8. What are off-screen images? How are they used? Why are they important? What does this have to do with animation?

    Answer: In Java, an off-screen image is an object belonging to the class Image and created with the createImage() function.An off-screen image is a segment of the computer's memory that can be used as a drawing surface.What is drawn to the off-screen image is not visible on the screen, but the Image can be quickly copied onto the screen with a drawImage() command.It is important to use an off-screen image in a situation where the user should not see the process of drawing the image.This is true, for example, in animation.Each frame of the animation can be composed in an off-screen Image and then copied to the screen when it is complete.The alternative would be to erase the screen and draw the next frame directly on the screen.This causes unacceptable flickering of the image.By default, Swing already uses an off-screen image for double-buffering an applet or frame, so you don't have to program it yourself just to do simple animation.

  9. One of the main classes in Swing is the JComponent class.What is meant by a component? What are some examples?

    Answer: A JComponent represents a visual component of the computer's graphical user interface.A JComponent is not completely independent.It must be added to a "container," such as an applet or a frame.Examples of JComponents are JButtons, JTextFields, and JPanels.

  10. What is the function of a LayoutManager in Java?

    Answer: A LayoutManager implements some policy for laying out all the visual components that have been added to a container, such as a JPanel or the content pane of a JApplet.That is, it sets the sizes and positions of the components.Different types of layout managers have different rules about how components are to be arranged.Some standard layout manager classes are BorderLayout and GridLayout.

« Previous || Next »

Java Sample Question Number: 1-10| 11-20| 21-30| 31-40| 41-50| 51-60| 61-70| 71-80| 81-90| 91-100| 101-110| 111-120| 121-130| 131-140| 141-150| 151-160| 161-170| 171-180| 181-190| 191-200| 201-210| 211-220| 221-230| 231-240| 241-250| 251-260| 261-270| 271-280| 281-290| 291-300| 301-310| 311-320| 321-330
Sample Test Questions
GRE Sample Questions
CAT Sample Questions
GMAT Sample Questions
TOEFL Sample Questions
ACT Sample Questions
SAT Sample Questions
LSAT Sample Questions
PSAT Sample Questions
MCAT Sample Questions
PMP Sample Questions
GED Sample Questions
ECDL Sample Questions
DMV Sample Questions
CCNA Sample Questions
MCSE Sample Questions
Network+ Sample Questions
A+ Sample Questions
Technical Sample Questions
WASL Sample Questions
CISA Sample Questions

Other Sample Questions
Sample Interview Questions
Sample Teacher Interview Questions
Sample Citizenship Questions
Accuplacer Sample Questions
Science Bowl sample Questions
Driving Test Sample Questions
Sample Survey Questions Sample Essay Questions
Sample Behavioral Interview Questions

Copyright © 2004-2013, Best BSQ. All Rights Reserved.