COMPUTER APPLICATIONS
(Theory)
Two Hours
Answers to this Paper must be written on the paper provided separately.
You will not be allowed to write during the first 15 minutes.
This time is to be spent in reading the Question Paper.
The time given at the head of the Paper is the time allowed for writing the answers.
This paper is divided into two Sections.
Attempt all questions from Section A and any four questions from Section B.
The intended marks for questions or parts of questions are given in bracket [ ]
SECTION – A (40 Marks)
(Theory)
Two Hours
Answers to this Paper must be written on the paper provided separately.
You will not be allowed to write during the first 15 minutes.
This time is to be spent in reading the Question Paper.
The time given at the head of the Paper is the time allowed for writing the answers.
This paper is divided into two Sections.
Attempt all questions from Section A and any four questions from Section B.
The intended marks for questions or parts of questions are given in bracket [ ]
SECTION – A (40 Marks)
Question 1.
(a) Define the term Byte Code. [2]
Ans. The Java compiler compiles the source programs into an intermediate code called the Java Byte Code which is interpreted by the Java Virtual Machine (JVM)
(b) What do you understand by type conversion? [2]
Ans. Type conversion or casting is the conversion of the data type of a literal from one type to another. There are tow types of types of casting – implicit casting and explicit casting.
(c) Name two jump statements and their use. [2]
Ans. break and continue are the two jump statements in Java. break is used to force early termination of a loop. continue is used to move to the next iteration of the loop while skipping the remaining code in the current iteration.
(d) What is Exception ? Name two Exception Handling Blocks. [2]
Ans. An Exception is an error which occurs during the execution of a program. The exception handling blocks are try, catch and finally.
(e) Write two advantages of using functions in a program. [2]
Ans. i) Function make code reusable.
ii) Functions improve modularity and facilitate easy debugging.
Question 2.
(a) State the purpose and return data type of the following String functions: [2]
(i) indexOf ( )
(ii) compareTo ( )
Ans. i) indexOf() returns the index of the character or String passed as the parameter in the string on which is invoked.
Return type is int.
ii) compareTo() it compares two string values whether the strings are same or which is bigger and which is smaller..
Return type is int.
(b) What is the result stored in x, after evaluating the following expression [2]
int x = 5;
x = x++ * 2 + 3 * –x;
Ans. x = 5 * 2 + 3 * -6
x = 10 – 18
x = -8
(c) Differentiate between static and non-static data members. [2]
Ans. i) static variables belong to the class and all object share a single instance of the static variables. Non static varaiables belong to the objects. Each object has a copy of these members.
ii) static functions can access only static data members. Non static function can access both static and non static data members.
(d) Write the difference between length and length() functions. [2]
Ans. length is a property of an array which gives the size of the array. length() is a function of the String class which returns the size of the String.
(e) Differentiate between private and protected visibility modifiers. [2]
Ans. private members are accessible only in the class in which they have been defined. protected members are accessible in the class in which they have been defined as well in the sub classes of that class.
Question 3
(a) What do you understand by data abstraction? Explain with an example.[2]
Ans. Abstraction refers to representing the essential features of a system without considering all the details. Example: When we drive a car, we concentrate on how to drive it without bothering ourselves on how the engine works and other things.
(b) What will be the output of the following code?
(i) [2]
int m=2;
int n=15;
for(int i = 1; i<5; i++);
m++; –-n;
System.out.println("m=" +m);
System.out.println("n="+n);
Ans.
m=3
n=14
Note that there is a semicolon at the end of the loop. So, it is an empty loop and does’t affect the values of m and n.
(ii) [2]
char x = 'A' ; int m;
m=(x=='a') ? 'A' : ‘a’;
System.out.println("m="+m);
Ans.
1 m=97
(c) Analyse the following program segment and determine how many times tne
loop will be executed and what will be the output of the program segment. [2]
int k=1, i=2;
while (++i<6)
k*=i;
System.out.println(k);
Ans. Following are the iterations of the loop
3 < 6 ---- k = 1 * 3 = 3
4 < 6 ---- k = 3 * 4 = 12
5 < 6 ---- k = 12 * 5 = 60
6 < 6 ---- false
The loop will run three times and output is 60
1
(d) Give the prototype of a function check which receives a character ch and an integer n and returns true or false. [2]
Ans.
public boolean function(char ch, int n)
(e) State two features of a constructor. [2]
Ans. i) A constructor has the same name as that of a class.
ii) A constructor does not have any return type.
iii) A constructor is automatically called during object creation.
(f) Write a statement each to perform the following task on a string:
(i) Extract the second last character of a word stored in the variable wd. [2]
Ans.
char ch = wd.charAt(wd.length()-2);
(ii) Check if the second character of a string str is in uppercase. [2]
Ans.
boolean result = Character.isUpperCase(str.charAt(1));
(g) What will the following functions return when executed? [2]
(j) Math.max(-17, -19)
(ii) Math.ceil(7.8)
Ans. i) -17
ii) 8.0
(h) (i) Why is an object called an instance of a class?
(ii) What is the use of the keyword import? [2]
Ans. i) An object is called an instance of a class because the objects contains a copy of all the instance variables of the class.
ii) The import keyword is used to import classes from external packages.
SECTION – B (60 Marks)
Attempt any four questions from this Section.
The answers in this Section should consist of the Programs in either Blue J environment or any program environment with Java as the base.
Each program should be written using Variable descriptions/Mnemonic Codes such that the logic of the program is clearly depicted.
Flow-Charts and Algorithms are not required.
Question 4.
Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found display the element along with its position, otherwise display the message “Search element not found”. [15]
5,7,9,11,15,20,30,45,89,97
Ans.
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
int[] array = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number you want to search for: ");
int target = scanner.nextInt();
int left = 0;
int right = array.length - 1;
int result = -1;
while (left <= right) { int middle = (left + right) / 2; if (array[middle] == target) { result = middle; break; } else if (array[middle] > target) {
right = middle - 1;
} else {
left = middle + 1;
}
}
if (result != -1) {
System.out.println(target + " found at index " + result);
} else {
System.out.println("Search element not found");
}
}
}
Sample Output 1:
Enter the number you want to search for: 7
7 found at index 1
Sample Output 2:
Enter the number you want to search for: 34
Search element not found
Question 5
Define a class ‘Student described as below: [15]
Data members/instance variables : name,age,m1,m2,m3 (marks in 3 subjects), maximum, average
Member methods :
(i) A parameterized constructor to initialize the data members.
(ii) To accept the details of a student.
(iii) To compute the average and the maximum out of three marks.
(iv) To display the name, age, marks in three subjects, maximum and average.
Write a main method to create an object of a class and call the above member methods.
Ans.
import java.util.Scanner;
public class Student {
String name;
int age;
int m1, m2, m3;
int maximum;
double average;
public Student(String n, int a, int marks1, int marks2, int marks3, int max, double avg) {
name = n;
age = a;
m1 = marks1;
m2 = marks2;
m3 = marks3;
maximum = max;
average = avg;
}
public void acceptDetails() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
name = scanner.next();
System.out.print("Enter age: ");
age = scanner.nextInt();
System.out.print("Enter marks1: ");
m1 = scanner.nextInt();
System.out.print("Enter marks2: ");
m2 = scanner.nextInt();
System.out.print("Enter marks3: ");
m3 = scanner.nextInt();
}
public void compute() {
average = (m1 + m2 + m3) / 3.0;
maximum = Math.max(m1, (Math.max(m2, m3)));
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks1 " + m1);
System.out.println("Marks2 " + m2);
System.out.println("Marks3 " + m3);
System.out.println("Maximum: " + maximum);
System.out.println("Average: " + average);
}
public static void main(String[] args) {
Student student = new Student("",0,0,0,0,0,0.0);
student.acceptDetails();
student.compute();
student.display();
}
}
Sample Output:
Enter name: Sai
Enter age: 20
Enter marks1: 95
Enter marks2: 100
Enter marks3: 99
Name: Sai
Age: 20
Marks1 95
Marks2 100
Marks3 99
Maximum: 100
Average: 98.0
Question 6
Shasha Travels Pvt. Ltd. gives the following discount to its customers: [15]
Ticket amount Discount
Above Rs 70000 – 18%
Rs 55001 to Rs 70000 – 16%
Rs 35001 to Rs 55000 – 12%
Rs 25001 to Rs 35000 – 10%
less than Rs 25001 – 2%
Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer :
1 SL. NO. Name Ticket charges Discount Net amount
2 1 - - - -
(Assume that there are 15 customers, first customer is given the serial no (SlNo.) 1, next customer 2 … and so on.
Ans.
import java.util.Scanner;
public class Travels {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter sno: ");
int sno = scanner.nextInt();
System.out.print("Enter name: ");
String name = scanner.next();
System.out.print("Enter ticket charges: ");
int charges = scanner.nextInt();
int discountPercentage = 0;
if (charges > 70000) {
discountPercentage = 18;
} else if (charges >= 55001 && charges <= 70000) { discountPercentage = 16; } else if (charges >= 35001 && charges <= 55000) { discountPercentage = 12; } else if (charges >= 25001 && charges <= 35000) {
discountPercentage = 10;
} else if (charges < 25001) {
discountPercentage = 2;
}
double discount = discountPercentage * charges / 100.0;
double netAmount = charges - discount;
System.out.println("Sl. No. \t Name \t Ticket Charges \t Discount \t Net Amount");
System.out.println(sno + "\t" + charges + "\t" + discount + "\t" + netAmount);
}
}
Sample Output:
Enter sno: 3
Enter name: Sai
Enter ticket charges: 27000
Sl. No. Name Ticket Charges Discount Net Amount
3 27000 2700.0 24300.0
Question 7
Write a menu driven program to accept a number and check and display whether it is a Prime Number or not OR an Automorphic Number or not. (Use switch-case statement). [15]
(a) Prime number : A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number. Example : 3,5,7,11,13 etc.
(b) Automorphic number : An automorphic number is the number which is contained in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last two digits.
Ans.
import java.util.Scanner;
public class Menu {
public boolean isPrime(int n) {
for (int i = 1; i < n; i++) {
if (n % i == 0) {
return true;
}
}
return false;
}
public boolean isAutomorphic(int n) {
int square = n * n;
String originalNumber = n + "";
String squareNumber = n + "";
String lastDigits = squareNumber.substring(squareNumber.length() - originalNumber.length(), squareNumber.length());
return lastDigits.equals(originalNumber);
}
public void menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter 1 for prime number");
System.out.println("Enter 2 for automorphic number");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
System.out.print("Enter a number: ");
int num = scanner.nextInt();
switch (choice) {
case 1:
boolean prime = isPrime(num);
if (prime) {
System.out.println(num + " is a prime number");
} else {
System.out.println(num + " is not a prime number");
}
break;
case 2:
0 boolean automorphic = isAutomorphic(num);
if (automorphic) {
System.out.println(num + " is an automorphic number");
} else {
System.out.println(num + " is not an automorphic number");
}
break;
default:
System.out.println("Invalid choice");
}
}
public static void main(String[] args) {
Menu menu = new Menu();
menu.menu();
}
}
Sample Output 1:
Enter 1 for prime number
Enter 2 for automorphic number
Enter your choice: 1
Enter a number: 13
13 is a prime number
Sample Output 2:
Enter 1 for prime number
Enter 2 for automorphic number
Enter your choice: 2
Enter a number: 25
25 is an automorphic number
Question 8.
Write a program to store 6 element in an array P, and 4 elements in an array Q and produce a third array R, containing all elements of array P and Q. Display the resultant array [15]
Example:
INPUT OUTPUT
P[] Q [] R []
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
23
7
8
Ans.
import java.util.Scanner;
public class ArrayStore {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] p = new int[6];
int[] q = new int[4];
int[] r = new int[10];
System.out.print("Enter elements of array P: ");
for (int i = 0; i < 6; i++) {
p[i] = scanner.nextInt();
}
System.out.print("Enter elements of array Q: ");
for (int i = 0; i < 4; i++) {
q[i] = scanner.nextInt();
}
for (int i = 0; i < 6; i++) {
r[i] = p[i];
}
for (int i = 0; i < 4; i++) {
r[i + 6] = q[i];
}
System.out.print("Elements of array R: ");
for (int i = 0; i < 10; i++) {
System.out.print(r[i] + " ");
}
}
}
Sample Output:
Enter elements of array P: 4 6 1 2 3 10
Enter elements of array Q: 19 23 7 8
Elements of array R: 4 6 1 2 3 10 19 23 7 8
Question 9
Write a program to input a string in uppercase and print the frequency of each character. [15]
INPUT : COMPUTER HARDWARE
OUTPUT :
CHARACTERS FREQUENCY
A 2
C 1
D 1
E 2
H 1
M 1
O 1
P 1
R 3
T 1
U 1
W 1
Ans
import java.util.Scanner;
public class Frequency {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = scanner.nextLine();
int[] frequency = new int[26];
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isUpperCase(ch)) {
frequency[ch - 65]++;
}
}
System.out.println("Characters Frequency");
for (int i = 0; i < 26; i++) {
if (frequency[i] != 0) {
System.out.println((char) (i + 65) + "\t" + frequency[i]);
}
}
}
}
(a) Define the term Byte Code. [2]
Ans. The Java compiler compiles the source programs into an intermediate code called the Java Byte Code which is interpreted by the Java Virtual Machine (JVM)
(b) What do you understand by type conversion? [2]
Ans. Type conversion or casting is the conversion of the data type of a literal from one type to another. There are tow types of types of casting – implicit casting and explicit casting.
(c) Name two jump statements and their use. [2]
Ans. break and continue are the two jump statements in Java. break is used to force early termination of a loop. continue is used to move to the next iteration of the loop while skipping the remaining code in the current iteration.
(d) What is Exception ? Name two Exception Handling Blocks. [2]
Ans. An Exception is an error which occurs during the execution of a program. The exception handling blocks are try, catch and finally.
(e) Write two advantages of using functions in a program. [2]
Ans. i) Function make code reusable.
ii) Functions improve modularity and facilitate easy debugging.
Question 2.
(a) State the purpose and return data type of the following String functions: [2]
(i) indexOf ( )
(ii) compareTo ( )
Ans. i) indexOf() returns the index of the character or String passed as the parameter in the string on which is invoked.
Return type is int.
ii) compareTo() it compares two string values whether the strings are same or which is bigger and which is smaller..
Return type is int.
(b) What is the result stored in x, after evaluating the following expression [2]
int x = 5;
x = x++ * 2 + 3 * –x;
Ans. x = 5 * 2 + 3 * -6
x = 10 – 18
x = -8
(c) Differentiate between static and non-static data members. [2]
Ans. i) static variables belong to the class and all object share a single instance of the static variables. Non static varaiables belong to the objects. Each object has a copy of these members.
ii) static functions can access only static data members. Non static function can access both static and non static data members.
(d) Write the difference between length and length() functions. [2]
Ans. length is a property of an array which gives the size of the array. length() is a function of the String class which returns the size of the String.
(e) Differentiate between private and protected visibility modifiers. [2]
Ans. private members are accessible only in the class in which they have been defined. protected members are accessible in the class in which they have been defined as well in the sub classes of that class.
Question 3
(a) What do you understand by data abstraction? Explain with an example.[2]
Ans. Abstraction refers to representing the essential features of a system without considering all the details. Example: When we drive a car, we concentrate on how to drive it without bothering ourselves on how the engine works and other things.
(b) What will be the output of the following code?
(i) [2]
int m=2;
int n=15;
for(int i = 1; i<5; i++);
m++; –-n;
System.out.println("m=" +m);
System.out.println("n="+n);
Ans.
m=3
n=14
Note that there is a semicolon at the end of the loop. So, it is an empty loop and does’t affect the values of m and n.
(ii) [2]
char x = 'A' ; int m;
m=(x=='a') ? 'A' : ‘a’;
System.out.println("m="+m);
Ans.
1 m=97
(c) Analyse the following program segment and determine how many times tne
loop will be executed and what will be the output of the program segment. [2]
int k=1, i=2;
while (++i<6)
k*=i;
System.out.println(k);
Ans. Following are the iterations of the loop
3 < 6 ---- k = 1 * 3 = 3
4 < 6 ---- k = 3 * 4 = 12
5 < 6 ---- k = 12 * 5 = 60
6 < 6 ---- false
The loop will run three times and output is 60
1
(d) Give the prototype of a function check which receives a character ch and an integer n and returns true or false. [2]
Ans.
public boolean function(char ch, int n)
(e) State two features of a constructor. [2]
Ans. i) A constructor has the same name as that of a class.
ii) A constructor does not have any return type.
iii) A constructor is automatically called during object creation.
(f) Write a statement each to perform the following task on a string:
(i) Extract the second last character of a word stored in the variable wd. [2]
Ans.
char ch = wd.charAt(wd.length()-2);
(ii) Check if the second character of a string str is in uppercase. [2]
Ans.
boolean result = Character.isUpperCase(str.charAt(1));
(g) What will the following functions return when executed? [2]
(j) Math.max(-17, -19)
(ii) Math.ceil(7.8)
Ans. i) -17
ii) 8.0
(h) (i) Why is an object called an instance of a class?
(ii) What is the use of the keyword import? [2]
Ans. i) An object is called an instance of a class because the objects contains a copy of all the instance variables of the class.
ii) The import keyword is used to import classes from external packages.
SECTION – B (60 Marks)
Attempt any four questions from this Section.
The answers in this Section should consist of the Programs in either Blue J environment or any program environment with Java as the base.
Each program should be written using Variable descriptions/Mnemonic Codes such that the logic of the program is clearly depicted.
Flow-Charts and Algorithms are not required.
Question 4.
Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found display the element along with its position, otherwise display the message “Search element not found”. [15]
5,7,9,11,15,20,30,45,89,97
Ans.
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
int[] array = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number you want to search for: ");
int target = scanner.nextInt();
int left = 0;
int right = array.length - 1;
int result = -1;
while (left <= right) { int middle = (left + right) / 2; if (array[middle] == target) { result = middle; break; } else if (array[middle] > target) {
right = middle - 1;
} else {
left = middle + 1;
}
}
if (result != -1) {
System.out.println(target + " found at index " + result);
} else {
System.out.println("Search element not found");
}
}
}
Sample Output 1:
Enter the number you want to search for: 7
7 found at index 1
Sample Output 2:
Enter the number you want to search for: 34
Search element not found
Question 5
Define a class ‘Student described as below: [15]
Data members/instance variables : name,age,m1,m2,m3 (marks in 3 subjects), maximum, average
Member methods :
(i) A parameterized constructor to initialize the data members.
(ii) To accept the details of a student.
(iii) To compute the average and the maximum out of three marks.
(iv) To display the name, age, marks in three subjects, maximum and average.
Write a main method to create an object of a class and call the above member methods.
Ans.
import java.util.Scanner;
public class Student {
String name;
int age;
int m1, m2, m3;
int maximum;
double average;
public Student(String n, int a, int marks1, int marks2, int marks3, int max, double avg) {
name = n;
age = a;
m1 = marks1;
m2 = marks2;
m3 = marks3;
maximum = max;
average = avg;
}
public void acceptDetails() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
name = scanner.next();
System.out.print("Enter age: ");
age = scanner.nextInt();
System.out.print("Enter marks1: ");
m1 = scanner.nextInt();
System.out.print("Enter marks2: ");
m2 = scanner.nextInt();
System.out.print("Enter marks3: ");
m3 = scanner.nextInt();
}
public void compute() {
average = (m1 + m2 + m3) / 3.0;
maximum = Math.max(m1, (Math.max(m2, m3)));
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks1 " + m1);
System.out.println("Marks2 " + m2);
System.out.println("Marks3 " + m3);
System.out.println("Maximum: " + maximum);
System.out.println("Average: " + average);
}
public static void main(String[] args) {
Student student = new Student("",0,0,0,0,0,0.0);
student.acceptDetails();
student.compute();
student.display();
}
}
Sample Output:
Enter name: Sai
Enter age: 20
Enter marks1: 95
Enter marks2: 100
Enter marks3: 99
Name: Sai
Age: 20
Marks1 95
Marks2 100
Marks3 99
Maximum: 100
Average: 98.0
Question 6
Shasha Travels Pvt. Ltd. gives the following discount to its customers: [15]
Ticket amount Discount
Above Rs 70000 – 18%
Rs 55001 to Rs 70000 – 16%
Rs 35001 to Rs 55000 – 12%
Rs 25001 to Rs 35000 – 10%
less than Rs 25001 – 2%
Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer :
1 SL. NO. Name Ticket charges Discount Net amount
2 1 - - - -
(Assume that there are 15 customers, first customer is given the serial no (SlNo.) 1, next customer 2 … and so on.
Ans.
import java.util.Scanner;
public class Travels {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter sno: ");
int sno = scanner.nextInt();
System.out.print("Enter name: ");
String name = scanner.next();
System.out.print("Enter ticket charges: ");
int charges = scanner.nextInt();
int discountPercentage = 0;
if (charges > 70000) {
discountPercentage = 18;
} else if (charges >= 55001 && charges <= 70000) { discountPercentage = 16; } else if (charges >= 35001 && charges <= 55000) { discountPercentage = 12; } else if (charges >= 25001 && charges <= 35000) {
discountPercentage = 10;
} else if (charges < 25001) {
discountPercentage = 2;
}
double discount = discountPercentage * charges / 100.0;
double netAmount = charges - discount;
System.out.println("Sl. No. \t Name \t Ticket Charges \t Discount \t Net Amount");
System.out.println(sno + "\t" + charges + "\t" + discount + "\t" + netAmount);
}
}
Sample Output:
Enter sno: 3
Enter name: Sai
Enter ticket charges: 27000
Sl. No. Name Ticket Charges Discount Net Amount
3 27000 2700.0 24300.0
Question 7
Write a menu driven program to accept a number and check and display whether it is a Prime Number or not OR an Automorphic Number or not. (Use switch-case statement). [15]
(a) Prime number : A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number. Example : 3,5,7,11,13 etc.
(b) Automorphic number : An automorphic number is the number which is contained in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last two digits.
Ans.
import java.util.Scanner;
public class Menu {
public boolean isPrime(int n) {
for (int i = 1; i < n; i++) {
if (n % i == 0) {
return true;
}
}
return false;
}
public boolean isAutomorphic(int n) {
int square = n * n;
String originalNumber = n + "";
String squareNumber = n + "";
String lastDigits = squareNumber.substring(squareNumber.length() - originalNumber.length(), squareNumber.length());
return lastDigits.equals(originalNumber);
}
public void menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter 1 for prime number");
System.out.println("Enter 2 for automorphic number");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
System.out.print("Enter a number: ");
int num = scanner.nextInt();
switch (choice) {
case 1:
boolean prime = isPrime(num);
if (prime) {
System.out.println(num + " is a prime number");
} else {
System.out.println(num + " is not a prime number");
}
break;
case 2:
0 boolean automorphic = isAutomorphic(num);
if (automorphic) {
System.out.println(num + " is an automorphic number");
} else {
System.out.println(num + " is not an automorphic number");
}
break;
default:
System.out.println("Invalid choice");
}
}
public static void main(String[] args) {
Menu menu = new Menu();
menu.menu();
}
}
Sample Output 1:
Enter 1 for prime number
Enter 2 for automorphic number
Enter your choice: 1
Enter a number: 13
13 is a prime number
Sample Output 2:
Enter 1 for prime number
Enter 2 for automorphic number
Enter your choice: 2
Enter a number: 25
25 is an automorphic number
Question 8.
Write a program to store 6 element in an array P, and 4 elements in an array Q and produce a third array R, containing all elements of array P and Q. Display the resultant array [15]
Example:
INPUT OUTPUT
P[] Q [] R []
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
23
7
8
Ans.
import java.util.Scanner;
public class ArrayStore {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] p = new int[6];
int[] q = new int[4];
int[] r = new int[10];
System.out.print("Enter elements of array P: ");
for (int i = 0; i < 6; i++) {
p[i] = scanner.nextInt();
}
System.out.print("Enter elements of array Q: ");
for (int i = 0; i < 4; i++) {
q[i] = scanner.nextInt();
}
for (int i = 0; i < 6; i++) {
r[i] = p[i];
}
for (int i = 0; i < 4; i++) {
r[i + 6] = q[i];
}
System.out.print("Elements of array R: ");
for (int i = 0; i < 10; i++) {
System.out.print(r[i] + " ");
}
}
}
Sample Output:
Enter elements of array P: 4 6 1 2 3 10
Enter elements of array Q: 19 23 7 8
Elements of array R: 4 6 1 2 3 10 19 23 7 8
Question 9
Write a program to input a string in uppercase and print the frequency of each character. [15]
INPUT : COMPUTER HARDWARE
OUTPUT :
CHARACTERS FREQUENCY
A 2
C 1
D 1
E 2
H 1
M 1
O 1
P 1
R 3
T 1
U 1
W 1
Ans
import java.util.Scanner;
public class Frequency {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a String: ");
String input = scanner.nextLine();
int[] frequency = new int[26];
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isUpperCase(ch)) {
frequency[ch - 65]++;
}
}
System.out.println("Characters Frequency");
for (int i = 0; i < 26; i++) {
if (frequency[i] != 0) {
System.out.println((char) (i + 65) + "\t" + frequency[i]);
}
}
}
}