Java II — Slip Solutions
Sabhi Slips • Easy Explanation • Viva Questions TY [Link]. Computer Science | SPPU
■ Total Slips Covered 15 Slips (Slip 1 – Slip 15)
■ Topics Threads, Collections, JDBC, JSP, Servlet
■ Style Beginner Friendly — Line by Line Explanation
■■ Viva Common viva questions har slip ke saath
Prepared for Pryu ■
SLIP 1 • Question 1
Display all alphabets A to Z after every 2 seconds using Thread
■ Pehle Samjho — Thread kya hota hai?
■ Thread = ek alag kaam jo background mein chalta hai. Jaise phone mein music bhi chal raha hai aur WhatsApp bhi
— dono alag threads hain!
■ Complete Code
public class Slip1 extends Thread // Thread class ko extend kar rahe hain
char c; // ek variable — alphabet store karega
public void run() // run() = thread start hone pe ye chalega
for(c = 'A'; c<='Z'; c++) // A se Z tak loop
[Link](''+c); // alphabet print karo
try
[Link](2000); // 2000ms = 2 seconds ruko
catch(Exception e) // agar error aaye toh pakdo
[Link]();
public static void main(String args[])
Slip1 t = new Slip1(); // thread ka object banao
[Link](); // thread shuru karo (run() automatically chalega)
■ Line by Line Explanation
extends Thread → Java ki Thread class se inherit kar rahe hain — matlab Thread ke features milenge
public void run() → Ye woh method hai jo thread start hone pe automatically call hota hai
[Link](2000) → Thread ko 2 second ke liye sulate hain — isliye har letter 2 sec baad aata hai
try-catch → sleep() exception throw karta hai — isliye try-catch likhna zaroori hai
[Link]() → start() call karne pe run() automatically call hota hai — direct run() mat bulao!
■■ [Link]() directly mat bulao — hamesha [Link]() use karo! Warna thread create nahi hoga.
■ Trick: Exam mein puchha jaaye toh: 'Thread extend kiya, run() override kiya, start() se shuru kiya'
■■ Viva Questions
Q: Thread kya hota hai?
→ Thread ek lightweight sub-process hai jo independently run karta hai.
Q: start() aur run() mein kya fark hai?
→ start() naya thread create karta hai aur run() call karta hai. Direct run() bulane se naya thread nahi banta.
Q: [Link]() kyon use kiya?
→ Program ko kuch milliseconds ke liye rok deta hai. Yahan 2000ms = 2 seconds.
Q: extends Thread ke alawa aur kaunsa tarika hai?
→ Runnable interface implement karna — implements Runnable.
Q: Thread ki states kaunsi hain?
→ New → Runnable → Running → Blocked/Sleeping → Dead
SLIP 1 • Question 2
Employee details accept karke database mein store karo (Swing + JDBC)
■ Swing = Java ka GUI framework (buttons, textfields banane ke liye). JDBC = Java Database Connectivity — Java se
database se baat karna.
■ Code Summary
// Key parts of the program:
// 1. GUI Components
Label l1 = new Label('Eno'); // label banao
TextField t1 = new TextField(); // input box banao
Button b = new Button('Save'); // button banao
// 2. Database Connection
[Link]('[Link]'); // driver load karo
cn = [Link]('jdbc:odbc:Ass','',''); // connect karo
// 3. Insert Query
String strr = 'insert into emp values(' + en + ',"' + enn + '",' + sal + ')';
int k = [Link](strr); // query execute karo
// 4. Success Check
if(k > 0) [Link](null, 'Record Is Added');
■■ Viva Questions
Q: JDBC kya hai?
→ Java Database Connectivity — Java program ko database se connect karne ki API hai.
Q: [Link]() kyon likhte hain?
→ Database driver load karne ke liye — bina iske connection nahi hoga.
Q: executeUpdate() vs executeQuery() mein fark?
→ executeUpdate() = INSERT/UPDATE/DELETE ke liye. executeQuery() = SELECT ke liye.
Q: JOptionPane kya hai?
→ Swing ka dialog box — message dikhane ke liye use hota hai.
Q: ActionListener kya karta hai?
→ Button click hone pe actionPerformed() method call hota hai.
SLIP 2 • Question 1
N names HashSet mein store karo aur ascending order mein display karo
■ HashSet = Collection jisme DUPLICATE values nahi rehti aur ORDER guarantee nahi hoti. Sorting ke liye List mein
convert karte hain.
■ Complete Code
import [Link].*;
public class GFG {
public static void main(String args[]) {
HashSet<String> set = new HashSet<String>(); // HashSet banao
[Link]('geeks'); // elements add karo
[Link]('practice');
[Link]('contribute');
[Link]('ide');
[Link]('Original HashSet: ' + set);
List<String> list = new ArrayList<String>(set); // List mein convert karo
[Link](list); // sort karo
[Link]('Sorted: ' + list);
■ Key Concepts
HashSet → No duplicates, no order guaranteed — fast hai kyunki hashing use karta hai
ArrayList(set) → HashSet ko List mein convert kiya — taaki sort ho sake
[Link]() → List ko alphabetically sort karta hai
■■ Viva Questions
Q: HashSet aur ArrayList mein kya fark hai?
→ HashSet: no duplicates, no order. ArrayList: duplicates allow, insertion order maintain.
Q: HashSet mein duplicate add karne ki koshish karein toh kya hoga?
→ Kuch nahi hoga — HashSet silently ignore kar dega.
Q: [Link]() kya karta hai?
→ List ko ascending order mein sort karta hai.
Q: TreeSet aur HashSet mein fark?
→ TreeSet automatically sorted hota hai. HashSet mein order nahi hota.
SLIP 2 • Question 2
Servlet — HTTP request info display karo (IP, browser, server)
■ Servlet = Java program jo web server pe chalta hai aur browser ke requests handle karta hai.
■ Key Code Parts
public class serverInfo extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
[Link]('text/html'); // output type set karo
PrintWriter pw = [Link](); // output likhne ke liye
[Link]([Link]()); // server ka naam
[Link]([Link]()); // server ka port
[Link]([Link]()); // client ka IP address
[Link]([Link]('User-Agent')); // browser info
■■ Viva Questions
Q: Servlet kya hai?
→ Java class jo web requests handle karti hai — HTTP protocol use karke.
Q: doGet() aur doPost() mein kya fark?
→ doGet() = URL se data, doPost() = form se data (secure).
Q: HttpServletRequest kya hai?
→ Browser se aane wali request ki information contain karta hai.
Q: [Link] kyon zaroori hai?
→ Servlet mapping define karta hai — URL ke saath servlet ko link karta hai.
SLIP 3 • Question 1
JSP — Patient details database se table mein display karo
■ JSP (Java Server Pages) = HTML ke andar Java code likh sakte hain. <% %> ke andar Java code aata hai.
■ Key Code
<%@ page import='[Link].*' %> // Java SQL import karo
<%
[Link]('[Link]'); // driver load
Connection cn = [Link](...);
Statement st = [Link]();
ResultSet rs = [Link]('select * from Patient');
%>
<table border='1'>
<tr><td>Patient No</td><td>Name</td></tr>
<% while([Link]()) { %> // loop — har row ke liye
<tr>
<td><%= [Link]('pno') %></td> // <%= %> = value print
<td><%= [Link]('pname') %></td>
</tr>
<% } %>
</table>
■ JSP Tags Yaad Rakho
Tag Kaam
<% %> Java code likhne ke liye (Scriptlet)
<%= %> Value print karne ke liye (Expression)
<%! %> Variable/Method declare karne ke liye (Declaration)
<%@ %> Import, page settings ke liye (Directive)
■■ Viva Questions
Q: JSP aur Servlet mein kya fark hai?
→ JSP = HTML mein Java. Servlet = Java mein HTML. JSP easy hai web pages ke liye.
Q: <% %> aur <%= %> mein kya fark hai?
→ <% %> Java code run karta hai. <%= %> value page pe print karta hai.
Q: ResultSet kya hai?
→ Database query ka result store karta hai — [Link]() se row by row padhte hain.
SLIP 3 • Question 2
LinkedList — add at end, delete first, display reverse
■ LinkedList = chain ki tarah connected nodes. Har node mein data + next node ka address hota hai.
■ Key Operations
LinkedList<String> ll = new LinkedList<String>();
[Link]('Ravi'); // add at end
[Link]('Vijay');
[Link]('Ajay');
[Link]('Vijay'); // specific element remove
[Link](0); // index se remove (first element)
[Link](); // pehla element remove
[Link](); // aakhri element remove
// Reverse display
Iterator i = [Link](); // ulta iterator
while([Link]()) {
[Link]([Link]());
■■ Viva Questions
Q: LinkedList aur ArrayList mein kya fark?
→ LinkedList: fast insert/delete. ArrayList: fast random access (index se).
Q: descendingIterator() kya karta hai?
→ List ko reverse order mein traverse karta hai.
Q: remove(0) aur removeFirst() mein fark?
→ Dono same kaam karte hain — pehla element remove karte hain.
SLIP 4 • Question 1
Runnable interface se text blink karo frame pe
■ Runnable ek interface hai — Thread extend karne ki jagah Runnable implement kar sakte hain. Ye better approach
hai kyunki Java mein multiple inheritance nahi hota.
■ Complete Code
public class BlinkText extends Frame implements Runnable {
Thread t;
Label l1;
int f; // flag: 0=show text, 1=hide text
public BlinkText() {
t = new Thread(this); // Runnable pass karo Thread ko
[Link](); // thread shuru karo
l1 = new Label('Hello JAVA');
// ... setup ...
f = 0;
public void run() {
try {
if(f == 0) {
[Link](200); // 0.2 second ruko
[Link](''); // text hatao (blink off)
f = 1;
if(f == 1) {
[Link](200); // 0.2 second ruko
[Link]('Hello Java'); // text dikhao (blink on)
f = 0;
} catch(Exception e) { [Link](e); }
run(); // recursive call — blink karta rahe
■ extends Thread vs implements Runnable
extends Thread implements Runnable
Thread class se inherit karo Interface implement karo
Sirf Thread extend ho sakta Dusri class bhi extend kar sakte hain
new Slip1(); [Link](); new Thread(obj); [Link]();
Kam flexible Zyada flexible — PREFERRED
■■ Viva Questions
Q: Runnable interface mein kaunsa method hota hai?
→ Sirf ek — public void run()
Q: Thread(this) mein 'this' kya hai?
→ 'this' current object hai jo Runnable implement karta hai.
Q: extends Thread aur implements Runnable mein kaun better hai?
→ implements Runnable better hai — multiple inheritance possible rehti hai.
Q: setText('') kyon kiya?
→ Label ka text empty karne ke liye — isse text disappear hota hai (blink effect).
SLIP 4 • Question 2
City name + STD code — Hashtable mein store karo (Add/Search/Remove)
■ Hashtable = Key-Value pair store karta hai. City name = Key, STD code = Value. No duplicates kyunki keys unique
hoti hain.
■ Key Code
Hashtable ts = new Hashtable();
// ADD
[Link](cityName, stdCode); // city aur code add karo
// SEARCH
if([Link](name)) // city exist karti hai?
[Link]([Link](name).toString()); // code dikhao
// REMOVE
[Link](name); // city hatao
// DISPLAY ALL
Enumeration k = [Link]();
while([Link]())
[Link]([Link]() + ' = ' + [Link](...));
■■ Viva Questions
Q: Hashtable aur HashMap mein kya fark?
→ Hashtable: synchronized (thread-safe), null nahi. HashMap: not synchronized, null allow.
Q: containsKey() kya karta hai?
→ Check karta hai ki given key Hashtable mein hai ya nahi.
Q: Enumeration kya hai?
→ Hashtable ke saare keys/values ek-ek karke padhne ke liye use hota hai.
SLIP 5 • Question 1
Hashtable — mobile number + student name, Enumeration se display
■ Hashtable mein mobile number ko key aur student name ko value ki tarah store karte hain. Enumeration se saare
records print karte hain.
■ Complete Code
import [Link].*;
class GFG {
public static void main(String[] args) {
Hashtable<String,String> ht = new Hashtable<>();
[Link]('Name', 'Rohan'); // key='Name', value='Rohan'
[Link]('Mobile_No', '8446049402'); // key='Mobile_No', value=number
Enumeration<String> e = [Link](); // saari keys lo
while([Link]()) { // jab tak elements hain
String key = [Link](); // next key lo
[Link](key + ':' + [Link](key)); // print karo
■■ Viva Questions
Q: Enumeration kaise kaam karta hai?
→ hasMoreElements() check karta hai aur nextElement() next value deta hai.
Q: [Link]() kya return karta hai?
→ Hashtable ki saari keys ka Enumeration object.
Q: [Link](key) kya karta hai?
→ Key ke corresponding value return karta hai.
SLIP 6 • Question 1
TreeSet — N integers, sorted, no duplicates, search karo
■ TreeSet = Automatically sorted collection, no duplicates. Sorted output chahiye aur duplicates nahi — TreeSet best
choice!
■ Complete Code
import [Link].*;
import [Link].*;
class Slip19_2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
TreeSet ts = new TreeSet(); // auto-sorted, no duplicates
[Link]('Enter N:');
int no = [Link]([Link]());
for(int i=0; i<no; i++) {
[Link]('Enter element:');
int element = [Link]([Link]());
[Link](element); // add karo — auto sort hoga
[Link]('Sorted: ' + ts); // sorted display
[Link]('Search element:');
int element = [Link]([Link]());
if([Link](element)) // search karo
[Link]('Found!');
else
[Link]('NOT found!');
■■ Viva Questions
Q: TreeSet kyu use kiya HashSet ki jagah?
→ TreeSet automatically sorted hota hai. HashSet sorted nahi hota.
Q: TreeSet mein duplicate add karne se kya hoga?
→ Silently ignore hoga — TreeSet duplicate allow nahi karta.
Q: contains() kya karta hai?
→ Element exist karta hai ya nahi — true/false return karta hai.
Q: BufferedReader kya hai?
→ Keyboard se input padhne ke liye use hota hai.
SLIP 6 • Question 2
Traffic signal simulate karo threads se (Applet)
■ Thread se Red→Yellow→Green signal animate karte hain. Applet = browser mein run hone wala Java program
(purana).
■ Logic Samjho
// i goes from 24 to 1
// i > 16 aur <= 24 → RED signal (8 steps)
// i > 8 aur <= 16 → YELLOW signal (8 steps)
// i > 1 aur <= 8 → GREEN signal (7 steps)
// paint() method mein:
if(r == 1) { [Link]([Link]); [Link](100,100,100,100); }
if(y == 1) { [Link]([Link]); [Link](100,200,100,100); }
if(g1== 1) { [Link]([Link]); [Link](100,300,100,100); }
■■ Viva Questions
Q: Applet aur Application mein kya fark?
→ Applet browser mein run hota hai, Application independently run hota hai.
Q: repaint() kya karta hai?
→ Screen ko dobara draw karta hai — paint() method call hota hai.
Q: fillOval() kya karta hai?
→ Filled circle/oval draw karta hai.
SLIP 7 • Question 1
3 threads — random number: even=square, odd=cube
■ Teen alag threads banate hain: (1) Random number generate karta hai, (2) Even number ka square nikalta hai, (3)
Odd number ka cube nikalta hai.
■ Complete Code
// Thread 1: Even number ka square
class EvenNum implements Runnable {
int a;
EvenNum(int a) { this.a = a; }
public void run() {
[Link](a + ' is EVEN, Square = ' + a*a);
// Thread 2: Odd number ka cube
class OddNum implements Runnable {
int a;
OddNum(int a) { this.a = a; }
public void run() {
[Link](a + ' is ODD, Cube = ' + a*a*a);
// Thread 3: Random number generate karo
class RandomNumGenerator extends Thread {
public void run() {
Random rand = new Random();
for(int i=0; i<10; i++) {
int n = [Link](20); // 0-19 mein random number
[Link]('Generated: ' + n);
if(n % 2 == 0) // even check
new Thread(new EvenNum(n)).start();
else
new Thread(new OddNum(n)).start();
[Link](1000); // 1 second wait
// Main
public class MultiThreadRandOddEven {
public static void main(String[] args) {
new RandomNumGenerator().start();
■■ Viva Questions
Q: Random class kahan se aati hai?
→ [Link] — nextInt(20) se 0 to 19 mein random number milta hai.
Q: n % 2 == 0 kya check karta hai?
→ Number even hai ya nahi — remainder 0 hone ka matlab even.
Q: new Thread(new EvenNum(n)).start() kya karta hai?
→ EvenNum ka Runnable object banao, Thread mein wrap karo, start karo.
Q: Is program mein kitne threads hain?
→ 1 main thread + 1 RandomNumGenerator + 10 EvenNum/OddNum threads = 12 max.
SLIP 8 • Question 1
3 threads — COVID19 (10x), LOCKDOWN2020 (20x), VACCINATED (30x)
■ Ek hi class se teen alag threads banate hain — constructor se text aur count pass karte hain.
■ Complete Code
class A1 extends Thread {
String text;
int count;
A1(String text, int count) { // constructor — text aur count lo
[Link] = text;
[Link] = count;
public void run() {
for(int i=1; i<=count; i++) { // count baar print karo
[Link](text);
public class Slip8 {
public static void main(String[] args) {
A1 t1 = new A1('COVID19', 10);
A1 t2 = new A1('LOCKDOWN2020', 20);
A1 t3 = new A1('VACCINATED2021', 30);
[Link](); // teen threads simultaneously chalenge
[Link]();
[Link]();
■ Trick: Teeno threads simultaneously run hoti hain — output mixed aayega! Yeh multithreading ka concept hai.
■■ Viva Questions
Q: Thread constructor mein data kaise pass kiya?
→ A1(String text, int count) — custom constructor se text aur count pass kiya.
Q: Teeno threads simultaneously run hoti hain?
→ Haan! [Link](), [Link](), [Link]() — teeno concurrently run hongi.
Q: Output predictable hoga?
→ Nahi — threads ka order OS decide karta hai, isliye output har baar alag ho sakta hai.
Q: [Link] = text kyon likhte hain?
→ '[Link]' = instance variable, 'text' = parameter — dono ka naam same hai isliye this lagaya.
SLIP 8 • Question 2
JSP — Prime number check karo, result red color mein
■ HTML form se number lo, JSP mein check karo prime hai ya nahi, result red color mein dikhao.
■ Key Code
// HTML form
<form action='[Link]' method='post'>
Enter number: <input type='text' name='t1'>
<input type='submit'>
</form>
// [Link]
<%
int n = [Link]([Link]('t1'));
int d = 2;
while(d < n) {
if(n % d == 0) {
[Link](n + ' is NOT Prime');
break;
else d++;
if(n == d) // loop pura hua bina break ke = prime
[Link]('' + n + ' is Prime</font>');
%>
■■ Viva Questions
Q: [Link]() kya karta hai?
→ Form se input value lo — HTML input ka 'name' attribute use hota hai.
Q: Prime number kaise check kiya?
→ 2 se n-1 tak divide karo — agar koi bhi divide kar de toh prime nahi.
Q: Font color red kaise kiya?
→ HTML tag use kiya — ya CSS color:red bhi use ho sakta.
SLIP 9 • Question 1
Ball move karo panel mein vertically using Thread
■ Thread se ball ki position change karte hain aur repaint() se screen update karte hain — animation effect aata hai.
■ Complete Code
class boucingthread extends JFrame implements Runnable {
Thread t;
int x, y; // ball ki position
boucingthread() {
t = new Thread(this);
x = 10; y = 10; // starting position
[Link]();
setSize(1000, 200);
setVisible(true);
public void run() {
try {
while(true) { // hamesha chalta rahe
x += 10; // x badhao
y += 10; // y badhao (vertically move)
repaint(); // screen update karo
[Link](1000); // 1 sec wait
} catch(Exception e) {}
public void paint(Graphics g) {
[Link](x, y, 7, 7); // ball draw karo
■■ Viva Questions
Q: repaint() kya karta hai?
→ paint() method ko dobara call karta hai — screen refresh hoti hai.
Q: drawOval() kya karta hai?
→ Circle ya oval draw karta hai — (x, y, width, height) parameters.
Q: while(true) kyon use kiya?
→ Ball continuously move karte rahe — infinite loop.
SLIP 10 • Question 1
Current Date display karo (SimpleDateFormat)
■ Code
import [Link].*;
import [Link].*;
public class GFG {
public static void main(String args[]) {
SimpleDateFormat formatDate = new SimpleDateFormat('dd/MM/yyyy HH:mm:ss z');
Date date = new Date(); // current date/time
[Link]([Link]('IST')); // Indian time
[Link]([Link](date)); // print karo
■ Format Codes Yaad Rakho
Code Meaning Example
dd Day 28
MM Month 03
yyyy Year 2025
HH Hour (24h) 14
mm Minutes 30
ss Seconds 45
■■ Viva Questions
Q: SimpleDateFormat kya karta hai?
→ Date ko specified format mein convert/display karta hai.
Q: new Date() kya return karta hai?
→ Current date aur time return karta hai.
Q: IST kya hai?
→ Indian Standard Time — GMT+5:30
SLIP 12 • Question 1
JSP — Perfect number check karo (Include directive)
■ Perfect number = jiske divisors ka sum us number ke barabar ho. Example: 6 = 1+2+3 = 6 ✓
■ Key Code
// [Link]
<%
Integer num = [Link]([Link]('num'));
int a = num;
int sum = 0;
for(int i=1; i<a; i++) { // 1 se a-1 tak
if(a % i == 0) // agar divisor hai
sum = sum + i; // sum mein add karo
if(sum == a) // sum == number = perfect!
[Link](num + ' is a PERFECT number');
else
[Link](num + ' is NOT a perfect number');
%>
■ Examples: 6 (1+2+3=6✓), 28 (1+2+4+7+14=28✓), 496 — ye teeno perfect numbers hain!
■■ Viva Questions
Q: Perfect number kya hota hai?
→ Jiske proper divisors ka sum us number ke equal ho. E.g. 6: 1+2+3=6
Q: Include directive kya karta hai?
→ <%@ include file='[Link]' %> — doosri file ko include karta hai compile time pe.
Q: Include directive aur include action mein fark?
→ Directive: compile time include. Action: runtime pe include.
SLIP 13 • Question 2
Thread lifecycle — creation, sleep, dead
■ Thread ka life cycle: New → Runnable → Running → Sleeping → Dead. Is program mein random sleep time ke
saath lifecycle demonstrate karte hain.
■ Key Code
class MyThread extends Thread {
public MyThread(String s) {
super(s); // thread ka naam set karo
public void run() {
[Link](getName() + ' thread created.');
while(true) {
int s = (int)([Link]() * 5000); // 0-4999 random sleep
[Link](getName() + ' sleeping for: ' + s + 'ms');
try { [Link](s); }
catch(Exception e) {}
// Main
MyThread t1 = new MyThread('Shradha');
MyThread t2 = new MyThread('Pooja');
[Link](); [Link]();
[Link](); // t1 khatam hone ka wait karo
[Link](); // t2 khatam hone ka wait karo
[Link]([Link]() + ' thread dead.');
■ Thread States
State Kab?
New new Thread() — abhi start nahi hua
Runnable start() call ke baad — run hone ke liye ready
Running CPU mein actually chal raha hai
Blocked/Sleeping sleep() ya wait() call ke baad
Dead run() method khatam ho gaya
■■ Viva Questions
Q: Thread ki states kaunsi hain?
→ New, Runnable, Running, Blocked/Sleeping, Dead — 5 states.
Q: join() kya karta hai?
→ Current thread tab tak wait karta hai jab tak called thread khatam na ho jaye.
Q: [Link]() kya return karta hai?
→ 0.0 se 1.0 ke beech random double. *5000 karne se 0-4999 milta hai.
Q: super(s) kyon likha?
→ Thread class ke constructor ko call kiya — thread ka naam set karne ke liye.
SLIP 14 • Question 1
Search engine — text files mein string dhundho (har file ke liye alag thread)
■ Folder ke saare .txt files mein ek string search karte hain. Har file ke liye alag thread banate hain — parallel search!
■ Key Code
class Mythread extends Thread {
String str, filename;
Mythread(String str, String filename) {
[Link] = str; [Link] = filename;
public void run() {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = '';
while((line = [Link]()) != null) { // line by line padho
if([Link](str)) { // string mili?
[Link]('Found in: ' + filename);
break;
// Main — folder ke saare txt files scan karo
File d = new File('thread'); // folder
String[] s = [Link](); // saari files ki list
for(int i=0; i<[Link]; i++) {
if(s[i].endsWith('.txt')) { // sirf .txt files
Mythread t = new Mythread(str, dirname+'/'+s[i]);
[Link](); // har file ke liye alag thread
■■ Viva Questions
Q: Har file ke liye alag thread kyon?
→ Parallel search hogi — sab files ek saath search hongi, fast hoga.
Q: [Link]() kya karta hai?
→ String mein substring exist karti hai ya nahi — true/false.
Q: BufferedReader kya hai?
→ File efficiently padhne ke liye — line by line read karta hai.
Q: [Link]() kya return karta hai?
→ Directory mein saari files aur folders ke naam ka String array.
SLIP 15 • Question 1
Thread ka naam aur priority display karo
■ Complete Code
class Slip15_1 {
public static void main(String a[]) {
Thread t = [Link](); // current thread lo
String S = [Link](); // naam lo
[Link]('Thread name: ' + S); // print: 'main'
int p = [Link](); // priority lo
[Link]('Priority: ' + p); // print: 5 (default)
[Link]('My Thread'); // naam badlo
[Link]('New name: ' + [Link]());
[Link](2); // priority badlo
[Link]('New Priority: ' + [Link]());
■ Thread Priority
MIN_PRIORITY = 1 | NORM_PRIORITY = 5 (default) | MAX_PRIORITY = 10
■■ Viva Questions
Q: [Link]() kya return karta hai?
→ Currently running thread ka reference return karta hai.
Q: Default thread priority kya hoti hai?
→ 5 — NORM_PRIORITY. Range 1 (MIN) se 10 (MAX) tak.
Q: setPriority() se kya hota hai?
→ Thread ki priority change hoti hai — high priority thread pehle run hone ke chances zyada.
Q: Default main thread ka naam kya hota hai?
→ 'main'
SLIP 15 • Question 2
Servlet — page visit counter (Cookie use karke)
■ Cookie = browser mein store hone wala small data. Pehli baar aao toh welcome, dobara aao toh count dikhao.
■ Key Code
public void doGet(HttpServletRequest req, HttpServletResponse res) {
Cookie ca[] = [Link](); // browser se cookies lo
if(ca == null) { // pehli visit — cookie nahi hai
[Link]('First Visit — Welcome!');
Cookie visit = new Cookie('vcnt', '1'); // cookie banao
[Link](24 * 3600); // 24 ghante tak valid
[Link](visit); // browser ko bhejo
else { // dobara visit — cookie hai
int counter = [Link](ca[0].getValue()); // count lo
counter++; // badhao
[Link](counter + ' Visit');
ca[0].setValue([Link](counter)); // update karo
[Link](ca[0]); // wapis bhejo
■■ Viva Questions
Q: Cookie kya hoti hai?
→ Browser mein store hone wala small data — server client ki information yaad rakh sakta hai.
Q: [Link]() null kab return karta hai?
→ Jab browser mein koi cookie store nahi hai — pehli visit.
Q: setMaxAge() kya karta hai?
→ Cookie ki expiry set karta hai — seconds mein. 24*3600 = 1 din.
Q: Cookie aur Session mein kya fark?
→ Cookie: browser mein store hota hai. Session: server pe store hota hai — zyada secure.
Quick Reference — Exam Ke Liye
Sabse Important Concepts Ek Jagah
■ Threads — Must Know
Concept Code / Answer
Thread create (Method 1) class X extends Thread { void run(){...} } → X t=new X(); [Link]();
Thread create (Method 2) class X implements Runnable { void run(){...} } → new Thread(new X()).
Sleep [Link](2000); // 2 seconds
Thread naam [Link]() / [Link]('name')
Priority [Link]() / [Link](5)
Join [Link](); // us thread ke khatam hone ka wait karo
States New → Runnable → Running → Sleeping → Dead
■ Collections — Kab Kaunsa Use Karein
Collection Use Karo Jab Key Feature
HashSet No duplicates chahiye, order matter nahi Fast, unordered, unique
TreeSet No duplicates + sorted chahiye Auto-sorted, unique
LinkedList Fast insert/delete chahiye Doubly linked, ordered
Hashtable Key-Value pair store karna hai No nulls, synchronized
ArrayList Index se access karna hai Fast get(), duplicates ok
■ JSP Tags — Ek Nazar Mein
Tag Naam Use
<% %> Scriptlet Java code likhne ke liye
<%= %> Expression Value print karne ke liye
<%! %> Declaration Variable/method declare karna
<%@ page import='...' %> Directive Java classes import karna
<%@ include file='...' %> Include Directive Doosri file include karna
■■ JDBC Steps — Hamesha Yaad Rakho
Step Code
1. Driver Load [Link]('[Link]');
2. Connection Connection con = [Link](url, user, pwd);
3. Statement Statement st = [Link]();
4. Query Fire ResultSet rs = [Link]('SELECT ...');
5. Result Read while([Link]()) { [Link]('col'); }
6. Close [Link](); [Link](); [Link]();
All the best Pryu! ■ Tum kar sakti ho! ■