0% found this document useful (0 votes)
4 views7 pages

Object Oriented Programming Tutorial 5

The document contains a tutorial for the Object Oriented Programming course at BITS Pilani, Dubai Campus for the first semester of 2025-2026. It includes various programming exercises with Java code snippets, focusing on concepts such as String manipulation, ArrayLists, LinkedLists, and iterators. Additionally, it outlines tasks for students to implement specific functionalities related to managing lists and processing strings.

Uploaded by

f20240278
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)
4 views7 pages

Object Oriented Programming Tutorial 5

The document contains a tutorial for the Object Oriented Programming course at BITS Pilani, Dubai Campus for the first semester of 2025-2026. It includes various programming exercises with Java code snippets, focusing on concepts such as String manipulation, ArrayLists, LinkedLists, and iterators. Additionally, it outlines tasks for students to implement specific functionalities related to managing lists and processing strings.

Uploaded by

f20240278
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

BITS PILANI, DUBAI CAMPUS

DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI

FIRST SEMESTER 2025 – 2026


COURSE: CSF213/ECOM213/MACF212 (Object Oriented Programming)
COMPONENT: Tutorial 5

1.
public class Test1 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abc");
[Link]();
[Link]("123");
[Link](2, "XYZ");
[Link](1, 4);
[Link](sb);
}
}

A. aYXZcb123
B. aZcb123
C. a123
D. aYZcb123

2.
public class Test2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
[Link]("Hello");
[Link]("World");
[Link]("Capacity: " + [Link]());
}
}

A. Capacity: 16
B. Capacity: 10
C. Capacity: 21
D. Capacity: 27

3.
import [Link];

public class Test3 {


public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("Java,,Python, ,C++", ",");
int count = 0;
while ([Link]()) {
[Link]("Token: " + [Link]());
count++;
}
[Link]("Total: " + count);
}
}

1
A. 3
B. 4
C. 5
D. 6

4.
import [Link];

public class Test4 {


public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("Hello World from Java");
while ([Link]()) {
[Link]([Link]() + "-");
}
}
}

A. Hello-World-from-Java-
B. Hello-World-from-
C. HelloWorldfromJava
D. Hello-World-from-Java

5.
import [Link];

public class Test5 {


public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("A");
[Link]("B");
[Link](1, "C");
[Link]("B");
[Link](list);
}
}

A. [A, C]
B. [A, B, C]
C. [A, C, B]
D. [A, B]

6.
import [Link];

public class Test6 {


public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(2);
for (int i = 0; i < 10; i++) {
[Link](i);
}
[Link]("Size: " + [Link]());
}
}

A. Size: 10
B. Size: 2
2
C. Size: 8
D. Compile-time error

7.
import [Link];

public class Test7 {


public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
[Link]("First");
[Link]("Second");
[Link]("Zeroth");
[Link]();
[Link](list);
}
}

A. [Zeroth, First, Second]


B. [Zeroth, First]
C. [First, Second]
D. [First]

8.
import [Link];

public class Test8 {


public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
[Link](1);
[Link](2);
[Link](3);
[Link](1, 10);
[Link](2, 20);
[Link](list);
}
}

A. [1, 10, 20, 3]


B. [1, 2, 10, 20]
C. [1, 2, 20, 3]
D. [1, 10, 3, 20]

9.
import [Link].*;

public class Test9 {


public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>([Link]("A", "B", "C", "D"));
Iterator<String> it = [Link]();
while ([Link]()) {
String val = [Link]();
if ([Link]("C")) [Link]();
}
[Link](list);
}
}

3
A. [A, B, C, D]
B. [A, B, D]
C. [A, C, D]
D. [B, C, D]

10.
import [Link].*;

public class Test10 {


public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>([Link]("X", "Y", "Z"));
for (String s : list) {
if ([Link]("Y")) {
[Link](s);
}
}
[Link](list);
}
}

A. [X, Z]
B. [X, Y, Z]
C. Runtime Exception
D. Compile-time Error

11.
import [Link].*;

public class Test11 {


public static void main(String[] args) {
List<String> list = new ArrayList<>([Link]("One", "Two", "Three"));
ListIterator<String> it = [Link]([Link]());
while ([Link]()) {
[Link]([Link]() + " ");
}
}
}

A. Three Two One


B. One Two Three
C. Two Three One
D. Compile-time Error

12.
import [Link].*;

public class Test12 {


public static void main(String[] args) {
List<String> list = new LinkedList<>([Link]("A", "B"));
ListIterator<String> it = [Link]();
[Link]();
[Link]("X");
[Link](list);
}
}

4
A. [A, X, B]
B. [X, A, B]
C. [A, B, X]
D. [A, X, B, X]

13.
import [Link].*;

public class Test13 {


public static void main(String[] args) {
List<Integer> list = new ArrayList<>([Link](10, 20, 30));
ListIterator<Integer> it = [Link]();
while ([Link]()) {
int val = [Link]();
if (val == 20) {
[Link](200);
}
}
[Link](list);
}
}

A. [10, 200, 30]


B. [10, 20, 30]
C. [10, 30]
D. [200, 30]

14.
import [Link].*;

public class Test14 {


public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>([Link]("A", "B", "C"));
Iterator<String> it = [Link](); // Error?
while ([Link]()) {
[Link]([Link]() + " ");
}
}
}

A. C B A
B. A B C
C. Runtime Error
D. Compile-time Error

15.
import [Link].*;

public class Test15 {


public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>([Link]("1", "2", "3"));
ListIterator<String> it = [Link]();
while ([Link]()) {
String s = [Link]();
[Link](s + s);
}

5
[Link](list);
}
}

A. [1, 11, 2, 22, 3, 33]


B. [1, 2, 3, 11, 22, 33]
C. [11, 22, 33]
D. Infinite loop

16. You are given a single string containing student names separated by commas. Some entries may be empty or
have leading/trailing spaces. Your task is to write a method that:

• Uses StringTokenizer to split the input string by commas.


• Ignores any empty tokens or names with only spaces.
• Trims whitespace from valid names.
• Eliminates duplicate names.
• Sorts the final list of names in alphabetical order.

17. Create a class called TaskManager that maintains a list of daily tasks using an ArrayList<String>. Your
class should support the following functionalities:

1. addTask(String task) – Adds a new task. Duplicate tasks should be ignored.


2. removeTask(String task) – Removes a task if it exists.
3. displayTasks() – Displays all tasks with their index numbers in the format:
1. Buy groceries

Example Usage:
TaskManager tm = new TaskManager();
[Link]("Buy groceries");
[Link]("Call mom");
[Link]("Buy groceries"); // Duplicate, ignored
[Link]();

18. Implement a class RecentHistory to track a user's recently accessed items (e.g., web pages or files) using a
LinkedList<String>. The history should maintain a maximum of N items, with the most recently accessed
items at the front.
• If an accessed item is already in the list, move it to the front.
• If the list exceeds the maximum capacity N, remove the oldest item from the end.
Constructor:
RecentHistory(int capacity)
Methods to Implement:
void access(String item)
List<String> getHistory()

19. You are given an ArrayList<String> containing several strings. Your task is to:
• Remove all strings with a length less than 5 characters.
• Use an Iterator to perform this operation safely, without causing
ConcurrentModificationException.
Method Signature:
void removeShortStrings(ArrayList<String> list)

6
Constraints:
• You must use an Iterator to traverse and remove elements.
• Do not use for-each loops or removeIf().

20. You are given a LinkedList<Integer> containing numbers. Write a method that uses a
ListIterator to:
• Replace all even numbers with 0.
• Insert -1 immediately after every odd number.
Constraints:
• All changes must be done in-place while iterating.
• Use only one traversal of the list using ListIterator.
Function Signature:
void transformList(LinkedList<Integer> list)

You might also like