Codekata Report 1749553424937
Codekata Report 1749553424937
Email: [email protected]
Section: Section-41
)
.in
ac
ty.
1. Reverse an Integer Problem Statement:Write a program to
rsi
ive
reverse an integer.
n
su
Description:1. Reverse the digits of an integer.2. Ignore leading zeros in the output.
tia
lgo
Input Format:A single integer n (-10^9 ≤ n ≤ 10^9).
ga
Sample Input:1234
07
18
Sample Output:4321
1
sec
4s
Concepts Included:
(n
rya
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int reversed=0;
int num=Math.abs(n);
while(num!=0){
int digit=num%10;
reversed=reversed*10+digit;
num/=10;
}
if(n<0){
reversed=-reversed;
}
System.out.println(reversed);
}
Compilation Details:
TestCase1:
Input:
)
< hidden >
.in
ac
Expected Output:
ty.
rsi
< hidden >
iven
Output:
su
-765
tia
lgo
ga
Execution Time:
07
18
0.09s
1
sec
TestCase2:
4s
t.2
Input:
an
ish
Expected Output:
tA
Output:
Ni
Sample Input:1234
Sample Output:10
Concepts Included:
gu 28 2nd semester java programming
)
.in
Language Used: JAVA 8
ac
ty.
rsi
Source Code:
ive
import java.util.Scanner;
n
su
public class Main{
public static void main(String[] args){ tia
lgo
Scanner sc=new Scanner(System.in);
ga
int n=sc.nextInt();
@
int sum=0;
21
while(n>0){
07
sum+=n%10;
18
n/=10;
1
se
}
c
System.out.println(sum);
4s
}
t.2
}
an
ish
(n
Compilation Details:
rya
tA
TestCase1:
an
sh
Input:
Ni
Expected Output:
< hidden >
Output:
35
Expected Output:
< hidden >
Output:
2
)
.in
ac
0.091s
ty.
rsi
ive
3. Swap Two Numbers Without Using a Third VariableProblem
n
su
Statement:Write a Java program to swap two numbers without tia
using a third variable.
lgo
ga
Description:The program should take two integer inputs, swap their values using
@
Sample Input:3 5
t.2
Sample Output:5 3
an
ish
(n
rya
Concepts Included:
sh
Ni
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
a=a+b;
b=a-b;
a=a-b;
System.out.println(a+" "+b);
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
)
.in
ac
< hidden >
ty.
rsi
Output:
iven
20 10
su
Compilation Status: Passed tia
lgo
ga
Execution Time:
@
21
0.11s
07
18
TestCase2:
1
sec
Input:
4s
t.2
Expected Output:
(n
rya
Output:
an
sh
-9 -1
Ni
Sample Input:5
Sample Output:120
Concepts Included:
gu 28 2nd semester java programming
)
.in
ac
Source Code:
ty.
rsi
import java.util.Scanner;
ive
public class Main{
n
public static void main(String[] args){
su
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
tia
lgo
long factorial=1;
ga
for(int i=2;i<=n;i++){
@
factorial*=i;
21
}
07
System.out.println(factorial);
18
}
1
se
}
c
4s
t.2
Compilation Details:
an
ish
TestCase1:
(n
rya
Input:
tA
an
Expected Output:
< hidden >
Output:
720
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
3628800
)
.in
ac
ty.
5. Check Valid Java IdentifierProblem Statement:Write a program to
rsi
ive
check whether a given string is a valid Java identifier. A valid
n
identifier must start with a letter (A-Z or a-z) or an underscore (_)
su
tia
and can be followed by letters, digits (0-9), or underscores. It must
lgo
Description:The program should read a string input and verify if it follows the Java
21
Output Format:A boolean value (true or false) indicating whether the input is a valid
t.2
Java identifier.
an
Sample Input:myVariable1
ish
(n
Sample Output:false
rya
tA
an
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
import java.util.Set;
public class Main{
private static final Set<String> KEYWORDS=Set.of(
"abstract","assert","boolean","break","byte","case","catch","char","class","const","continue","de
fault","do","double","else","enum","extends","final","finally","float","for","goto","if","implemen
ts","import","instanceof","int","interface","long","native","new","package","private","protected",
"public","return","short","static","strictfp","super","switch","synchronized","this","throw","throws
","transient","try","void","volatile","while","_","null","true","false"
);
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String identifier=sc.nextLine();
boolean isValid=isValidJavaIdentifier(identifier);
System.out.println(isValid);
}
public static boolean isValidJavaIdentifier(String s){
if(s==null || s.isEmpty()){
return false;
)
}
.in
ac
if (KEYWORDS.contains(s)){
ty.
return false;
rsi
}
ive
if(!Character.isLetter(s.charAt(0)) && s.charAt(0) !='_'){
n
return false;
su
}
tia
for(int i=1;i<s.length();i++){
lgo
char ch=s.charAt(i);
ga
return false;
21
}
07
}
18
return true;
1
se
}
c
4s
}
t.2
an
Compilation Details:
ish
(n
rya
TestCase1:
tA
Input:
an
sh
Expected Output:
< hidden >
Output:
true
Expected Output:
< hidden >
Output:
false
)
.in
ac
0.091s
ty.
rsi
ive
6. Check Armstrong Number Problem Statement:Write a program to
n
su
check whether a given number is an Armstrong number. lgo
tia
Description:An Armstrong number is a number that is equal to the sum of its own
ga
digits each raised to the power of the number of digits.For example, 153 is an
@
Sample Input:153
4s
t.2
Sample Output:true
an
ish
(n
Concepts Included:
sh
Ni
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner (System.in);
int number=sc.nextInt();
System.out.print(isArmstrong(number));
}
public static boolean isArmstrong(int n){
int original=n;
int digits=String.valueOf(n).length();
int sum=0;
while(n>0){
int digit=n%10;
sum+=Math.pow(digit,digits);
n/=10;
}
return sum==original;
}
}
Compilation Details:
)
.in
TestCase1:
ac
ty.
Input:
rsi
ive
< hidden >
n
su
Expected Output:
tia
lgo
< hidden >
ga
Output:
@
21
true
07
18
Execution Time:
4s
t.2
0.088s
an
ish
TestCase2:
(n
rya
Input:
tA
an
Expected Output:
< hidden >
Output:
false
Input Format:
)
.in
Completion Status: Completed
ac
ty.
rsi
Concepts Included:
ive
gu 28 2nd semester java programming
n
su
tia
Language Used: JAVA 8
lgo
ga
@
Source Code:
21
07
import java.util.Scanner;
18
int n=sc.nextInt();
t.2
if(isPrime(n)){
an
System.out.println("Prime");
ish
}
(n
else{
rya
System.out.println("Not Prime");
tA
}
an
}
sh
if(n<=1){
return false;
}
for(int i=2;i*i<=n;i++){
if(n%i==0){
return false;
}
}
return true;
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Prime
)
.in
ac
0.09s
ty.
rsi
ive
TestCase2:
n
su
Input:
tia
lgo
< hidden >
ga
Expected Output:
@
21
07
Output:
1
sec
Not Prime
4s
t.2
Execution Time:
(n
rya
0.084s
tA
an
sh
Input Format:
Two integers representing the length and width of the rectangle.Output Format:
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
class Rectangle{
int length;
int width;
)
.in
//Parameterized constructor
ac
Rectangle(int l,int w){
ty.
length=l;
rsi
width=w;
ive
}
n
su
int calculateArea(){
return length*width; tia
lgo
}
}
ga
@
int length=sc.nextInt();
1
int width=sc.nextInt();
se
System.out.println(rect.calculateArea());
t.2
}
an
}
ish
(n
Compilation Details:
rya
tA
an
TestCase1:
sh
Ni
Input:
< hidden >
Expected Output:
< hidden >
Output:
50
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
21
)
.in
Compilation Status: Passed
ac
ty.
Execution Time:
rsi
ive
0.097s
n
su
tia
9. Goto Statement (Simulated with Loops)Problem
lgo
input and stops only when the user inputs a specific keyword.
07
18
repeatedly ask the user to input a string and stop asking when the user inputs the
c
word "stop".
4s
t.2
Input Format:
an
ish
Each string entered by the user until the keyword "stop" is encountered.
rya
tA
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(true){
String input=sc.nextLine();
if(input.equals("stop")){
break;
}
System.out.println(input);
}
}
}
Compilation Details:
TestCase1:
)
.in
Input:
ac
ty.
rsi
< hidden >
ive
Expected Output:
n
su
< hidden > tia
lgo
Output:
ga
@
hello
21
world
07
18
Execution Time:
4s
t.2
0.085s
an
ish
TestCase2:
(n
rya
Input:
tA
an
Expected Output:
< hidden >
Output:
java
programming
Input Format:
)
.in
ac
Completion Status: Completed
ty.
rsi
ive
Concepts Included:
n
su
gu 28 2nd semester java programming
tia
lgo
Source Code:
07
18
import java.util.Scanner;
1
se
boolean isSenior=sc.nextBoolean();
an
boolean isStudent=sc.nextBoolean();
ish
boolean hasMembership=sc.nextBoolean();
(n
int count=0;
rya
if(isSenior) count++;
tA
if(isStudent) count++;
an
if(hasMembership) count++;
sh
boolean qualifiesForDiscount=count>=2;
Ni
System.out.println(qualifiesForDiscount);
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
true
TestCase2:
Input:
)
.in
< hidden >
ac
ty.
Expected Output:
rsi
ive
< hidden >
n
su
Output:
tia
lgo
false
ga
Execution Time:
07
18
0.085s
1
sec
4s
t.2
program that prints the first n numbers, but skips multiples of 3 and
ish
Description:The program should read an integer n and print the first n numbers. It
tA
should skip numbers that are multiples of 3 using the continue statement and stop if
an
Input Format:
Concepts Included:
gu 28 2nd semester java programming
Language Used: JAVA 8
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int count=0;
for(int i=1;;i++){
if(i>50) break;
if(i%3==0) continue;
System.out.print(i);
count++;
)
.in
if(count==n) break;
ac
System.out.print(" ");
ty.
}
rsi
System.out.println();
ive
}
n
su
}
tia
lgo
Compilation Details:
ga
@
21
TestCase1:
07
18
Input:
1
se
Expected Output:
an
ish
Output:
rya
tA
1 2 4 5 7 8 10 11 13 14
an
Execution Time:
0.089s
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
1 2 4 5 7 8 10 11 13 14 16 17 19 20 22
)
Description:The program should have a Utility class with a static method max that
.in
ac
takes two integers and returns the maximum of the two. The main method should call
ty.
this static method and print the result.
rsi
Input Format:
iven
su
Two integers separated by a space.Output Format:
tia
The maximum of the two integers.
lgo
ga
Concepts Included:
4s
t.2
Source Code:
an
sh
import java.util.Scanner;
Ni
class Utility{
public static int max(int a,int b){
return(a>b)?a:b;
}
}
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int num1=sc.nextInt();
int num2=sc.nextInt();
int result=Utility.max(num1,num2);
System.out.println(result);
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
)
.in
ac
10
ty.
Compilation Status: Passed
rsi
ive
Execution Time:
n
su
0.092s tia
lgo
ga
TestCase2:
@
21
Input:
07
18
Expected Output:
c
4s
t.2
Output:
(n
15
rya
tA
Execution Time:
Ni
0.087s
Sample Output:50.0
Concepts Included:
gu 28 2nd semester java programming
Source Code:
)
.in
ac
import java.util.Scanner;
ty.
class Shape{
rsi
double area(){
ive
return 0;
n
}
su
} tia
lgo
class Rectangle extends Shape{
int length,breadth;
ga
this.length=length;
07
this.breadth=breadth;
18
}
1
@Override
se
double area(){
c
4s
return length*breadth;
t.2
}
an
}
ish
int length=sc.nextInt();
an
int breadth=sc.nextInt();
sh
System.out.printf("%.1f",rect.area());
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
120.0
TestCase2:
Input:
)
.in
< hidden >
ac
ty.
Expected Output:
rsi
ive
< hidden >
n
su
Output:
tia
lgo
200.0
ga
Execution Time:
07
18
0.094s
1
sec
4s
t.2
Description:The program takes a string and finds the length of the longest substring
tA
Concepts Included:
gu 28 2nd semester java programming
)
}
.in
return maxLength;
ac
ty.
}
rsi
public static void main(String[] args){
ive
Scanner sc=new Scanner(System.in);
n
String s=sc.nextLine();
su
System.out.print(lengthOfLongestSubstring(s));
} tia
lgo
}
ga
@
21
Compilation Details:
07
18
TestCase1:
1
sec
Input:
4s
t.2
Expected Output:
(n
rya
Output:
an
sh
3
Ni
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
2
)
.in
ac
multiplication, and division and must be evaluated following the
ty.
correct order of operations (PEMDAS/BODMAS).
rsi
ive
Description:The program should take a string input representing the arithmetic
n
expression. It should parse the expression and compute the result accurately. The
su
expression can have nested parentheses, and the program must handle them
tia
correctly.
lgo
ga
Output Format:A single number, which is the result of the evaluated expression.
07
18
Sample Output:15
c
4s
t.2
an
Concepts Included:
rya
tA
Source Code:
import java.util.Scanner;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String expression=sc.nextLine();
try{
ScriptEngineManager mgr=new ScriptEngineManager();
ScriptEngine engine=mgr.getEngineByName("JavaScript");
Object result=engine.eval(expression);
System.out.println(result);
}
catch(Exception e){
System.out.println("Invalid Expression");
}
}
}
Compilation Details:
TestCase1:
)
Input:
.in
ac
< hidden >
ty.
rsi
Expected Output:
iven
su
< hidden >
tia
Output:
lgo
ga
17
@
21
Execution Time:
1
se
0.774s
c
4s
t.2
TestCase2:
an
ish
Input:
(n
rya
Expected Output:
sh
Ni
Output:
10
Sample Input:A B C D
Sample Output:D C B A
Concepts Included:
gu 28 2nd semester java programming
)
.in
ac
ty.
Language Used: JAVA 8
rsi
ive
Source Code:
n
su
import java.util.Scanner; tia
lgo
public class Main{
public static void main(String[] args){
ga
@
String input=sc.nextLine();
07
String reversed="";
18
for(int i=input.length()-1;i>=0;i--){
1
reversed += input.charAt(i);
se
}
c
4s
System.out.println(reversed);
t.2
}
an
}
ish
(n
Compilation Details:
rya
tA
an
TestCase1:
sh
Ni
Input:
< hidden >
Expected Output:
< hidden >
Output:
gnimmargorP avaJ
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
racecaR
)
.in
Compilation Status: Passed
ac
ty.
Execution Time:
rsi
ive
0.113s
n
su
tia
17. Reverse a StringProblem Statement:Write a program to reverse
lgo
Description:The program should take a string as input and return its reversed form.
07
Sample Input:A B C D
t.2
an
Sample Output:D C B A
ish
(n
rya
Concepts Included:
sh
Ni
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String reversed="";
for(int i=s.length()-1;i>=0;i--){
reversed += s.charAt(i);
}
System.out.println(reversed);
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
)
.in
ac
< hidden >
ty.
rsi
Output:
iven
gnimmargorP avaJ
su
Compilation Status: Passed tia
lgo
ga
Execution Time:
@
21
0.113s
07
18
TestCase2:
1
sec
Input:
4s
t.2
Expected Output:
(n
rya
Output:
an
sh
racecaR
Ni
Sample Input:16
Sample Output:true
Concepts Included:
gu 28 2nd semester java programming
)
.in
ac
Source Code:
ty.
rsi
import java.util.Scanner;
ive
public class Main{
n
public static boolean isPowerOfTwo(int n){
su
return n>0 && (n&(n-1))==0;
}
tia
lgo
int n=sc.nextInt();
21
System.out.println(isPowerOfTwo(n));
07
}
18
}
1
sec
4s
Compilation Details:
t.2
an
ish
TestCase1:
(n
Input:
rya
tA
Expected Output:
Ni
Output:
true
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
false
)
.in
ac
ty.
19. Implement an Abstract Class for Shape Problem
rsi
ive
Statement:Create an abstract class Shape with an abstract method
n
calculateArea().
su
tia
Description:1. The Shape class should have calculateArea().2. Implement Circle and
lgo
Input Format:1. The first line contains shape type (circle or rectangle).2. If circle, next
21
line contains radius.3. If rectangle, next two lines contain length and width.
07
18
Sample Input:circle5
c
4s
t.2
Sample Output:78.54
an
ish
(n
Concepts Included:
an
sh
Source Code:
import java.util.Scanner;
abstract class Shape{
abstract double calculateArea();
}
class Circle extends Shape{
double radius;
Circle(double radius){
this.radius=radius;
}
double calculateArea(){
return Math.PI*radius*radius;
}
}
class Rectangle extends Shape{
double length,width;
Rectangle(double length,double width){
this.length=length;
this.width=width;
}
double calculateArea(){
return length*width;
}
}
)
public class Main{
.in
ac
public static void main(String[] args){
ty.
Scanner sc=new Scanner(System.in);
rsi
String shapeType=sc.nextLine().toLowerCase();
ive
Shape shape;
n
if(shapeType.equals("circle")){
su
double radius=Double.parseDouble(sc.nextLine());
tia
shape=new Circle(radius);
lgo
}
ga
else if(shapeType.equals("rectangle")){
@
double length=Double.parseDouble(sc.nextLine());
21
double width=Double.parseDouble(sc.nextLine());
07
shape=new Rectangle(length,width);
18
}
1
se
else{
c
4s
System.out.println("Invalid Shape");
t.2
return;
an
}
ish
System.out.printf("%.2f\n",shape.calculateArea());
(n
}
rya
}
tA
an
Compilation Details:
sh
Ni
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
24.00
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
)
.in
314.16
ac
ty.
Compilation Status: Passed
rsi
ive
Execution Time:
n
su
0.091s
tia
lgo
ga
Sample Input:Hello
(n
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine().toLowerCase();
Map<Character, Integer> freqMap = new TreeMap<>();
for (char c : input.toCharArray()) {
freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : freqMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Compilation Details:
TestCase1:
)
.in
Input:
ac
ty.
rsi
< hidden >
ive
Expected Output:
n
su
< hidden > tia
lgo
Output:
ga
@
1: 2
21
2: 2
07
3: 2
18
a: 2
1
se
b: 2
c
4s
c: 2
t.2
Execution Time:
(n
rya
0.111s
tA
an
TestCase2:
sh
Ni
Input:
< hidden >
Expected Output:
< hidden >
Output:
!: 3
#: 3
@: 3
Sample Input:programming
)
.in
Sample Output:r g m
ac
ty.
rsi
Completion Status: Completed
iven
su
Concepts Included:
tia
lgo
gu 28 2nd semester java programming
ga
@
Source Code:
1
sec
import java.util.*;
4s
String input=sc.nextLine().toLowerCase();
(n
Map<Character,Integer>freqMap=new LinkedHashMap<>();
rya
for(char c:input.toCharArray()){
tA
freqMap.put(c,freqMap.getOrDefault(c,0)+1);
an
}
sh
List<Character>duplicates=new ArrayList<>();
Ni
for(Map.Entry<Character,Integer>entry:freqMap.entrySet()){
if(entry.getValue()>1){
duplicates.add(entry.getKey());
}
}
for(int i=0; i<duplicates.size();i++){
System.out.print(duplicates.get(i));
if(i<duplicates.size()-1){
System.out.print(" ");
}
}
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
sc
)
.in
Compilation Status: Passed
ac
ty.
Execution Time:
rsi
ive
0.093s
n
su
TestCase2: tia
lgo
Input:
ga
@
21
Expected Output:
1 18
se
Output:
t.2
an
an
ish
(n
Execution Time:
tA
an
0.091s
sh
Ni
Sample Input:listensilent
Sample Output:YES
Completion Status: Completed
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
)
.in
String str1=sc.nextLine().toLowerCase();
ac
String str2=sc.nextLine().toLowerCase();
ty.
if(isAnagram(str1,str2)){
rsi
System.out.println("YES");
ive
}
n
su
else{
System.out.println("NO"); tia
lgo
}
}
ga
@
if(s1.length() != s2.length()){
07
return false;
18
}
1
for(char c:s1.toCharArray()){
c
4s
count[c -'a']++;
t.2
}
an
for(char c:s2.toCharArray()){
ish
count[c-'a']--;
(n
}
rya
for(int c:count){
tA
}
sh
return true;
Ni
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
NO
TestCase2:
Input:
< hidden >
)
.in
Expected Output:
ac
ty.
< hidden >
rsi
ive
Output:
n
su
YES
tia
lgo
Compilation Status: Passed
ga
Execution Time:
@
21
07
0.091s
1 18
se
operations.
an
ish
Sample Input:105+
Sample Output:15
Concepts Included:
gu 28 2nd semester java programming
)
case'-':
.in
result=num1-num2;
ac
ty.
System.out.println(result);
rsi
break;
ive
case'*':
n
result=num1*num2;
su
System.out.println(result);
break; tia
lgo
case'/':
ga
if(num2 !=0){
@
result=num1/num2;
21
System.out.println(result);
07
}
18
else{
1
se
}
4s
t.2
break;
an
default:
ish
System.out.println("Invalid operator");
}
(n
}
rya
}
tA
an
sh
Compilation Details:
Ni
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
16
Compilation Status: Passed
Execution Time:
0.083s
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
Output:
ac
ty.
10
rsi
ive
Compilation Status: Passed
n
su
Execution Time:
tia
lgo
0.092s
ga
@
21
Description:1. The GCD of two numbers is the largest number that divides both
t.2
Sample Input:48 18
sh
Sample Output:6
Ni
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int gcd=findGCD(a,b);
System.out.println(gcd);
}
public static int findGCD(int a ,int b){
if(b==0)
return a;
return findGCD(b, a%b);
}
}
)
.in
ac
Compilation Details:
ty.
rsi
ive
TestCase1:
n
su
Input: lgo
tia
< hidden >
ga
@
Expected Output:
21
07
Output:
sec
4s
25
t.2
Execution Time:
(n
rya
0.085s
tA
an
TestCase2:
sh
Ni
Input:
< hidden >
Expected Output:
< hidden >
Output:
5
Sample Input:121
Sample Output:YES
)
.in
ac
ty.
Completion Status: Completed
rsi
iven
Concepts Included:
su
gu 28 2nd semester java programming tia
lgo
ga
Source Code:
1 18
se
import java.util.*;
c
int n=sc.nextInt();
ish
if(isPalindrome(n)){
(n
System.out.println("YES");
rya
}
tA
else{
an
System.out.println("NO");
sh
}
Ni
}
public static boolean isPalindrome(int n){
int original=n;
int reversed=0;
while(n>0){
int digit=n%10;
reversed=reversed*10+digit;
n=n/10;
}
return original==reversed;
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
NO
)
.in
Compilation Status: Passed
ac
ty.
Execution Time:
rsi
ive
0.09s
n
su
TestCase2: tia
lgo
Input:
ga
@
21
Expected Output:
1 18
se
Output:
t.2
an
YES
ish
(n
Execution Time:
tA
an
0.089s
sh
Ni
Input Format:1. The first line contains an integer n (1 ≤ n ≤ 100).2. The second line
contains n space-separated integers (−10^3 ≤ A[i] ≤ 10^3).
Sample Input:510 20 5 40 25
Sample Output:40
Completion Status: Completed
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
)
.in
int n=sc.nextInt();
ac
int[] arr=new int[n];
ty.
int max=Integer.MIN_VALUE;
rsi
for(int i=0;i<n;i++){
ive
arr[i]=sc.nextInt();
n
su
if(arr[i]>max){
max=arr[i]; tia
lgo
}
}
ga
@
System.out.println(max);
21
}
07
}
1 18
se
Compilation Details:
c
4s
t.2
TestCase1:
an
ish
Input:
(n
rya
Expected Output:
an
sh
Output:
-1
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
90
)
.in
27. Convert Decimal to Binary Problem Statement:Convert a given
ac
ty.
decimal number to binary.
rsi
ive
Description:1. Do not use built-in functions like Integer.toBinaryString().2. Use division
n
and modulo operations.
su
Input Format:A single integer n (1 ≤ n ≤ 1000). tia
lgo
ga
Sample Input:10
07
Sample Output:1010
1 18
sec
4s
Concepts Included:
ish
(n
Source Code:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String binary="";
if(n==0){
binary="0";
}
else{
while(n>0){
binary=(n%2)+binary;
n=n/2;
}
}
System.out.println(binary);
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
)
.in
Expected Output:
ac
ty.
< hidden >
rsi
ive
Output:
n
su
101
tia
lgo
Execution Time:
21
07
0.117s
1 18
se
TestCase2:
c
4s
Input:
t.2
an
Expected Output:
rya
tA
Output:
sh
Ni
100000
Sample Input:The quick brown fox jumps over the lazy dog
Sample Output:YES
Concepts Included:
gu 28 2nd semester java programming
)
.in
Language Used: JAVA 8
ac
ty.
rsi
Source Code:
ive
import java.util.*;
n
su
public class Main{
public static void main(String[] args){ tia
lgo
Scanner sc=new Scanner(System.in);
ga
Set<Character>letters=new HashSet<>();
21
for(char c:input.toCharArray()){
07
if(c>='a'&& c<='z'){
18
letters.add(c);
1
se
}
c
}
4s
if(letters.size()==26){
t.2
System.out.println("YES");
an
}
ish
else{
(n
System.out.println("NO");
rya
}
tA
}
an
}
sh
Ni
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
NO
TestCase2:
Input:
< hidden >
Expected Output:
)
.in
< hidden >
ac
ty.
Output:
rsi
ive
YES
n
su
Compilation Status: Passed
tia
lgo
Execution Time:
ga
0.094s
@
21
07
18
Description:1. Consider only alphabets (a-z, A-Z), ignore spaces and special
ish
Sample Output:3 7
Concepts Included:
gu 28 2nd semester java programming
)
vowelCount++;
.in
}
ac
ty.
else{
rsi
consonantCount++;
ive
}
n
}
su
}
System.out.println(vowelCount+" "+consonantCount); tia
lgo
}
ga
}
@
21
07
Compilation Details:
1 18
se
TestCase1:
c
4s
Input:
t.2
an
Expected Output:
rya
tA
Output:
sh
Ni
54
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
03
)
.in
Statement:Write a program to find the second-largest number in an
ac
array.
ty.
rsi
Description:1. The array contains n distinct integers.2. Print the second-largest
ive
number.
n
su
tia
Input Format:1. The first line contains an integer n (2 ≤ n ≤ 1000).2. The second line
lgo
contains n space-separated integers.
ga
Sample Input:53 1 9 4 6
07
18
Sample Output:6
1
sec
4s
Concepts Included:
(n
rya
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
sc.close();
//Initialise largest and second largest
int largest=Integer.MIN_VALUE;
int secondLargest=Integer.MIN_VALUE;
for(int i=0;i<n;i++){
if(arr[i]>largest){
secondLargest=largest;
largest=arr[i];
}
else if(arr[i]>secondLargest){
secondLargest=arr[i];
}
}
System.out.println(secondLargest);
}
}
Compilation Details:
)
.in
ac
ty.
TestCase1:
rsi
ive
Input:
n
su
< hidden >
tia
lgo
Expected Output:
ga
@
Output:
07
18
7
1
sec
Execution Time:
an
ish
0.085s
(n
rya
TestCase2:
tA
an
Input:
sh
Ni
Expected Output:
< hidden >
Output:
20
Sample Output:3
)
.in
ac
Concepts Included:
ty.
rsi
gu 28 2nd semester java programming
iven
su
Language Used: JAVA 8 tia
lgo
ga
Source Code:
@
21
import java.util.Scanner;
07
String s=sc.nextLine();
4s
sc.close();
t.2
String[] words=s.trim().split("\\s+");
an
if(s.trim().isEmpty()){
ish
System.out.println(0);
(n
}
rya
else{
tA
System.out.println(words.length);
an
}
sh
}
Ni
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
2
TestCase2:
Input:
< hidden >
)
.in
Expected Output:
ac
ty.
< hidden >
rsi
ive
Output:
n
su
4
tia
lgo
Compilation Status: Passed
ga
Execution Time:
@
21
07
0.088s
1 18
se
in a given string.
an
ish
Description:1. The first non-repeating character is the one that appears only once and
(n
Sample Input:swiss
Sample Output:w
Concepts Included:
gu 28 2nd semester java programming
)
if(entry.getValue()==1){
.in
System.out.println(entry.getKey());
ac
ty.
return;
rsi
}
ive
}
n
System.out.println(-1);
su
}
} tia
lgo
ga
Compilation Details:
@
21
07
TestCase1:
1 18
se
Input:
c
4s
Expected Output:
ish
(n
Output:
tA
an
-1
sh
Ni
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
z
)
Description:Given an array, find the two numbers whose product is maximum.
.in
ac
Input Format:1. First line: An integer n (2 ≤ n ≤ 10^5) – number of elements.2. Second
ty.
line: n space-separated integers (-10^6 ≤ ai ≤ 10^6).
rsi
ive
Output Format:Print the maximum product.
n
su
Sample Input:5 1 10 -5 2 -20 tia
lgo
Sample Output:100
ga
@
21
Concepts Included:
sec
4s
Source Code:
tA
an
import java.util.Scanner;
sh
import java.util.Arrays;
Ni
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
)
.in
ac
35
ty.
Compilation Status: Passed
rsi
ive
Execution Time:
n
su
0.09s tia
lgo
ga
TestCase2:
@
21
Input:
07
18
Expected Output:
c
4s
t.2
Output:
(n
56
rya
tA
Execution Time:
Ni
0.089s
Input Format:1. First line: An integer n (2 ≤ n ≤ 10^5).2. Second line: n-1 space-
separated integers.
Output Format:Print the missing number.
Sample Input:5 1 2 4 5
Sample Output:3
Concepts Included:
gu 28 2nd semester java programming
)
.in
ac
Source Code:
ty.
rsi
import java.util.Scanner;
ive
public class Main{
n
public static void main(String[] args){
su
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
tia
lgo
int sum=n*(n+1)/2;
ga
int actualSum=0;
@
for(int i=0;i<n-1;i++){
21
actualSum+=sc.nextInt();
07
}
18
sc.close();
1
se
System.out.println(sum-actualSum);
c
4s
}
t.2
}
an
ish
Compilation Details:
(n
rya
TestCase1:
tA
an
Input:
sh
Ni
Expected Output:
< hidden >
Output:
7
Expected Output:
< hidden >
Output:
2
)
.in
ac
0.09s
ty.
rsi
ive
35. Find the Longest Word in a SentenceProblem Statement:Write a
n
su
program to find the longest word in a given sentence. lgo
tia
Description:1. A word is a sequence of letters separated by spaces.2. If multiple
ga
words have the same longest length, print the first one.
@
21
Sample Output:programming
t.2
an
ish
Concepts Included:
an
sh
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
sc.close();
String[] words=s.trim().split("\\s+");
String longestWord="";
int maxLength=0;
for(String word:words){
if(word.length()>maxLength){
longestWord=word;
maxLength=word.length();
}
}
System.out.println(longestWord);
}
}
Compilation Details:
TestCase1:
)
Input:
.in
ac
< hidden >
ty.
rsi
Expected Output:
iven
su
< hidden >
tia
Output:
lgo
ga
banana
@
21
Execution Time:
1
se
0.087s
c
4s
t.2
TestCase2:
an
ish
Input:
(n
rya
Expected Output:
sh
Ni
Output:
everything
Sample Input:9875
Sample Output:2
Concepts Included:
gu 28 2nd semester java programming
)
.in
ac
ty.
Language Used: JAVA 8
rsi
ive
Source Code:
n
su
import java.util.Scanner; tia
lgo
public class Main{
public static void main(String[] args){
ga
@
int n=sc.nextInt();
07
sc.close();
18
while(n>=10){
1
int sum=0;
se
while(n>0){
c
4s
sum+=n%10;
t.2
n/=10;
an
}
ish
n=sum;
(n
}
rya
System.out.println(n);
tA
}
an
}
sh
Ni
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
9
TestCase2:
Input:
< hidden >
Expected Output:
)
.in
< hidden >
ac
ty.
Output:
rsi
ive
1
n
su
Compilation Status: Passed
tia
lgo
Execution Time:
ga
0.089s
@
21
07
18
separated integers.
rya
tA
Sample Input:5 3 3 4 2 3
sh
Ni
Sample Output:3
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
sc.close();
int count=0,candidate=-1;
for(int num:arr){
if(count==0){
candidate=num;
count=1;
)
}
.in
ac
else if(num==candidate){
ty.
count++;
rsi
}
ive
else{
n
count--;
su
}
tia
}
lgo
count=0;
ga
for(int num:arr){
@
if(num==candidate){
21
count++;
07
}
18
}
1
se
if(count>n/2){
c
4s
System.out.println(candidate);
t.2
}
an
else{
ish
System.out.println(-1);
(n
}
rya
}
}
tA
an
sh
Compilation Details:
Ni
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
2
Compilation Status: Passed
Execution Time:
0.088s
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
Output:
ac
ty.
7
rsi
ive
Compilation Status: Passed
n
su
Execution Time:
tia
lgo
0.09s
ga
@
21
Description:1. Words are separated by spaces.2. Maintain the words' internal order
c
4s
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
sc.close();
String[] words=s.trim().split("\\s+");
for(int i=words.length-1;i>=0;i--){
System.out.print(words[i]);
if(i!=0){
System.out.print(" ");
}
}
}
}
Compilation Details:
)
.in
ac
ty.
TestCase1:
rsi
ive
Input:
n
su
< hidden >
tia
lgo
Expected Output:
ga
@
Output:
07
18
Execution Time:
an
ish
0.09s
(n
rya
TestCase2:
tA
an
Input:
sh
Ni
Expected Output:
< hidden >
Output:
language! best the Java, to Welcome
Sample Input:5 1 2 3 4 5 6
Sample Output:(2,4)(1,5)
)
.in
Completion Status: Completed
ac
ty.
rsi
Concepts Included:
iven
gu 28 2nd semester java programming
su
tia
lgo
Language Used: JAVA 8
ga
@
Source Code:
21
07
import java.util.*;
18
int n=sc.nextInt();
t.2
Set<Integer>seen=new HashSet<>();
ish
Set<String>addedPairs=new HashSet();
(n
List<int[]>result=new ArrayList();
rya
for(int i=0;i<n;i++){
tA
arr[i]=sc.nextInt();
an
}
sh
int k=sc.nextInt();
Ni
sc.close();
for(int num:arr){
int complement=k-num;
if(seen.contains(complement)){
int a=Math.min(num,complement);
int b=Math.max(num,complement);
String key=a+","+b;
if(!addedPairs.contains(key)){
result.add(new int[]{a,b});
addedPairs.add(key);
}
}
seen.add(num);
}
if(result.isEmpty()){
System.out.println(-1);
}
else{
for(int[] pair:result){
System.out.println("(" + pair[0] + "," + pair[1] + ")");
}
}
}
}
Compilation Details:
)
.in
TestCase1:
ac
ty.
Input:
rsi
ive
< hidden >
n
su
Expected Output:
tia
lgo
< hidden >
ga
Output:
@
21
-1
07
18
Execution Time:
4s
t.2
0.086s
an
ish
TestCase2:
(n
rya
Input:
tA
an
Expected Output:
< hidden >
Output:
(5,6)
(2,9)
)
Sample Input:25
.in
ac
Sample Output:Automorphic Number
ty.
rsi
ive
Completion Status: Completed
n
su
tia
lgo
Concepts Included:
ga
Source Code:
c
4s
t.2
import java.util.Scanner;
an
int n=sc.nextInt();
sc.close();
tA
int square=n*n;
an
String numStr=String.valueOf(n);
sh
String squareStr=String.valueOf(square);
Ni
if(squareStr.endsWith(numStr)){
System.out.println("Automorphic Number");
}
else{
System.out.println("Not an Automorphic Number");
}
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Not an Automorphic Number
)
.in
ac
ty.
TestCase2:
rsi
ive
Input:
n
su
< hidden >
tia
lgo
Expected Output:
ga
Output:
07
18
Automorphic Number
1
sec
Execution Time:
an
ish
0.091s
(n
rya
tA
Input Format:1. The first line contains an integer n (1 ≤ n ≤ 100), representing the
number of strings.2. The next n lines each contain a string (1 ≤ length ≤ 100),
consisting of lowercase English letters.
Sample Output:fl
Concepts Included:
gu 28 2nd semester java programming
Source Code:
)
.in
ac
import java.util.Scanner;
ty.
public class Main{
rsi
public static void main(String[] args){
ive
Scanner sc=new Scanner(System.in);
n
int n=sc.nextInt();
su
sc.nextLine(); tia
lgo
String[] strs=new String[n];
for(int i=0;i<n;i++){
ga
strs[i]=sc.nextLine();
@
21
}
07
sc.close();
18
String prefix=strs[0];
1
for(int i=1;i<n;i++){
se
while(!strs[i].startsWith(prefix)){
c
4s
prefix=prefix.substring(0,prefix.length()-1);
t.2
if(prefix.isEmpty()){
an
System.out.println("");
ish
return;
(n
}
rya
}
tA
}
an
System.out.println(prefix);
sh
}
Ni
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
ap
TestCase2:
Input:
< hidden >
)
.in
Expected Output:
ac
ty.
< hidden >
rsi
ive
Output:
n
su
inter
tia
lgo
Compilation Status: Passed
ga
Execution Time:
@
21
07
0.093s
118
se
Fail
an
sh
thesisTitle
Concepts Included:
gu 28 2nd semester java programming
Language Used: JAVA 8
Source Code:
import java.util.Scanner;
// Base class
class Student {
String name;
int rollNumber;
int marks;
public Student(String name, int rollNumber, int marks) {
this.name = name;
this.rollNumber = rollNumber;
this.marks = marks;
}
)
.in
public String getGrade() {
ac
if (marks >= 90 && marks <= 100) return "A";
ty.
else if (marks >= 75 && marks < 90) return "B";
rsi
else if (marks >= 50 && marks < 75) return "C";
ive
else return "Fail";
n
su
}
} tia
lgo
// Subclass
ga
String thesisTitle;
21
this.thesisTitle = thesisTitle;
1
}
sec
System.out.println(getGrade());
t.2
System.out.println(thesisTitle);
an
}
ish
}
(n
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
B
Data Science Analysis
)
.in
0.087s
ac
ty.
rsi
TestCase2:
iven
Input:
su
< hidden > tia
lgo
ga
Expected Output:
@
21
Output:
118
se
Fail
c
AI Ethics Study
4s
t.2
Execution Time:
(n
rya
0.094s
tA
an
sh
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
)
.in
class Student {
ac
String name;
ty.
int rollNumber;
rsi
int marks;
ive
Student(String name, int rollNumber, int marks) {
n
su
this.name = name;
this.rollNumber = rollNumber; tia
lgo
this.marks = marks;
ga
}
@
String getGrade() {
21
}
4s
}
t.2
String thesisTitle;
ish
this.thesisTitle = thesisTitle;
tA
}
an
void displayInfo() {
sh
System.out.println(getGrade());
Ni
System.out.println(thesisTitle);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int rollNumber = sc.nextInt();
int marks = sc.nextInt();
sc.nextLine();
String thesisTitle = sc.nextLine();
GraduateStudent grad = new GraduateStudent(name, rollNumber, marks, thesisTitle);
grad.displayInfo();
sc.close();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
Output:
ac
ty.
B
rsi
Data Science Analysis
ive
Compilation Status: Passed
n
su
Execution Time: tia
lgo
ga
0.092s
@
21
TestCase2:
07
18
Input:
1
sec
Expected Output:
an
ish
Output:
rya
tA
Fail
an
AI Ethics Study
sh
Ni
Sample Output:2.0
Concepts Included:
)
.in
gu 28 2nd semester java programming
ac
ty.
rsi
Language Used: JAVA 8
iven
su
Source Code:
tia
lgo
import java.util.*;
public class Main{
ga
@
int n1=sc.nextInt();
18
for(int i=0;i<n1;i++){
se
nums1[i]=sc.nextInt();
c
4s
}
t.2
int n2=sc.nextInt();
an
for(int i=0;i<n2;i++){
(n
nums2[i]=sc.nextInt();
rya
}
tA
sc.close();
an
System.out.println(findMedianSortedArrays(nums1,nums2));
sh
}
Ni
)
.in
ac
Compilation Details:
ty.
rsi
ive
TestCase1:
n
su
Input: lgo
tia
< hidden >
ga
@
Expected Output:
21
07
Output:
sec
4s
3.5
t.2
Execution Time:
(n
rya
0.093s
tA
an
TestCase2:
sh
Ni
Input:
< hidden >
Expected Output:
< hidden >
Output:
15.0
Output Format:Print "Duck Number" if the number satisfies the condition, otherwise
print "Not a Duck Number".
)
Sample Input:4023
.in
ac
Sample Output:Duck Number
ty.
rsi
ive
Completion Status: Completed
n
su
tia
Concepts Included:
lgo
ga
Source Code:
c
4s
t.2
import java.util.Scanner;
an
String number=sc.nextLine().trim();
rya
sc.close();
tA
if(number.charAt(0)=='0'){
an
} else if(number.contains("0")){
Ni
System.out.println("Duck Number");
} else{
System.out.println("Not a Duck Number");
}
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Duck Number
)
.in
TestCase2:
ac
ty.
Input:
rsi
ive
< hidden >
n
su
Expected Output:
tia
lgo
< hidden >
ga
Output:
@
21
Execution Time:
4s
t.2
0.083s
an
ish
(n
its digits, each raised to the power of its position (1-based index), is
Ni
Sample Input:89
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
)
.in
int number=sc.nextInt();
ac
sc.close();
ty.
if(isDisarium(number)){
rsi
System.out.println("Disarium Number");
ive
} else{
n
su
System.out.println("Not a Disarium Number");
} tia
lgo
}
public static boolean isDisarium(int num){
ga
@
String numStr=Integer.toString(num);
21
int sum=0;
07
for(int i=0;i<numStr.length();i++){
18
int digit=numStr.charAt(i)-'0';
1
sum+=Math.pow(digit,i + 1);
se
}
c
4s
return sum==num;
t.2
}
an
}
ish
(n
Compilation Details:
rya
tA
an
TestCase1:
sh
Ni
Input:
< hidden >
Expected Output:
< hidden >
Output:
Not a Disarium Number
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Disarium Number
)
.in
Compilation Status: Passed
ac
ty.
Execution Time:
rsi
ive
0.088s
n
su
tia
47. Check If Two Objects Are Equal Using equals() MethodProblem
lgo
Description:Create a Person class with attributes name and age.Override the equals()
18
method to check equality based on these attributes.Take input for two persons and
1
check if they are the same.Input Format:Two lines, each containing a person's name
se
and age.Output Format:Print "Equal" if the objects are the same, otherwise "Not
c
4s
Equal".
t.2
an
Sample Output:Equal
(n
rya
tA
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
import java.util.Objects;
class Person{
String name;
int age;
Person(String name,int age){
this.name=name;
this.age=age;
}
@Override
public boolean equals(Object obj){
if(this == obj)return true;
if(obj == null || getClass() != obj.getClass()) return false;
Person other=(Person) obj;
return age==other.age&&name.equals(other.name);
}
}
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
)
String name1=sc.next();
.in
ac
int age1=sc.nextInt();
ty.
sc.nextLine();
rsi
String name2=sc.next();
ive
int age2=sc.nextInt();
n
sc.close();
su
Person p1=new Person(name1,age1);
tia
Person p2=new Person(name2,age2);
lgo
if(p1.equals(p2)){
ga
System.out.println("Equal");
@
}else{
21
System.out.println("Not Equal");
07
}
18
}
1
se
}
c
4s
t.2
Compilation Details:
an
ish
(n
TestCase1:
rya
Input:
tA
an
Expected Output:
< hidden >
Output:
Not Equal
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Not Equal
)
.in
ac
ty.
48. Demonstrate Method OverridingProblem Statement:Write a
rsi
ive
Java program to demonstrate method overriding using Animal and
n
Dog classes.
su
tia
Description:Create an Animal class with a method sound().Create a Dog class that
lgo
extends Animal and overrides sound().The program should print different sounds
ga
Sample Input:Dog
1 18
se
Concepts Included:
rya
tA
Source Code:
import java.util.Scanner;
class Animal {
void sound() {
System.out.println("Some generic animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark! Bark!");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow! Meow!");
}
}
class Cow extends Animal {
@Override
void sound() {
System.out.println("Moo! Moo!");
}
}
public class Main {
public static void main(String[] args) {
)
Scanner sc = new Scanner(System.in);
.in
ac
String input = sc.nextLine().trim();
ty.
sc.close();
rsi
Animal animal;
ive
if (input.equals("Dog")) {
n
animal = new Dog();
su
} else if (input.equals("Cat")) {
tia
animal = new Cat();
lgo
} else if (input.equals("Cow")) {
ga
} else {
21
}
18
animal.sound();
1
se
}
c
4s
}
t.2
an
Compilation Details:
ish
(n
rya
TestCase1:
tA
Input:
an
sh
Expected Output:
< hidden >
Output:
Meow! Meow!
Expected Output:
< hidden >
Output:
Moo! Moo!
)
.in
ac
0.088s
ty.
rsi
ive
49. Check If a Class Follows Java Naming ConventionsProblem
n
su
Statement:Write a program to check if a given class name follows
tia
Java naming conventions.
lgo
ga
Sample Input:MyClass
1
se
Concepts Included:
tA
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String className=sc.nextLine().trim();
sc.close();
if(!className.isEmpty() && Character.isUpperCase(className.charAt(0))){
System.out.println("Valid Naming");
} else{
System.out.println("Invalid Naming");
}
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
ac
Output:
ty.
rsi
Invalid Naming
ive
Compilation Status: Passed
n
su
Execution Time: tia
lgo
ga
0.088s
@
21
TestCase2:
07
18
Input:
1
sec
Expected Output:
an
ish
Output:
rya
tA
Valid Naming
an
sh
Execution Time:
0.086s
Sample Output:20
Concepts Included:
gu 28 2nd semester java programming
Source Code:
)
.in
ac
import java.util.Scanner;
ty.
class Rectangle{
rsi
int width;
ive
int height;
n
Rectangle(int w,int h){
su
width=w; tia
lgo
height=h;
}
ga
Rectangle(int w){
@
21
width=w;
07
height=1;
18
}
1
int getArea(){
se
return width*height;
c
4s
}
t.2
}
an
int w=sc.nextInt();
tA
if(sc.hasNextInt()){
an
int h=sc.nextInt();
sh
System.out.println(r.getArea());
} else{
Rectangle r=new Rectangle(w);
System.out.println(r.getArea());
}
sc.close();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
18
)
.in
ac
ty.
TestCase2:
rsi
ive
Input:
n
su
< hidden >
tia
lgo
Expected Output:
ga
Output:
07
18
16
1
sec
Execution Time:
an
ish
0.089s
(n
rya
tA
Sample Input:Savings2000Withdraw500
Sample Output:1500
Source Code:
import java.util.*;
abstract class BankAccount{
protected double balance;
BankAccount(double balance){
this.balance=balance;
}
)
abstract void deposit(double amount);
.in
abstract boolean withdraw(double amount);
ac
public double getBalance(){
ty.
rsi
return balance;
ive
}
}
n
su
class SavingsAccount extends BankAccount{
private static final double MIN_BALANCE=1000; tia
lgo
SavingsAccount(double balance){
ga
super(balance);
@
}
21
@Override
07
balance += amount;
1
se
}
c
@Override
4s
if(balance-amount>=MIN_BALANCE){
an
balance-=amount;
ish
return true;
(n
} else{
rya
System.out.println("Insufficient balance");
tA
return false;
an
}
sh
}
Ni
}
class CurrentAccount extends BankAccount{
CurrentAccount(double balance){
super(balance);
}
@Override
void deposit(double amount){
balance += amount;
}
@Override
boolean withdraw(double amount){
balance -= amount;
return true;
}
}
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String accountType=sc.next();
double initialBalance=sc.nextDouble();
String transactionType=sc.next();
double amount=sc.nextDouble();
BankAccount account;
if(accountType.equalsIgnoreCase("Savings")){
account=new SavingsAccount(initialBalance);
} else{
account=new CurrentAccount(initialBalance);
}
boolean success=false;
)
if(transactionType.equalsIgnoreCase("Deposit")){
.in
ac
account.deposit(amount);
ty.
success=true;
rsi
}else if(transactionType.equalsIgnoreCase("Withdraw")){
ive
success=account.withdraw(amount);
n
}
su
if(success){
tia
System.out.println((int)account.getBalance());
lgo
}
ga
}
@
}
21
07
18
Compilation Details:
1
sec
4s
TestCase1:
t.2
an
Input:
ish
Expected Output:
tA
an
Output:
Ni
2000
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Insufficient balance
)
.in
ac
Write a Java program that categorizes a list of integers into different ranges and
ty.
counts the number of integers in each range.
rsi
ive
Description:
n
su
Your program should:
tia
Take a series of integers as input until a negative number is encountered (the
lgo
categorize each integer into one of the following ranges:0 to 1011 to 2021 to 3031
@
21
Four integers representing the count of numbers in the ranges 0-10, 11-20, 21-30, and
4s
31 and above.
t.2
an
Concepts Included:
sh
Ni
Source Code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count0to10=0;
int count11to20=0;
int count21to30=0;
int count31above=0;
while(true){
int num=sc.nextInt();
if(num<0) break;
if(num<=10){
count0to10++;
} else if(num<=20){
count11to20++;
} else if(num<=30){
count21to30++;
} else{
count31above++;
}
}
System.out.println(count0to10 +" "+ count11to20 +" "+ count21to30 +" "+
)
count31above);
.in
ac
}
ty.
}
rsi
ive
Compilation Details:
n
su
tia
TestCase1:
lgo
ga
Input:
@
21
Expected Output:
1
se
Output:
an
ish
1111
(n
Execution Time:
an
sh
0.124s
Ni
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
2111
Compilation Status: Passed
Execution Time:
0.124s
)
numbersTwo floating-point numbersA list of integers (varargs)A list of floating-point
.in
numbers (varargs)Each method should return the result of the operation.The program
ac
should demonstrate method overloading by calling each method with different types
ty.
rsi
of inputs.Input Format:Operation Type (Addition, Subtraction, Multiplication, or
ive
Division).Type of Numbers (int or float).Two numbers (for single operation).A list of
n
numbers (for varargs operations).Output Format:The result of the specified operation.
su
sample input:Addition int 5 3sample output:Sum of 5 and 3 is: 8 tia
lgo
ga
@
Concepts Included:
1
se
Source Code:
rya
import java.util.Scanner;
tA
return a + b;
Ni
}
public static int subtract(int a, int b) {
return a - b;
}
public static int multiply(int a, int b) {
return a * b;
}
public static int divide(int a, int b) {
return b != 0 ? a / b : 0;
}
public static float add(float a, float b) {
return a + b;
}
public static float subtract(float a, float b) {
return a - b;
}
public static float multiply(float a, float b) {
return a * b;
}
public static float divide(float a, float b) {
return b != 0 ? a / b : 0;
}
public static int add(int... nums) {
int sum = 0;
for (int n : nums) sum += n;
return sum;
}
public static int multiply(int... nums) {
int product = 1;
)
for (int n : nums) product *= n;
.in
ac
return product;
ty.
}
rsi
public static float add(float... nums) {
ive
float sum = 0;
n
for (float n : nums) sum += n;
su
return sum;
tia
}
lgo
float product = 1;
@
return product;
07
}
18
if (sc.hasNextInt() || sc.hasNextFloat()) {
ish
if (type.equals("int")) {
(n
int a = sc.nextInt();
rya
case "Addition":
an
System.out.println("Sum of " + a + " and " + b + " is: " + add(a, b));
sh
break;
Ni
case "Subtraction":
System.out.println("Difference of " + a + " and " + b + " is: " + subtract(a, b));
break;
case "Multiplication":
System.out.println("Product of " + a + " and " + b + " is: " + multiply(a, b));
break;
case "Division":
System.out.println("Quotient of " + a + " and " + b + " is: " + divide(a, b));
break;
}
} else if (type.equals("float")) {
float a = sc.nextFloat();
float b = sc.hasNextFloat() ? sc.nextFloat() : 0;
switch (operation) {
case "Addition":
System.out.println("Sum of " + a + " and " + b + " is: " + add(a, b));
break;
case "Subtraction":
System.out.println("Difference of " + a + " and " + b + " is: " + subtract(a, b));
break;
case "Multiplication":
System.out.println("Product of " + a + " and " + b + " is: " + multiply(a, b));
break;
case "Division":
System.out.println("Quotient of " + a + " and " + b + " is: " + divide(a, b));
break;
}
}
}
)
}
.in
ac
}
ty.
rsi
Compilation Details:
iven
su
TestCase1: tia
lgo
Input:
ga
@
Expected Output:
1 18
Output:
t.2
an
Execution Time:
tA
an
0.138s
sh
Ni
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Quotient of 9.0 and 3.0 is: 3.0
Input Format:1. String name2. Integer id3. Integer basicSalary4. Integer bonus
)
.in
Sample Input:John 101 50000 7000
ac
ty.
Sample Output:57000
rsi
iven
su
Completion Status: Completed
tia
lgo
Concepts Included:
ga
@
Source Code:
t.2
import java.util.Scanner;
an
ish
//Base class
class Employee{
(n
String name;
rya
int id;
tA
int basicSalary;
an
//Constructor
sh
this.name=name;
this.id=id;
this.basicSalary=basicSalary;
}
}
//Derived class
class Engineer extends Employee{
int bonus;
//Constructor
public Engineer(String name,int id,int basicSalary,int bonus){
super(name,id,basicSalary);
this.bonus=bonus;
}
public int calculateTotalSalary(){
return basicSalary+bonus;
}
}
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String name=sc.next();
int id=sc.nextInt();
int basicSalary=sc.nextInt();
int bonus=sc.nextInt();
Engineer engineer=new Engineer(name,id,basicSalary,bonus);
System.out.println(engineer.calculateTotalSalary());
}
}
)
.in
Compilation Details:
ac
ty.
rsi
TestCase1:
iven
Input:
su
< hidden > tia
lgo
ga
Expected Output:
@
21
Output:
1
se
75000
c
4s
Execution Time:
ish
(n
0.095s
rya
tA
TestCase2:
an
sh
Input:
Ni
Expected Output:
< hidden >
Output:
90000
Input Format:1. String title2. String author3. String ISBN4. Integer fileSize
)
.in
Sample Output:Title: JavaProgramming Author: JohnDoe ISBN: 9781234567890
ac
File Size: 5MB
ty.
rsi
ive
Completion Status: Completed
n
su
tia
Concepts Included:
lgo
ga
Source Code:
c
4s
t.2
import java.util.Scanner;
an
class LibraryItem {
ish
String title;
String author;
(n
this.title = title;
tA
this.author = author;
an
}
sh
}
Ni
)
}
.in
ac
}
ty.
rsi
Compilation Details:
iven
su
TestCase1: tia
lgo
Input:
ga
@
Expected Output:
1 18
Output:
t.2
an
Title: BigDataAnalytics
ish
Author: AliceCooper
(n
ISBN: 9876543210
rya
Execution Time:
Ni
0.103s
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Title: IntroToAI
Author: BobMarley
ISBN: 1122334455
File Size: 1MB
)
Description:Create a class DivideByZeroException to handle cases where division by
.in
zero is attempted.
ac
ty.
Input Format:Two integers a and b, where a is divided by b.
rsi
ive
Output Format:The result of the division or an error message if division by zero is
n
attempted.Sample Input:10 0
su
Sample Output:Cannot divide by zero
tia
lgo
ga
@
Concepts Included:
1
sec
Source Code:
rya
tA
import java.util.Scanner;
an
Compilation Details:
TestCase1:
)
.in
Input:
ac
ty.
rsi
< hidden >
ive
Expected Output:
n
su
< hidden > tia
lgo
Output:
ga
@
Result: 15
21
07
Execution Time:
c
4s
0.107s
t.2
an
ish
TestCase2:
(n
Input:
rya
tA
Expected Output:
Ni
Output:
Result: 20
Input Format:
An error message if the age is less than 18, or a success message otherwise.Sample
Input:17
)
.in
Completion Status: Completed
ac
ty.
rsi
Concepts Included:
ive
gu 28 2nd semester java programming
n
su
tia
Language Used: JAVA 8
lgo
ga
@
Source Code:
21
07
import java.util.Scanner;
18
super(message);
t.2
}
an
}
ish
if(age<18){
tA
} else{
sh
System.out.println("Valid age");
Ni
}
}
public static void main(String[] agrs){
Scanner sc=new Scanner(System.in);
int age=sc.nextInt();
try{
checkAge(age);
} catch(InvalidAgeException e){
System.out.println(e.getMessage());
}
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Valid age
)
.in
ac
0.087s
ty.
rsi
ive
TestCase2:
n
su
Input:
tia
lgo
< hidden >
ga
Expected Output:
@
21
07
Output:
1
sec
Execution Time:
(n
rya
0.087s
tA
an
sh
calculates the bitwise AND, OR, XOR, and NOT operations on two
integers provided by the user.
Description:The program should read two integers and then compute and display the
results of the bitwise AND, OR, XOR, and NOT operations.
Input Format:
Four lines, each representing the result of bitwise AND, OR, XOR, and NOT operations
respectively.
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.Scanner;
)
.in
public class Main {
ac
public static void main(String[] args) {
ty.
Scanner sc = new Scanner(System.in);
rsi
int a = sc.nextInt();
ive
int b = sc.nextInt();
n
su
int and = a & b;
int or = a | b; tia
lgo
int xor = a ^ b;
ga
}
sec
}
4s
t.2
an
Compilation Details:
ish
(n
TestCase1:
rya
tA
Input:
an
sh
Expected Output:
< hidden >
Output:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT (first number): -6
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Bitwise AND: 0
Bitwise OR: 10
)
.in
Bitwise XOR: 10
ac
Bitwise NOT (first number): -9
ty.
rsi
Compilation Status: Passed
iven
Execution Time:
su
0.106s tia
lgo
ga
@
can deposit and withdraw money from a shared bank account. The
1
se
concurrently.The Main class should initialize multiple user threads to test the banking
rya
Concepts Included:
gu 28 2nd semester java programming
class BankAccount {
private int balance;
)
.in
public synchronized void withdraw(int amount) {
ac
ty.
if (amount <= balance) {
rsi
balance -= amount;
ive
System.out.println("Withdrawn");
n
} else {
su
System.out.println("Insufficient funds");
} tia
lgo
}
ga
@
return balance;
07
}
18
}
1
sec
this.account = account;
tA
this.transactionType = transactionType;
an
this.amount = amount;
sh
}
Ni
)
} catch (InterruptedException e) {
.in
ac
e.printStackTrace();
ty.
}
rsi
}
ive
}
n
}
su
tia
lgo
Compilation Details:
ga
@
TestCase1:
21
07
Input:
1 18
se
Expected Output:
t.2
an
Output:
rya
Withdrawn
tA
Withdrawn
an
sh
Execution Time:
0.09s
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Deposited
Deposited
Deposited
)
.in
the other thread prints odd numbers up to a given limit.
ac
ty.
Description:
rsi
ive
Your program should:
n
su
Take an integer input from the user.Create two threads: one to print even numbers
tia
and another to print odd numbers up to the given limit.Input Format:
lgo
Even and odd numbers up to the given limit printed by two separate threads.Sample
21
6Even Thread: 8Odd Thread: 1Odd Thread: 3Odd Thread: 5Odd Thread: 7
1 18
sec
4s
Concepts Included:
ish
(n
Source Code:
import java.util.Scanner;
)
}
.in
ac
}
ty.
rsi
public class Main {
ive
public static void main(String[] args) {
n
Scanner scanner = new Scanner(System.in);
su
int limit = scanner.nextInt();
tia
scanner.close();
lgo
ga
evenThread.start();
1
se
try {
c
4s
} catch (InterruptedException e) {
an
e.printStackTrace();
ish
}
(n
rya
try {
an
oddThread.join();
sh
} catch (InterruptedException e) {
Ni
e.printStackTrace();
}
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Even Thread: 0
Even Thread: 2
Even Thread: 4
Odd Thread: 1
Odd Thread: 3
Odd Thread: 5
)
.in
ac
0.105s
ty.
rsi
TestCase2:
iven
Input:
su
tia
< hidden >
lgo
ga
Expected Output:
@
21
Output:
1
se
Even Thread: 0
c
4s
Even Thread: 2
t.2
Even Thread: 4
an
Odd Thread: 1
ish
Odd Thread: 3
(n
Odd Thread: 5
rya
Execution Time:
sh
Ni
0.109s
Concepts Included:
gu 28 2nd semester java programming
Source Code:
import java.util.*;
)
.in
Queue<Integer> q1 = new LinkedList<>();
ac
Queue<Integer> q2 = new LinkedList<>();
ty.
rsi
public void push(int x) {
ive
q2.offer(x);
n
su
while (!q1.isEmpty()) {
q2.offer(q1.poll()); tia
lgo
}
Queue<Integer> temp = q1;
ga
@
q1 = q2;
21
q2 = temp;
07
}
1 18
return q1.poll();
t.2
}
an
ish
return q1.peek();
tA
}
an
}
sh
Ni
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.isEmpty()) break;
switch (command) {
case "push":
int value = Integer.parseInt(parts[1]);
stack.push(value);
break;
case "pop":
stack.pop(); // no print
break;
case "peek":
int top = stack.peek();
if (top != -1) System.out.println(top);
break;
}
}
sc.close();
}
}
)
.in
Compilation Details:
ac
ty.
rsi
TestCase1:
iven
Input:
su
< hidden > tia
lgo
ga
Expected Output:
@
21
Output:
1
se
30
c
4s
Execution Time:
ish
(n
0.088s
rya
tA
TestCase2:
an
sh
Input:
Ni
Expected Output:
< hidden >
Output:
5
)
.in
Completion Status: Completed
ac
ty.
rsi
Concepts Included:
ive
gu 28 2nd semester java programming
n
su
tia
Language Used: JAVA 8
lgo
ga
@
Source Code:
21
07
import java.util.Scanner;
1 18
while (b != 0) {
an
int temp = b;
ish
b = a % b;
(n
a = temp;
rya
}
tA
return a;
an
}
sh
Ni
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
6
)
.in
ac
ty.
TestCase2:
rsi
ive
Input:
n
su
< hidden >
tia
lgo
Expected Output:
ga
Output:
07
18
50
1
sec
Execution Time:
an
ish
0.089s
(n
rya
tA
an
sh
Ni