0% found this document useful (0 votes)
7 views112 pages

Codekata Report 1749553424937

The Codekata report details the programming tasks completed by Nishant Arya, a student at the School of Computer Science & Engineering. It includes various Java programming problems such as reversing an integer, finding the sum of digits, swapping numbers, computing factorials, validating Java identifiers, checking for Armstrong numbers, and determining prime numbers. Each task includes a problem statement, input/output format, sample inputs/outputs, source code, and compilation details.

Uploaded by

nishantarya383
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)
7 views112 pages

Codekata Report 1749553424937

The Codekata report details the programming tasks completed by Nishant Arya, a student at the School of Computer Science & Engineering. It includes various Java programming problems such as reversing an integer, finding the sum of digits, swapping numbers, computing factorials, validating Java identifiers, checking for Armstrong numbers, and determining prime numbers. Each task includes a problem statement, input/output format, sample inputs/outputs, source code, and compilation details.

Uploaded by

nishantarya383
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/ 112

Codekata Report:

Name: Nishant Arya

Email: [email protected]

Specialization: School of Computer Science & Engineering

Completion Year: 2028-2nd Sem

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

Output Format:Print the reversed integer.


@
21

Sample Input:1234
07
18

Sample Output:4321
1
sec
4s

Completion Status: Completed


t.2
an
ish

Concepts Included:
(n
rya

gu 28 2nd semester java programming


tA
an

Language Used: JAVA 8


sh
Ni

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

Compilation Status: Passed


@
21

Execution Time:
07
18

0.09s
1
sec

TestCase2:
4s
t.2

Input:
an
ish

< hidden >


(n
rya

Expected Output:
tA

< hidden >


an
sh

Output:
Ni

Compilation Status: Passed


Execution Time:
0.088s

2. Find the Sum of Digits of a NumberProblem Statement:Write a


program to find the sum of digits of an integer.
Description:Compute the sum of all digits of a given number.
Input Format:A single integer n (1 ≤ n ≤ 10^9).

Output Format:Print the sum of digits.

Sample Input:1234

Sample Output:10

Completion Status: Completed

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

< hidden >

Expected Output:
< hidden >

Output:
35

Compilation Status: Passed


Execution Time:
0.088s
TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
2

Compilation Status: Passed


Execution Time:

)
.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
@

arithmetic operations, and print the swapped values.


21
07

Input Format:Two space-separated integers.


1 18

Output Format:Two space-separated integers after swapping.


sec
4s

Sample Input:3 5
t.2

Sample Output:5 3
an
ish
(n
rya

Completion Status: Completed


tA
an

Concepts Included:
sh
Ni

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 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

< hidden >


an
ish

Expected Output:
(n
rya

< hidden >


tA

Output:
an
sh

-9 -1
Ni

Compilation Status: Passed


Execution Time:
0.113s

4. Factorial of a NumberProblem Statement:Write a program to


compute the factorial of a given number.
Description:Factorial of N is N! = N × (N-1) × (N-2) ... × 1.

Input Format:A single integer N (0 ≤ N ≤ 20).


Output Format:Print the factorial of N.

Sample Input:5

Sample Output:120

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

)
.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

< hidden >


sh
Ni

Expected Output:
< hidden >

Output:
720

Compilation Status: Passed


Execution Time:
0.089s

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
3628800

Compilation Status: Passed


Execution Time:
0.09s

)
.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

not be a reserved keyword in Java.


ga
@

Description:The program should read a string input and verify if it follows the Java
21

identifier rules. Additionally, it should check if the identifier is a Java reserved


07

keyword and return false in that case.


1 18

Input Format:A single string representing the identifier.


sec
4s

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

Completion Status: Completed


sh
Ni

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

if(!Character.isLetterOrDigit(ch) && ch!='_'){


@

return false;
21

}
07

}
18

return true;
1
se

}
c
4s

}
t.2
an

Compilation Details:
ish
(n
rya

TestCase1:
tA

Input:
an
sh

< hidden >


Ni

Expected Output:
< hidden >

Output:
true

Compilation Status: Passed


Execution Time:
0.086s
TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
false

Compilation Status: Passed


Execution Time:

)
.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
@

Armstrong number because:1^3 + 5^3 +3^3 = 153


21
07

Input Format:A single integer N (1 ≤ N ≤ 10^6).


1 18

Output Format:true if the number is an Armstrong number, otherwise false.


se
c

Sample Input:153
4s
t.2

Sample Output:true
an
ish
(n

Completion Status: Completed


rya
tA
an

Concepts Included:
sh
Ni

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 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

Compilation Status: Passed


1
sec

Execution Time:
4s
t.2

0.088s
an
ish

TestCase2:
(n
rya

Input:
tA
an

< hidden >


sh
Ni

Expected Output:
< hidden >

Output:
false

Compilation Status: Passed


Execution Time:
0.091s
7. Control StatementsProblem Statement:Write a program that
checks if a given number is prime using control statements.
Description:The program should read an integer and determine whether it is a prime
number. A prime number is a number greater than 1 that has no divisors other than 1
and itself.

Input Format:

A single integer.Output Format:

"Prime" if the number is prime, otherwise "Not Prime".

Sample Input:5Sample Output:Prime

)
.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

public class Main{


1
se

public static void main(String[] args){


c

Scanner sc=new Scanner(System.in);


4s

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

public static boolean isPrime(int n){


Ni

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

Compilation Status: Passed


Execution Time:

)
.in
ac
0.09s

ty.
rsi
ive
TestCase2:

n
su
Input:
tia
lgo
< hidden >
ga

Expected Output:
@
21
07

< hidden >


18

Output:
1
sec

Not Prime
4s
t.2

Compilation Status: Passed


an
ish

Execution Time:
(n
rya

0.084s
tA
an
sh

8. ConstructorsProblem Statement:Write a program that


Ni

demonstrates the use of parameterized constructors by creating a


class representing a rectangle and calculating its area.
Description:The program should have a Rectangle class with a parameterized
constructor to initialize the length and width. The class should have a method to
calculate the area of the rectangle.

Input Format:

Two integers representing the length and width of the rectangle.Output Format:

The area of the rectangle.Sample Input:5 10Sample Output:50


Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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
@

public class Main{


21

public static void main(String[] args){


07

Scanner sc=new Scanner(System.in);


18

int length=sc.nextInt();
1

int width=sc.nextInt();
se

Rectangle rect=new Rectangle(length,width);


c
4s

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

Compilation Status: Passed


Execution Time:
0.091s

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

Statement:Simulate a "goto" statement using loops and conditional


ga
@

statements. Write a program that continuously asks the user for


21

input and stops only when the user inputs a specific keyword.
07
18

Description:The program should use loops to simulate a "goto" statement. It should


1
se

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

Multiple strings, one per line.Output Format:


(n

Each string entered by the user until the keyword "stop" is encountered.
rya
tA

Sample Input:helloworldstopSample Output:helloworld


an
sh
Ni

Completion Status: Completed

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);
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

Compilation Status: Passed


1
sec

Execution Time:
4s
t.2

0.085s
an
ish

TestCase2:
(n
rya

Input:
tA
an

< hidden >


sh
Ni

Expected Output:
< hidden >

Output:
java
programming

Compilation Status: Passed


Execution Time:
0.097s
10. Boolean OperatorsProblem Statement:Write a program that
uses boolean operators to evaluate complex conditions. The
program should determine if a person qualifies for a special
discount based on multiple criteria.
Description:The program should take three boolean inputs representing whether a
person is a senior citizen, a student, and if they have a membership card. The
program should output true if the person qualifies for the discount and false
otherwise. A person qualifies if they meet at least two out of the three criteria.

Input Format:

Three boolean values separated by spaces.Output Format:A single boolean value


(true or fallse).Sample Input:true false trueSample Output:true

)
.in
ac
Completion Status: Completed

ty.
rsi
ive
Concepts Included:

n
su
gu 28 2nd semester java programming
tia
lgo

Language Used: JAVA 8


ga
@
21

Source Code:
07
18

import java.util.Scanner;
1
se

public class Main{


c

public static void main(String[] args){


4s

Scanner sc=new Scanner(System.in);


t.2

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

Compilation Status: Passed


Execution Time:
0.091s

TestCase2:
Input:

)
.in
< hidden >

ac
ty.
Expected Output:

rsi
ive
< hidden >

n
su
Output:
tia
lgo
false
ga

Compilation Status: Passed


@
21

Execution Time:
07
18

0.085s
1
sec
4s
t.2

11. Break and Continue StatementProblem Statement:Write a


an

program that prints the first n numbers, but skips multiples of 3 and
ish

stops if the number is greater than 50.


(n
rya

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

the number is greater than 50 using the break statement.


sh
Ni

Input Format:

A single integer n.Output Format:

The first n numbers with the specified conditions.

Sample input:10Sample Output:1 2 4 5 7 8 10 11 13 14

Completion Status: Completed

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

< hidden >


c
4s
t.2

Expected Output:
an
ish

< hidden >


(n

Output:
rya
tA

1 2 4 5 7 8 10 11 13 14
an

Compilation Status: Passed


sh
Ni

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

Compilation Status: Passed


Execution Time:
0.088s

12. Static MethodsProblem Statement:Write a program that


demonstrates the use of static methods by creating a utility class
with a method to find the maximum of two numbers.

)
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

Sample Input:5 10Sample Output:10


@
21
07

Completion Status: Completed


1 18
sec

Concepts Included:
4s
t.2

gu 28 2nd semester java programming


an
ish

Language Used: JAVA 8


(n
rya
tA

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

< hidden >


1
se

Expected Output:
c
4s
t.2

< hidden >


an
ish

Output:
(n

15
rya
tA

Compilation Status: Passed


an
sh

Execution Time:
Ni

0.087s

13. Demonstrate Inheritance and PolymorphismProblem


Statement:Write a program that demonstrates inheritance and
method overriding in Java. A superclass Shape contains a method
area(), and the subclass Rectangle overrides the method.
Description:The program should calculate the area of a rectangle using method
overriding in the subclass.

Input Format:Two integers representing the length and breadth of the


rectangle.Output Format:
Area of the rectangle.Sample Input:10 5

Sample Output:50.0

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

Rectangle(int length,int breadth){


@
21

this.length=length;
07

this.breadth=breadth;
18

}
1

@Override
se

double area(){
c
4s

return length*breadth;
t.2

}
an

}
ish

public class Main{


(n

public static void main(String[] args){


rya

Scanner sc=new Scanner(System.in);


tA

int length=sc.nextInt();
an

int breadth=sc.nextInt();
sh

Shape rect=new Rectangle(length,breadth);


Ni

System.out.printf("%.1f",rect.area());
}
}

Compilation Details:

TestCase1:
Input:
< hidden >

Expected Output:
< hidden >

Output:
120.0

Compilation Status: Passed


Execution Time:
0.093s

TestCase2:
Input:

)
.in
< hidden >

ac
ty.
Expected Output:

rsi
ive
< hidden >

n
su
Output:
tia
lgo
200.0
ga

Compilation Status: Passed


@
21

Execution Time:
07
18

0.094s
1
sec
4s
t.2

14. Find Longest Substring Without Repeating CharactersProblem


an

Statement:Write a program to find the length of the longest


ish

substring without repeating characters.


(n
rya

Description:The program takes a string and finds the length of the longest substring
tA

without repeating characters.


an
sh

Input Format:A string s.


Ni

Output Format:The length of the longest substring without repeating


characters.Sample Input:abcabcbb Sample Output:3

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8


Source Code:
import java.util.HashSet;
import java.util.Scanner;
public class Main{
public static int lengthOfLongestSubstring(String s){
HashSet<Character>set=new HashSet<>();
int left=0,maxLength=0;
for(int right=0;right<s.length();right++){
while(set.contains(s.charAt(right))){
set.remove(s.charAt(left));
left++;
}
set.add(s.charAt(right));
maxLength=Math.max(maxLength,right-left+1);

)
}

.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

< hidden >


an
ish

Expected Output:
(n
rya

< hidden >


tA

Output:
an
sh

3
Ni

Compilation Status: Passed


Execution Time:
0.089s

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
2

Compilation Status: Passed


Execution Time:
0.087s

15. Evaluate Arithmetic ExpressionProblem Statement:Write a


program that computes a complex arithmetic expression provided
by the user. The expression can include addition, subtraction,

)
.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

Input Format:A single string representing the arithmetic expression.


@
21

Output Format:A single number, which is the result of the evaluated expression.
07
18

Sample Input:(2+3) * (5-2)


1
se

Sample Output:15
c
4s
t.2
an

Completion Status: Completed


ish
(n

Concepts Included:
rya
tA

gu 28 2nd semester java programming


an
sh

Language Used: JAVA 8


Ni

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

Compilation Status: Passed


07
18

Execution Time:
1
se

0.774s
c
4s
t.2

TestCase2:
an
ish

Input:
(n
rya

< hidden >


tA
an

Expected Output:
sh
Ni

< hidden >

Output:
10

Compilation Status: Passed


Execution Time:
0.781s

16. Reverse a StringProblem Statement:Write a program to reverse


a given string without using built-in reverse methods.
Description:The program should take a string as input and return its reversed form.

Input Format:A single string S consisting of alphanumeric characters and spaces.

Output Format:A single string representing the reversed input.

Sample Input:A B C D

Sample Output:D C B A

Completion Status: Completed

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
@

Scanner sc=new Scanner(System.in);


21

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

Compilation Status: Passed


Execution Time:
0.113s

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

a given string without using built-in reverse methods.


ga
@
21

Description:The program should take a string as input and return its reversed form.
07

Input Format:A single string S consisting of alphanumeric characters and spaces.


1 18
se

Output Format:A single string representing the reversed input.


c
4s

Sample Input:A B C D
t.2
an

Sample Output:D C B A
ish
(n
rya

Completion Status: Completed


tA
an

Concepts Included:
sh
Ni

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);
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

< hidden >


an
ish

Expected Output:
(n
rya

< hidden >


tA

Output:
an
sh

racecaR
Ni

Compilation Status: Passed


Execution Time:
0.112s

18. Check if a Number is a Power of TwoProblem Statement:Write a


program to check if a given number is a power of two.
Description:A number is a power of two if there exists an integer x such that 2^x = N.

Input Format:A single integer N (1 ≤ N ≤ 10^9).


Output Format:true if the number is a power of two, otherwise false.

Sample Input:16

Sample Output:true

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

)
.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

public static void main(String[] args){


ga

Scanner sc=new Scanner(System.in);


@

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

< hidden >


an
sh

Expected Output:
Ni

< hidden >

Output:
true

Compilation Status: Passed


Execution Time:
0.09s

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
false

Compilation Status: Passed


Execution Time:
0.089s

)
.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

Rectangle classes that extend Shape.


ga
@

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

Output Format:Print the area of the shape.


1
se

Sample Input:circle5
c
4s
t.2

Sample Output:78.54
an
ish
(n

Completion Status: Completed


rya
tA

Concepts Included:
an
sh

gu 28 2nd semester java programming


Ni

Language Used: JAVA 8

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

Compilation Status: Passed


Execution Time:
0.093s

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

20. Count Occurrences of Each Character in a StringProblem


@

Statement:Given a string, count the occurrences of each character.


21
07

Description:1. Use a HashMap to store character frequencies.2. Ignore case (treat


1 18

uppercase and lowercase as the same).


sec

Input Format:A single string (1 ≤ length ≤ 100).


4s
t.2

Output Format:Print character frequencies in sorted order.


an
ish

Sample Input:Hello
(n

Sample Output:e: 1h: 1l: 2o: 1


rya
tA
an

Completion Status: Completed


sh
Ni

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

Compilation Status: Passed


an
ish

Execution Time:
(n
rya

0.111s
tA
an

TestCase2:
sh
Ni

Input:
< hidden >

Expected Output:
< hidden >

Output:
!: 3
#: 3
@: 3

Compilation Status: Passed


Execution Time:
0.111s

21. Find Duplicate Characters in a StringProblem Statement:Write a


program to find duplicate characters in a string.
Description:Given a string, find all duplicate characters.

Input Format:A single string (1 ≤ length ≤ 100).

Output Format:List of duplicate characters.

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
@

Language Used: JAVA 8


21
07
18

Source Code:
1
sec

import java.util.*;
4s

public class Main{


t.2

public static void main(String[] args){


an

Scanner sc=new Scanner(System.in);


ish

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

< hidden >


07

Expected Output:
1 18
se

< hidden >


c
4s

Output:
t.2
an

an
ish
(n

Compilation Status: Passed


rya

Execution Time:
tA
an

0.091s
sh
Ni

22. Check if Two Strings are AnagramsProblem Statement:Given


two strings, determine if they are anagrams.
Description:Two strings are anagrams if they contain the same characters with the
same frequency.

Input Format:Two strings (1 ≤ length ≤ 100).

Output Format:Print YES if they are anagrams, otherwise NO.

Sample Input:listensilent

Sample Output:YES
Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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
@

public static boolean isAnagram(String s1,String s2){


21

if(s1.length() != s2.length()){
07

return false;
18

}
1

int[] count=new int[26];


se

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

if(c!=0) return false;


an

}
sh

return true;
Ni

}
}

Compilation Details:

TestCase1:
Input:
< hidden >

Expected Output:
< hidden >
Output:
NO

Compilation Status: Passed


Execution Time:
0.089s

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

23. Implement a Basic Calculator Using Switch CaseProblem


c
4s

Statement:Implement a basic calculator that performs +, -, *, and /


t.2

operations.
an
ish

Description:1. Accept two numbers and an operator as input.2. Perform the


(n

calculation and print the result.


rya
tA

Input Format:Two integers and an operator (+, -, *, /).


an
sh

Output Format:Print the calculated result.


Ni

Sample Input:105+

Sample Output:15

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8


Source Code:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int num1=sc.nextInt();
int num2=sc.nextInt();
char operator=sc.next().charAt(0);
int result;
switch(operator){
case'+':
result=num1+num2;
System.out.println(result);
break;

)
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

System.out.println("Cannot Divide By Zero");


c

}
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

24. Find the Greatest Common Divisor (GCD) Using


07

RecursionProblem Statement:Write a recursive function to compute


1 18

the Greatest Common Divisor (GCD) of two numbers.


sec
4s

Description:1. The GCD of two numbers is the largest number that divides both
t.2

numbers without a remainder.2. Use the Euclidean algorithm:


an

GCD(a,b)=GCD(b,amodb)Base case: GCD(a, 0) = a.


ish

Input Format:Two integers a and b (1 ≤ a, b ≤ 10^6).


(n
rya

Output Format:Print the GCD of a and b.


tA
an

Sample Input:48 18
sh

Sample Output:6
Ni

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

< hidden >


1 18

Output:
sec
4s

25
t.2

Compilation Status: Passed


an
ish

Execution Time:
(n
rya

0.085s
tA
an

TestCase2:
sh
Ni

Input:
< hidden >

Expected Output:
< hidden >

Output:
5

Compilation Status: Passed


Execution Time:
0.088s

25. Check if a Number is a Palindrome Problem Statement:Write a


program to check if a number is a palindrome.
Description:1. A palindrome number reads the same forward and backward.2. Do not
use string conversion.

Input Format:A single integer n (1 ≤ n ≤ 10^6).

Output Format:Print YES if the number is a palindrome, otherwise NO.

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

Language Used: JAVA 8


@
21
07

Source Code:
1 18
se

import java.util.*;
c

public class Main{


4s

public static void main(String[] args){


t.2

Scanner sc=new Scanner(System.in);


an

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

< hidden >


07

Expected Output:
1 18
se

< hidden >


c
4s

Output:
t.2
an

YES
ish
(n

Compilation Status: Passed


rya

Execution Time:
tA
an

0.089s
sh
Ni

26. Find the Largest Element in an ArrayProblem Statement:Given


an array of integers, find the largest element.
Description:The program should return the maximum value in the array.

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).

Output Format:Print the largest element.

Sample Input:510 20 5 40 25

Sample Output:40
Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

< hidden >


tA

Expected Output:
an
sh

< hidden >


Ni

Output:
-1

Compilation Status: Passed


Execution Time:
0.087s

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
90

Compilation Status: Passed


Execution Time:
0.087s

)
.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

Output Format:Print the binary representation of n.


@
21

Sample Input:10
07

Sample Output:1010
1 18
sec
4s

Completion Status: Completed


t.2
an

Concepts Included:
ish
(n

gu 28 2nd semester java programming


rya
tA

Language Used: JAVA 8


an
sh
Ni

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

Compilation Status: Passed


ga
@

Execution Time:
21
07

0.117s
1 18
se

TestCase2:
c
4s

Input:
t.2
an

< hidden >


ish
(n

Expected Output:
rya
tA

< hidden >


an

Output:
sh
Ni

100000

Compilation Status: Passed


Execution Time:
0.115s

28. Check if a String is a PangramProblem Statement:Check if a


given string is a pangram (contains all 26 English letters).
Description:Ignore case and spaces.
Input Format:A single string (1 ≤ length ≤ 1000).

Output Format:Print YES if it's a pangram, otherwise NO.

Sample Input:The quick brown fox jumps over the lazy dog

Sample Output:YES

Completion Status: Completed

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

String input=sc.nextLine().toLowerCase().replaceAll(" ","");


@

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

Compilation Status: Passed


Execution Time:
0.086s

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

29. Count Vowels and Consonants in a StringProblem


1
se

Statement:Write a program to count the number of vowels and


c
4s

consonants in a given string.


t.2
an

Description:1. Consider only alphabets (a-z, A-Z), ignore spaces and special
ish

characters.2. Vowels: {a, e, i, o, u} (both uppercase and lowercase).3. Consonants: all


other alphabets.
(n
rya

Input Format:A single string s (1 ≤ length ≤ 1000).


tA
an

Output Format:Print two integers: vowel count and consonant count.


sh

Sample Input:Hello World


Ni

Sample Output:3 7

Completion Status: Completed

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);
String s=sc.nextLine();
sc.close();
int vowelCount=0;
int consonantCount=0;
s=s.toLowerCase();
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
if(ch>='a' && ch<='z'){
if(ch=='a'||ch=='e'|| ch=='i'||ch=='o'||ch=='u'){

)
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

< hidden >


ish
(n

Expected Output:
rya
tA

< hidden >


an

Output:
sh
Ni

54

Compilation Status: Passed


Execution Time:
0.107s

TestCase2:
Input:
< hidden >
Expected Output:
< hidden >

Output:
03

Compilation Status: Passed


Execution Time:
0.105s

30. Find the Second Largest Number in an ArrayProblem

)
.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

Output Format:Print the second-largest number.


@
21

Sample Input:53 1 9 4 6
07
18

Sample Output:6
1
sec
4s

Completion Status: Completed


t.2
an
ish

Concepts Included:
(n
rya

gu 28 2nd semester java programming


tA
an

Language Used: JAVA 8


sh
Ni

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
@

< hidden >


21

Output:
07
18

7
1
sec

Compilation Status: Passed


4s
t.2

Execution Time:
an
ish

0.085s
(n
rya

TestCase2:
tA
an

Input:
sh
Ni

< hidden >

Expected Output:
< hidden >

Output:
20

Compilation Status: Passed


Execution Time:
0.089s
31. Count Words in a StringProblem Statement:Write a program to
count the number of words in a given string.
Description:1. A word is defined as a sequence of characters separated by spaces.2.
Ignore multiple spaces.

Input Format:A single line string s (1 ≤ length ≤ 1000).

Output Format:Print the word count.

Sample Input:Java is fun

Sample Output:3

Completion Status: Completed

)
.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

public class Main{


18

public static void main(String[] args){


1
se

Scanner sc=new Scanner(System.in);


c

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

Compilation Status: Passed


Execution Time:
0.09s

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

32. Find the First Non-Repeating Character in a StringProblem


c
4s

Statement:Write a program to find the first non-repeating character


t.2

in a given string.
an
ish

Description:1. The first non-repeating character is the one that appears only once and
(n

first in order.2. If all characters repeat, return -1.


rya
tA

Input Format:A single string s (1 ≤ length ≤ 1000).


an
sh

Output Format:Print the first non-repeating character or -1 if none exists.


Ni

Sample Input:swiss

Sample Output:w

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8


Source Code:
import java.util.Scanner;
import java.util.LinkedHashMap;
import java.util.Map;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
sc.close();
Map<Character,Integer>freqMap=new LinkedHashMap<>();
for(char ch:s.toCharArray()){
freqMap.put(ch,freqMap.getOrDefault(ch,0)+1);
}
for(Map.Entry<Character,Integer>entry:freqMap.entrySet()){

)
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

< hidden >


t.2
an

Expected Output:
ish
(n

< hidden >


rya

Output:
tA
an

-1
sh
Ni

Compilation Status: Passed


Execution Time:
0.086s

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >
Output:
z

Compilation Status: Passed


Execution Time:
0.086s

33. Find the Maximum Product of Two Numbers in an ArrayProblem


Statement:Write a program to find the maximum product of any two
numbers in an array.

)
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

Completion Status: Completed


07
1 18

Concepts Included:
sec
4s

gu 28 2nd semester java programming


t.2
an
ish

Language Used: JAVA 8


(n
rya

Source Code:
tA
an

import java.util.Scanner;
sh

import java.util.Arrays;
Ni

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();
Arrays.sort(arr);
int product1=arr[n-1]*arr[n-2];
int product2=arr[0]*arr[1];
System.out.println(Math.max(product1,product2));
}
}

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

< hidden >


1
se

Expected Output:
c
4s
t.2

< hidden >


an
ish

Output:
(n

56
rya
tA

Compilation Status: Passed


an
sh

Execution Time:
Ni

0.089s

34. Find Missing Number in an Array Problem Statement:Write a


program to find the missing number in an array containing n-1
distinct numbers from 1 to n.
Description:1. The array contains numbers from 1 to n but one number is missing.2.
Find the missing number efficiently.

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

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

)
.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

< hidden >

Expected Output:
< hidden >

Output:
7

Compilation Status: Passed


Execution Time:
0.086s
TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
2

Compilation Status: Passed


Execution Time:

)
.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

Input Format:A single line string s (1 ≤ length ≤ 1000).


07
18

Output Format:Print the longest word.


1
se

Sample Input:I love programming in Java


c
4s

Sample Output:programming
t.2
an
ish

Completion Status: Completed


(n
rya
tA

Concepts Included:
an
sh

gu 28 2nd semester java programming


Ni

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);
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

Compilation Status: Passed


07
18

Execution Time:
1
se

0.087s
c
4s
t.2

TestCase2:
an
ish

Input:
(n
rya

< hidden >


tA
an

Expected Output:
sh
Ni

< hidden >

Output:
everything

Compilation Status: Passed


Execution Time:
0.087s

36. Sum of Digits Until Single Digit (Digital Root)Problem


Statement:Write a program to find the digital root of a number.
Description:Keep summing the digits of a number until a single-digit remains.

Input Format:A single integer n (1 ≤ n ≤ 10^9).

Output Format:Print the digital root.

Sample Input:9875

Sample Output:2

Completion Status: Completed

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
@

Scanner sc=new Scanner(System.in);


21

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

Compilation Status: Passed


Execution Time:
0.088s

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

37. Find the Majority Element in an ArrayProblem Statement:Write a


1
se

program to find the majority element in an array. A majority element


c
4s

appears more than n/2 times in an array of size n.


t.2
an

Description:If no majority element exists, return -1.


ish

Input Format:1. First line: An integer n (1 ≤ n ≤ 10^5).2. Second line: n space-


(n

separated integers.
rya
tA

Output Format:Print the majority element or -1 if none exists.


an

Sample Input:5 3 3 4 2 3
sh
Ni

Sample Output:3

Completion Status: Completed

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[] 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

38. Reverse Words in a String Problem Statement:Write a program


07

to reverse the order of words in a given string.


1 18
se

Description:1. Words are separated by spaces.2. Maintain the words' internal order
c
4s

but reverse their sequence.


t.2

Input Format:A single string s (1 ≤ length ≤ 1000).


an
ish

Output Format:Print the reversed sentence.


(n
rya

Sample Input:Hello World in Java


tA

Sample Output:Java in World Hello


an
sh
Ni

Completion Status: Completed

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);
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
@

< hidden >


21

Output:
07
18

#code @java 456 123


1
sec

Compilation Status: Passed


4s
t.2

Execution Time:
an
ish

0.09s
(n
rya

TestCase2:
tA
an

Input:
sh
Ni

< hidden >

Expected Output:
< hidden >

Output:
language! best the Java, to Welcome

Compilation Status: Passed


Execution Time:
0.086s
39. Find Pairs with Given Sum in an ArrayProblem Statement:Write
a program to find all unique pairs in an array that sum up to a given
number k.
Description:Output each pair in ascending order.

Input Format:1. First line: An integer n (1 ≤ n ≤ 10^5).2. Second line: n space-


separated integers.3. Third line: An integer k.

Output Format:Print each unique pair. If no pairs exist, print -1.

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

public class Main{


1
se

public static void main(String[] args){


c

Scanner sc=new Scanner(System.in);


4s

int n=sc.nextInt();
t.2

int[] arr=new int[n];


an

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

Compilation Status: Passed


1
se
c

Execution Time:
4s
t.2

0.086s
an
ish

TestCase2:
(n
rya

Input:
tA
an

< hidden >


sh
Ni

Expected Output:
< hidden >

Output:
(5,6)
(2,9)

Compilation Status: Passed


Execution Time:
0.111s
40. Check if the Number is an Automorphic NumberProblem
Statement:Write a program to check whether a given number is an
Automorphic Number or not. A number is called Automorphic if its
square ends with the number itself.
Description:The program should take an integer input and determine whether it is an
Automorphic Number. The program should calculate the square of the number,
extract the last digits equivalent to the length of the original number, and compare
them.

Input Format:A single integer N (1 ≤ N ≤ 10000)

Output Format:Print "Automorphic Number" if the number satisfies the condition,


otherwise print "Not an Automorphic Number".

)
Sample Input:25

.in
ac
Sample Output:Automorphic Number

ty.
rsi
ive
Completion Status: Completed

n
su
tia
lgo
Concepts Included:
ga

gu 28 2nd semester java programming


@
21
07

Language Used: JAVA 8


1 18
se

Source Code:
c
4s
t.2

import java.util.Scanner;
an

public class Main{


ish

public static void main(String[] args){


(n

Scanner sc=new Scanner(System.in);


rya

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

Compilation Status: Passed


Execution Time:
0.091s

)
.in
ac
ty.
TestCase2:

rsi
ive
Input:

n
su
< hidden >
tia
lgo
Expected Output:
ga

< hidden >


@
21

Output:
07
18

Automorphic Number
1
sec

Compilation Status: Passed


4s
t.2

Execution Time:
an
ish

0.091s
(n
rya
tA

41. Find the Longest Common Prefix of StringsProblem


an

Statement:Write a program to find the longest common prefix


sh

among an array of strings. If there is no common prefix, return an


Ni

empty string ("").


Description:1. The program should take an integer n as input, representing the
number of strings.2. The next n lines contain the strings.3. The output should be the
longest common prefix shared among all strings.4. If no common prefix exists, return
an empty string.

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.

Output Format:1. A single string representing the longest common prefix.2. If no


common prefix exists, return "".
Sample Input:3 flower flow flight

Sample Output:fl

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

Compilation Status: Passed


Execution Time:
0.092s

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

42. Implement a Student Management System


c
4s

(Inheritance)Problem Statement:Create a Student class and a


t.2

GraduateStudent class that inherits from Student.


an
ish

Description:1. Student should have attributes: name, rollNumber, and marks.2.


(n

GraduateStudent extends Student and adds a thesisTitle attribute.3. Implement a


rya

method to display final grade based on marks:90-100 A75-89 B50-74 CBelow 50


tA

Fail
an
sh

Input Format:1. A string name2. An integer rollNumber3. An integer marks4. A string


Ni

thesisTitle

Output Format:Print grade and thesis title.

Sample Input:Alice10195Deep Learning Research

Sample Output:ADeep Learning Research

Completion Status: Completed

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

class GraduateStudent extends Student {


@

String thesisTitle;
21

public GraduateStudent(String name, int rollNumber, int marks, String thesisTitle) {


07

super(name, rollNumber, marks);


18

this.thesisTitle = thesisTitle;
1

}
sec

public void displayInfo() {


4s

System.out.println(getGrade());
t.2

System.out.println(thesisTitle);
an

}
ish

}
(n

public class Main {


rya

public static void main(String[] args) {


tA

Scanner scanner = new Scanner(System.in);


an

String name = scanner.nextLine();


sh

int rollNumber = scanner.nextInt();


Ni

int marks = scanner.nextInt();


scanner.nextLine();
String thesisTitle = scanner.nextLine();
GraduateStudent grad = new GraduateStudent(name, rollNumber, marks, thesisTitle);
grad.displayInfo();
scanner.close();
}
}

Compilation Details:

TestCase1:
Input:
< hidden >

Expected Output:
< hidden >

Output:
B
Data Science Analysis

Compilation Status: Passed


Execution Time:

)
.in
0.087s

ac
ty.
rsi
TestCase2:

iven
Input:

su
< hidden > tia
lgo
ga

Expected Output:
@
21

< hidden >


07

Output:
118
se

Fail
c

AI Ethics Study
4s
t.2

Compilation Status: Passed


an
ish

Execution Time:
(n
rya

0.094s
tA
an
sh

43. Implement a Student Management System


Ni

(Inheritance)Problem Statement:Create a Student class and a


GraduateStudent class that inherits from Student.
Description:1. Student should have attributes: name, rollNumber, and marks.2.
GraduateStudent extends Student and adds a thesisTitle attribute.3. Implement a
method to display final grade based on marks:90-100 A75-89 B50-74 CBelow 50
Fail

Input Format:1. A string name2. An integer rollNumber3. An integer marks4. A string


thesisTitle

Output Format:Print grade and thesis title.

Sample Input:Alice10195Deep Learning Research


Sample Output:ADeep Learning Research

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

if (marks >= 90 && marks <= 100) return "A";


07

else if (marks >= 75) return "B";


18

else if (marks >= 50) return "C";


1

else return "Fail";


sec

}
4s

}
t.2

class GraduateStudent extends Student {


an

String thesisTitle;
ish

GraduateStudent(String name, int rollNumber, int marks, String thesisTitle) {


(n

super(name, rollNumber, marks);


rya

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

< hidden >


4s
t.2

Expected Output:
an
ish

< hidden >


(n

Output:
rya
tA

Fail
an

AI Ethics Study
sh
Ni

Compilation Status: Passed


Execution Time:
0.092s

44. Find the Median of Two Sorted ArraysProblem Statement:Given


two sorted arrays nums1 and nums2, find the median of the
combined sorted array.
Description:1. The median of a sorted array is the middle element if the number of
elements is odd.2. If the number of elements is even, the median is the average of the
two middle elements.3. We need to efficiently find the median without merging and
sorting the two arrays completely.4. We use a binary search approach to achieve an
optimal solution.

Input Format:Two sorted integer arrays nums1 and nums2.

Output Format:Print the median as a floating-point number.

Sample Input:21 312

Sample Output:2.0

Completion Status: Completed

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
@

public static void main(String[] args){


21

Scanner sc=new Scanner(System.in);


07

int n1=sc.nextInt();
18

int[] nums1=new int[n1];


1

for(int i=0;i<n1;i++){
se

nums1[i]=sc.nextInt();
c
4s

}
t.2

int n2=sc.nextInt();
an

int[] nums2=new int[n2];


ish

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

public static double findMedianSortedArrays(int[] nums1,int[] nums2){


if(nums1.length>nums2.length){
return findMedianSortedArrays(nums2,nums1);
}
int x=nums1.length;
int y=nums2.length;
int low=0,high=x;
while(low<=high){
int partitionX=(low + high)/2;
int partitionY=(x+ y + 1)/2-partitionX;
int maxLeftX=(partitionX==0)?Integer.MIN_VALUE:nums1[partitionX - 1];
int minRightX=(partitionX==x)?Integer.MAX_VALUE:nums1[partitionX];
int maxLeftY=(partitionY==0)?Integer.MIN_VALUE:nums2[partitionY - 1];
int minRightY=(partitionY==y)?Integer.MAX_VALUE:nums2[partitionY];
if(maxLeftX<=minRightY && maxLeftY<=minRightX){
if((x+y)%2==0){
return(Math.max(maxLeftX,maxLeftY)+Math.min(minRightX,minRightY))/2.0;
}else {
return Math.max(maxLeftX,maxLeftY);
}
} else if(maxLeftX>minRightY){
high=partitionX - 1;
}else{
low=partitionX + 1;
}
}
throw new IllegalArgumentException("Input arrays are not sorted properly.");
}
}

)
.in
ac
Compilation Details:

ty.
rsi
ive
TestCase1:

n
su
Input: lgo
tia
< hidden >
ga
@

Expected Output:
21
07

< hidden >


1 18

Output:
sec
4s

3.5
t.2

Compilation Status: Passed


an
ish

Execution Time:
(n
rya

0.093s
tA
an

TestCase2:
sh
Ni

Input:
< hidden >

Expected Output:
< hidden >

Output:
15.0

Compilation Status: Passed


Execution Time:
0.088s

45. Check if a Number is a Duck NumberProblem Statement:A Duck


Number is a positive number that contains at least one '0' but does
not start with '0'.
Description:The program should check whether the given number is a Duck Number
or not.

Input Format:A single integer N (1 ≤ N ≤ 100000)

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

gu 28 2nd semester java programming


@
21
07

Language Used: JAVA 8


1 18
se

Source Code:
c
4s
t.2

import java.util.Scanner;
an

public class Main{


ish

public static void main(String[] args){


Scanner sc=new Scanner(System.in);
(n

String number=sc.nextLine().trim();
rya

sc.close();
tA

if(number.charAt(0)=='0'){
an

System.out.println("Not a Duck Number");


sh

} 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

Compilation Status: Passed


Execution Time:
0.088s

)
.in
TestCase2:

ac
ty.
Input:

rsi
ive
< hidden >

n
su
Expected Output:
tia
lgo
< hidden >
ga

Output:
@
21

Not a Duck Number


07
18

Compilation Status: Passed


1
sec

Execution Time:
4s
t.2

0.083s
an
ish
(n

46. Check if a Number is a Disarium NumberProblem


rya

Statement:Write a program that checks whether a given number is a


tA

Disarium Number or not.A number is called Disarium if the sum of


an
sh

its digits, each raised to the power of its position (1-based index), is
Ni

equal to the number itself.


Description:The program should take an integer input and check if it satisfies the
Disarium property.For example, 89 is a Disarium Number because:8¹ + 9² = 8 + 81 =
89

Input Format:A single integer N (1 ≤ N ≤ 10⁶).

Output Format:Print "Disarium Number" if the number is a Disarium Number,


otherwise print "Not a Disarium Number".

Sample Input:89

Sample Output:Disarium Number


Completion Status: Completed

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);

)
.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

Compilation Status: Passed


Execution Time:
0.087s

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

Statement:Write a program that checks if two objects of a class are


ga
@

equal using the equals() method.


21
07

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 Input:Alice 25 Alice 25


ish

Sample Output:Equal
(n
rya
tA

Completion Status: Completed


an
sh
Ni

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

< hidden >


sh
Ni

Expected Output:
< hidden >

Output:
Not Equal

Compilation Status: Passed


Execution Time:
0.093s

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
Not Equal

Compilation Status: Passed


Execution Time:
0.089s

)
.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

based on the object type.Input Format:A single string: "Animal" or "Dog".Output


@
21

Format:Print the respective sound.


07

Sample Input:Dog
1 18
se

Sample Output:Bark! Bark!


c
4s
t.2

Completion Status: Completed


an
ish
(n

Concepts Included:
rya
tA

gu 28 2nd semester java programming


an
sh

Language Used: JAVA 8


Ni

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

animal = new Cow();


@

} else {
21

animal = new Animal();


07

}
18

animal.sound();
1
se

}
c
4s

}
t.2
an

Compilation Details:
ish
(n
rya

TestCase1:
tA

Input:
an
sh

< hidden >


Ni

Expected Output:
< hidden >

Output:
Meow! Meow!

Compilation Status: Passed


Execution Time:
0.085s
TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
Moo! Moo!

Compilation Status: Passed


Execution Time:

)
.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

Description:Class names should start with an uppercase letter.Print "Valid Naming" if


@

correct, else "Invalid Naming".Input Format:A single string representing a class


21

name.Output Format:"Valid Naming" or "Invalid Naming".


07
18

Sample Input:MyClass
1
se

Sample Output:Valid Naming


c
4s
t.2
an

Completion Status: Completed


ish
(n
rya

Concepts Included:
tA

gu 28 2nd semester java programming


an
sh
Ni

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);
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

< hidden >


4s
t.2

Expected Output:
an
ish

< hidden >


(n

Output:
rya
tA

Valid Naming
an
sh

Compilation Status: Passed


Ni

Execution Time:
0.086s

50. Demonstrate Constructor Overloading in JavaProblem


Statement:Write a Java program to demonstrate constructor
overloading.
Description:Create a class Rectangle with overloaded constructors.One constructor
initializes both width and height, another initializes only width with a default
height.Input Format:Two integers representing width and height.Output
Format:"<calculated_area>"
Sample Input:4 5

Sample Output:20

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

public class Main{


ish

public static void main(String[] args){


(n

Scanner sc=new Scanner(System.in);


rya

int w=sc.nextInt();
tA

if(sc.hasNextInt()){
an

int h=sc.nextInt();
sh

Rectangle r=new Rectangle(w,h);


Ni

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

Compilation Status: Passed


Execution Time:
0.086s

)
.in
ac
ty.
TestCase2:

rsi
ive
Input:

n
su
< hidden >
tia
lgo
Expected Output:
ga

< hidden >


@
21

Output:
07
18

16
1
sec

Compilation Status: Passed


4s
t.2

Execution Time:
an
ish

0.089s
(n
rya
tA

51. Implement an Abstract Class for Bank Accounts


an
sh

Problem Statement:Write a program using an abstract class BankAccount with


Ni

abstract methods deposit() and withdraw(). Implement these methods in


SavingsAccount and CurrentAccount.

Description:The SavingsAccount class allows deposits and withdrawals but


maintains a minimum balance of 1000.The CurrentAccount class allows unlimited
transactions.Input Format:Account Type (Savings or Current)Initial
balanceTransaction type (Deposit or Withdraw)Transaction amountOutput
Format:The updated account balance.

Sample Input:Savings2000Withdraw500

Sample Output:1500

Completion Status: Completed


Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

void deposit(double amount){


18

balance += amount;
1
se

}
c

@Override
4s

boolean withdraw(double amount){


t.2

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

< hidden >


(n
rya

Expected Output:
tA
an

< hidden >


sh

Output:
Ni

2000

Compilation Status: Passed


Execution Time:
0.097s

TestCase2:
Input:
< hidden >
Expected Output:
< hidden >

Output:
Insufficient balance

Compilation Status: Passed


Execution Time:
0.099s

52. Branching StatementsQuestion:

)
.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

negative number should not be included in the list).Use branching statements to


ga

categorize each integer into one of the following ranges:0 to 1011 to 2021 to 3031
@
21

and aboveOutput the count of integers in each range.Input Format:


07

A series of integers separated by spaces. The sequence ends when a negative


18

number is encountered.Output Format:


1
sec

Four integers representing the count of numbers in the ranges 0-10, 11-20, 21-30, and
4s

31 and above.
t.2
an

Sample Input:5 15 25 35 -1Sample Output:1 1 1 1


ish
(n
rya

Completion Status: Completed


tA
an

Concepts Included:
sh
Ni

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 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

< hidden >


07
18

Expected Output:
1
se

< hidden >


c
4s
t.2

Output:
an
ish

1111
(n

Compilation Status: Passed


rya
tA

Execution Time:
an
sh

0.124s
Ni

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
2111
Compilation Status: Passed
Execution Time:
0.124s

53. Question:Problem Statement:Write a Java program that


performs mathematical operations (addition, subtraction,
multiplication, and division) using method overloading. The
program should support operations on integers, floating-point
numbers, and lists (varargs).
Description:Implement method overloading to perform operations on:Two integer

)
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
@

Completion Status: Completed


21
07
18

Concepts Included:
1
se

gu 28 2nd semester java programming


c
4s
t.2

Language Used: JAVA 8


an
ish
(n

Source Code:
rya

import java.util.Scanner;
tA

public class Main {


an

public static int add(int a, int b) {


sh

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

public static float multiply(float... nums) {


ga

float product = 1;
@

for (float n : nums) product *= n;


21

return product;
07

}
18

public static void main(String[] args) {


1
se

Scanner sc = new Scanner(System.in);


c
4s

String operation = sc.next();


t.2

String type = sc.next();


an

if (sc.hasNextInt() || sc.hasNextFloat()) {
ish

if (type.equals("int")) {
(n

int a = sc.nextInt();
rya

int b = sc.hasNextInt() ? sc.nextInt() : 0;


switch (operation) {
tA

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
@

< hidden >


21
07

Expected Output:
1 18

< hidden >


sec
4s

Output:
t.2
an

Product of 2.5 and 4.0 is: 10.0


ish

Compilation Status: Passed


(n
rya

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

Compilation Status: Passed


Execution Time:
0.134s

54. Employee Payroll System (Single Inheritance)Problem


Statement:Create a Base Employee class and an Engineer subclass
to calculate salary.
Description:1. Employee (Base class) has attributes: name, id, basicSalary.2. Engineer
(Derived class) has bonus and calculates total salary as basicSalary + bonus.

Input Format:1. String name2. Integer id3. Integer basicSalary4. Integer bonus

Output Format:Print total salary.

)
.in
Sample Input:John 101 50000 7000

ac
ty.
Sample Output:57000

rsi
iven
su
Completion Status: Completed
tia
lgo

Concepts Included:
ga
@

gu 28 2nd semester java programming


21
07
18

Language Used: JAVA 8


1
sec
4s

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

public Employee(String name,int id,int basicSalary){


Ni

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

< hidden >


07
18

Output:
1
se

75000
c
4s

Compilation Status: Passed


t.2
an

Execution Time:
ish
(n

0.095s
rya
tA

TestCase2:
an
sh

Input:
Ni

< hidden >

Expected Output:
< hidden >

Output:
90000

Compilation Status: Passed


Execution Time:
0.09s

55. Library System (Multilevel Inheritance)Problem


Statement:Design a Book Management System using multilevel
inheritance.
Description:1. LibraryItem (Base class): title, author.2. Book (Intermediate class):
ISBN.3. EBook (Derived class): fileSize (MB).

Input Format:1. String title2. String author3. String ISBN4. Integer fileSize

Output Format:Print all details.

Sample Input: JavaProgramming JohnDoe 9781234567890 5

)
.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

gu 28 2nd semester java programming


@
21
07

Language Used: JAVA 8


1 18
se

Source Code:
c
4s
t.2

import java.util.Scanner;
an

class LibraryItem {
ish

String title;
String author;
(n

public LibraryItem(String title, String author) {


rya

this.title = title;
tA

this.author = author;
an

}
sh

}
Ni

class Book extends LibraryItem {


String ISBN;
public Book(String title, String author, String ISBN) {
super(title, author);
this.ISBN = ISBN;
}
}
class EBook extends Book {
int fleSize;
public EBook(String title, String author, String ISBN, int fleSize) {
super(title, author, ISBN);
this.fleSize = fleSize;
}
public void displayDetails() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ISBN: " + ISBN);
System.out.println("File Size: " + fleSize + "MB");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String title = sc.next();
String author = sc.next();
String ISBN = sc.next();
int fleSize = sc.nextInt();
EBook ebook = new EBook(title, author, ISBN, fleSize);
ebook.displayDetails();

)
}

.in
ac
}

ty.
rsi
Compilation Details:

iven
su
TestCase1: tia
lgo

Input:
ga
@

< hidden >


21
07

Expected Output:
1 18

< hidden >


sec
4s

Output:
t.2
an

Title: BigDataAnalytics
ish

Author: AliceCooper
(n

ISBN: 9876543210
rya

File Size: 250MB


tA

Compilation Status: Passed


an
sh

Execution Time:
Ni

0.103s

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >

Output:
Title: IntroToAI
Author: BobMarley
ISBN: 1122334455
File Size: 1MB

Compilation Status: Passed


Execution Time:
0.109s

56. Custom Exception HandlingProblem Statement:Write a program


that handles division by zero using a custom exception.

)
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
@

Completion Status: Completed


21
07
18

Concepts Included:
1
sec

gu 28 2nd semester java programming


4s
t.2
an

Language Used: JAVA 8


ish
(n

Source Code:
rya
tA

import java.util.Scanner;
an

// Custom Exception Class


sh

class DivideByZeroException extends Exception {


Ni

public DivideByZeroException(String message) {


super(message);
}
}
public class Main {
// Method to perform division
public static int divide(int a, int b) throws DivideByZeroException {
if (b == 0) {
throw new DivideByZeroException("Cannot divide by zero");
}
return a / b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
try {
int result = divide(a, b);
System.out.println("Result: " + result);
} catch (DivideByZeroException e) {
System.out.println(e.getMessage());
}
}
}

Compilation Details:

TestCase1:

)
.in
Input:

ac
ty.
rsi
< hidden >

ive
Expected Output:

n
su
< hidden > tia
lgo

Output:
ga
@

Result: 15
21
07

Compilation Status: Passed


1 18
se

Execution Time:
c
4s

0.107s
t.2
an
ish

TestCase2:
(n

Input:
rya
tA

< hidden >


an
sh

Expected Output:
Ni

< hidden >

Output:
Result: 20

Compilation Status: Passed


Execution Time:
0.104s

57. Program to Handle User-Defined ExceptionProblem


Statement:Write a program that defines a user-defined exception
and uses it to check if the entered age is valid (greater than 18).
Description: If the age entered by the user is less than 18, a custom exception
InvalidAgeException should be thrown.

Input Format:

An integer representing the age.Output Format:

An error message if the age is less than 18, or a success message otherwise.Sample
Input:17

Sample Output:Age must be at least 18

)
.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

//Custom Exception class


1
se

class InvalidAgeException extends Exception{


c

public InvalidAgeException(String message){


4s

super(message);
t.2

}
an

}
ish

public class Main{


(n

public static void checkAge(int age) throws InvalidAgeException{


rya

if(age<18){
tA

throw new InvalidAgeException("Age must be at least 18");


an

} 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

Compilation Status: Passed


Execution Time:

)
.in
ac
0.087s

ty.
rsi
ive
TestCase2:

n
su
Input:
tia
lgo
< hidden >
ga

Expected Output:
@
21
07

< hidden >


18

Output:
1
sec

Age must be at least 18


4s
t.2

Compilation Status: Passed


an
ish

Execution Time:
(n
rya

0.087s
tA
an
sh

58. Binary OperatorsProblem Statement:Create a program that


Ni

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:

Two integers separated by a space.Output Format:

Four lines, each representing the result of bitwise AND, OR, XOR, and NOT operations
respectively.

Sample Input:10 4Sample Output:


Bitwise AND: 0Bitwise OR: 14Bitwise XOR: 14Bitwise NOT (first number): -11

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

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

int notA = ~a;


@

System.out.println("Bitwise AND: " + and);


21

System.out.println("Bitwise OR: " + or);


07

System.out.println("Bitwise XOR: " + xor);


18

System.out.println("Bitwise NOT (first number): " + notA);


1

}
sec

}
4s
t.2
an

Compilation Details:
ish
(n

TestCase1:
rya
tA

Input:
an
sh

< hidden >


Ni

Expected Output:
< hidden >

Output:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT (first number): -6

Compilation Status: Passed


Execution Time:
0.107s

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
@

59. QuestionProblem Statement:Write a Java program that


21
07

simulates a multi-threaded banking system where multiple users


18

can deposit and withdraw money from a shared bank account. The
1
se

program should use threads and synchronization to ensure that the


c
4s

operations are handled safely without race conditions.


t.2
an

Description:The BankAccount class should contain a shared account balance and


ish

synchronized methods for deposit() and withdraw().The UserThread class should


simulate multiple users performing transactions (deposits & withdrawals)
(n

concurrently.The Main class should initialize multiple user threads to test the banking
rya

system.Input Format:The initial balance of the bank account.Number of users


tA

(threads) performing transactions.Each user specifies a transaction type (Deposit/


an

Withdraw) and amount.Output Format:The transactions performed by each user.The


sh

final account balance after all transactions.


Ni

Sample input:1000 3 Deposit 500Withdraw 700Withdraw 1000Sample


output: Deposited 500. New Balance: 1500Withdrawn 700. New Balance:
800Insufficient funds! Withdrawal of 1000 failed.Final Balance: 800

Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8


Source Code:
import java.util.Scanner;

class BankAccount {
private int balance;

public BankAccount(int initialBalance) {


this.balance = initialBalance;
}

public synchronized void deposit(int amount) {


balance += amount;
System.out.println("Deposited");
}

)
.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
@

public int getBalance() {


21

return balance;
07

}
18

}
1
sec

class UserThread extends Thread {


4s
t.2

private BankAccount account;


an

private String transactionType;


ish

private int amount;


(n

public UserThread(BankAccount account, String transactionType, int amount) {


rya

this.account = account;
tA

this.transactionType = transactionType;
an

this.amount = amount;
sh

}
Ni

public void run() {


if (transactionType.equalsIgnoreCase("Deposit")) {
account.deposit(amount);
} else if (transactionType.equalsIgnoreCase("Withdraw")) {
account.withdraw(amount);
}
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int initialBalance = scanner.nextInt();
int numberOfUsers = scanner.nextInt();
scanner.nextLine(); // Consume newline

BankAccount account = new BankAccount(initialBalance);


UserThread[] users = new UserThread[numberOfUsers];

for (int i = 0; i < numberOfUsers; i++) {


String[] parts = scanner.nextLine().split(" ");
users[i] = new UserThread(account, parts[0], Integer.parseInt(parts[1]));
}

for (UserThread user : users) user.start();


for (UserThread user : users) {
try {
user.join();

)
} catch (InterruptedException e) {

.in
ac
e.printStackTrace();

ty.
}

rsi
}

ive
}

n
}

su
tia
lgo
Compilation Details:
ga
@

TestCase1:
21
07

Input:
1 18
se

< hidden >


c
4s

Expected Output:
t.2
an

< hidden >


ish
(n

Output:
rya

Withdrawn
tA

Withdrawn
an
sh

Compilation Status: Passed


Ni

Execution Time:
0.09s

TestCase2:
Input:
< hidden >

Expected Output:
< hidden >
Output:
Deposited
Deposited
Deposited

Compilation Status: Passed


Execution Time:
0.096s

60. Java Threaded ModelQuestion:


Write a Java program that creates two threads. One thread prints even numbers and

)
.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

A single integer.Output Format:


ga
@

Even and odd numbers up to the given limit printed by two separate threads.Sample
21

Input:8Sample Output:Even Thread: 0Even Thread: 2Even Thread: 4Even Thread:


07

6Even Thread: 8Odd Thread: 1Odd Thread: 3Odd Thread: 5Odd Thread: 7
1 18
sec
4s

Completion Status: Completed


t.2
an

Concepts Included:
ish
(n

gu 28 2nd semester java programming


rya
tA

Language Used: JAVA 8


an
sh
Ni

Source Code:
import java.util.Scanner;

class EvenThread extends Thread {


private final int limit;

public EvenThread(int limit) {


this.limit = limit;
}

public void run() {


for (int i = 0; i <= limit; i += 2) {
System.out.println("Even Thread: " + i);
}
}
}

class OddThread extends Thread {


private final int limit;

public OddThread(int limit) {


this.limit = limit;
}

public void run() {


for (int i = 1; i <= limit; i += 2) {
System.out.println("Odd Thread: " + i);
}

)
}

.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

Thread evenThread = new EvenThread(limit);


@

Thread oddThread = new OddThread(limit);


21
07

// Start even thread and wait for it to finish


18

evenThread.start();
1
se

try {
c
4s

evenThread.join(); // Wait for even thread to complete before starting odd


t.2

} catch (InterruptedException e) {
an

e.printStackTrace();
ish

}
(n
rya

// Then start and wait for odd thread


oddThread.start();
tA

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

Compilation Status: Passed


Execution Time:

)
.in
ac
0.105s

ty.
rsi
TestCase2:

iven
Input:
su
tia
< hidden >
lgo
ga

Expected Output:
@
21

< hidden >


07
18

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

Compilation Status: Passed


tA
an

Execution Time:
sh
Ni

0.109s

61. Implement a Stack Using Two QueuesProblem Statement:Write


a program to implement a stack using two queues.
Description:The program should implement the basic operations of a stack (push,
pop, and peek) using two queues.

Input Format:A sequence of operations: push x, pop, and peek.

Output Format:For each operation, output the result.Sample Input:push 1push


2peekpoppeek Sample Output:221
Completion Status: Completed

Concepts Included:
gu 28 2nd semester java programming

Language Used: JAVA 8

Source Code:
import java.util.*;

public class Main {


static class StackUsingQueues {

)
.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

public int pop() {


se

if (q1.isEmpty()) return -1;


c
4s

return q1.poll();
t.2

}
an
ish

public int peek() {


(n

if (q1.isEmpty()) return -1;


rya

return q1.peek();
tA

}
an

}
sh
Ni

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
StackUsingQueues stack = new StackUsingQueues();

while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.isEmpty()) break;

String[] parts = line.split(" ");


String command = parts[0];

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

< hidden >


07
18

Output:
1
se

30
c
4s

Compilation Status: Passed


t.2
an

Execution Time:
ish
(n

0.088s
rya
tA

TestCase2:
an
sh

Input:
Ni

< hidden >

Expected Output:
< hidden >

Output:
5

Compilation Status: Passed


Execution Time:
0.088s

62. Find the GCD of Two NumbersProblem Statement:Write a


program to find the greatest common divisor (GCD) of two
numbers.
Description:The program takes two integers as input and returns their GCD using
Euclid’s algorithm.

Input Format:Two integers a and b.

Output Format:The GCD of the two numbers.Sample Input:56 98 Sample Output:14

)
.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

public class Main {


sec
4s

public static int findGCD(int a, int b) {


t.2

while (b != 0) {
an

int temp = b;
ish

b = a % b;
(n

a = temp;
rya

}
tA

return a;
an

}
sh
Ni

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(findGCD(a, b));
scanner.close();
}
}

Compilation Details:

TestCase1:
Input:
< hidden >

Expected Output:
< hidden >

Output:
6

Compilation Status: Passed


Execution Time:
0.09s

)
.in
ac
ty.
TestCase2:

rsi
ive
Input:

n
su
< hidden >
tia
lgo
Expected Output:
ga

< hidden >


@
21

Output:
07
18

50
1
sec

Compilation Status: Passed


4s
t.2

Execution Time:
an
ish

0.089s
(n
rya
tA
an
sh
Ni

You might also like