Java Week8
Java Week8
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 8_Q1
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
Write a program to validate the email address and display suitable
01
01
01
01
10
10
10
24
24
24
7
01
01
01
1
10
01
01
01
0
10
10
10
10
Get the email address from the user, validate the email by checking the
24
24
24
24
above-mentioned criteria, and print the validity status of the input email
17
7
01
01
01
address.
10
01
01
01
0
10
10
10
10
24
24
24
24
Input Format
The first line of input contains the email to be validated.
Output Format
The output prints a Valid email address or an Invalid email address along with
the suitable exception
17
17
01
01
10
0
01
01
01
Invalid email address
00
10
10
10
1
24
24
24
24
If @ appears not exactly once, it throws:
7
01
01
01
01
01
01
01
01
10
10
10
If the part after the last dot is not among accepted domains: 10
24
24
24
24
7
01
01
01
01
01
0
10
10
10
10
24
24
24
24
17
7
01
01
01
10
01
01
01
0
10
10
10
10
Refer to the sample input and output for format specifications.
24
24
24
24
Sample Test Case
Input: sample@[Link]
Output: Valid email address
Answer
import [Link];
17
17
01
01
10
0
super(message);
01
01
01
00
10
10
10
}
1
24
24
24
24
}
7
01
01
01
01
}
01
01
01
01
10
10
10
10
}
24
24
24
24
7
01
01
01
1
10
}
01
01
01
0
10
10
10
10
24
24
24
24
if([Link](".") || [Link](".") || [Link]("..") ||
17
7
01
01
01
[Link](".@") || [Link]("@.")){
10
01
01
01
0
10
10
10
}
24
24
24
24
if([Link]('.', atIndex) == -1){
throw new DotException("Invalid Dot usage");
}
17
17
01
01
throw new DomainException("Invalid Domain");
10
0
01
01
01
00
}
10
10
10
1
}
24
24
24
24
public static void main(String args[]){
Scanner scanner = new Scanner ([Link]);
String email = [Link]();
try{
validateEmail(email);
[Link]("Valid email address");
}
catch(DotException e){
7
7
01
01
01
01
[Link]([Link]().getSimpleName() + ": " + [Link]());
01
01
01
01
[Link]("Invalid email address");
10
10
10
} 10
24
24
24
24
catch(AtTheRateException e){
[Link]([Link]().getSimpleName() + ": " + [Link]());
[Link]("Invalid email address");
}
catch(DomainException e){
[Link]([Link]().getSimpleName() + ": " + [Link]());
[Link]("Invalid email address");
}
[Link]();
}
7
7
01
01
01
}
10
01
01
01
0
10
10
10
10
24
24
24
24
24 24 24 24
10 10 10 10
01 01 01 0 10
01 01 01
7 7 7 17
Status : Correct
24 24 24 24
10 10 1 00 10
01 01 10 01
01 01 01
7 7 17 7
24 24 24 24
10 10 10 10
01 01 01 01
01 01 01 01
7 7 7 7
24 24 24 24
10 10 10 10
Marks : 10/10
0 10 01 01 01
1 01 0 01
7 7 17 7
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 8_Q2
Attempt : 1
Total Mark : 10
Marks Obtained : 0
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
Elsa, a busy professional, is using a scheduling application to plan her
01
01
01
01
10
10
10
24
24
24
durations in minutes, ensuring that the duration is a positive integer and
does not exceed 240 minutes (4 hours). Elsa needs a program to assist her
in scheduling meetings securely with proper exception handling.
7
01
01
01
1
10
01
01
0
10
10
10
10
24
24
24
24
Implement a custom exception, InvalidDurationException, to handle cases
17
7
01
01
01
where the entered meeting duration does not meet the specified criteria.
10
01
01
01
0
10
10
10
10
24
24
24
24
Input Format
The input consists of an integer value 'n', representing the meeting duration.
Output Format
The output is displayed in the following format:
If the entered meeting duration meets the specified criteria, the program outputs
17
17
01
01
If the entered meeting duration is invalid, the program outputs an error message
10
0
01
01
01
indicating the issue.
00
10
10
10
1
24
24
24
24
"Error: Invalid meeting duration. Please enter a positive integer not exceeding
240 minutes (4 hours)."
7
01
01
01
01
Output: Meeting scheduled successfully!
01
01
01
01
10
10
10
Answer 10
24
24
24
24
import [Link];
7
01
01
01
1
10
01
01
10
10
10
24
24
24
24
throw new InvalidDurationException(n);
17
7
01
01
01
}
10
01
01
01
0
}
10
10
10
10
24
24
24
24
public static void main(String args[]){
Scanner scanner = new Scanner ([Link]);
int duration = [Link]();
try{
checkDuration(duration);
[Link]("Meeting scheduled successfully!");
}
catch(InvalidDurationException e){
[Link]("Error: Invalid meeting duration. Please enter a positive
integer not exceeding 240 minutes (4 hours).");
7
17
17
01
01
}
10
0
01
01
01
00
10
10
10
1
[Link]();
24
24
24
24
}
}
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 8_Q3
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
In a user registration system, there is a requirement to implement a
01
01
01
01
10
10
10
24
24
24
specific criteria for their usernames to be considered valid.
Your task is to develop a program that takes user input for a desired
username and validates it according to the following rules:
The username must not contain any [Link] username must be at
least 5 characters long.
Implement a custom exception, InvalidUsernameException, to handle
cases where the entered username does not meet the specified criteria.
7
7
01
01
01
1
10
Input Format
01
01
01
0
10
10
10
10
24
24
24
24
The input consists of a string S, representing the desired username.
17
7
01
01
01
10
01
01
01
Output Format
0
10
10
10
10
24
24
24
24
If the username is valid, print "Username is valid: [S]".
17
17
01
01
10
0
01
01
01
00
10
10
1
24
24
24
24
Sample Test Case
Input: John
Output: Invalid Username: Username must be at least 5 characters long
Answer
import [Link];
7
super(message);
01
01
01
01
}
01
01
01
01
10
10
10
10
}
24
24
24
24
class UsernameValidator{
}
01
01
01
1
10
if([Link]()<MIN_LENGTH){
01
01
01
0
10
10
10
10
24
24
24
MIN_LENGTH + " characters long");
17
7
01
01
01
}
10
01
01
01
0
}
10
10
10
10
24
24
24
24
public static void main(String args[]){
Scanner scanner = new Scanner ([Link]);
String username = [Link]();
try{
validateUsername(username);
[Link]("Username is valid: " +username);
}
catch( InvalidUsernameException e){
[Link]("Invalid Username: " +[Link]());
}
7
17
17
01
01
finally{
10
0
01
01
01
00
[Link]();
10
10
10
1
}
24
24
24
24
}
}
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 8_Q4
Attempt : 1
Total Mark : 10
Marks Obtained : 2.5
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
A local municipality is implementing an online voting system for a
01
01
01
01
10
10
10
community event and wants to ensure that only eligible voters (those aged 10
24
24
24
24
18 or older) can participate.
Input Format
7
01
01
1
10
01
01
01
0
10
10
10
10
Output Format
24
24
24
24
If the age is 18 or older, print "Eligible to vote"
17
7
01
01
01
10
01
01
01
0
If the age is below 18, print "Exception occurred: InvalidAgeException: Age is not
10
10
10
10
valid to vote"
24
24
24
24
If there is any other type of exception, print "An error occurred: " followed by the
in-built exception message.
17
17
01
01
10
0
Input: 20
01
01
01
00
10
10
10
Output: Eligible to vote
1
24
24
24
24
Answer
import [Link];
import [Link];
7
01
01
01
01
class VotingValidator{
01
01
01
01
10
10
10
10
public static void validateAge(int age) throws InvalidAgeException{
24
24
24
24
if(age < 18){
throw new InvalidAgeException ("Age is not valid to vote");
}
}
try{
age = [Link]();
7
7
01
01
01
1
10
validateAge(age);
01
01
01
0
10
10
10
10
[Link]("Eligible to vote");
24
24
24
24
}
17
7
01
01
01
10
01
01
01
0
catch(InvalidAgeException e){
10
10
10
10
[Link]("Exception occured: InvalidAgeException: " +
24
24
24
24
[Link]());
}
catch(Exception e){
[Link]("An error occured: " +[Link]());
}
finally{
[Link]();
}
7
17
17
01
01
}
10
0
01
01
01
00
}
10
10
10
1
24
24
24
24
Status : Partially correct Marks : 2.5/10
7
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 8_Q5
Attempt : 1
Total Mark : 10
Marks Obtained : 4.5
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
In a file management system, users are required to provide a valid file
01
01
01
01
10
10
10
name when creating new files. The system enforces specific rules for file 10
24
24
24
24
names to maintain consistency and avoid potential issues. Your task is to
implement a Java program named FileNameValidator that takes user input
for a file name and validates it according to the specified rules.
7
01
01
01
1
10
01
01
01
0
10
10
10
10
Input Format
24
24
24
24
The input consists of a string S, representing the desired filename.
17
7
01
01
01
10
01
01
01
Output Format
0
10
10
10
10
24
24
24
24
The output is displayed in the following format:
If the entered file name meets the specified criteria, the program outputs
If the entered file name does not meet the criteria and triggers the
InvalidFileNameException, the program outputs
"Error: Invalid file name. It must be alphanumeric and have a minimum length of
3 characters."
7
17
17
01
01
10
0
01
01
01
00
10
10
10
1
24
24
24
24
Refer to the sample output for formatting specifications.
Sample Test Case
Input: myfile123
Output: Valid file name
Answer
import [Link];
7
7
01
01
01
01
class InvalidFileNameException extends Exception{
01
01
01
01
10
10
10
10
public InvalidFileNameException(String message){
24
24
24
24
super(message);
}
}
class FileNameValidator{
01
01
1
10
01
01
0
10
10
10
10
}
24
24
24
24
17
7
01
01
01
for(char c : [Link]()){
10
01
01
01
0
if(){
10
10
10
10
throw new InvalidFileNameException("File name contains invalid
24
24
24
24
characters.");
}
}
}
try{
7
17
17
01
01
validateFileName(fName);
10
0
01
01
01
00
10
10
1
}
24
24
24
24
catch(InvalidFileNameException e){
[Link]("Error: Invalid file name. It must be alphanumerical and
have a minimum length of 3 characters.");
}
finally{
[Link]();
}
}
7
7
01
01
01
01
}
01
01
01
01
10
10
10
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
REC_2028_OOPS using Java_Week 8_MCQ
Attempt : 1
Total Mark : 15
Marks Obtained : 15
Section 1 : MCQ
7
01
01
01
01
Answer
01
01
01
01
10
10
10
By extending RuntimeException 10
24
24
24
24
7
01
01
01
}
10
01
01
01
0
10
10
10
10
24
24
24
24
class Test {
17
7
01
01
01
public static void main(String[] args) {
10
01
01
01
0
try {
10
10
10
10
24
24
24
24
throw new MyException();
} catch (MyException e) {
[Link]([Link]());
}
}
}
Answer
Default Exception Message
Status : Correct Marks : 1/1
7
17
17
01
01
10
0
01
01
01
00
10
10
10
1
24
24
24
24
3. What will be the output for the following code?
import [Link].*;
7
01
01
01
01
class Test {
01
01
01
01
public static void main(String[] args) {
10
10
10
10
24
24
24
24
try {
int age = 17;
if (age < 18) {
throw new UnderageException("Underage, cannot proceed");
}
} catch (UnderageException e) {
[Link]([Link]());
}
}
}
7
7
01
01
01
1
10
Answer
01
01
01
0
10
10
10
10
24
24
24
24
Underage, cannot proceed
17
7
01
01
01
10
01
01
01
Status : Correct Marks : 1/1
0
10
10
10
10
24
24
24
24
4. What will be the output for the following code?
class Test {
7
17
17
01
01
10
0
public static void main(String[] args) {
01
01
01
00
10
10
10
try {
1
24
24
24
24
double balance = -500;
if (balance < 0) {
throw new NegativeBalanceException("Balance cannot be
negative");
}
} catch (NegativeBalanceException e) {
[Link]("Error: " + [Link]());
}
}
}
7
7
01
01
01
01
01
01
01
01
Answer
10
10
10
10
24
24
24
24
Error: Balance cannot be negative
Status : Correct Marks : 1/1
Answer
To create user-defined exceptions for specific scenarios
Status : Correct Marks : 1/1
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
6. What will be the output for the following code?
17
7
01
01
01
10
01
01
01
0
10
10
10
24
24
24
24
public InvalidUsernameException(String message) {
super(message);
}
}
class Test {
public static void main(String[] args) {
try {
String username = "abc";
if ([Link]() < 5) {
7
17
17
01
01
throw new InvalidUsernameException("Username must be at
10
0
01
01
01
00
10
10
1
24
24
24
24
}
} catch (InvalidUsernameException e) {
[Link]([Link]());
}
}
}
Answer
Username must be at least 5 characters long
Status : Correct Marks : 1/1
7
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7. Which keyword is used to explicitly throw a custom exception?
Answer
throw
Status : Correct Marks : 1/1
Answer
01
01
01
1
10
01
01
01
10
10
10
24
24
24
24
Status : Correct Marks : 1/1
17
7
01
01
01
10
01
01
01
0
10
10
10
10
9. What will be the output for the following code?
24
24
24
24
import [Link].*;
17
17
01
01
class Test {
10
0
01
01
01
public static void main(String[] args) {
00
10
10
10
1
try {
24
24
24
24
int stock = 0;
if (stock == 0) {
throw new OutOfStockException("Item is out of stock");
}
} catch (OutOfStockException e) {
[Link]([Link]());
}
}
}
7
7
01
01
01
01
Answer
01
01
01
01
10
10
10
24
24
24
import [Link].*;
super(message);
01
01
01
1
10
01
01
01
}
0
10
10
10
10
24
24
24
24
}
17
7
01
01
01
10
01
01
01
0
class Test {
10
10
10
10
24
24
24
24
public static void main(String[] args) {
try {
int age = -5;
if (age < 0) {
throw new NegativeAgeException("Age cannot be negative");
}
} catch (NegativeAgeException e) {
[Link]([Link]());
}
}
7
17
17
01
01
}
10
0
01
01
01
00
10
10
10
Answer
1
24
24
24
24
Age cannot be negative
Status : Correct Marks : 1/1
7
01
01
01
01
}
01
01
01
01
}
10
10
10
10
24
24
24
24
class Test {
public static void main(String[] args) {
try {
int age = 15;
if (age < 18) {
throw new InvalidVotingAgeException("You are not eligible to
vote");
}
[Link]("Eligible to vote");
7
7
01
01
01
} catch (InvalidVotingAgeException e) {
10
01
01
01
[Link]([Link]());
10
10
10
10
24
24
24
24
}
17
7
01
01
01
}
10
01
01
01
0
}
10
10
10
10
24
24
24
24
Answer
You are not eligible to vote
Status : Correct Marks : 1/1
import [Link].*;
7
17
17
01
01
class TemperatureTooHighException extends Exception {
10
0
01
01
01
00
10
10
1
24
24
24
24
super(message);
}
}
class Test {
public static void main(String[] args) {
try {
int temperature = 110;
if (temperature > 100) {
throw new TemperatureTooHighException("Temperature too
7
7
01
01
01
01
high");
01
01
01
01
}
10
10
10
10
24
24
24
24
} catch (TemperatureTooHighException e) {
[Link]([Link]());
}
}
}
Answer
Temperature too high
Status : Correct Marks : 1/1
7
7
01
01
01
1
10
01
01
01
10
10
10
24
24
24
24
17
7
class MyException extends Exception {
01
01
01
10
01
01
01
public MyException(String message) {
0
10
10
10
10
super(message);
24
24
24
24
}
}
class Test {
public static void main(String[] args) {
try {
throw new MyException("Error occurred");
} catch (MyException e) {
[Link](e);
7
17
17
}
01
01
10
0
01
01
01
}
00
10
10
10
}
1
24
24
24
24
Answer
MyException: Error occurred
Status : Correct Marks : 1/1
7
Answer
01
01
01
01
01
01
01
01
Compilation Error
10
10
10
10
24
24
24
24
Status : Correct Marks : 1/1
7
01
01
01
1
10
01
01
01
class Test {
10
10
10
10
24
24
24
24
static void check() throws MyException {
17
7
01
01
01
throw new MyException("Custom Exception Occurred");
10
01
01
01
0
}
10
10
10
10
24
24
24
24
public static void main(String[] args) {
try {
check();
} catch (Exception e) {
[Link]([Link]());
}
}
}
7
17
17
Answer
01
01
10
0
01
01
01
00
10
10
1
24
24
24
24
Status : Correct Marks : 1/1
7
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24