COMPUTER SCIENCE Mock Practical Paper AK
COMPUTER SCIENCE Mock Practical Paper AK
A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as well as an
Adam number.
Prime number: A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 ... etc.
Adam number: The square of a number and the square of its reverse are reverse to each other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam
integers that are in the range between m and n (both inclusive) and output them along with the
frequency, in the format given below:
Test your program with the following data and some random data:
Example 1
INPUT:
m=5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 700
n = 450
OUTPUT:
Invalid Output
Page 1 of 11
Question 2
Write a program to declare a square matrix A[ ][ ] of order M × M where `M' is the number of rows
and the number of columns, such that M must be greater than 2 and less than 10. Accept the value of
M as user input. Display an appropriate message for an invalid input. Allow the user to input integers
into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Check if the given matrix is Symmetric or not.
(A square matrix is said to be Symmetric, if the element of the ith row and jth column is equal to the
element of the jth row and ith column.)
(c) Find the sum of the elements of left diagonal and the sum of the elements of right diagonal of
the matrix and display them.
Test your program for the following data and some random data:
Example 1
INPUT: M = 3
Enter elements of the matrix:
123
245
356
Example 2
INPUT: M = 4
Enter elements of the matrix:
7892
4563
8531
7642
Example 3
INPUT: M = 12
Page 2 of 11
Question 3
Caesar Cipher is an encryption technique which is implemented as ROT13 ('rotate by 13 places'). It
is a simple letter substitution cipher that replaces a letter with the letter 13 places after it in the
alphabets, with the other characters remaining unchanged. Write a program to accept a plain text of
length L, where L must be greater than 3 and less than 100. Encrypt the text if valid as per the Caesar
Cipher.
Example 1
INPUT:
Hello! How are you?
OUTPUT:
The cipher text is:
Uryyb! Ubj ner lbh?
Example 2
INPUT:
Encryption helps to secure data.
OUTPUT:
The cipher text is:
Rapelcgvba urycf gb frpher qngn.
Example 3
INPUT:
You
OUTPUT:
INVALID LENGTH
ANSWER KEY
QUESTION 1:
Algorithm:
Step 1: Start
Step 2: Create class primeAdam.
Step 3: Instance Variables: Declare integers m and n.
Program:
import java.util.*;
public class primeAdam
{
int m,n;
30QA
void input() 2
{
Scanner sc=new Scanner (System.in);
while (true)
{
System.out.println("Enter the value of m");
m=sc.nextInt();
System.out.println("Enter the value of n");
n=sc.nextInt();
if(m>0 && n>0 && m<n)
{
break;
Page 4 of 11
}
else
{
System.out.println("INVALID INPUT");
}
}
}
boolean isPrime(int s)
{
int c=0;
for(int i=1;i<=s;i++)
{
if(s%i==0) //checking if number is prime or not
c++;
}
if(c==2)
return true;
else
return false;
}
int reverse(int a)
{
int rev=0;
while(a>0) //reversing the digits to check
{
int r=a%10;
rev=rev*10+r;
a=a/10;
}
return rev;
}
void primeadam()
{
System.out.println("The prime adam integers are"); 3
int z ;
Page 5 of 11
int c=0;
for(int i=m;i<=n;i++)
{
int num=i;
int sqnum=num*num;
int rev=reverse(num);
int revsq=rev*rev;
int revofrevsq= reverse(revsq);
if(isPrime (num)==true && revofrevsq==sqnum)
{
System.out.print(i+" ");
c++;
}
}
System.out.println();
System.out.println("The number of prime adam integers are "+c);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
primeAdam ob=new primeAdam();
while (true)
{
ob.input();
ob.primeadam();
System.out.println("Enter 0 to stop or any other number to continue");
int ch=sc.nextInt();
if (ch==0)
break;
}
}
}
OUTPUT:
Page 6 of 11
QUESTION 2:
Algorithm:
Step1: Start
Step2: Input Matrix Size
Step3: Accept M (size of the matrix) such that 3 ≤ M < 10.
Step4: If M is invalid, display an error and stop.
Step5: Input Matrix Elements
Step6: Initialize matrix A[M][M].
Step7: Accept integer values into the matrix.
Step8: Display Original Matrix
Step9: Print the original matrix.
Step10: Check Symmetry
Step11: Compare each element A[i][j] with A[j][i].
Step12: If any pair is unequal, set a flag indicating the matrix is not symmetric.
Step13: Calculate Diagonal Sums
Step14: Compute the sum of the left diagonal (A[i][i]) and right diagonal (A[i][M-1-i]).
Step15: Display Results
Step16: Print if the matrix is symmetric or not.
Step17: Display the sums of both diagonals.
Step18: Stop
Program:
import java.util.Scanner;
public class SymmetricMatrixCheck {
public static void main() {
Page 7 of 11
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the matrix (M > 2 and M < 10): ");
int M = sc.nextInt();
if (M <= 2 || M >= 10) {
System.out.println("Invalid input. M should be greater than 2 and less than 10.");
return;
}
int[][] matrix = new int[M][M];
System.out.println("Enter elements of the matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
matrix[i][j] = sc.nextInt(); 63
}
}
System.out.println("Original Matrix:");
displayMatrix(matrix, M);
boolean isSymmetric = true;
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (matrix[i][j] != matrix[j][i]) {
isSymmetric = false;
break;
}
}
}
if (isSymmetric) {
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
} else {
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
}
// Calculate the sum of left diagonal and right diagonal
int leftDiagonalSum = 0, rightDiagonalSum = 0;
for (int i = 0; i < M; i++) {
leftDiagonalSum += matrix[i][i]; // Left diagonal elements
rightDiagonalSum += matrix[i][M - 1 - i]; // Right diagonal elements
}
Page 8 of 11
System.out.println("The sum of the left diagonal = " + leftDiagonalSum);
System.out.println("The sum of the right diagonal = " + rightDiagonalSum);
}
public static void displayMatrix(int[][] matrix, int M) {
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Question No 3:
Algorithm:
Step 1: Start
Step 2: Input Plain Text
Step 3: Accept a plain text of length L.
Step 4: Check if L is greater than 3 and less than 100.
Step 5: If L is invalid, display an error and stop.
Step 6: Encrypt Text
Step 7:For each character in the text:
Step 8: If the character is a letter:
Page 9 of 11
Step 9: Replace it with the letter 13 places after it in the alphabet.
Step 10: Wrap around to the start of the alphabet if necessary.
Step 11: If the character is not a letter, leave it unchanged.
Step 12: Display Encrypted Text
Step 13: Print the encrypted text.
Step 14: Stop
Program
import java.util.Scanner;
public class CaesarCipher {
public static void main() {
Page 10 of 11
Page 11 of 11