Online-Academy
Look, Read, Understand, Apply

Advanced Java Q and A

Java Swing (GUI Programming)

  1. What is Java Swing?

    Java Swing is a part of Java's GUI toolkit that allows the creation of window-based applications. It's built on top of AWT (Abstract Window Toolkit) and provides lightweight components such as JButton, JLabel, and JTable.

  2. Difference between AWT and Swing?
    Feature AWT Swing
    Platform Dependency Dependent (uses native OS) Platform independent
    Components Heavyweight Lightweight
    Look and Feel Fixed (OS-based) Pluggable (customizable)
  3. What is the use of JFrame class?

    JFrame is the top-level container used to create a window in Swing. It provides a title bar, close/minimize buttons, and a content pane to hold other components.

  4. What is the Event Delegation Model in Swing?
    • It is the mechanism that separates event generation from event handling.
    • Event Source: Component that generates the event.
    • Event Listener: Object that handles the event.
  5. How to display a message box in Swing?

    Using JOptionPane.showMessageDialog(null, "Hello World!");

  6. What is a Layout Manager in Swing?

    A layout manager controls the positioning and sizing of components in a container, e.g.:

    1. FlowLayout
    2. BorderLayout
    3. GridLayout
    4. BoxLayout
  7. What is a JTable used for?

    It displays data in a tabular form (rows and columns) and supports editing, sorting, and custom rendering.

  8. How can you change the Look and Feel of Swing?

    By calling: UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

  9. JDBC (Java Database Connectivity)

  10. What is JDBC?

    JDBC is an API that enables Java applications to interact with relational databases using SQL.

  11. Explain the main steps in JDBC.
    1. Load the driver
    2. Establish a connection
    3. Create a statement
    4. Execute the query
    5. Process the result
    6. Close connection
  12. What are the types of JDBC drivers?
    1. JDBC-ODBC Bridge Driver
    2. Native-API Driver
    3. Network Protocol Driver
    4. Thin Driver (Pure Java)
  13. What is a PreparedStatement?

    A precompiled SQL statement that improves performance and prevents SQL injection.
    PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE id=?"); ps.setInt(1, 101);

  14. How do you perform a transaction in JDBC?
       con.setAutoCommit(false);
       try {
          // SQL operations
          con.commit();
       } catch(Exception e) {
          con.rollback();
       }
    
  15. What is the difference between Statement and PreparedStatement?
    Feature Statement PreparedStatement
    Compilation Compiled each time Compiled once
    Performance Slower Faster
    Security Vulnerable to SQL injection Secure
  16. How to retrieve data from a ResultSet?
       while(rs.next()) {
          System.out.println(rs.getString("name"));
       }
    

    Servlet

  17. What is a Servlet?

    A Servlet is a Java class that handles HTTP requests and responses on a web server, extending the capabilities of a web application.

  18. Lifecycle methods of a Servlet?
    1. init() — initialization
    2. service() — handles request
    3. destroy() — cleanup before shutdown
  19. Difference between doGet() and doPost()?
    Feature doGet() doPost()
    Data in URL Visible Hidden
    Data limit Limited (~2KB) No limit
    Use case Retrieve data Submit data
  20. How to get form data in a Servlet?

    String name = request.getParameter("username");

  21. What is the web.xml file used for?

    It is the deployment descriptor that defines servlet mappings, initialization parameters, and configuration details.

  22. What is a ServletContext?

    It represents the web application environment and allows servlets to share information (e.g., application-wide parameters).

  23. How can you redirect from one servlet to another?

    Using: response.sendRedirect("NextServlet");

    JSP (Java Server Pages)

  24. What is JSP?

    JSP is a server-side technology that allows embedding Java code inside HTML to create dynamic web pages.

  25. How is JSP different from Servlet?
    Feature JSP Servlet
    Syntax HTML + Java Pure Java
    Use Presentation layer Logic layer
    Compilation Auto-translated to Servlet Manually written
  26. What are JSP directives?
    They give global information about the JSP page.
    Example:
    
    <%@ page language="java" contentType="text/html" %>
    <%@ include file="header.jsp" %>
    
  27. What are JSP implicit objects?

    JSP provides 9 implicit objects automatically, e.g.: request, response, session, application, out, page, pageContext, config, exception.

  28. How can you include another JSP page?

    <jsp:include page="footer.jsp" />

  29. What is the difference between <jsp:include> and <%@ include %>?
    Feature <jsp:include> <%@ include %>
    Type Dynamic Static
    Evaluation Runtime Translation time
  30. What is the use of JSP expression language (EL)?

    EL simplifies access to data stored in JavaBeans, request, session, etc. Example:
    ${user.name} instead of <%= user.getName() %>

  31. How can you forward a request in JSP?

    <jsp:forward page="next.jsp" />