Mahendra Educational Institutions
Name: CHARULATHA R Scan to verify results
Email: r.charulatha28012007@[Link]
Roll no: null
Phone: null
Branch: MIT
Department: AIML - A
Batch: 2028
Degree: B.E
2028_IV_JAVA Programming_AIML A
MEI_2029_Java_Week 6_COD
Attempt : 1
Total Mark : 50
Marks Obtained : 50
Section 1 : COD
1. Problem Statement
Sabrina is working on a project that involves analyzing a set of numbers. In
her exploration, she encounters scenarios where extracting even numbers
and finding their sum is essential.
Create a program that calculates the sum of even numbers from a given
array of integers using a lambda expression.
Input Format
The first line of input consists of an integer N, representing the size of the array.
The second line consists of N space-separated integers, representing the
elements of the array.
Output Format
The output prints the sum of the even integers from the array.
Refer to the sample output for formatting specifications.
Sample Test Case
Input: 3
29 37 45
Output: 0
Answer
import [Link].*;
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = [Link]();
}
// Lambda with stream to filter even numbers and find sum
int sum = [Link](arr)
.filter(x -> x % 2 == 0)
.sum();
[Link](sum);
}
}// You are using Java
Status : Correct Marks : 10/10
2. Problem Statement
In the mystical realm of programming, there exists a magical incantation to
reveal hidden words.
Elara, the skilled enchantress, wishes to summon a word using her spell
and then reverse its characters to uncover its enchanted reflection.
Write a program that uses the predefined functional interface
Supplier<String> and a lambda expression to:
Supply (generate) a string, and Display its reversed form.
Input Format
The input consist of single supplies string.
Output Format
The output prints the reversed version of the supplied string.
Refer to the sample output for formatting specifications.
Sample Test Case
Input: Wizard!!
Output: !!draziW
Answer
import [Link].*;
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String input = [Link]();
// Supplier using lambda expression
Supplier<String> supplier = () -> input;
String original = [Link]();
// Reverse the string
String reversed = new StringBuilder(original).reverse().toString();
[Link](reversed);
}
}// You are using Java
Status : Correct Marks : 10/10
3. Problem Statement
Problem Statement
Sophia, a data analyst, is studying experimental results collected from
various lab sensors. Each sensor provides a list of numeric readings, and
Sophia wants to calculate the average of these readings to analyze
consistency.
She decides to use lambda expressions and the Function functional
interface to compute the average of all the recorded values efficiently.
Your Task
Write a Java program that:
Reads the total number of [Link] all the measurement
values as [Link] a Function<double[], Double> lambda expression
to calculate the average [Link] the final average, formatted to two
decimal places.
Input Format
The first line of input consists of an integer N, representing the number of
measurements.
The second line contains N space-separated double values.
Output Format
Print the average of the entered values, rounded to two decimal places.
Refer to the sample output for formatting specifications.
Sample Test Case
Input: 6
2.2 1.2 5.4 4.6 2.9 55.7
Output: 12.00
Answer
import [Link].*;
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
double[] arr = new double[n];
for(int i = 0; i < n; i++) {
arr[i] = [Link]();
}
// Lambda expression to calculate average
Function<double[], Double> avgFunction = (a) -> {
double sum = 0;
for(double val : a) {
sum += val;
}
return sum / [Link];
};
double result = [Link](arr);
// Print with 2 decimal places
[Link]("%.2f", result);
}
}// You are using Java
Status : Correct Marks : 10/10
4. Problem Statement
Alex is learning about Java’s functional interfaces and lambda expressions.
He wants to write a simple program that prints the square of each number
in an array using a predefined functional interface.
Help Alex complete this task using the Consumer functional interface.
Input Format
- The first line contains an integer N, the number of elements in the array.
- The second line contains N space-separated integers.
Output Format
- Print the squares of all elements in the array, separated by a space.
Refer to the sample output for formatting specifications.
Sample Test Case
Input: 4
1234
Output: 1 4 9 16
Answer
import [Link].*;
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = [Link]();
}
// Consumer lambda to print square of each number
Consumer<Integer> printSquare = (x) -> [Link]((x * x) + " ");
for(int num : arr) {
[Link](num);
}
}
}// You are using Java
Status : Correct Marks : 10/10
5. Problem Statement
Abi is working on a text analysis project where she needs to categorize
words based on their length. Words that have three or fewer characters are
considered “Short”, while words with more than three characters are
classified as “Long.”
Write a Java program that takes a sentence as input, analyzes each word,
and prints a list showing whether each word is “Short” or “Long.”
Use the predefined functional interface Function<String, String> along with
a lambda expression for categorization.
Input Format
A single line containing a sentence (words separated by spaces).
Output Format
- A single line with each word categorized as "Short" or "Long", separated by
spaces.
Refer to the sample output for formatting specifications.
Sample Test Case
Input: I love my cat
Output: Short Long Short Short
Answer
import [Link].*;
import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String sentence = [Link]();
String[] words = [Link](" ");
// Lambda to categorize words
Function<String, String> categorize = (word) -> {
if([Link]() <= 3) {
return "Short";
} else {
return "Long";
}
};
for(String word : words) {
[Link]([Link](word) + " ");
}
}
}// You are using Java
Status : Correct Marks : 10/10