0% found this document useful (0 votes)
5 views8 pages

Arraylist Questions

The document contains a series of programming questions focused on using ArrayLists in Java. It includes tasks such as creating a class for item types, managing guest lists for a party, and handling student registrations with specific input and output formats. Each question specifies constraints and provides sample test cases for clarity.

Uploaded by

jetblackaqua957
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)
5 views8 pages

Arraylist Questions

The document contains a series of programming questions focused on using ArrayLists in Java. It includes tasks such as creating a class for item types, managing guest lists for a party, and handling student registrations with specific input and output formats. Each question specifies constraints and provides sample test cases for clarity.

Uploaded by

jetblackaqua957
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
You are on page 1/ 8

ARRAY LIST

QUESTION 1

Single File Programming Question


Create a class ItemType with the following attributes.

Mark all of them as private and included appropriate getters/setters, also include
constructors. And override the toString method to print details of the ItemType
object.

Create a class ArrayListObjectMain and in the main method get the details of
ItemTypes and store them in an ArrayList.

All class names, attribute names, and method names should be the same as specified
in the problem statement.

Note: Use "%-20s%-20s%-20s" for formatting output in tabular format, display


double values with 1 decimal place.

Input format :
Number of items(N) in first-line as integer

Name, Deposit, and cost per day for each item in next lines as shown in sample
input

Output format :
Print the item details as shown in the sample output.

Sample test cases :


Input 1 :
3
Laptop
10000
250
Light
1000
50
Fan
1000
100
Output 1 :
Name Deposit Cost Per Day
Laptop 10000.0 250.0
Light 1000.0 50.0
Fan 1000.0

QUESTION 2

Problem Statement

Misha is planning a birthday party and wants to create a list of guests for the
party. She decided to use an ArrayList to store the names of the guests who were
invited.

She writes a program that takes input for the number of guests and then reads and
stores their names in an ArrayList. Finally, the program should display the list of
guests invited.

Input format :
The first line of input is an integer N, representing the number of guests Misha
wants to invite.

The next N lines each of input consists of a string, representing the name of a
guest.

Output format :
The output displays the names of the guests, each on a new line.

Refer to the sample output for the formatting specifications.

Code constraints :
In this scenario, the test cases will fall under the following constraints:

1 ≤ N ≤ 10

1 ≤ Length of the name ≤ 50

Sample test cases :


Input 1 :
1
Sri
Output 1 :
Sri
Input 2 :
5
Alice
Bob
Charlie
David
Eva
Output 2 :
Alice
Bob
Charlie
David
Eva
Input 3 :
10
MS Dhoni
Virat Kohli
Rohit Sharma
Kane Williamson
Steve Smith
Babar Azam
David Warner
Shakib Al Hasan
Jasprit Bumrah
Rashid Khan
Output 3 :
MS Dhoni
Virat Kohli
Rohit Sharma
Kane Williamson
Steve Smith
Babar Azam
David Warner
Shakib Al Hasan
Jasprit Bumrah
Rashid Khan

QUESTION 3

Problem Statement

Mesa, a store manager, needs a program to manage inventory items. Define a class
ItemType with private attributes for name, deposit, and cost per day. Create an
ArrayList in the Main class to store ItemType objects, allowing input and display.

Note: Use "%-20s%-20s%-20s" for formatting output in tabular format, display double
values with 1 decimal place.

Input format :
The first line of input consists of an integer n, representing the number of items.

For each of the n items, there are three lines:

The name of the item (a string)


The deposit amount (a double)
The cost per day (a double)
Output format :
The output prints a formatted table with columns for name, deposit and cost per
day.

Refer to the sample output for formatting specifications.

Code constraints :
1 ≤ n ≤ 10

Sample test cases :


Input 1 :
3
Laptop
10000.0
250.0
Light
1000.0
50.0
Fan
1000.0
100.0
Output 1 :
Name Deposit Cost Per Day
Laptop 10000.0 250.0
Light 1000.0 50.0
Fan 1000.0 100.0
Input 2 :
5
Headphones
300.0
15.0
Mouse
100.0
5.0
Desk
800.0
40.0
Printer
1500.0
70.0
Speakers
1200.0
60.0
Output 2 :
Name Deposit Cost Per Day
Headphones 300.0 15.0
Mouse 100.0 5.0
Desk 800.0 40.0
Printer 1500.0 70.0
Speakers 1200.0

QUESTION 4

Problem Statement

Assist Pranitha in developing a program that takes an integer N as input,


representing the number of names to be read. Then read N names and store them in an
ArrayList. Finally, input a search string and output the frequency of that string
in the list of names.

Input format :
The first line of input consists of an integer N, representing the number of names
to be read.

The following N lines consist of N names, as a string.

The last line consists of a string, representing the name to be searched.

Output format :
The output prints a single integer, representing the frequency of the specified
name in the given list.

If the specified name is not found, print 0.

Refer to the sample output for formatting specifications.


Code constraints :
2 ≤ N ≤ 10

The maximum length of the input string is 100.

The input strings are case-sensitive.

Sample test cases :


Input 1 :
5
Alice
Bob
Ankit
Alice
Pranitha
Alice
Output 1 :
2
Input 2 :
3
Mitchell
Sofia
Lily
Cameron
Output 2 :
0
Input 3 :
6
Benjamin
Emma
Emma
Benjamin
emma
Daniel
Emma
Output 3 :
2

QUESTION 5
Problem Statement

Raman, a computer science teacher, is responsible for registering students for his
programming class. To streamline the registration process, he wants to develop a
program that stores students' names and allows him to retrieve a student's name
based on their index in the list.
Raman has decided to use an ArrayList to store the names of students, as it
provides efficient dynamic resizing and indexing.

Write a program that enables Raman to input the names of students and fetch a
student's name using the specified index. If the entered index is invalid, the
program should return an appropriate message.

Input format :
The first line of input consists of an integer n, representing the number of
students to register.

The next n lines of input consist of the names of each student, one by one.

The last line of input is an integer, representing the index (0-indexed) of the
element to retrieve.

Output format :
If the index is valid (within the bounds of the ArrayList), print "Element at index
[index]: " followed by the element.

If the index is invalid, print "Invalid index".

Refer to the sample output for formatting specifications.

Code constraints :
The given test cases fall under the following constraints:

2 ≤ n ≤ 10

3 ≤ student's name ≤ 50

0 ≤ index < n

Sample test cases :


Input 1 :
5
Alice
Bob
Ankit
Alice
Prajit
2
Output 1 :
Element at index 2: Ankit
Input 2 :
2
Alice
Bob
2
Output 2 :
Invalid index

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy