STS Fat
STS Fat
You will be given two integers x and y as input, you have to compute x/y.
If x and y are not 32 bit signed integers or if y is zero, exception will occur
and you have to report it. Read sample Input/Output to know what to report
in case of exceptions.
Sample Input 1
10
3
Sample Output 1
3
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be
named Solution. */
try {
int x = scan.nextInt();
int y = scan.nextInt();
System.out.println(x / y);
catch(InputMismatchException e) {
catch(ArithmeticException e) {
// Print F
System.out.println(e);
scan.close();
Rithvik likes to play with numbers, every day he used to solve one Brain
Teaser, today he attempted to solve the GAPFUL NUMBER.
A gapful number is a number of 3 digits that is divisible by the number
formed by the first and last digits of the original number. Help him write a
program to check whether the given number is a gapful number or not.
For Example:
Input:192
Output : true(192 is a gapful number because it is divisible by 12)
import java.util.Scanner;
class Main
int first_number = 0;
int second_number = 0;
try {
first_number = Integer.parseInt(input);
return;
return;
if (first_number % second_number == 0) {
System.out.println("True");
} else {
System.out.println("False");
Get five integers from user and store it in an Array ARR[],next get an integer
'X' and integer 'Y' from user.
Then print ARR[X] / ARR[Y].
(int ARR[]=new int[5];)
if X or Y is not in the range of index of ARR[] i.e., 0 to 4 ,then the JVM will
throw an Exception, handle that Exception with proper Exception Handling
class by printing "index must be 0 to 4";
if ARR[Y] is equal to 0(zero),then the JVM will throw an Exception, handle
that Exception with proper Exception Handling class by printing
"denominator should not be zero";
If there is no Exception in the program then print ARR[X] divided by ARR[Y]
(Quotient)
Use Exception Handling Mechanism :(try, catch..)
[Hint:- Multiple catch block must be used]
Input Format
The first five integers are the values for the Array (ARR)
The next integer 'X' represents the index of ARR.
The next integer 'Y' represents the index of ARR.
Output Format
An integer i.e. the result of ARR[X] / ARR[Y]
Or
index must be 0 to 4
Or
denominator should not be zero
Sample Input 1
3 4 5 1 2
2 4
Sample Output 1
2
Sample Input 2
5 6 0 1 3
4 2
Sample Output 2
denominator should not be zero
Sample Input 3
7 8 9 0 1
5 3
Sample Output 3
index must be 0 to 4
import java.util.Scanner;
class Main
for(int i=0;i<5;i++)
ARR[i]=ip.nextInt();
X=ip.nextInt();
Y=ip.nextInt();
try
System.out.println(ARR[X]/ARR[Y]);
catch(ArithmeticException e)
catch(ArrayIndexOutOfBoundsException e)
catch(Exception e)
System.out.println("UnKnown Exception");
import java.util.*;
public IncorrectAgeException(String m)
super(m);
class person
int age;
person (int a)
age=a;
if (age>150)
else
System.out.println("Welcome");
}class Main
{
int a;
a = scan.nextInt();
try
p.checkage();
catch(IncorrectAgeException e)
System.out.println(e);
Input Format
Ten Strings without white space(s)
All the strings must be in small case.
Output Format
Ten Strings after swapping the second with fith position
Sample Input 1
qwerty
asdfg
mnop
cvbn
poiuy
bhuijn
sge
dgdfgasf
rty
cvbcvb
Sample Output 1
qwerty
poiuy
mnop
cvbn
asdfg
bhuijn
sge
dgdfgasf
rty
cvbcvb
Sample Input 2
a
b
c
d
e
f
g
h
i
j
Sample Output 2
a
e
c
d
b
f
g
h
i
j
import java.util.*;
class Main
LinkedList<String>list=new LinkedList<String>();
for(int i=0;i<10;i++)
list.add(ip.next());
Collections.swap(list,1,4);
for(String str:list)
System.out.println(str);
Get 10 integers from user,find and print the second largest number.
If all numbers are same i.e no second largest number then print -1.
Sample Input 1
5 5 5 5 5 5 5 5 5 5
Sample Output 1
-1
Sample Input 2
1 2 3 4 5 6 7 8 9 0
Sample Output 2
8
Sample Input 3
7 7 7 7 7 6 6 6 6 3
Sample Output 3
6
import java.util.*;
class Main
{
public static void main(String[]args)
for(int i=0;i<10;i++)
ts.add(ip.nextInt());
if(list.size()>1)
System.out.println(list.get(list.size()-2));
else
System.out.println("-1");
Input Format
565
Output Format
DivisiblebyFiveException: Number should not be divide by five
Sample Input 1
565
Sample Output 1
DivisiblebyFiveException: Number should not be divide by five
import java.util.Scanner;
import java.io.*;
super(message);
class exe
int x = sc.nextInt();
try
if(x%5==0)
else
System.out.print("Valid Number");
catch(Exception e)
System.out.print(e);
}
Create three threads namely Thread1,Thread2,Thread3 in java.,
The Thread1 should print 2,4,6,....20.
The Thread2 should print 5,10,15,...100.
The Thread3 should print 11,22,33,...110.
(Its Multithreaded program so,the output order is depends upon the system
and time and it's cosidered)
Sample Input 1
Sample Output 1
2 5 10 15 20 25 30 4 35 40 45 50 11 22 33 44 55 66 77 88 99 110 6 8 10
12 14 16 18 20
Sample Input 2
Sample Output 2
2 4 6 8 10 12 14 16 18 20 5 10 15 20 25 30 35 40 45 50 11 22 33
for(int i=1;i<=10;i++)
System.out.print(" "+i*2);
for(int i=1;i<=10;i++)
System.out.print(" "+i*5);
}
class C extends Thread
for(int i=1;i<=10;i++)
System.out.print(" "+i*11);
class MyClass {
A t1=new A();
B t2=new B();
C t3=new C();
t1.start();
t2.start();
t3.start();
Given a singly linked list, find the middle of the linked list.
For example, if the given linked list is 1->2->3->4->5 then the output should
be 3.
If there are even nodes, then there would be two middle nodes, we need to
print the second middle element.
For example, if given linked list is 1->2->3->4->5->6 then the output should
be 4.
Input Format
n - get size of linked list
get number elements in linked list
5
1
2
3
4
5
Output Format
Print the elements of the Linked List
Print middle element of the linked List
5->4->3->2->1->NULL
The middle element is [3]
Sample Input 1
5
1
2
3
4
5
Sample Output 1
5->4->3->2->1->NULL
The middle element is [3]
import java.util.*;
class LinkedList
Node head;
class Node
int data;
Node next;
Node(int d)
data = d;
next = null;
}
void printMiddle()
if (head != null)
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
new_node.next = head;
head = new_node;
}
public void printList()
System.out.print(tnode.data+"->");
tnode = tnode.next;
System.out.println("NULL");
class Main {
int n = scan.nextInt();
llist.push(scan.nextInt());
llist.printList();
llist.printMiddle();
}}
Examples of some unbalanced strings are: "{}(", "({)}", "[[", "}{" etc.
Given a string, determine if it is balanced or not.
Input Format
There will be multiple lines in the input file, each having a single non-empty
string. You should read input till the end-of-file.
The part of the code that handles input operation is already provided in the
editor.
Output Format
For each case, print 'true' if the string is balanced, 'false' otherwise.
Sample Input 1
{}()
({()})
{}(
[]
Sample Output 1
true
true
false
true
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution {
while(scanner.hasNextLine()) {
for(char c : line.toCharArray()) {
stack.push(c);
continue;
}
stack.pop();
continue;
stack.pop();
continue;
stack.pop();
continue;
stack.push(c);
break;
System.out.println(stack.isEmpty());
Get a string from user and encode the string using Base64 Algorithm and
print encode MIME message for the given string.
Input Format
A sting
Output Format
Encode MIME String
Sample Input 1
HELLO
Sample Output 1
SEVMTE8=
Sample Input 2
good morning
Sample Output 2
Z29vZA==
import java.util.*;
class Main
System.out.println(eStr);
Output Format:
Refer sample output statement to print proper result
Input Format
DivideByZeroException
Output Format
import java.util.Scanner;
import java.util.*;
class Main
int n=str.length();
for(int i=0;i<n;i++)
char c=str.charAt(i);
Integer val=map.get(c);
if(val!=null)
map.put(c,new Integer(val+1));
}
else
map.put(c,1);
for(Map.Entry<Character,Integer>entry: map.entrySet())
Input Format
Each line of the input contains two integers, n and p. The locked stub code
in the editor reads the input and sends the values to the method as
parameters.
Output Format
Each line of the output contains the result np , if both n and p are positive. If
either n or p is negative, the output contains "n and p should be non-
negative". If both n and p are zero, the output contains "n and p should not
be zero.". This is printed by the locked stub code in the editor.
Constraints
-10<= n <= 10
-10<= p <= 10
Sample Input 1
3 5
2 4
0 0
-1 -2
-1 3
Sample Output 1
243
16
java.lang.Exception: n and p should not be zero.
java.lang.Exception: n or p should not be negative.
java.lang.Exception: n or p should not be negative.
import java.util.Scanner;
class MyCalculator {
if (n < 0 || p < 0) {
} else if (n == 0 && p == 0) {
class MyClass {
int n = in .nextInt();
int p = in .nextInt();
try {
System.out.println(my_calculator.power(n, p));
} catch (Exception e) {
System.out.println(e);
Consider you have two sets and each set contains 5 integers,your task is to
print the non common elements in both the sets.
if there is no non common elements in both the stes,then print -1.
Input Format
The first 5 integers are the values for Set1
The next 5 integers are the values for Set2.
Output Format
Print integers which are non common in both the sets,otherwise -1.
Sample Input 1
1 2 3 4 5
3 4 5 6 7
Sample Output 1
1 2 6 7
Sample Input 2
2 2 2 2 2
2 2 2 2 2
Sample Output 2
-1
import java.util.*;
class Main
{
for(int i=0;i<5;i++)
a.add(ip.nextInt());
for(int i=0;i<5;i++)
b.add(ip.nextInt());
union.addAll(b);
intersection.retainAll(b);
union.removeAll(intersection);
if(union.size()==0)
System.out.println("-1");
else
System.out.print(" "+x);
}}
Get 10 integers from user,find and print the second largest number.
If all numbers are same i.e no second largest number then print -1.
[ Hint:- you may use any Collection classes like
ArrayList,TreeSet,Hashset,LinkedList..etc]
Sample Input 1
7 7 7 7 7 6 6 6 6 3
Sample Output 1
6
Sample Input 2
4 4 4 4 4 4 4 4 4 4
Sample Output 2
-1
import java.util.*;
class Main
for(int i=0;i<10;i++)
ts.add(ip.nextInt());
if(list.size()>1)
System.out.println(list.get(list.size()-2));
else
System.out.println("-1");
}
Raju is a little boy studying the fifth standard in ABC matric higher
secondary school. Next week is annual at his school. He wants to
participate in a contest. His friend Babu registered himself and Raju in a
contest. The contest was named as Memory Zone. Raju has no idea about
the contest. So he willing to practice in order to prevent humiliation on an
annual day. As a boy studying 5th standard, he not able to practice for the
contest. The contest Memory Zone is about memorizing the words. Before
the contest starts Judge give some words. Contestants have to memorize
them and write them in the same order. Help Raju to practice this using a
computer program. Note: Using LinkedList and Iterator
Input Format:
Next n line containing the word (One word per each line)
Output Format:
Input Format
3
Ball
Cap
Computer`c
Output Format
Ball
Cap
Computer
Sample Input 1
3
Ball
Cap
Computer
Sample Output 1
Ball
Cap
Computer
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Iterator;
class Main
int n,i;
n=sc.nextInt();
//sc.nextLine();
for(i=1;i<=n;i++)
String str=sc.next();
a.add(str);
Iterator<String> it=a.iterator();
while(it.hasNext())
System.out.print(it.next()+"\n");
Input Format
5
1
2
3
4
5
Output Format
5->4->3->2->1->NULL
The middle element is [3]
Sample Input 1
5
1
2
3
4
5
Sample Output 1
5->4->3->2->1->NULL
The middle element is [3]
import java.util.*;
class LinkedList
{
Node head;
class Node
int data;
Node next;
Node(int d)
data = d;
next = null;
void printMiddle()
if (head != null)
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}
public void push(int new_data)
new_node.next = head;
head = new_node;
System.out.print(tnode.data+"->");
tnode = tnode.next;
System.out.println("NULL");
class Main {
int n = scan.nextInt();
llist.push(scan.nextInt());
llist.printList();
llist.printMiddle();
}}
Input Format
You do not need to read anything from stdin. The locked stub code in your
editor reads a single line containing string s. It then calls the methods
specified above to pass each character to your instance variables.
Output Format
You are not responsible for printing any output to stdout.
If your code is correctly written and s is a palindrome, the locked stub code
will print The word s, is a palindrome; otherwise, it will print The word s, is
not a palindrome
Constraints
S is composed of lowercase English letters.
Sample Input 1
Racecar
Sample Output 1
The word, Racecar, is not a palindrome.
import java.io.*;
import java.util.*;
class MyClass {
Queue<Character> queue;
Stack<Character> stack;
MyClass(){
this.stack.push(ch);
this.queue.add(ch);
char popCharacter(){
return this.stack.pop();
char dequeueCharacter(){
return this.queue.remove();
scan.close();
char[] s = input.toCharArray();
for (char c : s) {
p.pushCharacter(c);
p.enqueueCharacter(c);
// Pop/Dequeue the chars at the head of both data structures and compare them:
if (p.popCharacter() != p.dequeueCharacter()) {
isPalindrome = false;
break;
}
Task:Collection Interface - Sort an ArrayList<Integer> in descending
chronological order.
Get ‘N’ Integers from the user and store it in ArrayList class object, and sort
the elements in descending chronological order and print it.
Input Format:
The first Line ‘N’ represents number of elements in the ArrayList.
The next N-Lines represents values of the ArrayList object.
Output Format:
The sorted ArrayList
Sample Input :
8
5
-23
12
98
34
78
0
92
Sample Output :
[98, 92, 78, 34, 12, 5, 0, -23]
Constraints
0<N<=100
Sample Input 1
6
1
4
78
-2
78
0
Sample Output 1
[78, 78, 4, 1, 0, -2]
import java.util.*;
class Main
{
public static void main(String args[])
int N;
N=ip.nextInt();
for(int i=0;i<N;i++)
num.add(ip.nextInt());
Collections.sort(num,Collections.reverseOrder());
System.out.println(num);