Web Based Java Programming MCQ Bank
Web Based Java Programming MCQ Bank
3. Which component of J2EE is responsible for managing security and access control for the
applications?
a) EJB Container
b) Web Container
c) Application Client Container
d) JMS Container
Answer: b) Web Container
5. The J2EE platform provides a standardized __________ to develop, deploy, and manage
applications.
a) IDE
b) API
c) Compiler
d) Database
Answer: b) API
A. J2EE Container
4. Which statement is true about the relationship between a web container and an EJB container?
a) They are the same thing; the terms can be used interchangeably.
b) The web container is a part of the EJB container.
c) The EJB container is a part of the web container.
d) They are independent and separate entities.
Answer: d) They are independent and separate entities.
7. Which type of container is responsible for managing the lifecycle of servlets and JSPs?
a) Web container
b) EJB container
c) Application client container
d) Resource container
Answer: a) Web container
8. The container provides __________, which allows developers to build components that are portable
across different J2EE implementations.
a) Java Virtual Machine (JVM)
b) Java API for XML Web Services (JAX-WS)
c) Java Naming and Directory Interface (JNDI)
d) Java EE Application Deployment Descriptor (DD)
Answer: d) Java EE Application Deployment Descriptor (DD)
3. In a web application, where should the JSP files be placed within the .war file?
a) In the WEB-INF/classes directory
b) In the root directory of the .war file
c) In the WEB-INF/lib directory
d) In the WEB-INF directory
Answer: d) In the WEB-INF directory
6. When deploying a web application, where should the .war file be placed?
a) In the root directory of the J2EE server
b) In the bin directory of the J2EE server
c) In the lib directory of the J2EE server
d) In the deploy directory of the J2EE server
Answer: d) In the deploy directory of the J2EE server
7. What is the purpose of the J2EE deployment descriptor (DD) in a web application?
a) To specify the database schema
b) To define the Java classes
c) To configure security settings and URL mappings
d) To implement business logic
Answer: c) To configure security settings and URL mappings
8. The packaging of a web application into a .war file is done using __________.
a) The J2EE SDK
b) An Integrated Development Environment (IDE)
c) A web browser
d) A command-line tool like 'jar'
Answer: d) A command-line tool like 'jar'
2. Which of the following is a mandatory requirement for a web application to be J2EE compliant?
a) It must be packaged as a .jar file
b) It must use JDBC for database access
c) It must follow the J2EE naming conventions
d) It must implement a specific business logic
Answer: c) It must follow the J2EE naming conventions
4. To be J2EE compliant, a web application must use standardized APIs for __________.
a) Database access and security
b) Graphics rendering and animations
c) Audio and video playback
d) User authentication via social media accounts
Answer: a) Database access and security
5. Which component of a J2EE compliant web application is responsible for handling user requests and
generating dynamic content?
a) EJB component
b) Servlet or JSP component
c) Web service component
d) JMS component
Answer: b) Servlet or JSP component
6. What is the role of the J2EE application client container in a J2EE compliant web application?
a) Managing Enterprise JavaBeans (EJBs)
b) Handling user interface interactions
c) Providing access to databases
d) Securing web services
Answer: b) Handling user interface interactions
8. A J2EE compliant web application is tested for compatibility with the J2EE specification using
__________.
a) JUnit tests
b) Java applets
c) Compliance testing tools provided by J2EE vendors
d) Automated web crawlers
Answer: c) Compliance testing tools provided by J2EE vendors
D. Deployment tools
1. Deployment tools in J2EE are used to __________.
a) Write Java code
b) Package applications into deployable units
c) Run web applications on web browsers
d) Configure database connections
Answer: b) Package applications into deployable units
2. Which of the following is a commonly used build automation tool for J2EE applications?
a) Eclipse
b) NetBeans
c) Maven
d) IntelliJ IDEA
Answer: c) Maven
3. Deployment tools help in managing __________ during the application development life cycle.
a) Source code versioning
b) Software testing
c) Database transactions
d) Application configurations
Answer: d) Application configurations
5. Which type of deployment tool is used to manage different versions of a J2EE application and
rollback to a previous version if needed?
a) Integrated Development Environment (IDE)
b) Configuration Management Tool
c) Build Automation Tool
d) Web Server
Answer: b) Configuration Management Tool
7. Which deployment tool is used to package and deploy applications to a J2EE-compliant application
server?
a) Java Compiler (javac)
b) Ant
c) Jenkins
d) Apache Tomcat
Answer: d) Apache Tomcat
8. Which deployment tool can be used to automate the process of configuring and deploying Docker
containers for J2EE applications?
a) Docker Compose
b) Ansible
c) Puppet
d) Chef
Answer: a) Docker Compose
3. During the initialization phase of the web application life cycle, the container __________.
a) Parses the web.xml deployment descriptor
b) Sends an HTTP request to the server
c) Loads the web application's resources into memory
d) Compiles the Java source code
Answer: a) Parses the web.xml deployment descriptor
4. What happens during the request processing phase of the web application life cycle?
a) The container generates dynamic content and sends it to the client.
b) The container compiles the JSP pages into servlets.
c) The container initializes the servlets and filters.
d) The container handles the incoming HTTP request and routes it to the appropriate servlet.
Answer: d) The container handles the incoming HTTP request and routes it to the appropriate servlet.
5. The session management in the web application life cycle is used to __________.
a) Maintain a user's state across multiple HTTP requests
b) Terminate a user's session after a fixed period of inactivity
c) Manage the lifecycle of servlets
d) Store static resources in a cache for faster access
Answer: a) Maintain a user's state across multiple HTTP requests
6. Which of the following is NOT part of the web application life cycle?
a
) Initialization
b) Request processing
c) Destruction
d) Compilation
Answer: d) Compilation
7. The destruction phase of the web application life cycle is triggered when __________.
a) The web server is shut down
b) The application is redeployed
c) A user logs out of the application
d) The session times out
Answer: a) The web server is shut down
3. Which section of the deployment descriptor defines the URL patterns for servlets?
a) <servlet>
b) <servlet-mapping>
c) <url-pattern>
d) <web-app>
Answer: b) <servlet-mapping>
6. When deploying a web application, what does the server do with the .war file?
a) The server extracts the .war file's contents and deploys them to a temporary directory.
b) The server converts the .war file into a .jar file for deployment.
c) The server directly executes the .war file as-is.
d) The server moves the .war file to the root directory of the server.
Answer: a) The server extracts the .war file's contents and deploys them to a temporary directory.
7. After deploying a web application, users can access the application using __________.
a) The URL specified in the web.xml deployment descriptor
b) Any URL with the appropriate file extension (e.g., .jsp or .html)
c) A randomly generated URL provided by the server
d) The server's IP address only
Answer: b) Any URL with the appropriate file extension (e.g., .jsp or .html)
2. Which of the following is a standard protocol used in web services to communicate between
applications?
a) HTTP
b) FTP
c) SMTP
d) SNMP
Answer: a) HTTP
3. Web services use __________ to describe their functionalities and data formats.
a) Web browsers
b) SOAP
c) UML diagrams
d) WSDL and XML
Answer: d) WSDL and XML
6. Which statement is true about the communication method used in SOAP web services?
a) It uses a binary protocol for data exchange.
b) It relies on direct database connections for data retrieval.
c) It uses XML-based messages for communication.
d) It cannot be used over the internet, only on intranets.
Answer: c) It uses XML-based messages for communication.
1. Which of the following is an advantage of servlets over Common Gateway Interface (CGI) programs?
a) Servlets are platform-dependent.
b) Servlets are slower in performance compared to CGI programs.
c) Servlets maintain a single process for multiple requests, reducing overhead.
d) Servlets can be written in any programming language.
Answer: c) Servlets maintain a single process for multiple requests, reducing overhead.
6. Servlets provide a persistent __________, unlike CGI, which spawns a new process for each request.
a) Class loader
b) Session
c) Thread
d) Cookie
Answer: c) Thread
3. Which method of the servlet life cycle is responsible for handling client requests?
a) `init()`
b) `start()`
c) `service()`
d) `process()`
Answer: c) `service()`
5. If a servlet is already initialized, and a new request comes in, which method will be called?
a) `init()`
b) `service()`
c) `doGet()`
d) `doPost()`
Answer: b) `service()`
7. Which of the following statements is true regarding the servlet life cycle?
a) The `init()` method is called only once during the lifetime of the servlet.
b) The `destroy()` method is automatically called by the servlet container when the server starts up.
c) The `service()` method is optional, and servlets can work without it.
d) The `doGet()` and `doPost()` methods are called before the `init()` method.
Answer: a) The `init()` method is called only once during the lifetime of the servlet.
2. In which directory should servlet class files be placed inside a web application?
a) WEB-INF/lib
b) WEB-INF/classes
c) /src/main/java
d) /WEB-INF/resources
Answer: b) WEB-INF/classes
4. To deploy a web application, the application's files and directories should be placed in the
__________.
a) /src/main/webapp directory
b) /webapps directory of the servlet container
c) /WEB-INF directory
d) /classes directory of the servlet container
Answer: b) /webapps directory of the servlet container
D. Servlet Annotations
6. The `@WebServlet` annotation can be used with which of the following classes/interfaces?
a) HttpServlet and HttpServletRequest
b) Servlet and ServletContext
c) JSP and JAX-RS
d) HttpServlet and ServletContext
Answer: a) HttpServlet and HttpServletRequest
7. The `GenericServlet` class provides a default implementation for which method(s) of the `Servlet`
interface?
a) `init()`
b) `destroy()`
c) `service()`
d) All of the above
Answer: d) All of the above
7. Which method of `HttpServletResponse` is used to set the content type of the response?
a) `setContentType()`
b) `setHeader()`
c) `setContentTypeHeader()`
d) `setMimeType()`
Answer: a) `setContentType()`
G. Exception Handling
1. In servlets, exceptions can occur during __________.
a) Servlet deployment
b) Servlet initialization
c) Servlet destruction
d) Request processing
Answer: d) Request processing
2. Which method is responsible for handling exceptions that occur during request processing in a
servlet?
a) `init()`
b) `doGet()`
c) `doPost()`
d) `service()`
Answer: d) `service()`
3. The `Throwable` class is the superclass of all error and exception classes in Java. Which method of
`Throwable` is used to print the stack trace of an exception?
a) `printStackTrace()`
b) `getMessage()`
c) `toString()`
d) `getCause()`
Answer: a) `printStackTrace()`
5. Which section of the `web.xml` file is used to define error pages for handling exceptions?
a) `<servlet>`
b) `<error-page>`
c) `<web-app>`
d) `<exception>`
Answer: b) `<error-page>`
6. The `status-code` element in the error page configuration of `web.xml` is used to specify
__________.
a) The error code for the exception
b) The error message for the exception
c) The URL of the error page
d) The display name for the error page
Answer: a) The error code for the exception
4. Which layer of the three-tier architecture interacts directly with the database to perform CRUD
(Create, Read, Update, Delete) operations?
a) Servlet layer
b) DAO layer
c) POJO layer
d) Presentation layer
Answer: b) DAO layer
7. Which layer of the three-tier architecture is primarily responsible for defining the user interface
design and layout?
a) Servlet layer
b) DAO layer
c) POJO layer
d) Presentation layer
Answer: d) Presentation layer
I. Session
1. In a web application, what is a session?
a) A specific period of time during which a user interacts with the website
b) A unique identifier for the user's web browser
c) A virtual meeting space for users to chat with each other
d) A mechanism to track user interactions across multiple HTTP requests
Answer: d) A mechanism to track user interactions across multiple HTTP requests
J. Session Management
1. What is session management in the context of web applications?
a) Tracking user behavior and interactions on the website
b) Maintaining a record of login/logout times for users
c) Managing and maintaining user sessions during their visit to the website
d) Managing access to various sections of the website based on user roles
Answer: c) Managing and maintaining user sessions during their visit to the website
3. Which of the following is NOT a typical method of session management in web applications?
a) Using cookies to store session data
b) Storing session data in a server-side database
c) Tracking sessions based on the user's IP address
d) Passing session data as URL parameters
Answer: c) Tracking sessions based on the user's IP address
4. When a user logs in to a web application, a session is typically created, and a unique session ID is
__________.
a) Stored on the client's web browser
b) Stored in a server-side database
c) Sent to the user as a cookie
d) Displayed on the website's login page
Answer: c) Sent to the user as a cookie
7. Hidden Form Fields for session tracking are commonly used in __________.
a) Modern web applications built with JavaScript frameworks
L. Cookies
5. What is the maximum size of a cookie that can be stored on a client's web browser?
a) 256 bytes
b) 1 KB
c) 4 KB
d) 8 KB
Answer: c) 4 KB
M. HttpSession
1. In a Java web application, what is an `HttpSession`?
a) A special type of servlet used for handling HTTP requests
b) An interface that defines the session tracking methods
c) A mechanism to store user-specific data during a session
d) A server-side database used to store user login information
Answer: c) A mechanism to store user-specific data during a session
3. What is the default timeout period for an `HttpSession` in most Java web application servers?
a) 5 minutes
b) 15 minutes
c) 30 minutes
d) 60 minutes
Answer: c) 30 minutes
N. Request Dispatcher
1. What is the purpose of the `RequestDispatcher` interface in Java servlets?
a) To dispatch HTTP requests to multiple servlets simultaneously
b) To dispatch HTTP responses to the client's web browser
c) To forward the current request to another resource (servlet or JSP) for further processing
d) To process user authentication and authorization
Answer: c) To forward the current request to another resource (servlet or JSP) for further processing
4. When using `RequestDispatcher` to forward a request, the URL displayed in the user's browser
__________.
a) Remains unchanged
b) Changes to the URL of the resource the request is forwarded to
c) Displays
6. What is the primary difference between `forward()` and `include()` methods of `RequestDispatcher`?
a) The `forward()` method is used for forwarding requests, while `include()` is used for including
responses.
b) The `forward()` method changes the URL displayed in the user's browser, while `include()` keeps
the URL unchanged.
c) The `forward()` method can only be used with JSP pages, while `include()` can be used with
servlets and JSP pages.
d) The `forward()` method allows the included resource to modify the response, while `include()`
does not.
Answer: b) The `forward()` method changes the URL displayed in the user's browser, while
`include()` keeps the URL unchanged.
7. What is the scope of objects (request, response, session) shared between the original servlet and the
forwarded servlet when using `RequestDispatcher`?
a) The objects are shared within the forwarded servlet only and not accessible in the original servlet.
b) The objects are shared within the original servlet only and not accessible in the forwarded servlet.
c) The objects are shared between both the original and forwarded servlets, and changes are reflected
in both.
d) The objects are copied, and changes in one servlet do not affect the other.
Answer: c) The objects are shared between both the original and forwarded servlets, and changes are
reflected in both.
O. Page Navigation
1. Page navigation in web applications refers to __________.
a) The process of creating dynamic content for web pages
b) The process of optimizing web pages for search engines
c) The movement of users from one web page to another within the application
d) The process of encrypting sensitive data transmitted between the client and server
Answer: c) The movement of users from one web page to another within the application
2. Which of the following is NOT a typical way to navigate between pages in a web application?
a) Clicking on hyperlinks
b) Using the back button in the web browser
c) Typing the page URL in the address bar
d) Submitting an HTML form
Answer: d) Submitting an HTML form
7. In a typical web application, how can you ensure that users navigate to specific pages after
successful login or logout?
a) By using JavaScript to modify the page URL
b) By setting the `pageNavigation` attribute in the `web.xml` file
c) By configuring the login/logout URLs in the server's settings
d) By using servlets to handle login/logout requests and sending appropriate redirects
Answer: d) By using servlets to handle login/logout requests and sending appropriate redirects
- MCQ 2: What is the primary purpose of user login in the context of the online shopping website?
a) To view the website's homepage
b) To access the shopping cart
c) To browse products
d) To personalize the shopping experience
Answer: d) To personalize the shopping experience
- MCQ 4: How can the server-side retrieve the list of products to display on the product listing page?
a) By querying the database in the JSP page using SQL
b) By embedding the product details directly in the JSP page
c) By calling a servlet to fetch the product data from the database and passing it to the JSP
d) By using JavaScript to fetch product data from a remote API
Answer: c) By calling a servlet to fetch the product data from the database and passing it to the JSP
- MCQ 6: How can the shopping cart data be maintained across multiple requests?
a) By storing the cart data as a session attribute
b) By passing the cart data as URL parameters
c) By embedding the cart data directly in the JSP page
d) By using cookies to store the cart data on the client-side
Answer: a) By storing the cart data as a session attribute
- MCQ 8: Which component is responsible for displaying the order confirmation to the user after
successful payment?
a) Servlet
b) JSP
c) HTML
d) CSS
Answer: b) JSP
3. Lecture: JSP: Separating UI from Content generation code (7 MCQs on Each
Subtopic)
Total No. of MCQs : 56
A. MVC architecture
3. Which component of the MVC architecture is responsible for rendering the user interface?
a) Model
b) View
c) Controller
d) Servlet
Answer: b) View
6. In the MVC architecture, communication between the Model, View, and Controller is typically done
through:
a) Cookies
b) Session objects
c) Direct method calls
d) URL parameters
Answer: c) Direct method calls
3. Which component of the MVC pattern is responsible for handling user interactions and updating the
Model and View?
a) Model
b) View
c) Controller
d) Servlet
Answer: c) Controller
7. In a web application using the MVC design pattern, how does the View interact with the Model?
a) The View directly calls methods in the Model.
b) The View indirectly communicates with the Model through the Controller.
c) The View and Model are not connected in the MVC pattern.
d) The Model accesses data from the View directly.
Answer: c) The View and Model are not connected in the MVC pattern.
3. What happens during the compilation phase of a JSP page's life cycle?
a) The JSP code is executed and the response is generated.
b) The JSP code is translated into a servlet class by the
JSP container.
c) The JSP page is loaded into memory and initialized.
d) The JSP page is destroyed and removed from memory.
Answer: b) The JSP code is translated into a servlet class by the JSP container.
5. During the execution phase of a JSP page's life cycle, which method is called to handle user requests?
a) `init()`
b) `service()`
c) `destroy()`
d) `doGet()`
Answer: b) `service()`
6. When does the destruction phase of a JSP page's life cycle occur?
a) When the JSP page is first accessed by a user
b) After the `init()` method is called
c) When the JSP page is no longer in use and removed from memory
d) After the `service()` method is executed
Answer: c) When the JSP page is no longer in use and removed from memory
2. Which JSP directive is used to include the content of another resource, such as a text file, into the
JSP page?
a) `@include`
b) `@import`
c) `@includeFile`
d) `@page`
Answer: a) `@include`
4. Which Implicit Object is used to access the request parameters sent by the client?
a) `request`
b) `response`
c) `out`
d) `session`
Answer: a) `request`
6. Which JSP expression is used to evaluate and display the value of a variable or expression?
a) `<%= %>`
b) `<%! %>`
c) `<% %>`
d) `<@ @>`
Answer: a) `<%= %>`
E. Language
1. Which programming language(s) can be used in JSP pages for scripting?
a) Java only
b) Java and JavaScript
c) Java and Python
d) JavaScript only
Answer: a) Java only
6. In a JSP page, what is the role of Java code written within scriptlets?
a) To define and call custom functions for complex logic
b) To declare and initialize variables used in the JSP page
c) To handle user interactions and process data
d) To render the user interface and display content
Answer: c) To handle user interactions and process data
7. Which programming constructs are commonly used in scriptlets to control the flow of a JSP page?
a) Loops and conditional statements
b) Class and interface definitions
c) Constructors and methods
d) Try-catch blocks for exception handling
Answer: a) Loops and conditional statements
F. Scope
1. In the context of JSP, what is the scope of a variable?
a
2. Which scope makes a variable accessible across all JSP pages during a user's session?
a) Request scope
b) Page scope
c) Session scope
d) Application scope
Answer: c) Session scope
3. What is the primary difference between the Request scope and the Session scope?
a) The Request scope is used for variables that remain constant throughout a user's session, while the
Session scope is used for variables that change with each request.
b) The Request scope is used for variables that are accessible within a single JSP page, while the
Session scope is used for variables that persist across multiple JSP pages during a user's session.
c) The Request scope is used for variables related to user authentication, while the Session scope is
used for variables related to database operations.
d) The Request scope is used for variables that are shared across all users of the application, while the
Session scope is used for variables that are specific to each user's session.
Answer: b) The Request scope is used for variables that are accessible within a single JSP page,
while the Session scope is used for variables that persist across multiple JSP pages during a user's
session.
4. What is the scope of a variable declared within a scriptlet in a JSP page?
a) Request scope
b) Page scope
c) Session scope
d) Application scope
Answer: b) Page scope
5. In a JSP page, which scope is typically used for variables that store user input data from a form?
a) Request scope
b) Page scope
c) Session scope
d) Application scope
Answer: a) Request scope
6. Which scope makes a variable accessible to all JSP pages and servlets within a web application?
a) Request scope
b) Page scope
c) Session scope
d) Application scope
Answer: d) Application scope
2. Which directive is used to specify the error page for a JSP application?
a) `@errorPage`
b) `@pageError`
c) `@exception`
d) `@page`
Answer: a) `@errorPage`
3. What happens if an unhandled exception occurs in a JSP page and no error page is defined?
a) The JSP container displays the detailed exception message to the user.
b) The user is redirected to the web application's homepage.
c) The JSP container automatically redirects the user to the default error page.
d) The application displays a generic error message to the user.
Answer: c) The JSP container automatically redirects the user to the default error page.
5. How can you retrieve the error message and stack trace in a custom error page to display to the user?
a) By using the `out` implicit object to print the error message directly
b) By calling the `getError()` method on the `HttpServletResponse` object
c) By using the `exception` implicit object to access the exception information
d) By declaring a variable named `error` and using it to store the error information
Answer: c) By using the `exception` implicit object to access the exception information
6. In a custom error page, how can you display a friendly message to the user instead of showing the
detailed exception stack trace?
a) By using the `errorPage` directive to handle specific error codes
b) By defining an error message property in the `web.xml` file
c) By wrapping the error-handling code in a try-catch block
d) By using conditional statements in the custom error page
Answer: d) By using conditional statements in the custom error page
7. Which configuration is required in the `web.xml` file to specify a custom error page for a specific
error code, such as 404 (Not Found)?
a) `<error-page>` with `<error-code>` and `<location>` elements
b) `<custom-error>` with `<error-code>` and `<location>` attributes
c) `<exception>` with `<error-code>` and `<location>` elements
d) `<error>` with `<error-code>` and `<location>` attributes
Answer: a) `<error-page>` with `<error-code>` and `<location>` elements
H. JSTL
1. What does JSTL stand for in the context of JSP development?
a) Java Server Transition Library
b) Java Standard Template Library
c) Java Server Tag Library
d) Java Server Task Library
Answer: c) Java Server Tag Library
3. Which JSTL tag library is used for performing iteration and loop constructs in JSP pages?
a) `<fmt>`
b) `<core>`
c) `<logic>`
d) `<c:forEach>`
Answer: d) `<c:forEach>`
5. How can you use the `<c:forEach>` tag to iterate over a collection of objects in a JSP page?
a) By providing the collection directly within the `<c:forEach>` tag as a string
b) By using a scriptlet to loop through the collection
c) By accessing the collection through an Implicit Object
d) By providing the collection as a JavaBeans property using EL
Answer: d) By providing the collection as a JavaBeans property using EL
6. Which JSTL tag is used to access the application scope attribute in a JSP page?
a) `<c:out>`
b) `<c:set>`
c) `<c:remove>`
d) `<c:application>`
Answer: d) `<c:application>`
3. Which Java package contains the core JDBC API classes and interfaces?
a) `java.sql`
b) `javax.sql`
c) `java.database`
d) `javax.database`
Answer: a) `java.sql`
4. The JDBC API provides a standard set of classes and interfaces for:
a) Connecting to databases and executing SQL queries
b) Building user interfaces for web applications
c) Handling JavaScript and CSS in Java applications
d) Managing session data in servlets
Answer: a) Connecting to databases and executing SQL queries
7. The JDBC API uses which programming paradigm for database access?
a) Procedural programming
b) Object-oriented programming
c) Functional programming
d) Event-driven programming
Answer: b) Object-oriented programming
B. JDBC Architecture
1. The JDBC architecture is based on which design pattern?
a) Observer pattern
b) Factory pattern
c) MVC pattern
d) Singleton pattern
Answer: b) Factory pattern
3. Which component of the JDBC architecture is responsible for loading the appropriate JDBC driver
at runtime?
a) JDBC API
b) JDBC driver manager
c) JDBC driver
d) JDBC connection pool
Answer: b) JDBC driver manager
4. The JDBC driver manager searches for a suitable driver from the available drivers based on:
a) The version of the Java Virtual Machine (JVM) installed
b) The user's preferences set in the JDBC driver configuration file
c) The database URL provided by the Java application
d) The type of database being accessed (e.g., Oracle, MySQL, etc.)
Answer: c) The database URL provided by the Java application
5. What is the purpose of the JDBC connection pool in the JDBC architecture?
a) To manage the database connections and execute SQL queries
b) To store the database credentials securely
c) To cache the results of executed SQL queries for faster access
d) To optimize database access and minimize the overhead of creating new connections
Answer: d) To optimize database access and minimize the overhead of creating new connections
7. Which component of the JDBC architecture is responsible for executing SQL queries and returning
results to the Java application?
a) JDBC API
b) JDBC driver manager
c) JDBC driver
d) JDBC connection pool
Answer: c) JDBC driver
8. In the JDBC architecture, how is the JDBC driver typically loaded into the Java application?
a) By including the JDBC driver's JAR file in the classpath of the application
b) By importing the JDBC driver classes in the application code
c) By adding the JDBC driver's package name in the `web.xml` file
d) By including the JDBC driver's URL in the database
configuration file
Answer: a) By including the JDBC driver's JAR file in the classpath of the application
C. JDBC Drivers
1. Which type of JDBC driver translates JDBC calls to the database-specific protocol using a
middleware server?
a) Type 1 driver
b) Type 2 driver
c) Type 3 driver
d) Type 4 driver
Answer: c) Type 3 driver
3. Which type of JDBC driver directly converts JDBC calls into the database-specific protocol,
eliminating the need for any middleware server?
a) Type 1 driver
b) Type 2 driver
c) Type 3 driver
d) Type 4 driver
Answer: d) Type 4 driver
5. Which type of JDBC driver requires the installation of database-specific client software on the client
machine?
a) Type 1 driver
b) Type 2 driver
c) Type 3 driver
d) Type 4 driver
Answer: b) Type 2 driver
1. Which JDBC interface is responsible for creating and managing a connection to the database?
a) `java.sql.Driver`
b) `java.sql.Connection`
c) `java.sql.Statement`
d) `java.sql.ResultSet`
Answer: b) `java.sql.Connection`
4. Which JDBC interface is an extension of the `java.sql.Statement` interface and is used to execute
parameterized SQL queries efficiently?
a) `java.sql.Statement`
b) `java.sql.PreparedStatement`
c) `java.sql.ResultSet`
d) `java.sql.Connection`
Answer: b) `java.sql.PreparedStatement`
7. What is the relationship between the `java.sql.Driver` interface and database-specific JDBC driver
implementations?
a) The `java.sql.Driver` interface is implemented by the database-specific JDBC drivers.
b) The `java.sql.Driver` interface is responsible for executing SQL queries.
c) The `java.sql.Driver` interface creates the `java.sql.Connection` instances for the Java application.
d) The `java.sql.Driver` interface is only used when the database-specific driver is not available.
Answer: a) The `java.sql.Driver` interface is implemented by the database-specific JDBC drivers.
.DriverManager`.
d) It is responsible for creating and managing database connections.
Answer: d) It is responsible for creating and managing database connections.
2. What is the primary advantage of using stored procedures and functions in a database?
a) They allow the Java application to directly interact with the database tables.
b) They provide a way to optimize and centralize business logic in the database.
c) They eliminate the need for JDBC drivers to communicate with the database.
d) They are more secure compared to standard SQL queries.
Answer: b) They provide a way to optimize and centralize business logic in the database.
3. In a Java application, how can you invoke a stored procedure or function using JDBC?
a) By using the `java.sql.Driver` interface to execute the stored procedure directly
b) By using a `java.sql.Statement` to execute the stored procedure as a SQL query
c) By using a `java.sql.PreparedStatement` to call the stored procedure with parameters
d) By creating a custom `java.sql.StoredProcedure` interface for the specific database
Answer: c) By using a `java.sql.PreparedStatement` to call the stored procedure with parameters
4. Which JDBC interface is commonly used to execute stored procedures and functions with input and
output parameters?
a) `java.sql.Driver`
b) `java.sql.Statement`
c) `java.sql.PreparedStatement`
d) `java.sql.ResultSet`
Answer: c) `java.sql.PreparedStatement`
6. In a Java application, how can you retrieve the output of a stored procedure or function that returns a
result set?
a) By using the `java.sql.Statement` interface to execute the stored procedure
b) By using the `java.sql.PreparedStatement` interface with `executeQuery()` method
c) By using the `java.sql.CallableStatement` interface with `getResultSet()` method
d) By using the `java.sql.ResultSet` interface with the stored procedure name
Answer: c) By using the `java.sql.CallableStatement` interface with `getResultSet()` method
7. Which statement is true about stored procedures and functions invocation in JDBC?
a) The JDBC API does not provide any support for calling stored procedures and functions.
b) The `java.sql.Driver` interface is used to execute stored procedures directly without the need for a
`java.sql.CallableStatement`.
c) The `java.sql.CallableStatement` interface is used to call stored procedures and functions with
input and output parameters.
d) Stored procedures and functions can only be invoked through direct SQL queries in JDBC.
Answer: c) The `java.sql.CallableStatement` interface is used to call stored procedures and functions
with input and output parameters.
4. What is one common method used to prevent SQL injection attacks in JDBC?
a) Using a more permissive database user account for JDBC connections
b) Hard-coding SQL queries in the Java application without using user input
c) Using parameterized queries or prepared statements to handle user input
d) Disabling database logs to prevent attackers from tracking their actions
Answer: c) Using parameterized queries or prepared statements to handle user input
6. Which statement is true about using parameterized queries or prepared statements in JDBC?
a) Parameterized queries are more efficient than standard SQL queries, but they cannot handle user
input.
b) Prepared statements are compiled by the JDBC driver before execution, making them faster and
more secure.
c) Parameterized queries and prepared statements are interchangeable and can be used
interchangeably in JDBC applications.
d) Using parameterized queries or prepared statements eliminates the need for sanitizing user input in
SQL queries.
Answer: b) Prepared statements are compiled by the JDBC driver before execution, making them
faster and more secure.
7. In a parameterized query or prepared statement, how are user-provided values included in the SQL
query?
a) They are concatenated as strings within the query.
b) They are added as parameters and assigned using `?` placeholders.
c) They are included within double quotes (" ") in the query.
d) They are directly embedded within single quotes (' ') in the query.
Answer: b) They are added as parameters and assigned using `?` placeholders.
8. What is the best practice for preventing SQL injection attacks in JDBC applications?
a) Avoid using user input in SQL queries altogether.
b) Perform regular security audits of the JDBC driver and database server.
c) Sanitize and validate all user input before using it in SQL queries.
d) Use stored procedures instead of direct SQL queries in JDBC applications.
Answer: c) Sanitize and validate all user input before using it in SQL queries.
2. What is the primary benefit of using the Data Access Object (DAO) pattern in JDBC applications?
a) It improves database performance by optimizing SQL queries.
b) It allows direct communication between the Java application and the database server.
c) It decouples the business logic from the data access logic, making the code more maintainable and
testable.
d) It provides a standardized way to handle SQL exceptions and errors.
Answer: c) It decouples the business logic from the data access logic, making the code more
maintainable and testable.
3. In the Data Access Object (DAO) pattern, the DAO interface is responsible for:
a) Defining the SQL queries and operations for database access
b) Executing SQL queries and processing the results
c) Handling database connections and transactions
d) Defining the data model and entity classes for the application
Answer: a) Defining the SQL queries and operations for database access
4. Which statement is true regarding the Data Access Object (DAO) pattern implementation?
a) The DAO interface is implemented by the JDBC driver directly.
b) The DAO interface should be implemented as a singleton class.
c) The DAO implementation should be tightly coupled with the business logic.
d) The DAO interface should be agnostic to the underlying data source and technology.
Answer: d) The DAO interface should be agnostic to the underlying data source and technology.
5. What is the purpose of using a separate DAO implementation for each entity or data model in the
application?
a) To reduce the number of database connections and improve performance
b) To simplify the data access logic by centralizing all operations in a single class
c) To encapsulate the data access logic specific to each entity and improve code organization
d) To enforce strict data validation rules on each entity before database operations
Answer: c) To encapsulate the data access logic specific to each entity and improve code
organization
6. Which statement is true about the usage of the Data Access Object (DAO) pattern in JDBC
applications?
a) The DAO pattern is only applicable to web applications and cannot be used in standalone Java
applications.
b) The DAO pattern should be used in conjunction with the Singleton pattern for optimal
performance.
c) The DAO pattern is primarily used for executing complex SQL queries in the application.
d) The DAO pattern is not a replacement for prepared statements and parameterized queries in JDBC
applications.
Answer: d) The DAO pattern is not a replacement for prepared statements and parameterized queries
in JDBC applications.
7. In the Data Access Object (DAO) pattern, how is the coupling between the business logic and data
access logic reduced?
a) By using a single DAO interface for all data access operations in the application
b) By tightly integrating the JDBC driver with the application code
c) By encapsulating the data access logic within the DAO implementation classes
d) By directly embedding SQL queries within the business logic classes
Answer: c) By encapsulating the data access logic within the DAO implementation classes
8. What is the role of the DAO interface and DAO implementation classes in the Data Access Object
(DAO) pattern?
a) The DAO interface defines the data model, while the DAO implementation classes handle
database connections and transactions.
b) The DAO interface defines the database operations, while the DAO implementation classes
contain the business logic.
c) The DAO interface contains the business logic, while the DAO implementation classes define the
data model.
d) The DAO interface defines the database operations, while the DAO implementation classes
perform those operations using JDBC.
Answer: d) The DAO interface defines the database operations, while the DAO implementation
classes perform those operations using JDBC.
5. Lecture: Hibernate Framework (7 MCQs on Each Subtopic)
Total No. of MCQs : 64
1. What is Hibernate?
a) A programming language
b) A web framework
c) An object-relational mapping (ORM) tool
d) A database management system
Answer: c) An object-relational mapping (ORM) tool
B. Architecture
1. What is the core component of the Hibernate architecture responsible for managing the persistence
of Java objects?
a) SessionFactory
b) Session
c) Configuration
d) Transaction
Answer: b) Session
6. Which component of the Hibernate architecture is responsible for managing the transactional
behavior of Hibernate operations?
a) Session
b) SessionFactory
c) Transaction
d) Configuration
Answer: c) Transaction
7. How does the Hibernate architecture ensure data consistency and integrity during transactions?
a) By using the JDBC driver's transaction management features
b) By executing SQL queries in a batch mode
c) By using the ACID properties of database transactions
d) By automatically creating backups of the database
Answer: c) By using the ACID properties of database transactions
C. Hibernate in IDE
1. Which Integrated Development Environment (IDE) is commonly used for Hibernate development in
Java applications?
a) Eclipse
b) NetBeans
c) IntelliJ IDEA
d) Visual Studio Code
Answer: a) Eclipse
3. Which Hibernate tool is used to generate Java classes from the database tables and mappings?
a) Hibernate Console
b) Hibernate Code Generation Wizard
c) Hibernate Query Language (HQL)
d) Hibernate Session Manager
Answer: b) Hibernate Code Generation Wizard
2. In a web application using Hibernate, how does the web framework interact with Hibernate?
a) The web framework directly executes SQL queries on the database.
b) The web framework uses Hibernate SessionFactory to manage database connections.
c) The web framework interacts with Java objects, which are persisted to the database using
Hibernate.
d) The web framework handles user sessions and authentication, while Hibernate manages the
database transactions.
Answer: c) The web framework interacts with Java objects, which are persisted to the database using
Hibernate.
3. Which Hibernate component is responsible for mapping Java objects to database tables?
a) SessionFactory
b) Configuration
c) Entity Manager
d) Mapping Annotations
Answer: d) Mapping Annotations
4. In a web application using Hibernate, what is the purpose of the Entity Manager?
a) To manage the database connections and transactions
b) To handle user authentication and authorization
c) To automatically create database tables based on Java entities
d) To manage the lifecycle of Hibernate entities in the web application
Answer: d) To manage the lifecycle of Hibernate entities in the web application
5. What is the role of the web framework in a web application using Hibernate?
a) To provide a graphical user interface for Hibernate configuration
b) To automatically generate Java entities from the database schema
c) To manage user sessions and authentication in the web application
d) To handle the presentation layer of the application and interact with Hibernate for data access
Answer: d) To handle the presentation layer of the application and interact with Hibernate for data
access
7. Which statement is true about creating a web application using Hibernate API?
a) Hibernate can only be used in standalone Java applications and is not suitable for web applications.
b) Hibernate eliminates the need for a web framework and can directly handle web requests and
responses.
c) In a web application, the web framework handles the presentation layer, and Hibernate handles
data access and persistence.
d) Hibernate requires a separate installation of a database server and cannot work with existing
databases.
Answer: c) In a web application, the web framework handles the presentation layer, and Hibernate
handles data access and persistence.
2. In the Hibernate entity lifecycle, what does the "Transient" state represent?
a) The entity has been created and saved in the database.
b) The entity is no longer associated with any Hibernate Session.
c) The entity is actively being used and modified in the application.
d) The entity is marked for deletion from the database.
Answer: b) The entity is no longer associated with any Hibernate Session.
3. What happens to a Hibernate entity when it transitions from the "Transient" state to the "Persistent"
state?
a) The entity is marked for deletion from the database.
b) The entity is removed from the database.
c) The entity is associated with a Hibernate Session and becomes managed.
d) The entity is detached from the database and becomes transient.
Answer: c) The entity is associated with a Hibernate Session and becomes managed.
4. In the Hibernate entity lifecycle, what does the "Persistent" state represent?
a) The entity has been created but not yet saved in the database.
b) The entity is actively being used and modified in the application.
c) The entity is associated with a Hibernate Session and managed.
d) The entity is no longer associated with any Hibernate Session.
Answer: c) The entity is associated with a Hibernate Session and managed.
5. When does a Hibernate entity transition from the "Persistent" state to the "Detached" state?
a) When the Hibernate Session is closed or the entity is explicitly evicted from the Session.
b) When the entity is marked for deletion from the database.
c) When the entity is actively being used and modified in the application.
d) When the entity is removed from the database.
Answer: a) When the Hibernate Session is closed or the entity is explicitly evicted from the Session.
7. Which statement is true about the "Detached" state in the Hibernate entity lifecycle?
a) The entity is actively being used and modified in the application.
b) The entity is marked for deletion from the database.
c) The entity is no longer associated with any Hibernate Session but can still be modified and
reattached to a new Session.
d) The entity is removed from the database and cannot be reattached to any Hibernate Session.
Answer: c) The entity is no longer associated with any Hibernate Session but can still be modified
and reattached to a new Session.
4. Which annotation is used to define the mapping of a property to a database column in Hibernate?
a) @Property
b) @Column
c) @Mapping
d) @DatabaseColumn
Answer: b) @Column
6. In Hibernate with annotation, how is the one-to-many relationship between two entities defined?
a) By using the @OneToMany annotation
b) By explicitly defining foreign key columns in both entities
c) By using the @ManyToOne annotation in one entity and the @OneToMany annotation in the
other
d) By using the @OneToOne annotation with a mapped-by attribute
Answer: c) By using the @ManyToOne annotation in one entity and the @OneToMany annotation in
the other
7. How is the many-to-many relationship between two entities defined in Hibernate with annotation?
a) By using the @ManyToMany annotation
b) By explicitly defining a join table and foreign key columns in both entities
c) By using the @ManyToOne annotation with a mapped-by attribute
d) By using the @OneToMany annotation with a join table attribute
Answer: a) By using the @ManyToMany annotation
By using the @OneToOne annotation in one entity and the @ManyToOne annotation in the other
d) By using the @OneToOne annotation with a join column attribute
Answer: d) By using the @OneToOne annotation with a join column attribute
6. In Hibernate component mapping, how are the component properties represented in the database
table?
a) Each component property is stored in a separate database table.
b) Each component property is stored as a separate column in the owning entity's table.
c) All component properties are stored in the same database table as the owning entity.
d) Component properties are not stored in the database; they are only used in the application.
Answer: b) Each component property is stored as a separate column in the owning entity's table.
8. What is the primary advantage of using the Criteria API over HQL?
a) The Criteria API is more efficient and faster in executing queries.
b) The Criteria API allows for direct SQL query execution in Hibernate.
c) The Criteria API provides better security against SQL injection attacks.
d) The Criteria API allows for dynamic and type-safe queries at runtime.
Answer: d) The Criteria API allows for dynamic and type-safe queries at runtime.
6. Lecture: What is Spring Framework (5 MCQs on Each Subtopic)
Total No. of MCQs : 90
3. What is the core feature of the Spring framework that promotes loose coupling and easy unit testing?
a) Aspect-oriented programming (AOP)
b) Dependency injection (DI)
c) JavaServer Pages (JSP)
d) Servlets
Answer: b) Dependency injection (DI)
4. Which design principle does the Spring framework heavily rely on for its architecture?
a) Model-View-Controller (MVC)
b) Don't Repeat Yourself (DRY)
c) Inversion of Control (IoC)
d) Object-Oriented Programming (OOP)
Answer: c) Inversion of Control (IoC)
5. How does the Spring framework achieve loose coupling between components?
a) By using tightly integrated class hierarchies
b) By defining complex XML configuration files
c) By using annotations to define component relationships
d) By dynamically injecting dependencies at runtime
Answer: d) By dynamically injecting dependencies at runtime
4. How does the Controller communicate with the Model and View in the Spring MVC architecture?
a) The Controller directly updates the Model and View objects.
b) The Controller uses JavaServer Pages (JSP) to render the View.
c) The Controller communicates with the Model through HTTP requests and updates the View
accordingly.
d) The Controller communicates with the Model and View using method calls and annotations.
Answer: c) The Controller communicates with the Model through HTTP requests and updates the
View accordingly.
5. In the Spring MVC architecture, what is the primary responsibility of the View?
a) To handle user input and update the Model.
b) To represent the application's data to the user.
c) To contain the business logic and data of the application.
d) To render the data and provide the user interface elements.
Answer: d) To render the data and provide the user interface elements.
3. Which Spring module is used for building web applications and RESTful services?
a) Spring Core
b) Spring Web
c) Spring Data
d) Spring Boot
Answer: b) Spring Web
4. What does the Spring Data module provide in the Spring framework?
a) Utilities for handling web requests and responses
b) Utilities for handling data access and database operations
c) Utilities for managing beans and their dependencies
d) Utilities for creating RESTful services and web applications
Answer: b) Utilities for handling data access and database operations
1. What does Inversion of Control (IoC) refer to in the context of the Spring framework?
a) It is a design pattern used to create loosely coupled components in Spring applications.
b) It is the process of injecting dependencies into a class during runtime.
c) It is the concept of letting the container manage the creation and lifecycles of objects.
d) It is the process of manually handling the flow of control in a Spring application.
Answer: c) It is the concept of letting the container manage the creation and lifecycles of objects.
2. Which of the following statements best describes the benefits of IoC in Spring?
a) IoC reduces the complexity of the application by eliminating the need for configuration files.
b) IoC promotes tight coupling between components, leading to improved performance.
c) IoC allows for more flexibility and easier testing by decoupling components.
d) IoC only applies to web-based Spring applications, not standalone applications.
Answer: c) IoC allows for more flexibility and easier testing by decoupling components.
3. In IoC, what is the role of the Spring container?
a) To manage database connections and transactions in the application.
b) To handle user authentication and authorization in the application.
c) To automatically resolve and inject dependencies into the objects.
d) To generate HTML and CSS templates for web pages.
Answer: c) To automatically resolve and inject dependencies into the objects.
F. IOC Container
2. Which of the following statements is true about the Spring IoC container?
a) The IoC container is only available in the paid version of the Spring framework.
b) The IoC container creates objects, wires them together, configures them, and manages their
complete lifecycle.
c) The IoC container can only be used with Spring Boot applications, not standalone Spring
applications.
d) The IoC container is used for managing database connections and transactions in the application.
Answer: b) The IoC container creates objects, wires them together, configures them, and manages
their complete lifecycle.
4. What is the difference between the Bean Factory Container and the Application Context Container?
a) The Bean Factory Container is used for standalone applications, while the Application Context
Container is used for web applications.
b) The Bean Factory Container supports only singleton beans, while the Application Context
Container supports prototype beans.
c) The Application Context Container is a more advanced container that includes all the
functionalities of the Bean Factory Container.
d) The Bean Factory Container requires explicit XML configuration, while the Application Context
Container supports annotations for configuration.
Answer: c) The Application Context Container is a more advanced container that includes all the
functionalities of the Bean Factory Container.
G. Dependency Injection
1. What is Dependency Injection (DI) in the context of the Spring framework?
a) It is a technique used to manage database connections and transactions.
b) It is the process of manually wiring the dependencies of objects in a Spring application.
c) It is the process of injecting dependencies into a class during runtime by the container.
d) It is a design pattern used to create loosely coupled components in Spring applications.
Answer: c) It is the process of injecting dependencies into a class during runtime by the container.
H. Spring Beans
1. In the context of Spring, what is a "Bean"?
a) A Java class that represents a data entity in the application.
b) A reusable software component managed by the Spring IoC container.
c) A web page template used for rendering views in a Spring web application.
d) A special type of collection used to store and manage data in Spring applications.
Answer: b) A reusable software component managed by the Spring IoC container.
2. How does Spring identify and manage beans in the IoC container?
a) Beans are identified based on their class names and are automatically registered in the container.
b) Beans are explicitly registered in the container using XML configuration or annotations.
c) Spring automatically scans the classpath to discover and register beans with specific annotations.
d) Beans are identified based on their unique names provided during configuration.
Answer: c) Spring automatically scans the classpath to discover and register beans with specific
annotations.
I. Autowiring Beans
1. What is Autowiring in the context of Spring?
a) The process of automatically creating new beans in the application context.
b) The process of automatically injecting dependencies into a Spring bean.
c) The process of managing database connections and transactions in a Spring application.
d) The process of mapping Java classes to database tables using annotations.
Answer: b) The process of automatically injecting dependencies into a Spring bean.
3. In Autowiring, how does Spring know which dependency to inject when there are multiple
candidates?
a) Spring injects all available dependencies into the bean property.
b) Spring uses a round-robin algorithm to rotate between different dependencies.
c) Spring throws an exception and asks for explicit configuration using @Qualifier.
d) Spring automatically resolves the dependency based on its type.
Answer: d) Spring automatically resolves the dependency based on its type.
4. Which Autowiring mode in Spring requires the existence of at least one bean of the dependency type?
a) no
b) byName
c) byType
d) constructor
Answer: a) no
J. Bean Scopes
1. In Spring, what does the "scope" of a bean refer to?
a) The number of instances created for a specific bean.
b) The visibility of a bean in the application context.
c) The number of dependencies injected into a bean.
d) The number of methods defined in a bean.
Answer: a) The number of instances created for a specific bean.
4. In the Singleton scope, how many instances of the bean are created within the container?
a) One instance, shared by all requests and threads
b) One instance per HTTP request in a web application
c) A new instance for every thread that accesses the bean
d) A new instance for every new HTTP session in a web application
Answer
5. What is the key difference between the Singleton and Prototype scopes in Spring?
a) The Singleton scope creates a new instance of the bean for each request, while the Prototype scope
reuses the same instance across requests.
b) The Singleton scope creates a new instance of the bean for each new HTTP session, while the
Prototype scope reuses the same instance across sessions.
c) The Singleton scope creates a single instance of the bean per container, while the Prototype scope
creates a new instance for each request or lookup.
d) The Singleton scope is used only for web applications, while the Prototype scope is used for
standalone applications.
Answer: c) The Singleton scope creates a single instance of the bean per container, while the
Prototype scope creates a new instance for each request or lookup.
K. Spring MVC
1. What does MVC stand for in the context of the Spring framework?
a) Model-View-Configuration
b) Model-View-Controller
c) Multi-Version-Container
d) Mapping-View-Cache
Answer: b) Model-View-Controller
2. Which pattern does Spring MVC implement for building web applications?
a) Singleton Pattern
b) Factory Pattern
c) Front Controller Pattern
d) Observer Pattern
Answer: c) Front Controller Pattern
4. Which component is responsible for handling user requests and delegating them to the appropriate
controllers in Spring MVC?
a) Model
b) View
c) Controller
d) Front Controller
Answer: d) Front Controller
5. In Spring MVC, which component is responsible for rendering the response back to the user?
a) Model
b) View
c) Controller
d) Front Controller
Answer: b) View
2. What is the purpose of the "Model & View" object in Spring MVC?
a) It contains the request data and is used to pass data between the controller and the view.
b) It handles the mapping of URLs to specific controllers in the application.
c) It is responsible for rendering the response back to the user.
d) It represents the user interface elements and layout of a web page.
Answer: a) It contains the request data and is used to pass data between the controller and the view.
2. In the Front Controller Pattern, how are requests dispatched to the appropriate handlers?
a) The Front Controller handles all requests without dispatching them further.
b) Each request is directly mapped to a specific handler without going through the Front Controller.
c) The Front Controller uses the HandlerMapping to determine the appropriate handler for each
request.
d) The Front Controller relies on the ViewResolver to determine the appropriate handler for each
request.
Answer: c) The Front Controller uses the HandlerMapping to determine the appropriate handler for
each request.
3. What is the main advantage of using the Front Controller Pattern in Spring MVC?
a) It simplifies the handling of user input and form submissions in the application.
b) It promotes loose coupling between components, making the application more maintainable and
testable.
c) It allows for automatic generation of database schemas from entity classes.
d) It provides a clear separation between the application's business logic and presentation layer.
Answer: d) It provides a clear separation between the application's business logic and presentation
layer.
4. How does the Front Controller Pattern handle security concerns in a Spring MVC application?
a) It relies on the ViewResolver to ensure secure views and templates are used for rendering.
b) It uses the HandlerInterceptor to intercept and perform security checks on incoming requests.
c) It directly delegates security checks to the Controller methods.
d) The Front Controller Pattern does not address security concerns in Spring MVC applications.
Answer: b) It uses the HandlerInterceptor to intercept and perform security checks on incoming
requests.
5. What other design patterns are often used in combination with the Front Controller Pattern in Spring
MVC?
a) Singleton Pattern and Factory Pattern
b) Observer Pattern and Strategy Pattern
c) Model-View-Controller (MVC) Pattern and Dependency Injection Pattern
d) Proxy Pattern and Template Method Pattern
Answer: c) Model-View-Controller (MVC) Pattern and Dependency Injection Pattern
N. Spring MVC Web application with JSP views (without Spring Boot)
1. What is the primary purpose of Spring MVC in a web application?
a) To manage the application's business logic and data access.
b) To handle user authentication and authorization.
c) To handle user input and form submissions in the application.
d) To generate HTML and CSS templates for web pages.
Answer: c) To handle user input and form submissions in the application.
2. Which component in Spring MVC is responsible for rendering the response back to the user?
a) Model
b) View
c) Controller
d) Front Controller
Answer: b) View
4. Which file is typically used for configuring Spring MVC in a web application?
a) web.xml
b) application.properties
c) spring-config.xml
d) applicationContext.xml
Answer: a) web.xml
1. What is Thymeleaf?
a) A server-side programming language used in Spring Boot applications.
b) A JavaScript library for client-side rendering of web pages.
c) A view technology used in Spring applications as an alternative to JSP.
d) A relational database management system used with Spring Data JPA.
Answer: c) A view technology used in Spring applications as an alternative to JSP.
2. What are the advantages of using Thymeleaf over JSP as a view technology in Spring?
a) Thymeleaf allows for better performance and faster rendering of web pages.
b) Thymeleaf provides more advanced features for handling user input and form submissions.
c) Thymeleaf templates are based on XML, making them easier to read and maintain.
d) Thymeleaf templates are more secure and less vulnerable to common web attacks like XSS.
Answer: d) Thymeleaf templates are more secure and less vulnerable to common web attacks like
XSS.
P. Spring Validations
1. What is data validation in the context of Spring applications?
a) The process of encrypting sensitive data to secure it during transmission.
b) The process of validating user input to ensure it meets certain criteria and is free of errors.
c) The process of converting Java objects to JSON format for use in web services.
d) The process of managing database connections and transactions in a Spring application.
Answer: b) The process of validating user input to ensure it meets certain criteria and is free of errors.
4. Which annotation is commonly used for defining validation rules on model attributes in Spring?
a) @ValidationRule
b) @Validated
c) @NotNull
d) @ModelAttribute
Answer: c) @NotNull
3. How can you configure Spring to handle file uploads in a web application?
a) By specifying the maximum allowed file size in the application properties.
b) By using the @EnableFileUpload annotation on the main configuration class.
c) By configuring the DispatcherServlet to support file uploads in web.xml.
d) File uploads are automatically handled by Spring without any specific configuration.
Answer: c) By configuring the DispatcherServlet to support file uploads in web.xml.
4. Which method in the controller is commonly used to handle file uploads in Spring?
a) @GetMapping
b) @PostMapping
c) @PutMapping
d) @DeleteMapping
Answer: b) @PostMapping
5. How can you access the uploaded file in the controller method?
a) By directly accessing the HttpServletRequest object and retrieving the file data.
b) By using the @RequestParam annotation and specifying the file parameter name.
c) By using the @ModelAttribute annotation and mapping the file to a specific model attribute.
d) File uploads are automatically handled by Spring without any explicit access in the controller.
Answer: b) By using the @RequestParam annotation and specifying the file parameter name.
7 Lecture: Spring Boot essentials (8 MCQs on Each Subtopic)
Total No. of MCQs : 56
3. What does "convention over configuration" mean in the context of Spring Boot?
a) Developers must manually configure every aspect of the Spring Boot application.
b) Spring Boot provides sensible defaults and configuration based on common conventions, reducing
the need for explicit configuration.
c) Spring Boot completely eliminates the need for any configuration.
d) Spring Boot only works with specific configurations and does not allow customizations.
Answer: b) Spring Boot provides sensible defaults and configuration based on common conventions,
reducing the need for explicit configuration.
5. What is the primary advantage of using Spring Boot for microservices development?
a) Spring Boot provides a lightweight container specifically designed for microservices.
b) Spring Boot enables easy integration with non-Spring technologies for microservices.
c) Spring Boot automatically manages microservices deployment on cloud platforms.
d) Spring Boot replaces the need for using RESTful APIs in microservices.
Answer: a) Spring Boot provides a lightweight container specifically designed for microservices.
3. How does Spring Boot address the problem of version conflicts among dependencies?
a) Spring Boot automatically updates all dependencies to the latest version to avoid conflicts.
b) Spring Boot uses a centralized dependency management system to manage versions and avoid
conflicts.
c) Spring Boot does not handle version conflicts; it is the developer's responsibility to manage them.
d) Spring Boot prevents the use of external dependencies to avoid version conflicts.
Answer: b) Spring Boot uses a centralized dependency management system to manage versions and
avoid conflicts.
5. Which annotation is commonly used in Spring Boot to mark a class as a Spring bean?
a) @Service
b) @Controller
c) @Component
d) @Entity
Answer: c) @Component
6. Spring Boot uses __________ to minimize the need for explicit XML configuration.
a) YAML files
b) JSON files
c) Properties files
d) Annotations
Answer: d) Annotations
.
Answer: d) Apache Maven is used for project management and build automation in Spring Boot
applications.
2. What is the primary purpose of a Maven Project Object Model (POM) file?
a) To define the properties and settings for a Maven project.
b) To configure the build process and specify the project's dependencies.
c) To provide metadata about the project, including its name, description, and version.
d) To define the project's packaging type, such as JAR, WAR, or POM.
Answer: b) To configure the build process and specify the project's dependencies.
3. Which command is used to build a Maven project and generate the JAR or WAR file?
a) mvn run
b) mvn install
c) mvn build
d) mvn package
Answer: d) mvn package
7. In Maven, what does the "compile" phase do during the build process?
a) It compiles the Java source code of the project.
b) It packages the compiled classes into a JAR or WAR file.
c) It runs the automated tests for the project.
d) It installs the project artifacts to the local repository.
Answer: a) It compiles the Java source code of the project.
2. Which of the following annotations is used to define a Spring web controller in Spring Boot?
a) @Service
b) @Controller
c) @Repository
d) @Component
Answer: b) @Controller
3. How can you enable Spring Boot's auto-configuration for a Spring web application?
a) By adding the @EnableAutoConfiguration annotation to the main application class.
b) Spring Boot's auto-configuration is enabled by default and does not require any additional
annotations.
c) By manually specifying the configuration settings in the application.properties file.
d) Spring Boot does not support auto-configuration for web applications.
Answer: b) Spring Boot's auto-configuration is enabled by default and does not require any additional
annotations.
5. In Spring Boot, how can you specify the base package for component scanning?
a) By adding the @ComponentScan annotation to the main application class and specifying the base
package.
b) By defining the base package in the application.properties file.
c) By specifying the base package in the <scan> element of the POM file.
d) Component scanning is not supported in Spring Boot.
Answer: a) By adding the @ComponentScan annotation to the main application class and specifying
the base package.
6. How does Spring Boot handle the configuration of data sources for a web application?
a) Spring Boot automatically detects the database type and configures the data source accordingly.
b) Spring Boot requires developers to manually configure the data source using XML-based
configuration.
c) Spring Boot only supports a specific type of database, and other databases are not supported.
d) Spring Boot does not support database configurations for web applications.
Answer: a) Spring Boot automatically detects the database type and configures the data source
accordingly.
7. What is the purpose of the application.properties (or application.yml) file in Spring Boot?
a) To define the Spring Boot's auto-configuration settings for the application.
b) To specify the database connection settings and credentials for the application.
c) To provide externalized configuration for the application, such as port numbers and logging
settings.
d) The application.properties (or application.yml) file is not used in Spring Boot.
Answer: c) To provide externalized configuration for the application, such as port numbers and
logging settings.
8. How can you run a Spring Boot web application in an embedded web server?
a) By deploying the application to an external web server like Tomcat or Jetty.
b) By running the application as a standalone executable JAR file with the embedded web server.
c) By manually configuring the web server settings in the application.properties
file.
d) Spring Boot does not support running web applications in an embedded web server.
Answer: b) By running the application as a standalone executable JAR file with the embedded web
server.
E. Spring Boot in detail (Use Spring Boot for all demo & assignments here
onwards)
1. Which of the following statements is true about Spring Boot's embedded web server?
a) Spring Boot does not support an embedded web server; it requires an external web server for
deployment.
b) Spring Boot's embedded web server is a lightweight server that can be run as part of the
application.
c) Spring Boot's embedded web server is only suitable for development and not recommended for
production use.
d) Spring Boot's embedded web server is only compatible with Apache Tomcat.
Answer: b) Spring Boot's embedded web server is a lightweight server that can be run as part of the
application.
3. In Spring Boot, how can you define the properties of a data source for a database connection?
a) By specifying the data source properties directly in the application.properties (or application.yml)
file.
b) By using XML-based configuration files to define the data source properties.
c) By defining the data source properties in the <properties> section of the POM file.
d) Spring Boot automatically detects the data source properties based on the application's
dependencies.
Answer: a) By specifying the data source properties directly in the application.properties (or
application.yml) file.
7. How does Spring Boot handle the configuration of a data source for a Spring Data JPA application?
a) Spring Boot automatically detects the database type and configures the data source accordingly.
b) Spring Boot requires developers to manually configure the data source using XML-based
configuration.
c) Spring Boot uses the application.properties (or application.yml) file to define the data source
properties.
d) Spring Boot does not support data source configurations for Spring Data JPA applications.
Answer: a) Spring Boot automatically detects the database type and configures the data source
accordingly.
F. Running a web application using Spring Boot with CRUD (with Static Data
not DB)
1. What is CRUD in the context of web applications?
a) A programming language used for building web applications.
b) A design pattern used for database management in web applications.
c) An acronym for Create, Read, Update, and Delete operations on data.
d) A specific web framework used for building web applications in Spring Boot.
Answer: c) An acronym for Create, Read, Update, and Delete operations on data.
2. How can you handle CRUD operations with static data (not using a database) in a Spring Boot
application?
a) By manually writing Java code for each CRUD operation and mapping it to appropriate URL
endpoints.
b) By using the `@RestController` and `@RequestMapping` annotations to define CRUD operations
in a controller.
c) By defining the CRUD operations in the application.properties (or application.yml) file.
d) CRUD operations with static data are not supported in Spring Boot.
Answer: b) By using the `@RestController` and `@RequestMapping` annotations to define CRUD
operations in a controller.
3. What HTTP method is commonly used for creating new resources in a RESTful API?
a) GET
b) POST
c) PUT
d) DELETE
Answer: b) POST
4. How can you test the POST request for creating a new resource in a Spring Boot application?
a) By using a web browser and navigating to the corresponding URL.
b) By using the `curl` command in the command-line interface.
c) By sending a POST request using tools like Postman or Insomnia.
d) POST requests cannot be tested in a Spring Boot application without a database.
Answer: c) By sending a POST request using tools like Postman or Insomnia.
5. Which HTTP method is used for retrieving a specific resource in a RESTful API?
a) GET
b) POST
c) PUT
d) DELETE
Answer: a) GET
7. How can you test the GET request for retrieving a specific resource in a Spring Boot application?
a) By using a web browser and navigating to the corresponding URL.
b) By using the `curl` command in the command-line interface.
8. Which HTTP method is used for updating an existing resource in a RESTful API?
a) GET
b) POST
c) PUT
d) DELETE
Answer: c) PUT
2. What is the primary advantage of using Spring Data JDBC over plain JDBC?
a) Spring Data JDBC provides a more efficient and faster data access mechanism.
b) Spring Data JDBC eliminates the need for writing SQL queries.
c) Spring Data JDBC automatically handles database connections and transactions.
d) Spring Data JDBC provides a simplified API for performing CRUD operations.
Answer: d) Spring Data JDBC provides a simplified API for performing CRUD operations.
3. How does Spring Data JDBC map Java objects to database tables?
a) Spring Data JDBC uses reflection to automatically map Java objects to database tables based on
naming conventions.
b) Developers must manually define the mapping between Java objects and database tables using
annotations.
c) Spring Data JDBC requires the use of an XML-based configuration for object-table mapping.
d) Spring Data JDBC does not support mapping Java objects to database tables.
Answer: a) Spring Data JDBC uses reflection to automatically map Java objects to database tables
based on naming conventions.
5. How can you define custom queries using Spring Data JDBC?
a) By using the `@Query` annotation and providing the SQL query as a string parameter.
b) By specifying the custom queries in the application.properties (or application.yml) file.
c) By extending the `JdbcRepository` interface and implementing custom methods.
d) Spring Data JDBC does not support custom queries.
Answer: a) By using the `@Query` annotation and providing the SQL query as a string parameter.
2. Spring Data JPA allows developers to create repository interfaces. What are these interfaces used for?
a) To define the entity classes for JPA entities.
b) To define custom queries using SQL for database access.
c) To enable the automatic generation of JPA entity classes.
d) To define CRUD (Create, Read, Update, Delete) operations for JPA entities.
Answer: d) To define CRUD (Create, Read, Update, Delete) operations for JPA entities.
3. How does Spring Data JPA implement the methods defined in the repository interfaces?
a) Spring Data JPA relies on the Hibernate ORM framework to implement the methods.
b) Developers need to manually implement the methods in the repository interfaces.
c) Spring Data JPA generates the method implementations at runtime using reflection.
d) Spring Data JPA translates the method names into SQL queries for database access.
Answer: c) Spring Data JPA generates the method implementations at runtime using reflection.
4. Which annotation is commonly used in Spring Data JPA to mark a repository interface?
a) @PersistenceContext
b) @EntityManager
c) @Repository
d) @JpaRepository
Answer: c) @Repository
6. What is the default naming strategy used by Spring Data JPA to generate SQL queries for CRUD
methods?
a) CamelCase strategy
b) SnakeCase strategy
c) UpperCamelCase strategy
d) LowerCamelCase strategy
Answer: a) CamelCase strategy
7. How can you enable Spring Data JPA in a Spring Boot application?
a) By adding the `@EnableJpaRepositories` annotation to the main application class.
b) Spring Data JPA is enabled by default in all Spring Boot applications and does not require
additional configuration.
c) By specifying the data source properties in the application.properties (or application.yml) file.
d) Spring Data JPA cannot be used in Spring Boot applications.
Answer: a) By adding the `@EnableJpaRepositories` annotation to the main application class.
9. How does Spring Data JPA handle the creation of database tables from entity classes?
a) Spring Data JPA automatically creates the database tables based on the entity classes and their
annotations.
b) Developers must manually write SQL scripts to create the database tables.
c) Spring Data JPA does not support the automatic creation of database tables.
d) Spring Data JPA requires the use of a separate tool for creating database tables.
Answer: a) Spring Data JPA automatically creates the database tables based on the entity classes and
their annotations.
10. Which of the following is true about Spring Data JPA's support for pagination?
a) Spring Data JPA does not support pagination for large result sets.
b) Spring Data JPA provides built-in support for pagination using the `Pageable` interface.
c) Pagination can only be achieved through custom SQL queries in Spring Data JPA.
d) Pagination is only supported for specific database types in Spring Data JPA.
Answer: b) Spring Data JPA provides built-in support for pagination using the `Pageable` interface.
1. What is the difference between `CrudRepository` and `JpaRepository` in Spring Data JPA?
a) `CrudRepository` is a generic interface for basic CRUD operations, while `JpaRepository` extends
`CrudRepository` and provides additional JPA-specific methods.
b) `JpaRepository` is a generic interface for basic CRUD operations, while `CrudRepository` extends
`JpaRepository` and provides additional JPA-specific methods.
c) Both `CrudRepository` and `JpaRepository` are the same and can be used interchangeably.
d) `CrudRepository` and `JpaRepository` are used for different types of databases, depending on the
data source.
Answer: a) `CrudRepository` is a generic interface for basic CRUD operations, while
`JpaRepository` extends `CrudRepository` and provides additional JPA-specific methods.
2. Which of the following operations are supported by `CrudRepository` in Spring Data JPA?
a) Create and Read operations only.
b) Read and Update operations only.
c) Create, Read, Update, and Delete operations.
d) Create, Read, and Update operations only.
Answer: c) Create, Read, Update, and Delete operations.
4. How can you retrieve all records from a database table using `CrudRepository`?
a) By calling the `findAll()` method.
b) By calling the `getAll()` method.
c) By calling the `retrieveAll()` method.
d) Retrieving all records is not supported in `CrudRepository`.
Answer: a) By calling the `findAll()` method.
5. Which method is used to delete an entity from the database using `CrudRepository`?
a) `remove()`
b) `delete()`
c) `erase()`
d) `drop()`
Answer: b) `delete()`
6. How can you find a record in the database table by its primary key using `CrudRepository`?
a) By calling the `findById()` method.
b) By calling the `getById()` method.
c) By calling the `findByPrimaryKey()` method.
d) Finding a record by its primary key is not supported in `CrudRepository`.
Answer: a) By calling the `findById()` method.
8. Which of the following is NOT true about `JpaRepository` in Spring Data JPA?
a) `JpaRepository` provides additional JPA-specific methods beyond basic CRUD operations.
b) `JpaRepository` extends `CrudRepository`.
c) `JpaRepository` is designed for NoSQL databases.
d) `JpaRepository` provides support for sorting and pagination in queries.
Answer: c) `JpaRepository` is designed for NoSQL databases.
9. In Spring Data JPA, how can you execute custom SQL queries using `JpaRepository`?
a) By using the `@Query` annotation and providing the SQL query as a string parameter.
b) By using the `@Sql` annotation and specifying the SQL query as a value.
c) By calling the `executeQuery()` method and passing the SQL query as a parameter.
d) Custom SQL queries cannot be executed using `JpaRepository`.
Answer: a) By using the `@Query` annotation and providing the SQL query as a string parameter.
C. Query methods
2. How are query methods named in Spring Data JPA to define specific queries?
a) By using the `@NamedQuery` annotation and providing a query name.
b) By prefixing the method name with keywords like `find`, `get`, `query`, etc.
c) By using the `@Query` annotation and providing the query as a string parameter.
d) Query methods do not require specific naming conventions.
Answer: b) By prefixing the method name with keywords like `find`, `get`, `query`, etc.
3. What is the default return type of a query method in Spring Data JPA?
a) List
b) Set
c) Object
d) Custom entity class
Answer: a) List
4. How can you define a query method in Spring Data JPA to retrieve data based on a specific
condition?
a) By using the `@NamedQuery` annotation and providing a query name.
b) By using the `@Query` annotation and providing the query as a string parameter.
c) By defining the method with the appropriate name convention based on the condition.
d) Query methods cannot be defined to retrieve data based on specific conditions.
Answer: c) By defining the method with the appropriate name convention based on the condition.
5. Which keyword is used in the method name to specify the property to be used for filtering in a query
method?
a) `filterBy`
b) `findBy`
c) `with`
d) `having`
Answer: b) `findBy`
7. How can you limit the number of results returned by a query method in Spring Data JPA?
a) By using the `@Limit` annotation and specifying the maximum number of results.
b) By calling the `limit()` method on the query result.
c) By using the `Top` keyword in the method name followed by the desired number of results.
d) Limiting the number of results is not supported in query methods.
Answer: c) By using the `Top` keyword in the method name followed by the desired number of
results.
8. In Spring Data JPA, how can you use the `OrderBy` keyword in a query method?
a
) By calling the `orderBy()` method on the query result and providing the sorting criteria.
b) By using the `@OrderBy` annotation and specifying the sorting criteria.
c) The `OrderBy` keyword is not used in query methods.
d) By using the `Asc` or `Desc` keywords in the method name followed by the property name for
sorting.
Answer: d) By using the `Asc` or `Desc` keywords in the method name followed by the property
name for sorting.
9. Which of the following is NOT a supported keyword in query methods for defining conditions?
a) `Between`
b) `IsNull`
c) `Greater`
d) `In`
Answer: c) `Greater`
10. How can you use the `IgnoreCase` keyword in a query method?
a) By using the `IgnoreCase` keyword in the method name to ignore case sensitivity in the query.
b) By using the `@IgnoreCase` annotation and specifying the property name for case-insensitive
filtering.
c) The `IgnoreCase` keyword is not used in query methods.
d) By calling the `ignoreCase()` method on the query result.
Answer: a) By using the `IgnoreCase` keyword in the method name to ignore case sensitivity in the
query.
D. Using custom query (@Query)
2. How can you use the `@Query` annotation to define a custom SQL query in Spring Data JPA?
a) By using the `@Query` annotation and providing the SQL query as a string parameter.
b) By using the `@Sql` annotation and specifying the SQL query as a value.
c) By calling the `executeQuery()` method and passing the SQL query as a parameter.
d) Custom SQL queries cannot be executed using the `@Query` annotation.
Answer: a) By using the `@Query` annotation and providing the SQL query as a string parameter.
3. What is the advantage of using `@Query` annotation over naming query methods conventionally?
a) `@Query` provides better performance compared to conventional naming.
b) `@Query` allows for more complex and dynamic queries that cannot be achieved using naming
convention.
c) Naming convention is preferred over `@Query` for better maintainability and readability.
d) There is no advantage of using `@Query` over naming query methods conventionally.
Answer: b) `@Query` allows for more complex and dynamic queries that cannot be achieved using
naming convention.
4. How can you pass parameters to a custom query defined using `@Query`?
a) By using the `@Param` annotation and specifying the parameter names in the method parameters.
b) By concatenating the parameter values directly in the SQL query string.
c) By calling the `setParameter()` method on the `EntityManager`.
d) Parameters cannot be passed to a custom query defined using `@Query`.
Answer: a) By using the `@Param` annotation and specifying the parameter names in the method
parameters.
5. Which of the following is true about named parameters in a custom query defined using `@Query`?
a) Named parameters are optional and can be omitted in the query.
b) Named parameters must be used in the order they are declared in the method signature.
c) Named parameters can be replaced with indexed parameters (e.g., `?1`, `?2`) for better
performance.
d) Named parameters are not supported in a custom query defined using `@Query`.
Answer: a) Named parameters are optional and can be omitted in the query.
6. In Spring Data JPA, how can you define a native SQL query using the `@Query` annotation?
a) By adding the `nativeQuery=true` attribute to the `@Query` annotation.
b) By using the `@NativeQuery` annotation instead of `@Query`.
c) Spring Data JPA does not support native SQL queries with `@Query`.
d) By using the `@SqlQuery` annotation and providing the SQL query as a string parameter.
Answer: a) By adding the `nativeQuery=true` attribute to the `@Query` annotation.
7. How can you use the `@Modifying` annotation in a custom query method?
a) By marking the query method as read-only to improve performance.
b) By specifying the type of the result returned by the query method.
c) The `@Modifying` annotation is not used in custom query methods.
d) By indicating that the query method modifies the database and needs to be executed within a
transaction.
Answer: d) By indicating that the query method modifies the database and needs to be executed
within a transaction.
8. When using the `@Modifying` annotation in a custom query, what is the default behavior if no
transaction is active?
a) The query method will throw a `TransactionRequiredException`.
b) The query method will execute without any transactional support.
c) The query method will automatically start a new transaction if none exists.
d) The behavior of the `@Modifying` annotation is not affected by the transactional status.
Answer: a) The query method will throw a `TransactionRequiredException`.
10. Which of the following is true about using the `@Query` annotation for custom queries?
a) The `@Query` annotation can only be used for simple queries and does not support complex
operations.
b) The `@Query` annotation is recommended for all custom queries as it provides better
performance than naming convention.
c) The use of `@Query` is limited to specific database types and is not recommended for general use.
d) The choice between `@Query` and naming convention depends on the complexity and dynamic
nature of the query.
Answer: d) The choice between `@Query` and naming convention depends on the complexity and
dynamic nature of the query.
9. Lecture: Spring AOP (12 MCQs on Each Subtopic)
Total No. of MCQs : 36
A. AOP Overview
1. What does AOP stand for in the context of Spring?
a) Aspect-Oriented Programming
b) Application Object Protocol
c) Advanced Object Processing
d) All Object Programming
Answer: a) Aspect-Oriented Programming
2. Which of the following best describes the main purpose of AOP in Spring?
a) To provide a way to create new Java classes.
b) To separate the application logic from the presentation layer.
c) To encapsulate cross-cutting concerns and improve modularity.
d) To define the database schema for the application.
Answer: c) To encapsulate cross-cutting concerns and improve modularity.
7. Which component in Spring is responsible for managing AOP proxies and applying advice to the
target objects?
a) AOP Engine
b) Proxy Factory
c) Aspect Manager
d) AspectJ Weaver
Answer: b) Proxy Factory
10. How can you enable AOP in a Spring application configuration file?
a) By adding the `<aop:enable>` element and specifying the aspect classes.
b) AOP is enabled by default in all Spring applications and does not require additional configuration.
c) By specifying the `<aspectj>` element in the application context.
d) AOP is enabled automatically when using Spring Boot.
Answer: a) By adding the `<aop:enable>` element and specifying the aspect classes.
11. Which of the following is true about the AOP terminology "Join Point"?
a) Join Point represents the point in the application where an aspect is applied.
b) Join Point is used to define the conditions for advice execution.
c) Join Point is a specific type of advice in Spring AOP.
d) Join Point is not related to AOP terminology.
Answer: a) Join Point represents the point in the application where an aspect is applied.
B. Spring AOP
6. How can you define a Pointcut expression using annotations in Spring AOP?
a) By using the @Advice annotation and specifying the pointcut expression.
b) By using the @JoinPoint annotation and specifying the pointcut expression.
c) By using the @Pointcut annotation and specifying the pointcut expression.
d) Pointcut expressions cannot be defined using annotations.
Answer: c) By using the @Pointcut annotation and specifying the pointcut expression.
8. How can you define an advice to execute after the target method returns a value?
a) By using the @Before annotation with the "returning" attribute.
b)
12. How can you define an Aspect using XML configuration in Spring AOP?
a) By using the <aspect> element in the application context.
b) By using the <advice> element and specifying the aspect class.
c) By using the <aop:aspect> element and specifying the aspect class.
d) Aspect cannot be defined using XML configuration in Spring AOP.
Answer: c) By using the <aop:aspect> element and specifying the aspect class.
10. Lecture: Building REST services with Spring (8 MCQs on Each Subtopic)
Total No. of MCQs : 64
5. What is the key benefit of using web services in a distributed application environment?
a) Simplified user interface design
b) Improved performance of local applications
c) Seamless integration of diverse systems and technologies
d) Elimination of the need for network connections
Answer: c) Seamless integration of diverse systems and technologies
2. Which of the following statements is true about the message format in SOAP web services?
a) SOAP messages are typically formatted using JSON.
b) SOAP messages use a standardized XML-based format.
c) SOAP messages are plain text with no specific format.
d) SOAP messages use binary data for faster communication.
Answer: b) SOAP messages use a standardized XML-based format.
8. Which of the following is an advantage of using RESTful web services over SOAP?
a) RESTful web services provide better security features.
b) RESTful web services are more efficient due to the smaller message size.
c) RESTful web services support only XML for data exchange.
d) RESTful web services are based on a custom communication protocol.
Answer: b) RESTful web services are more efficient due to the smaller message size.
5. Which HTTP methods are commonly used in RESTful web services for CRUD (Create, Read,
Update, Delete) operations?
a) GET, POST, PUT, DELETE
b) SELECT, INSERT, UPDATE, DELETE
c) FETCH, ADD, MODIFY, REMOVE
d) REQUEST, RESPOND, UPDATE, REMOVE
Answer: a) GET, POST, PUT, DELETE
6. What is the significance of the HTTP GET method in RESTful web services?
a) It is used for creating new resources on the server.
b) It is used for updating existing resources on the server.
c) It is used for retrieving data from the server.
d) It is used for deleting resources from the server.
Answer: c) It is used for retrieving data from the server.
8. What does the status code "200 OK" indicate in the HTTP response of a RESTful web service?
a) The request was successful, and the resource has been created.
b) The request was successful, and the resource has been updated.
c) The request was successful, and the resource has been deleted.
d) The request was successful, and the server has returned the requested data.
Answer: d) The request was successful, and the server has returned the requested data.
2. How can you create a new RESTful web service in Java using Spring Boot?
a) By manually writing HTTP request handlers and configuring the server.
b) By using Spring Boot's built-in annotations and configurations.
c) By including the Spring Boot JAR file in the project.
d) RESTful web services cannot be created in Java using Spring Boot.
Answer: b) By using Spring Boot's built-in annotations and configurations.
3. Which annotation is used to define a class as a RESTful web service controller in Spring Boot?
a) @RestController
b) @WebService
c) @Controller
d) @Service
Answer: a) @RestController
5. What is the role of the Spring Boot application class in a RESTful web service project?
a) It contains the main method to start the web service.
b) It defines the database schema for the web service.
c) It handles all HTTP requests and responses.
d) The Spring Boot application class is not required for a RESTful web service project.
Answer: a) It contains the main method to start the web service.
6. How can you enable CORS (Cross-Origin Resource Sharing) in a Spring Boot RESTful web service?
a) By setting the @EnableCORS annotation on the controller class.
b) By configuring the web.xml file with CORS settings.
c) By adding the appropriate CORS headers to the HTTP response.
d) CORS is automatically enabled by default in Spring Boot.
Answer: c) By adding the appropriate CORS headers to the HTTP response.
7. Which HTTP method is typically used for creating a new resource in a RESTful web service?
a) GET
b) POST
c) PUT
d) DELETE
Answer: b) POST
8. How can you pass data to a RESTful web service endpoint in Spring Boot?
a) By using the HTTP request headers.
b) By including the data in the URL path.
c) By sending the data in the HTTP request body.
d) Data cannot be passed to a RESTful web service in Spring Boot.
Answer: c) By sending the data in the HTTP request body.
4. How can you convert a Java object to JSON format in a Spring Boot RESTful web service?
a) By using the @JsonFormat annotation on the Java class.
b) By manually converting the object to JSON using string concatenation.
c) By including the Jackson library in the project and using its ObjectMapper class.
d) Java objects cannot be converted to JSON format in a Spring Boot web service.
Answer: c) By including the Jackson library in the project and using its ObjectMapper class.
5. What is the HTTP status code returned by the server if a JSON request is successful?
a) 200 OK
b) 201 Created
c) 204 No Content
d) 404 Not Found
Answer: a) 200 OK
7. How can a client specify which data format (JSON or XML) it prefers when making a request to a
RESTful web service?
a) By including the desired data format in the URL.
b) By sending a header in the HTTP request.
c) By passing a query parameter in the URL.
d) The client cannot specify the data format in a RESTful request.
Answer: b) By sending a header in the HTTP request.
8. What is the purpose of the @ResponseBody annotation in a Spring Boot RESTful web service?
a) It indicates that the method will handle HTTP POST requests.
b) It specifies the response data format (JSON or XML).
c) It indicates that the method will handle HTTP GET requests.
d) It indicates that the method's return value should be serialized and sent as the response body.
Answer: d) It indicates that the method's return value should be serialized and sent as the response
body.
2. Which HTTP method is typically used to retrieve data from a RESTful web service?
a) GET
b) POST
c) PUT
d) DELETE
Answer: a) GET
3. How can you create a new resource in a Spring Boot RESTful web service?
a) By sending an HTTP GET request.
b) By sending an HTTP POST request.
c) By sending an HTTP PUT request.
d) By sending an HTTP DELETE request.
Answer: b) By sending an HTTP POST request.
4. In the context of RESTful web services, what does the term "resource" refer to?
a) The URL of the web service.
b) The data representation used in the web service.
c) The client application accessing the web service.
d) The entity or object that the web service manages.
Answer: d) The entity or object that the web service manages.
5. Which HTTP method is used to update an existing resource in a RESTful web service?
a) GET
b) POST
c) PUT
d) DELETE
Answer: c) PUT
6. How can you delete a resource in a Spring Boot RESTful web service?
a) By sending an HTTP GET request with the ID of the resource to be deleted.
b) By sending an HTTP POST request with the ID of the resource to be deleted.
c) By sending an HTTP DELETE request with the ID of the resource to be deleted.
d) By sending an HTTP PUT request with the ID of the resource to be deleted.
Answer: c) By sending an HTTP DELETE request with the ID of the resource to be deleted.
7. In the context of CRUD operations, what is the purpose of the HTTP status code "201 Created"?
a) It indicates that the resource has been successfully created.
b) It indicates that the resource has been successfully updated.
c) It indicates that the resource has been successfully deleted.
d) It indicates that the resource has been successfully retrieved.
Answer: a) It indicates that the resource has been successfully created.
8. What is the purpose of the @PathVariable annotation in a Spring Boot RESTful web service?
a) It specifies the data format used for communication (e.g., JSON or XML).
b) It extracts a value from the URL and binds it to a method parameter.
c) It defines the request URL pattern that the controller handles.
d) It indicates that the method should handle HTTP GET requests.
Answer: b) It extracts a value from the URL and binds it to a method parameter.
2. How can you send HTTP requests and receive responses using POSTMAN?
a) By writing code in a programming language to handle HTTP requests.
b) By using a web browser to manually enter HTTP request URLs.
c) By using POSTMAN's user interface to configure and send requests.
d) HTTP requests cannot be sent using POSTMAN.
Answer: c) By using POSTMAN's user interface to configure and send requests.
3. Which HTTP methods can you use in POSTMAN to interact with RESTful web services?
a) GET and POST only
b) GET, POST, and DELETE only
c) GET, POST, PUT, and DELETE
d) POSTMAN does not support HTTP methods for RESTful web services.
Answer: c) GET, POST, PUT, and DELETE
6. How can you view the HTTP response status code in POSTMAN?
a) In the request body.
b) In the response viewer.
c) In the URL.
d) POSTMAN does not display the status code.
Answer: b) In the response viewer.
8. How can you organize and group related HTTP requests in POSTMAN?
a) By creating folders in the request collection.
b) By using nested request URLs.
c) By adding comments within the request.
d) POSTMAN does not support organizing requests.
Answer: a) By creating folders in the request collection.
H. REST service invocation using REST Template
1. What is REST Template in the context of Spring Boot?
a) A Java class used for defining RESTful web services.
b) A utility class provided by Spring for making RESTful API calls.
c) A template engine used for generating HTML pages in Spring Boot.
d) A configuration file used for mapping RESTful URLs to controller methods.
Answer: b) A utility class provided by Spring for making RESTful API calls.
2. How does REST Template simplify the process of making RESTful API calls?
a) It automatically generates the JSON or XML request data.
b) It handles the serialization and deserialization of data between Java objects and JSON or XML.
c) It eliminates the need to use HTTP methods for making API calls.
d) REST Template does not simplify the process of making API calls.
Answer: b) It handles the serialization and deserialization of data between Java objects and JSON or
XML.
3. Which HTTP methods can REST Template use for making API calls?
a) GET, POST, and DELETE only
b) GET, POST, PUT, and DELETE
c) GET, POST, PUT, DELETE, and PATCH
d) REST Template does not support making API calls.
Answer: c) GET, POST, PUT, DELETE, and PATCH
6. How can you deserialize the JSON response from a RESTful API call using REST Template?
a) By manually parsing the JSON data in the response body.
b) By using annotations on the controller methods.
c) By using the Response Entity class to automatically deserialize the data.
d) REST Template does not support deserializing JSON data.
Answer: c) By using the Response Entity class to automatically deserialize the data.
7. In REST Template, how can you pass query parameters in a GET request?
a) By including the parameters in the request body.
b) By appending the parameters to the URL.
c) By using HTTP headers to pass the parameters.
d) REST Template does not support passing query parameters.
Answer: b) By appending the parameters to the URL.
8. What is the purpose of the @PathVariable annotation in a Spring Boot RESTful web service?
a) It specifies the data format used for communication (e.g., JSON or XML).
b) It extracts a value from the URL and binds it to a method parameter.
c) It defines the request URL pattern that the controller handles.
d) It indicates that the method should handle HTTP GET requests.
Answer: b) It extracts a value from the URL and binds it to a method parameter.
11. Lecture: Testing in Spring (10 MCQs on Each Subtopic )
Total No. Of MCQs : 50
A. Testing in Spring:
1. What is the primary purpose of testing in Spring applications?
a) To ensure the security of the application.
b) To measure the performance of the application.
c) To identify and fix defects in the code.
d) To optimize the database queries.
Answer: c) To identify and fix defects in the code.
2. Which testing framework is commonly used for unit testing in Spring applications?
a) JUnit
b) TestNG
c) Mockito
d) Cucumber
Answer: a) JUnit
7. How can you enable the Spring test context framework in a test class?
a) By importing the required packages at the beginning of the class.
b) By using the @SpringBootTest annotation on the test class.
c) By adding the application context XML file in the test resources folder.
d) Spring test context framework is automatically enabled in all test classes.
Answer: b) By using the @SpringBootTest annotation on the test class.
10. Which testing approach is generally used for testing the functionality of a complete application in
Spring?
a) Unit testing
b) Integration testing
c) End-to-End testing
d) Manual testing
Answer: c) End-to-End testing
4. How can you mock the dependencies of a Spring MVC controller during unit testing?
a) By using the @Autowired annotation on the dependencies.
b) By creating instances of the dependencies manually in the test class.
c) By using the @MockBean annotation on the dependencies.
d) Mocking dependencies is not possible in Spring unit testing.
Answer: c) By using the @MockBean annotation on the dependencies.
6. How can you verify the output of a controller method in Spring unit testing?
a) By checking the logs generated during testing.
b) By manually inspecting the response object returned by the controller.
c) By using assertions on the response object returned by MockMvc.
d) Verifying the output is not possible in Spring unit testing.
Answer: c) By using assertions on the response object returned by MockMvc.
10. What is the purpose of the @MockMvcTest annotation in Spring unit testing?
a) It is used to test the integration of the controller with other components.
b) It replaces the real application context with a mock context.
c) It starts up the Spring application context for testing.
d) It enables testing of web-related components, such as controllers.
Answer: a) It is used to test the integration of the controller with other components.
2. Which testing framework is commonly used for unit testing Spring service layer components?
a) JUnit
b) TestNG
c) Mockito
d) Cucumber
Answer: c) Mockito
4. How can you mock the dependencies of a Spring service layer component during unit testing?
a) By using the @Autowired annotation on the dependencies.
b) By creating instances of the dependencies manually in the test class.
c) By using the @MockBean annotation on the dependencies.
d) Mocking dependencies is not possible in Spring unit testing.
Answer: c) By using the @MockBean annotation on the dependencies.
6. How can you verify the behavior of a Spring service layer component during unit testing?
a) By checking the logs generated during testing.
b) By manually inspecting the response object returned by the component.
c) By using assertions and mock object verifications.
d) Verifying the behavior is not possible in Spring unit testing.
Answer: c) By using assertions and mock object verifications.
9. How
can you simulate database interactions in Spring service layer unit testing?
a) By using the real database instance for testing.
b) By using in-memory databases such as H2 or an embedded database.
c) By creating a separate test database.
d) Database interactions are not possible in Spring service layer unit testing.
Answer: b) By using in-memory databases such as H2 or an embedded database.
10. What is the purpose of the @Transactional annotation in Spring service layer unit testing?
a) It enables transactions for the service layer methods during testing.
b) It replaces the real application context with a mock context.
c) It defines the URL mapping for the controller method.
d) @Transactional is not used in Spring service layer unit testing.
Answer: a) It enables transactions for the service layer methods during testing.
1. What is the primary purpose of integration testing in Spring MVC applications with REST APIs?
a) To ensure the security of the application.
b) To measure the performance of the application.
c) To identify and fix defects in the integrated components.
d) To optimize database queries.
Answer: c) To identify and fix defects in the integrated components.
2. Which annotation is commonly used in Spring for integration testing of RESTful APIs?
a) @Test
b) @RunWith
c) @WebMvcTest
d) @SpringBootTest
Answer: d) @SpringBootTest
4. How can you verify the response of a REST API in Spring integration testing?
a) By checking the logs generated during testing.
b) By manually inspecting the response object returned by TestRestTemplate.
c) By using assertions on the response object returned by TestRestTemplate.
d) Verifying the response is not possible in Spring integration testing.
Answer: c) By using assertions on the response object returned by TestRestTemplate.
6. How can you send HTTP requests with custom headers in Spring integration testing?
a) By including the headers directly in the URL.
b) By specifying the headers in the request body.
c) By using the @RequestHeader annotation on the test method.
d) By using the HttpHeaders class provided by Spring.
Answer: d) By using the HttpHeaders class provided by Spring.
8. How can you pass request parameters in a GET request during Spring integration testing?
a) By appending the parameters to the URL.
b) By including the parameters in the request body.
c) By using the @RequestParam annotation on the test method.
d) Request parameters are not used in Spring integration testing.
Answer: a) By appending the parameters to the URL.
10. How can you verify the status code of the HTTP response in Spring integration testing?
a) By manually inspecting the response object returned by TestRestTemplate.
b) By using assertions on the response object returned by TestRestTemplate.
c) By checking the logs generated during testing.
d) Verifying the status code is not possible in Spring integration testing.
Answer: b) By using assertions on the response object returned by TestRestTemplate.
2. Which testing framework is commonly used for unit testing Spring MVC controllers with REST
endpoints?
a) JUnit
b) TestNG
c) Mockito
d) Cucumber
Answer: a) JUnit
4. How can you mock the dependencies of a Spring MVC controller during unit testing?
a) By using the @Autowired annotation on the dependencies.
b) By creating instances of the dependencies manually in the test class.
c) By using the @MockBean annotation on the dependencies.
d) Mocking dependencies is not possible in Spring unit testing.
Answer: c) By using the @MockBean annotation on the dependencies.
6. How can you verify the output of a controller method in Spring unit testing?
a) By checking the logs generated during testing.
b) By manually inspecting the response object returned by the controller.
c) By using assertions on the response object returned by MockMvc.
d) Ver
10. What is the purpose of the @MockMvcTest annotation in Spring unit testing?
a) It is used to test the integration of the controller with other components.
b) It replaces the real application context with a mock context.
c) It starts up the Spring application context for testing.
d) It enables testing of web-related components, such as controllers.
Answer: a) It is used to test the integration of the controller with other components.
12. Securing Web Application with Spring Security (10 MCQs on Each Subtopic )
Total No. Of MCQs : 40
4. Which configuration file is commonly used to define security-related settings in a Spring application?
a) applicationContext.xml
b) web.xml
c) security-context.xml
d) application.properties
Answer: c) security-context.xml
7. What is the default behavior of Spring Security when access to a resource is denied?
a) It returns a 404 Not Found error.
b) It redirects the user to the login page.
c) It throws an AccessDeniedException.
d) It allows access without any restrictions.
Answer: b) It redirects the user to the login page.
9. How can you configure custom login and logout URLs in Spring Security?
a) By modifying the web.xml file of the application.
b) By using the @RequestMapping annotation in the controller.
c) By configuring them in the security-context.xml file.
d) Customizing login and logout URLs is not possible in Spring Security.
Answer: c) By configuring them in the security-context.xml file.
3. How can you configure custom login and logout URLs in a Spring Boot application with Spring
Security?
a) By modifying the application.properties file.
b) By using the @RequestMapping annotation in the controller.
c) By configuring them in the application.yml file.
d) Customizing login and logout URLs is not possible in Spring Boot with Spring Security.
Answer: c) By configuring them in the application.yml file.
5. How can you enable HTTP Basic authentication in a Spring Boot application with Spring Security?
a) By using the @EnableHttpBasic annotation in the main class.
b) By configuring it in the application.properties file.
c) HTTP Basic authentication is automatically enabled in Spring Boot with Spring Security.
d) By including the spring-boot-starter-security dependency in the pom.xml file.
Answer: c) HTTP Basic authentication is automatically enabled in Spring Boot with Spring Security.
6. Which configuration file is commonly used to define security-related settings in a Spring Boot
application with Spring Security?
a) applicationContext.xml
b) web.xml
c) security-context.xml
d) application.properties
Answer: d) application.properties
7. How can you define custom roles and access control in a Spring Boot application with Spring
Security?
a) By modifying the web.xml file of the application.
b) By using the @PreAuthorize and @Secured annotations in the controller.
c) By configuring them in the application.properties file.
d) Customizing roles and access control is not possible in Spring Boot with Spring Security.
Answer: b) By using the @PreAuthorize and @Secured annotations in the controller.
9. Which configuration class is commonly used to configure Spring Security settings in a Spring Boot
application?
a) WebSecurityConfig
b) SecurityConfig
c) SecurityConfiguration
d) SpringSecurityConfig
Answer: b) SecurityConfig
10. How can you customize the login page in a Spring Boot application with Spring Security?
a) By modifying the login.jsp file in the application.
b) By using the @RequestMapping annotation in the controller.
c) By configuring it in the application.properties file.
d
) Customizing the login page is not possible in Spring Boot with Spring Security.
Answer: c) By configuring it in the application.properties file.
C. Basic Authentication
1. What is Basic Authentication in the context of web security?
a) A security mechanism that uses advanced encryption algorithms for user credentials.
b) A mechanism that requires users to provide a one-time password for each request.
c) A simple authentication mechanism where users provide their credentials in each request.
d) A mechanism that uses OAuth for user authentication and authorization.
Answer: c) A simple authentication mechanism where users provide their credentials in each request.
2. In Basic Authentication, how are user credentials transmitted from the client to the server?
a) They are transmitted as URL parameters.
b) They are transmitted as request headers.
c) They are encrypted using a public key.
d) They are not transmitted; instead, a session ID is used for authentication.
Answer: b) They are transmitted as request headers.
6. How can you enable Basic Authentication in a Spring Boot application with Spring Security?
a) By using the @EnableBasicAuthentication annotation in the main class.
b) By configuring it in the application.properties file.
c) Basic Authentication is automatically enabled in Spring Boot with Spring Security.
d) By including the spring-boot-starter-security dependency in the pom.xml file.
Answer: c) Basic Authentication is automatically enabled in Spring Boot with Spring Security.
8. How can you customize the error message for failed Basic Authentication attempts in a Spring Boot
application with Spring Security?
a) By modifying the login.jsp file in the application.
b) By using the @ExceptionHandler annotation in the controller.
c) By configuring it in the application.properties file.
d) Customizing the error message is not possible in Spring Boot with Spring Security.
Answer: c) By configuring it in the application.properties file.
9. What is the purpose of the @Order annotation when using Basic Authentication in a Spring Boot
application?
a) It specifies the order in which filters are applied to incoming requests.
b) It enables caching of security-related data.
c) It defines the request parameters for the controller method.
d) The @Order annotation is not used in Spring Boot with Basic Authentication.
Answer: a) It specifies the order in which filters are applied to incoming requests.
10. In Basic Authentication, how often do users need to provide their credentials?
a) Once, and then they are remembered for the session duration.
b) Once, and then they are remembered indefinitely.
c) For each request, as the credentials are not stored on the server.
d) Basic Authentication does not require user credentials.
Answer: c) For each request, as the credentials are not stored on the server.
4. How can you implement custom authentication logic using the AuthenticationProvider interface in
Spring Security?
a) By using the @Provider annotation in the authentication class.
b) By modifying the web.xml file of the application.
c) By creating a separate authentication service and injecting it into the provider.
d) Implementing custom authentication logic is not possible in Spring Security.
Answer: c) By creating a separate authentication service and injecting it into the provider.
6. How can you define custom roles and access control in a Spring application with Spring Security
?
a) By modifying the web.xml file of the application.
b) By using the @PreAuthorize and @Secured annotations in the controller.
c) By configuring them in the application.properties file.
d) Customizing roles and access control is not possible in Spring Security.
Answer: b) By using the @PreAuthorize and @Secured annotations in the controller.
8. How can you enable URL-based access control in a Spring Boot application with Spring Security?
a) By using the @EnableWebSecurity annotation in the main class.
b) By configuring it in the application.properties file.
c) URL-based access control is automatically enabled in Spring Boot with Spring Security.
d) By including the spring-boot-starter-security dependency in the pom.xml file.
Answer: c) URL-based access control is automatically enabled in Spring Boot with Spring Security.
10. How can you define custom roles and access control in a Spring application with Spring Security?
a) By modifying the web.xml file of the application.
b) By using the @PreAuthorize and @Secured annotations in the controller.
c) By configuring them in the application.properties file.
d) Customizing roles and access control is not possible in Spring Security.
Answer: b) By using the @PreAuthorize and @Secured annotations in the controller.