Sample Paper 1 solve
Question 1
A Digit Permutation Cipher is a simple form of number encryption where the digits of a number are
rearranged based on a given key where (1<= key <=size of the number). The key is a sequence of integers
that defines the new positions of the digits.
Example: If number = 2613 and key = 4213, then the encrypted number will be 1632 by positioning the first
digit to the 4th place, second digit to the second place, third digit to the first place and the fourth digit to the
third place as per the key given.
Write a program to enter a number and a permutation key (a sequence of digits which is greater than 0 and
less than or equal to the size of the number). The program should encrypt the number by permuting its digits
according to the key. The number of digits in the key must match the number of digits in the number to be
encrypted.
Test your program with the following data and some random data:
Example 1
INPUT: Number: 12345
Key: 31524
OUTPUT: The encrypted number is 24153
Example 2
INPUT: Number: 9876
Key: 4132
OUTPUT: The encrypted number is 8679
Example 3
INPUT: Number: 5239
Key: 4765
OUTPUT: INVALID KEY DIGITS
Example 4
INPUT: Number: 123
Key: 2134
OUTPUT: INVALID KEY SIZE 4
Answer
import java.util.Scanner;
class permutation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
String n= sc.next();
System.out.print("Enter a permutation key (same number of digits): ");
String key = sc.next();
if (n.length() != key.length())
{
System.out.println("The number of digits in the key must match the number of digits in the
number.");
System.exit(0);
}
else
{
int x=key.length();
char ar[]=new char[x];
for(int i=0;i<x;i++)
{
int p=key.charAt(i);
p=p-48;
if(p>x)
{
System.out.println("INVALID KEY DIGITS");
System.exit(0);
}
ar[p-1]=n.charAt(i);
}
String st="";
for(int i=0;i<x;i++)
{
st=st+ar[i];
}
System.out.println("The encrypted number is:" + st);
}
}
}
Question 2
Write a program to declare a single-dimensional array A [ ] of size L, where L is an integer greater than or
equal to 3 and less than or equal to 50. Allow the user to input integers into this array. Display an appropriate
error message for an invalid input.
Perform the following tasks on the array:
(a) Display the array.
(b) Find and print all equilibrium indices in increasing order. An index i (0-based) is called
an equilibrium index if the sum of elements on its left equals the sum of elements on its
right.
(c) If no equilibrium index exists, display an appropriate message.
Test your program with the following data and some random data:
Example 1
INPUT: L = 7
Array Elements: 2, 3, -1, 8, 4, -2, 2
OUTPUT: Array: 2, 3, -1, 8, 4, -2, 2
Equilibrium Indices: 3
Example 2
INPUT: L = 5
Array Elements: 0, -7, 3, -2, 6
OUTPUT: Array: 0, -7, 3, -2, 6
Equilibrium Indices: 0
Example 3
INPUT: L = 6
Array Elements: 1, 2, 3, 4, 5, 6
OUTPUT: Array: 1, 2, 3, 4, 5, 6
Equilibrium Indices: NIL
Example 4
INPUT: L = 7
Array Elements: -7, 1, 5, 2, -4, 3, 0
OUTPUT: Array: -7, 1, 5, 2, -4, 3, 0
Equilibrium Indices: 3, 6
Example 5
INPUT: L = 2
OUTPUT: INVALID INPUT 5
Answer
import java.util.*;
class equil {
public static void main(String[] args)
{
Scanner sc=new Scanner (System.in);
int f=0;
System.out.println("Enter size of the array");
int L=sc.nextInt();
int a[] = new int[L];
if(L<3 && L>50)
{
System.out.println("Invalid size");
System.exit(0);
}
else
{
System.out.println("Enter the elements:");
for (int i = 0; i <L; i++)
{
a[i]= sc.nextInt();
}
for (int i = 0; i < L; i++)
{
int left_sum = 0;
for (int j = 0; j < i; j++)
left_sum = left_sum +a[j];
int right_sum = 0;
for (int k = i + 1; k < L; k++)
right_sum = right_sum + a[k];
if (left_sum == right_sum)
{
System.out.println("Equilibrium Indices: " + i);
f++;
}
}
if(f==0)
System.out.println("Equilibrium Indices Nil");
}
}
Question 3
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’or ‘!’ only. The words may be
separated by a single blank space and should be case-insensitive.
Perform the following tasks:
If the sentence is a pangram (a sentence or a phrase that uses every letter of the alphabet
at least once), then print PANGRAM.
If it misses exactly one letter of the alphabet, print PANGRAMMATIC LIPOGRAM
and the missing letter.
(c) Else print NEITHER and list all missing letters in alphabetical order.
Test your program for the following data and some random data:
Example 1
INPUT: The quick brown fox jumps over the lazy dog!
OUTPUT: IT IS A PANGRAM
Example 2
INPUT: A quick movement of the enemy will jeopardize.
OUTPUT: PANGRAMMATIC LIPOGRAM
MISSING: x
Example 3
INPUT: Hello world.
OUTPUT: NEITHER
MISSING: a b c f g i j k m n p q s t u v x y z
Example 4
INPUT: Alas! it failed #
OUTPUT: INVALID INPUT 6
Answer
import java.util.Scanner;
public class pangram1
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String s = in.nextLine();
int c,f=1,cn=0;
String st="",st1="",st2="";
s=s.toUpperCase();
int l=s.length();
if(s.charAt(l-1)=='.' ||s.charAt(l-1)=='?' || s.charAt(l-1)==','|| s.charAt(l-1)=='!')
{
for(int i=65;i<=90;i++)
{
c=0;
for(int j=0;j<l;j++)
{
if(s.charAt(j)==(char)i)
c++;
}
if(c==0)
{
f=0;
System.out.println("Missing:" + (char)i);
cn++;
}
if(f==1)
System.out.println("Pangram");
else if(f==0)
{
if(cn==1)
System.out.println("PANGRAMMATIC LIPOGRAM ");
else
System.out.println("NEITHER");
}
}
else
System.out.println("Invalid input:");
}
}