0% found this document useful (0 votes)
3 views12 pages

OOP Java2 Repeated Questions With Answers

The document is a compilation of frequently asked questions and model answers for the CS-365 Object Oriented Programming Using Java II course, covering topics such as Collections, JDBC, Multithreading, and the Spring Framework. It includes sections with 1-mark, 2-mark, and 4-mark questions, detailing definitions, differences, and life cycles of various Java concepts and components. The document serves as a study guide for students preparing for exams from 2022 to 2025.

Uploaded by

solomonpillai25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

OOP Java2 Repeated Questions With Answers

The document is a compilation of frequently asked questions and model answers for the CS-365 Object Oriented Programming Using Java II course, covering topics such as Collections, JDBC, Multithreading, and the Spring Framework. It includes sections with 1-mark, 2-mark, and 4-mark questions, detailing definitions, differences, and life cycles of various Java concepts and components. The document serves as a study guide for students preparing for exams from 2022 to 2025.

Uploaded by

solomonpillai25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

T.Y. [Link].

Computer Science — Semester VI

CS-365 : Object Oriented Programming Using Java - II


(2019 Pattern | CBCS)

Most Repeated Questions with Model Answers

Compiled from: 2022 Apr & Nov | 2023 Apr & Nov | 2024 Apr & Nov | 2025 Apr

SECTION A — 1-Mark Questions (Q1 Type) [Most Repeated]

1. What is Collection? / Define Collection.


■ Appeared in: 2022 Nov, 2023 Apr, 2024 Nov, 2025 Apr — 4 times
A Collection is a framework in Java that provides an architecture to store and manipulate a group of objects.
It is part of the [Link] package. Key interfaces: List, Set, Queue, Map. It provides ready-made classes like
ArrayList, LinkedList, HashSet, TreeSet, HashMap, etc.

2. What is JDBC? / Define JDBC.


■ Appeared in: 2022 Nov, 2022 Apr, 2023 Apr, 2024 Apr, 2025 Apr — 5 times
JDBC (Java Database Connectivity) is an API in Java that enables Java programs to connect and interact
with relational databases. It provides methods to query and update data in a database. JDBC is defined in
the [Link] package. It acts as a bridge between Java application and the database.

3. Define Multithreading.
■ Appeared in: 2023 Apr, 2024 Nov — 2 times
Multithreading is a Java feature that allows concurrent execution of two or more threads. A thread is the
smallest unit of a process. Multithreading maximizes CPU utilization, is used for animation, gaming, and
server-side programming. Java supports multithreading via the Thread class and Runnable interface.

4. What is Spring Framework? / Define Spring.


■ Appeared in: 2022 Apr, 2022 Nov, 2023 Nov, 2024 Nov, 2025 Apr — 5 times
Spring Framework is an open-source, lightweight Java framework for building enterprise-level applications.
It provides features like Dependency Injection (DI), Aspect-Oriented Programming (AOP), MVC architecture,
and data access integration. Applications: Web apps, RESTful services, microservices, batch processing.

5. Define Thread Priority.


■ Appeared in: 2022 Nov — and related in 2023 Nov, 2024 Nov
Thread Priority determines the order in which threads are scheduled for execution. Java assigns priorities
from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY), with 5 (NORM_PRIORITY) as default. Methods:
setPriority(int p) and getPriority().

6. What is Cookie? / Define Cookie.


■ Appeared in: 2022 Apr, 2023 Nov, 2024 Nov, 2024 Apr, 2025 Apr — 5 times
A Cookie is a small piece of data sent from a web server and stored on the client's browser. It is used for
session tracking, remembering user preferences, and storing login information. In Java, the Cookie class is
in [Link] package. Syntax: Cookie c = new Cookie("name", "value");

7. What is Session? / Define Session.


■ Appeared in: 2022 Nov, 2023 Apr, 2025 Apr — 3 times
A Session is a server-side mechanism to maintain state information of a user across multiple HTTP
requests. In Java, it is managed by the HttpSession interface. The server creates a unique session ID for
each user. Example: HttpSession s = [Link]();

8. What is use of join() method?


■ Appeared in: 2022 Nov, 2024 Apr — 2 times
The join() method in Java is used to wait for a thread to finish before the current thread continues
execution. It makes one thread wait until another thread completes. Defined in the Thread class. Syntax:
[Link](); — the calling thread pauses until thread t finishes.

9. Define Hashtable.
■ Appeared in: 2022 Nov, 2024 Apr — 2 times
Hashtable is a class in Java that implements the Map interface. It stores data in key-value pairs and uses
hashing for fast retrieval. It is synchronized (thread-safe). Neither keys nor values can be null. Part of
[Link] package.

10. What is use of yield() method?


■ Appeared in: 2023 Nov, 2024 Nov, 2025 Apr — 3 times
The yield() method pauses the currently executing thread temporarily and gives a chance to other threads of
the same or higher priority to execute. It does not release the lock. It is a static method of the Thread class.
Syntax: [Link]();

11. What is ArrayList?


■ Appeared in: 2022 Apr — also in related Q2s across 4+ papers
ArrayList is a resizable-array implementation of the List interface in Java. It allows duplicate elements,
maintains insertion order, and allows null values. It is part of [Link] package. Random access is fast (O(1)),
but insertion/deletion is slow compared to LinkedList.

12. Define Map Interface.


■ Appeared in: 2022 Apr, 2024 Nov — 2 times
The Map interface in Java stores data as key-value pairs. Keys are unique but values can be duplicated. It
does not extend the Collection interface. Implementations: HashMap, TreeMap, LinkedHashMap,
Hashtable. Key methods: put(), get(), remove(), containsKey().
SECTION B — 2-Mark Questions (Q2 Type) [Most Repeated]

1. Differences between ArrayList and LinkedList.


■ Appeared in: 2022 Nov, 2024 Nov — 2 times (+ related in 2023 Apr)

ArrayList LinkedList

Uses dynamic array internally. Uses doubly linked list internally.

Fast random access — O(1). Slow random access — O(n).

Slow insertion/deletion in middle. Fast insertion/deletion.

Less memory (no node pointers). More memory (stores pointers).

Best for read-heavy operations. Best for write-heavy operations.

2. Differentiate between Set and List interface.


■ Appeared in: 2022 Apr, 2023 Apr, 2025 Apr — 3 times

List Interface Set Interface

Allows duplicate elements. Does not allow duplicates.

Maintains insertion order. No guaranteed order (except LinkedHashSet).

Classes: ArrayList, LinkedList. Classes: HashSet, TreeSet.

Elements accessed by index. No index-based access.

3. How to create a Thread in Multithreading?


■ Appeared in: 2022 Apr, 2024 Nov, 2023 Apr — 3 times
Two ways to create a thread in Java:
Method 1: Extending Thread class
class MyThread extends Thread {
public void run() {
[Link]("Thread running");
}
}
public class Test {
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}

Method 2: Implementing Runnable interface


class MyThread implements Runnable {
public void run() {
[Link]("Thread running");
}
}
public class Test {
public static void main(String[] args) {
Thread t = new Thread(new MyThread());
[Link]();
}
}

4. Differentiate between Statement and PreparedStatement interface.


■ Appeared in: 2022 Apr, 2024 Nov — 2 times
Statement PreparedStatement

SQL is compiled each time. SQL is pre-compiled once.

Slower for repeated queries. Faster for repeated queries.

Does not accept parameters. Accepts IN parameters using '?'.

Less secure (SQL injection risk). More secure (prevents SQL injection).

Use: <code>createStatement()</code> Use: <code>prepareStatement(sql)</code>

5. What is ResultSet Interface? List any two methods/fields.


■ Appeared in: 2022 Nov, 2023 Apr, 2024 Apr — 3 times
The ResultSet interface represents the result of a database query. It maintains a cursor pointing to the
current row. It is part of [Link] package.
Important fields: ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY
Important methods:

Method Description

next() Moves cursor to next row; returns true if row exists.

getString(col) Returns column value as String.

getInt(col) Returns column value as int.

close() Closes the ResultSet object.

6. Advantages of Servlet over CGI / Advantages of JSP over Servlet.


■ Appeared in: 2023 Apr (JSP>Servlet), 2024 Nov (Servlet>CGI) — 2 times
Advantages of Servlet over CGI:
1. Servlets run inside JVM — faster (no new process per request). 2. Platform independent. 3. Persistent —
stays in memory between requests. 4. Supports session management and cookies.
Advantages of JSP over Servlet:
1. Easy to write — HTML + Java together. 2. Less code than Servlet. 3. Automatic recompilation when JSP is
modified. 4. Supports custom tags.

7. List any two implicit objects in JSP.


■ Appeared in: 2022 Nov, 2024 Apr — 2 times

Implicit Object Description

request HttpServletRequest — carries client request data.

response HttpServletResponse — sends response to client.

session HttpSession — stores user session data.

out JspWriter — writes output to client.

application ServletContext — shared across all users.


SECTION C — 4-Mark Questions (Q3/Q4 Type) [Most Repeated]

1. Life Cycle of Thread (explain in detail).


■ Appeared in: 2022 Apr, 2022 Nov, 2023 Nov, 2024 Nov, 2025 Apr — 5 TIMES ■ MOST REPEATED
A thread in Java goes through the following 5 states:
1. New: Thread object is created but start() not called yet.
2. Runnable: start() is called; thread is ready to run but waiting for CPU allocation by the scheduler.
3. Running: Thread is actually executing its run() method.
4. Blocked/Waiting: Thread is waiting for a resource, lock, or another thread (e.g., after sleep(), wait(), or
join() calls).
5. Dead/Terminated: Thread finishes execution or is stopped. Cannot be restarted.
Diagram (States):
New ■■start()■■■ Runnable ■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■ ■
(CPU allocated) (back to runnable)
■ ■
▼ ■
Running ■■sleep()/wait()/join()■■■ Blocked/Waiting

run() ends


Dead/Terminated

2. Life Cycle of JSP (explain in detail).


■ Appeared in: 2022 Nov, 2023 Apr, 2024 Nov — 3 times ■
JSP Life Cycle has 5 phases:
1. Translation: JSP file is converted into a Servlet (.java) by the JSP engine.
2. Compilation: The generated Servlet is compiled into a .class file.
3. Loading & Instantiation: The Servlet class is loaded into memory and an instance is created.
4. Initialization: jspInit() method is called (only once), similar to Servlet's init().
5. Request Processing: For each request, _jspService() is called. This is where the actual response is
generated.
6. Destruction: jspDestroy() is called before the JSP is removed from memory.
JSP File ■■Translation■■■ [Link]
■■Compilation■■■ [Link]
■■Loading■■■ Object Created
■■jspInit()■■■ Initialized (once)
■■_jspService()■■■ Per Request (many times)
■■jspDestroy()■■■ Destroyed

3. Explain Life Cycle of Servlet.


■ Appeared in: 2024 Apr — also conceptually in 2022 Nov (servlet execution)
Servlet Life Cycle has 3 main methods managed by the Servlet Container:
1. init(): Called once when the servlet is first loaded. Used for initialization (e.g., opening DB connection).
Signature: public void init(ServletConfig config)
2. service(): Called for each HTTP request. Dispatches to doGet() or doPost() based on request type.
3. destroy(): Called once when servlet is unloaded. Used for cleanup. Signature: public void destroy()
Client Request ■■■ Web Server ■■■ Servlet Container


load & instantiate Servlet ■■■ init() [once]


service() / doGet() / doPost() [per request]


destroy() [once, on shutdown]

4. Explain Synchronization with an example.


■ Appeared in: 2022 Nov, 2023 Apr, 2024 Nov — 3 times ■
Synchronization is a Java mechanism to control access to shared resources by multiple threads, preventing
data inconsistency. The synchronized keyword ensures only one thread executes a block/method at a time
by acquiring a lock.
Types: 1. Synchronized method 2. Synchronized block
Example — without sync (problem):
class Counter {
int count = 0;
void increment() { count++; } // Not thread-safe!
}

Example — with synchronization (solution):


class Counter {
int count = 0;

synchronized void increment() {


count++;
[Link]("Count: " + count);
}
}

class MyThread extends Thread {


Counter c;
MyThread(Counter c) { this.c = c; }
public void run() { [Link](); }
}

public class SyncDemo {


public static void main(String[] args) throws Exception {
Counter c = new Counter();
MyThread t1 = new MyThread(c);
MyThread t2 = new MyThread(c);
[Link](); [Link]();
[Link](); [Link]();
[Link]("Final: " + [Link]);
}
}

5. JDBC Program — Store and Display Entity Details (Student/Employee/Teacher/Book).


■ Appeared in: Appears in ALL 7 papers — every year ■■ MUST PREPARE
Below is a generic JDBC program. Adjust table/column names as needed (Student: RN, Name, Percentage |
Employee: Eno, Ename, Salary | Teacher: Tid, Tname, Subject)
import [Link].*;
import [Link];

public class JDBCDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
// 1. Load Driver
[Link]("[Link]");

// 2. Create Connection
Connection con = [Link](
"jdbc:mysql://localhost:3306/mydb", "root", "password");

// 3. Accept input
[Link]("Enter Roll No: "); int rn = [Link]();
[Link]("Enter Name: "); String name = [Link]();
[Link]("Enter Percentage: "); float per = [Link]();

// 4. Insert using PreparedStatement


PreparedStatement ps = [Link](
"INSERT INTO Student VALUES(?, ?, ?)");
[Link](1, rn);
[Link](2, name);
[Link](3, per);
[Link]();
[Link]("Record inserted successfully!");

// 5. Display all records


Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM Student");
[Link]("\nRN\tName\tPercentage");
[Link]("----------------------------");
while ([Link]()) {
[Link]([Link](1) + "\t" +
[Link](2) + "\t" +
[Link](3));
}
[Link]();
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}

6. Multithreading Program — Display Numbers/Alphabets with Delay (sleep()).


■ Appeared in: 2022 Nov (1-10, 2sec), 2022 Apr (A-Z, 2sec), 2024 Nov (odd 1-100, 5sec), 2024 Apr (even 1-100, 5sec)
— ALL papers ■■
Program — Display numbers 1 to 10 with 2-second delay:
class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
[Link]("Number: " + i);
try {
[Link](2000); // 2000 ms = 2 seconds
} catch (InterruptedException e) {
[Link]([Link]());
}
}
}
}

public class ThreadDemo {


public static void main(String[] args) {
NumberThread t = new NumberThread();
[Link]();
}
}
Variant — Display odd numbers 1 to 100 with 5-second delay:
public void run() {
for (int i = 1; i <= 100; i += 2) { // odd only
[Link](i);
try { [Link](5000); }
catch (InterruptedException e) { [Link](); }
}
}

Variant — Display alphabets A to Z with 2-second delay:


public void run() {
for (char c = 'A'; c <= 'Z'; c++) {
[Link](c);
try { [Link](2000); }
catch (InterruptedException e) { [Link](); }
}
}

7. JSP Program — Check Prime Number / Perfect Number.


■ Appeared in: 2022 Nov (prime-blue), 2022 Apr (perfect-yellow), 2024 Nov (prime 1-n red) — 3 times
JSP — Check if number is Prime (display in Blue):
<%@ page language="java" %>

<%
int n = [Link]([Link]("num"));
boolean prime = true;
if (n <= 1) prime = false;
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) { prime = false; break; }
}
%>
<% if(prime) { %>
<%= n %> is a Prime Number
<% } else { %>
<%= n %> is NOT a Prime Number
<% } %>

JSP — Check if number is Perfect (display in Yellow):


<%
int n = [Link]([Link]("num"));
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) sum += i;
}
String result = (sum == n) ? n + " is Perfect" : n + " is NOT Perfect";
%>
<%= result %>

8. Java Program — ArrayList with n names (sort / store / display using iterator).
■ Appeared in: 2022 Nov (sort names), 2024 Nov (employee names + iterator), 2023 Apr related — 3 times
import [Link].*;

public class ArrayListDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
ArrayList list = new ArrayList<>();

[Link]("Enter n: "); int n = [Link]();


[Link]("Enter " + n + " names:");
for (int i = 0; i < n; i++) {
[Link]([Link]());
}
// Sort in ascending order
[Link](list);

// Display using Iterator


[Link]("\nSorted Names:");
Iterator it = [Link]();
while ([Link]()) {
[Link]([Link]());
}
}
}

9. Session Tracking — What is it and How to implement?


■ Appeared in: 2023 Nov — also related in 2022 Apr (cookies), 2024 Apr, 2024 Nov
Session Tracking is a mechanism to maintain state/information of a client across multiple HTTP requests
(since HTTP is stateless).
4 Techniques:
1. Cookies — Small data stored in browser. 2. URL Rewriting — Session ID appended to URL. 3. Hidden
Fields — Data stored in hidden HTML form fields. 4. HttpSession — Server-side session object.
Implementation using HttpSession:
// In Servlet:
HttpSession session = [Link]();
[Link]("username", "Alice");

// Retrieve on another page:


String user = (String) [Link]("username");

// Invalidate session:
[Link]();

10. Write a Servlet program to get server information.


■ Appeared in: 2022 Nov — Servlet for server name, port, version
import [Link].*;
import [Link].*;
import [Link].*;

public class ServerInfoServlet extends HttpServlet {


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

[Link]("text/html");
PrintWriter out = [Link]();

ServletContext ctx = getServletContext();

[Link]("");
[Link]("Server Information");
[Link]("Server Name: " + [Link]() + "");
[Link]("Server Port: " + [Link]() + "");
[Link]("Server Info: " + [Link]() + "");
[Link]("");
}
}
SECTION D — 3-Mark Questions (Q5 Type) [Most Repeated]

1. Explain JDBC Architecture.


■ Appeared in: 2022 Apr, 2024 Nov — 2 times ■
JDBC Architecture has two layers:
Layer 1 — JDBC API: Provides interfaces and classes for the Java application to communicate with the
database. Key components: DriverManager, Connection, Statement, PreparedStatement,
CallableStatement, ResultSet.
Layer 2 — JDBC Driver API: Provides the actual database drivers that implement the JDBC interfaces.
4 Types of JDBC Drivers:

Type Name Description

Type 1 JDBC-ODBC Bridge Uses ODBC driver; platform-dependent.

Type 2 Native-API Driver Converts JDBC calls to DB-specific calls.

Type 3 Network Protocol Driver Uses middleware server; database-independent.

Type 4 Thin Driver Pure Java; directly connects to DB. Best choice.
JDBC Steps (Process):
1. Load Driver: [Link]("[Link]");
2. Get Connection: Connection con = [Link](url, user, pass);
3. Create Statement: Statement st = [Link]();
4. Execute Query: ResultSet rs = [Link]("SELECT * FROM table");
5. Process Results: while([Link]()) { ... }
6. Close: [Link]();

2. Explain Execution Process of Servlet Application.


■ Appeared in: 2022 Nov, 2023 Apr — 2 times
Steps in Servlet execution:
1. Client Request: User types URL in browser → HTTP request sent to web server.
2. Web Server: Passes request to Servlet Container (e.g., Tomcat).
3. Check Servlet: Container checks if servlet is already loaded. If not, it loads and instantiates the servlet
class.
4. init(): Called once for initialization.
5. service(): Called for each request. Routes to doGet() or doPost() based on HTTP method.
6. Response: Servlet generates HTML/data response sent back to browser.
7. destroy(): Called when servlet is unloaded (server shutdown/redeploy).

3. Explain Spring Framework — Architecture / Applications.


■ Appeared in: 2023 Nov (architecture), 2024 Apr (applications), 2022 Apr (applications) — 3 times
Spring Architecture consists of several modules grouped into layers:

Layer Modules

Core Container Beans, Core, Context, SpEL — provides DI/IoC.

Data Access JDBC, ORM, OXM, JMS, Transactions.

Web Layer Web, Web-MVC, Web-Socket, Web-Portlet.

AOP & Instrumentation AOP, Aspects, Instrumentation.


Test Spring Test module for unit/integration testing.
Applications of Spring:
1. Web applications (Spring MVC). 2. RESTful Web Services. 3. Microservices (Spring Boot). 4. Batch
Processing (Spring Batch). 5. Security (Spring Security). 6. Data Access with Hibernate integration.

4. Differentiate between JSP and Servlet.


■ Appeared in: 2024 Apr — also conceptually throughout

JSP Servlet

HTML with embedded Java code. Pure Java class generating HTML.

Easy to write for web designers. Requires Java programming knowledge.

Auto-compiled by server. Must be compiled manually.

Maintenance is easy. Hard to maintain (HTML in println).

Implicit objects available. No implicit objects.

Extension: .jsp Extension: .java / .class


EXAM TIPS — Priority Study Guide

Priority Topic Times Repeated

■■■ JDBC Program (store + display) All 7 papers

■■■ Multithreading with sleep() 6 out of 7 papers

■■■ Life Cycle of Thread 5 out of 7 papers

■■ Life Cycle of JSP 3 out of 7 papers

■■ Synchronization with example 3 out of 7 papers

■■ JDBC Architecture 3 out of 7 papers

■■ JSP Prime/Perfect Number 3 out of 7 papers

■■ Life Cycle of Servlet 2+ papers

■■ Spring Framework All papers Q1

■ ArrayList/LinkedList program 3 papers

■ Session Tracking 3 papers

■ Servlet Execution Process 2 papers

■ Set vs List / ArrayList vs LinkedList 3+ papers

■ Statement vs PreparedStatement 2+ papers

■ Note: Focus on JDBC programs, Thread Life Cycle, and Synchronization — they appear in virtually every exam. Also
prepare JSP programs (prime/perfect numbers). Good luck with your exams! ■

You might also like