Module 4 Pyq
Module 4 Pyq
Sure! Let’s break this down into easy parts so you can understand and
remember it well.
3. ArrayList vs LinkedList
Feature ArrayList LinkedList
// Remove an element
fruits.remove("Banana"); // Removes first "Banana"
// Add elements
numbers.add(10);
numbers.add(20);
numbers.add(30);
Let me know if you want a table with methods and meanings or a practice
question!
Sure! Let’s break down both your questions into clear, easy-to-study sections:
1. sleep() Method
• Belongs to the Thread class.
• Makes the thread pause for a specified time (in milliseconds).
• Does not release the lock if the thread is holding one.
Syntax:
Thread.sleep(milliseconds);
Example:
try {
System.out.println("Sleeping for 2 seconds...");
Thread.sleep(2000); // 2000 ms = 2 seconds
System.out.println("Awake!");
} catch (InterruptedException e) {
e.printStackTrace();
}
2. wait() Method
• Defined in the Object class (not Thread).
• Causes the thread to wait until another thread calls notify() or notifyAll() on
the same object.
• Releases the lock during waiting.
Syntax:
synchronized (object) {
object.wait();
}
Example:
Used in thread communication – not usually shown alone.
Summary Table
Release Status
Method Class Usefulness
s Lock
Threa Pause Safe
sleep() No
d thread
Thread
wait() Object Yes coordinatio Safe
n
Release Status
Method Class Usefulness
s Lock
Draw and Explain the lifecycle of a thread showing the different states and
methods invoked.
Sure! Here's a clear explanation and diagram of the Thread Lifecycle in Java,
broken into parts for easy studying.
1. New State
• Thread is created but not started yet.
• Code:
• Thread t = new Thread();
2. Runnable State
• Thread is ready to run, waiting for CPU time.
• It may be picked anytime by the thread scheduler.
• Code:
• t.start();
3. Running State
• Thread is currently executing.
• Only one thread runs at a time on a single core.
• Comes here from Runnable when scheduler picks it.
Would you like me to generate a neat diagram image for this thread lifecycle?
Sure! Here's a clear explanation and sample code for each of the String
methods you mentioned in Java. I've divided it into parts so it's easy to study.
Replaces
replace() characters or "fun".replace("f", "r") → "run"
substrings
Compares two
equals() strings (case- "Hi".equals("hi") → false
sensitive)
Converts to "HELLO".toLowerCase() →
toLowerCase() "hello"
lowercase
Let me know if you'd like all these in a combined runnable Java file!
Here's a simple Java program that counts the occurrence of a particular word
within a sentence using String handling methods like split(), equals(), and
toLowerCase():
// Input sentence
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
int count = 0;
System.out.println("The word '" + word + "' occurs " + count + " time(s).");
sc.close();
}
}
Sample Output:
Enter a sentence: Java is easy. Java is powerful. Java is everywhere.
Enter the word to count: Java
The word 'Java' occurs 3 time(s).
Describe the different character extraction methods of String class along with
their syntax. 3 8 Write notes on Array List in java.
Sure! Here's a concise 3-mark answer for each question as per typical exam
expectations:
Let me know if you want these formatted for handwritten notes or flashcards!
Write short note on any four methods of String class.
Sure! Here's a short note on four commonly used methods of the String
class in Java, suitable for a 3-mark answer:
2. charAt(int index)
• Returns the character at the specified index (0-based).
• Syntax: str.charAt(2);
• Example: "Java".charAt(1) → 'a'
4. toLowerCase()
• Converts the entire string to lowercase.
• Syntax: str.toLowerCase();
• Example: "JAVA".toLowerCase() → "java"
2. String(String original)
• Purpose: Creates a string that is a copy of the given string.
• Syntax:
• String str = new String("Hello");
• Example:
• String str = new String("Hello");
• System.out.println(str); // Output: Hello
3. String(char[] value)
• Purpose: Creates a string from an array of characters.
• Syntax:
• String str = new String(char[] value);
• Example:
• char[] chars = {'J', 'a', 'v', 'a'};
• String str = new String(chars);
• System.out.println(str); // Output: Java