Java Question Bank - A Section (Answers)
1. Characteristics of identifiers: Must begin with a letter, $, or _; cannot start with a digit; are case-sensitive;
cannot be a keyword; can be of any length.
2. Purpose of comments: Improve code readability and maintainability; help in documentation; ignored by the
compiler.
3. Types of inheritance: Single, Multilevel, Hierarchical, Multiple (via interfaces), and Hybrid inheritance.
4. Operators in Java: Arithmetic, Relational, Logical, Bitwise, Assignment, Unary, Ternary, and instanceof
operator.
5. Downcasting with an instance: Refers to casting a superclass reference to a subclass type. Example:
Animal a = new Dog(); Dog d = (Dog) a;
6. Purpose of `:`: Used in enhanced for-loops (for-each) and in ternary conditional operators.
7. Reason behind `public class Main {`: `public` allows access from outside the package; `class Main` defines
the class named Main; Java starts execution from public static void main.
8. Static class methodology: In Java, static nested classes are associated with the outer class and can
access static members of the outer class.
9. Packages vs Interfaces similarities: Both promote modularity, help in code reusability, improve
encapsulation, and are used for organizing and structuring code.
10. Using static methods: Static methods belong to the class, not instances. Example: class MathUtil { static
int add(int a, int b) { return a + b; } }
11. Types of packages: Built-in (java.lang, java.util, java.io) and user-defined packages.
12. `super` and `final`: `super` refers to the parent class; `final` prevents method overriding, inheritance, and
reassignment of variables.
13. Exception handling reason: Handles runtime errors, maintains normal program flow, and improves fault
tolerance.
14. Multithreading with example: class MyThread extends Thread { public void run() {
System.out.println("Thread running"); } }
15. Applet life cycle methods: init(), start(), stop(), destroy().
16. Reasons for Exception handling: Avoid abnormal termination; provide alternate solutions; error tracking.
17. Purpose of Multithreading: Perform multiple tasks simultaneously; improves performance.
18. States in Multithreading: New, Runnable, Blocked, Waiting, Timed Waiting, Terminated.
19. Print Output methodology: Use of System.out.print(), System.out.println(), and System.out.printf() for
output display.
20. Pass parameters to applet: Using <param> tags in HTML and getParameter(String name) in applet code.
21. Properties of Java Servlet: Platform-independent, secure, efficient, scalable, server-side components.
22. Servlet lifecycle methods: init(), service(), and destroy().
23. Use of == and =: = is assignment; == compares primitive values or reference addresses.
24. Class and constructor relation: Constructor is a special method invoked during object creation to initialize
members.
25. Method overriding: Redefining a superclass method in a subclass with the same signature.
26. Using static methods: Same as Q10; helps utility methods, shared logic without needing instances.
27. Print Output methodology: Same as Q19.
28. Multithreading states: Same as Q18.
29. Using layout managers & menus: FlowLayout, BorderLayout, GridLayout. Menus: MenuBar, Menu,
MenuItem.
30. Working with graphics and fonts: public void paint(Graphics g) { g.setFont(new Font("Arial", Font.BOLD,
20)); g.setColor(Color.RED); g.drawString("Hello", 50, 50); }
31. Event with example: button.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { System.out.println("Button clicked!"); } });
32. Windows Fundamentals purpose: Develop GUI apps; provide platform for graphical components like
frames, buttons.
33. Event handling & Adapter classes: Adapter classes provide empty implementations of listener interfaces;
used for overriding only necessary methods.
34. AWT Controls: Button, Label, TextField, Checkbox.
35. Servlet parameter passing: String param = request.getParameter("name");
36. Servlet container reason: Manages servlet lifecycle, communication between client and server, and
resources.
37. Key features of servlets: Persistent, efficient, scalable, support for sessions, platform-independent.
38. Servlet Container role: Also known as servlet engine (e.g., Apache Tomcat); executes servlets and
handles HTTP requests/responses.
39. Application Architecture example: MVC (Model-View-Controller). Example: JSP (View), Servlet
(Controller), JavaBeans (Model).
40. Working with ResultSet example: ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while(rs.next()) { System.out.println(rs.getString("username")); }