Practical Exam
Practical Exam
Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N
> 2 and N < 10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
1. Sort the elements of the single-dimensional array in ascending order using any standard
sorting technique and display the sorted elements.
2. Fill the square matrix b[][] in the following format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:
1258
1251
1212
1125
3. Display the filled matrix in the above format.
Test your program for the following data and some random data:
Example 1
INPUT:
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
137
131
113
Code
import java.util.Scanner;
sortArray(a);
System.out.println("SORTED ARRAY:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println("FILLED MATRIX:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. The words
are to be separated by a single blank space and are in uppercase.
Perform the following tasks:
(a) Check for the validity of the accepted sentence.
(b) Convert the non-palindrome words of the sentence into palindrome words by concatenating the
word by its reverse (excluding the last character).
Example:
The reverse of the word HELP would be LEH (omitting the last alphabet) and by concatenating both,
the new palindrome word is HELPLEH. Thus, the word HELP becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB would become ABBA and not
ABBBA and XAZZZ becomes XAZZZAX.
[Palindrome word: Spells same from either side. Example: DAD, MADAM etc.]
(c) Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1
INPUT:
THE BIRD IS FLYING.
OUTPUT:
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF
CODE
import java.util.*;
return palin;
}
return sb.toString();
}
while (st.hasMoreTokens()) {
String word = st.nextToken();
boolean isPalinWord = isPalindrome(word);
if (isPalinWord) {
sb.append(word);
}
else {
String palinWord = makePalindrome(word);
sb.append(palinWord);
}
sb.append(" ");
}
System.out.println();
System.out.println(ipStr);
System.out.println(convertedStr);
}
}
Program 3
An Evil number is a positive whole number which has even number of 1's in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of 1's. A few evil numbers are
3, 5, 6, 9…. Design a program to accept a positive whole number and find the binary equivalent of
the number and count the number of 1's in it and display whether it is a Evil number or not with an
appropriate message. Output the result in format given below:
Example 1
Input: 15
Binary Equivalent: 1111
No. of 1's: 4
Output: Evil Number
CODE
import java.util.Scanner;
while (n > 0) {
int d = n % 2;
if (d == 1)
count++;
binNum += (int)(d * Math.pow(10, p));
p++;
n /= 2;
}
if (count % 2 == 0)
System.out.println("Output: Evil Number");
else
System.out.println("Output: Not an Evil Number");
}
}
PROGRAM 4
Snowball string
CODE
import java.io.*;
class snowball
{
public static void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string terminated by a full stop(.)");
String s=br.readLine();
int l = s.length();
int iofs = s.indexOf(' ');
String sw = s.substring(0,iofs);
int lsw = sw.length();
int i, p;
boolean flag = true;
p = iofs+1;
char ch = s.charAt(i);
if(ch == ' ' || ch == '.'){
String w = s.substring(p,i);
if(flag)
System.out.println("Snowball string");
else
System.out.println("Not a snowball string");
}//eom
}