1. What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is designed to be platform-independent, meaning that Java programs can run on any device that has the Java Virtual Machine (JVM) installed.2. What are the main features of Java?
- Some of the main features of Java include:
- Object-oriented: Java supports object-oriented programming principles such as encapsulation, inheritance, and polymorphism.
- Platform independence: Java programs can run on any device with the Java Virtual Machine (JVM), making them platform-independent.
- Automatic memory management: Java has a built-in garbage collector that automatically manages memory allocation and deallocation.
- Robust and secure: Java has features like strong type checking, exception handling, and security measures to ensure robust and secure code.
- Multi-threading: Java supports multi-threading, allowing concurrent execution of multiple tasks.
3. What is the difference between JDK, JRE, and JVM?
- JDK (Java Development Kit) is a software development kit that includes tools for developing, debugging, and monitoring Java applications. It contains JRE and development tools such as compilers and debuggers.
- JRE (Java Runtime Environment) is a runtime environment that provides the necessary libraries and resources for running Java applications. It includes the JVM and core libraries.
- JVM (Java Virtual Machine) is an abstract computing machine that provides a runtime environment for executing Java bytecode. It converts bytecode into machine code and handles memory management and other runtime tasks.
4. What are the primitive data types in Java?
Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean.5. What is the difference between == and equals() method in Java?
- The == operator in Java is used to compare primitive data types or to check if two objects reference the same memory location.
- The equals() method is a method of the Object class that is used to compare the contents of two objects for equality. It should be overridden in user-defined classes to provide meaningful comparison based on the object's state.
6. What is the difference between public, private, protected, and default access modifiers in Java?
- public: The member is accessible from any other class.
- private: The members are accessible only within the same class.
- protected: The member is accessible within the same package and subclasses.
- default (no modifier specified): The member is accessible only within the same package.
7. What is method overloading and method overriding in Java?
- Method overloading is the process of defining multiple methods in the same class with the same name but different parameter lists.
- Method overriding is the process of providing a new implementation for a method in a subclass, which has the same signature as a method in its superclass.
8. What is a constructor in Java?
A constructor in Java is a special type of method that is called when an object of a class is instantiated. It is used to initialize the object and allocate memory for it.9. What is the static keyword in Java?
The static keyword in Java is used to declare variables, methods, and blocks that belong to the class rather than instances of the class. Static members are shared among all instances of the class.10. What is the difference between interface and abstract class in Java?
- An interface in Java is a blueprint of a class that defines a set of methods without providing their implementation. All methods in an interface are implicitly abstract and public.
- An abstract class is a class that cannot be instantiated and may contain both abstract and non-abstract methods. Abstract methods are declared without providing an implementation and must be overridden by subclasses.
11. What is the difference between ArrayList and LinkedList in Java?
- ArrayList is implemented as a resizable array, while LinkedList is implemented as a doubly linked list.
- ArrayList provides fast random access but slower insertion and deletion of elements, whereas LinkedList provides fast insertion and deletion but slower random access.
12. What is a Servlet?
A Servlet is a Java class that extends the capabilities of servers and responds to the requests sent by clients (usually web browsers). It operates on the server side and generates dynamic content.13. What are the life cycle methods of a Servlet?
The life cycle of a Servlet includes three main methods:- init(): Called by the servlet container to initialize the servlet.
- service(): Called by the servlet container to handle client requests. It dispatches requests to appropriate methods such as doGet(), doPost(), etc.
- destroy(): Called by the servlet container to indicate that the servlet is being taken out of service.
14. What is the difference between doGet() and doPost() methods in Servlets?
- doGet() is used to handle HTTP GET requests, typically used for fetching data from the server, while
- doPost() is used to handle HTTP POST requests, usually used for submitting data to the server.
15. What is the purpose of the init() and destroy() methods?
The init() method is used to perform any initialization tasks needed by the servlet, such as loading configuration data or establishing database connections. The destroy() method is called when the servlet is being taken out of service and is used to perform cleanup tasks, such as closing database connections or releasing resources.16.What is the difference between ServletConfig and ServletContext?
- ServletConfig is an object associated with a particular servlet instance and contains initialization parameters specific to that servlet.
- ServletContext is an object associated with the entire web application and contains initialization parameters that are shared across all servlets within the
17. What is a Servlet Filter?
A Servlet Filter is a Java class that intercepts requests and responses to perform preprocessing or postprocessing tasks, such as logging, authentication, or data compression. Filters are configured in the deployment descriptor and executed in the order they are defined.18. What is JDBC?
JDBC (Java Database Connectivity) is an API (Application Programming Interface) that allows Java applications to interact with relational databases.19. What are the main components of JDBC architecture?
- JDBC architecture consists of:
- JDBC API: Provides interfaces and classes for database connectivity.
- JDBC Driver Manager: Manages the JDBC drivers. It loads the appropriate driver and establishes a connection to the database.
- JDBC Drivers: Implementations of the JDBC interfaces for specific databases.
20. What are the steps involved in connecting to a database using JDBC?
- Load the JDBC driver using forName().
- Create a connection to the database using getConnection().
- Create a statement object using the connection.
- Execute SQL queries using the statement.
- Process the results returned by the database.
- Close the connection, statement, and result set objects.
21. What are the different types of JDBC drivers?
There are four types of JDBC drivers:- Type 1: JDBC-ODBC bridge driver (now deprecated).
- Type 2: Native-API driver, which uses a database-specific API to communicate with the database.
- Type 3: Network protocol driver, which communicates with a middleware server using a database-independent protocol.
- Type 4: Pure Java driver, which communicates directly with the database using the vendor's proprietary protocol.
22. What is a PreparedStatement in JDBC?
PreparedStatement is a precompiled SQL statement that is stored in a PreparedStatement object and executed multiple times with different parameters. It improves performance and prevents SQL injection attacks by automatically escaping parameters.23. What is the difference between Statement and PreparedStatement in JDBC?
Statement is used to execute static SQL queries, while PreparedStatement is used to execute dynamic SQL queries with parameters. PreparedStatement is precompiled, which can improve performance and security.24. How do you retrieve data from a ResultSet in JDBC?
Data can be retrieved from a ResultSet using methods like getString(), getInt(), getDouble(), etc., based on the data type of the columns. Iteration over the ResultSet is typically done using a while loop.25. What is JSP (JavaServer Pages)?
JSP is a technology used to create dynamic web pages by embedding Java code within HTML. It allows developers to write server-side code that generates dynamic content, which is then sent to the client's web browser.26. What are the different types of directives in JSP?
There are three types of directives in JSP:- Page directive (<%@ page %>): Defines page-specific attributes such as error handling, language, session management, etc.
- Include directive (<%@ include %>): Includes content of another resource (such as HTML, JSP, or text file) in the current JSP page during translation.
- Taglib directive (<%@ taglib %>): Declares a tag library that contains custom tags used in the JSP page.
No comments:
Post a Comment