Web Design and Development MCQs with Answers
Practice important Web Design and Development MCQs with answers and explanations.
Chapter: 19
Q301:
In the Test class example, what is the role of setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)?
A. Ensures the JFrame closes when the window is closed
B. Sets the default size of the JFrame
C. Sets the layout manager for the JFrame
D. Adds MyPanel to the JFrame
Correct Answer:
A
Chapter: 19
Q302:
What is the purpose of the repaint() method in Java Swing?
A. To directly call paintComponent()
B. To schedule a call to paint() at the appropriate time
C. To immediately clear the component's drawing
D. To update the component's layout
Correct Answer:
B
Chapter: 19
Q303:
Why should you avoid calling paintComponent() directly for animations?
A. It can be called before the component is visible
B. It may not handle repainting correctly
C. It may lead to incorrect graphics object handling
D. It requires manual management of painting
Correct Answer:
B
Chapter: 19
Q304:
What method should be overridden to handle custom painting in Swing components?
A. paintBorder()
B. paintComponent()
C. paintChildren()
D. repaint()
Correct Answer:
B
Chapter: 19
Q305:
In the example code, what does g.fillRect(mX, mY, 20, 20) do?
A. Draws a filled rectangle at position (mX, mY)
B. Draws an oval at position (mX, mY)
C. Clears the drawing area
D. Changes the color of the graphics object
Correct Answer:
A
Chapter: 19
Q306:
What is the role of super.paintComponent(g) in the paintComponent method?
A. It clears previous drawings before new painting
B. It sets the color of the graphics object
C. It paints the component's border
D. It updates the component's layout
Correct Answer:
A
Chapter: 19
Q307:
How is the mX and mY position updated during mouse drag events?
A. By calling repaint()
B. By using MouseEvent.getX() and MouseEvent.getY()
C. By directly assigning hard-coded values
D. By calling paintComponent()
Correct Answer:
B
Chapter: 19
Q308:
What is the purpose of the Timer class in the ball animation example?
A. To schedule repeated updates to the ball's position
B. To handle mouse events
C. To change the ball's color
D. To create a new instance of JFrame
Correct Answer:
A
Chapter: 19
Q309:
How does the actionPerformed method handle the ball's movement?
A. By directly setting mX and mY to random values
B. By updating the direction variables and calling repaint()
C. By changing the JPanel size
D. By stopping the Timer
Correct Answer:
B
Chapter: 19
Q310:
What happens if you comment out super.paintComponent(g) in the paintComponent method?
A. Previous drawings will not be cleared
B. The component will not be visible
C. The component's border will not be painted
D. The component will not respond to mouse events
Correct Answer:
A
Chapter: 19
Q311:
In the AnimTest class, what does the Timer object do?
A. It handles mouse events
B. It schedules repeated calls to actionPerformed()
C. It changes the size of the JFrame
D. It updates the ball's color
Correct Answer:
B
Chapter: 19
Q312:
What does the if (p.mX == 0) condition check in the actionPerformed method?
A. If the ball has reached the minimum width of the frame
B. If the ball is moving in the opposite direction
C. If the ball should change color
D. If the ball's position is within the frame's boundaries
Correct Answer:
A
Chapter: 19
Q313:
How does p.repaint() affect the ball animation?
A. It clears the ball from the screen
B. It updates the ball's position on the screen
C. It stops the ball's movement
D. It changes the ball's color
Correct Answer:
B
Chapter: 20
Q314:
Which Swing method is recommended for custom painting rather than calling paintComponent() directly?
A. repaint()
B. paint()
C. paintBorder()
D. paintChildren()
Correct Answer:
A
Chapter: 20
Q315:
How is mouse dragging handled in the provided example?
A. By overriding the paintComponent() method
B. By using the MouseMotionAdapter to capture drag events
C. By calling super.paintComponent()
D. By setting the position manually
Correct Answer:
B
Chapter: 20
Q316:
What happens when the if (f.getWidth()-40 == p.mX) condition is true in the actionPerformed method?
A. The X-direction of the ball changes
B. The ball is drawn at the top-left corner
C. The ball's size is increased
D. The Timer is stopped
Correct Answer:
A
Chapter: 20
Q317:
What is an applet in Java?
A. A standalone application
B. A small program embedded in a web page
C. A type of Java class
D. A database application
Correct Answer:
B
Chapter: 20
Q318:
Which method initializes an applet when it is loaded?
A. start()
B. paint()
C. init()
D. destroy()
Correct Answer:
C
Chapter: 20
Q319:
What is the purpose of the paint() method in an applet?
A. To stop the applet's execution
B. To perform final cleanup
C. To draw or display content
D. To initialize the applet
Correct Answer:
C
Chapter: 20
Q320:
How can you view an applet without using a browser?
A. By running it directly in Java IDE
B. By using the appletviewer command
C. By compiling it to a .class file
D. By using an external tool
Correct Answer:
B