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 this 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 brackets [ ].
SECTION A (40 Marks)
Attempt all questions.
(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 this 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 brackets [ ].
SECTION A (40 Marks)
Attempt all questions.
Question 1:
(a) Define abstraction. [2]
Ans. Abstraction refers to specifying the behaviour of a class without going into the details of the implementation.
(b) Differentiate between searching and sorting. [2]
Ans. In searching, we are provided with an array which contains multiple elements and asked to find the index of a specific element.
In sorting, an array is provided and we are asked to order the elements in either ascending or descending order.
(c) Write a difference between the functions isUpperCase() and toUpperCase(). [2]
Ans. Both isUpperCase() and toUpperCase() are functions of the Character class.
isUpperCase() accepts a character and returns true if the provided character is uppercase. Else, it returns false.
Ex:
boolean b1 = Character.isUpperCase('a');
boolean b2 = Character.isUpperCase('A');
System.out.println(b1 + " " + b2);will print
false true
(d) How are private members of a class different from public members? [2]
Ans. Private members of a class can be accessed only by the methods of the class in which they are declared. While public members can be accessed outside of the class also.
(e) Classify the following as primitive or non-primitive data types: [2]
(i) char
(ii) arrays
(iii) int
(iv) classes
Ans. (i) char – Primitive
(ii) arrays – Non primitive
(iii) int – Primitive
(iv) Classes – Non proimitive
Question 2
(a) (i) int res = ‘A’;
What is the value of res?
(ii) Name the package that contains wrapper classes. [2]
Ans. (i) res will hold the ASCII value of ‘A’ which is 65.
(ii) java.lang package contains wrapper classes.
(b) State the difference between while and do-while loop. [2]
Ans. A while is an entry controlled loop i.e. the loop condition is checked before the loop is executed while do-while is an exit controlled loop i.e. the loop condition is checked after the loop is executed.
Because of this a do-while loop gets executed atleast once which is not true for a while loop.
(c) System.out.print(“BEST “);
System.out.println(“OF LUCK”);
Choose the correct option for the output of the above statements: [2]
(i) BEST OF LUCK
(ii) BEST
OF LUCK
Ans. (i) is the correct output.
System.out.print() does not add a new line at the end because of which ‘OF LUCK’ gets printed on the same line as ‘BEST’.
(d) Write the prototype of a function check which takes an integer as an argument and returns a character. [2]
Ans.
char function(int a)(e) Write the return data type of the following functions: [2]
(i) endsWith()
(ii) log()
Ans. i) boolean
ii) double
Question 3
(a) Write a Java expression for the following: [2]
√(3x + x2) / (a + b)
Ans. Math.sqrt(3 * x + Math.pow(x, 2)) / (a + b)
(b) What is the value of y after evaluating the expression given below? [2]
y += ++y + y– + –y; when int y = 8.
Ans.
y += ++y + y– + –y
y = 8 + ++y + y– + –y
y = 8 + 9 + 9 + 7
y = 33
(c) Give the output of the following: [2]
(i) Math.floor(-4.7)
(ii) Math.ceil(3.4) + Math.pow(2, 3)
Ans. i) -5.0
ii) 4.0 + 8.0 = 12.0
(d) Write two characteristics of a constructor. [2]
Ans. i) A constructor has the same name as that of the class
ii) A constructor does not have a return type
(e) Write the output for the following: [2]
System.out.println(“Incredible” + “\n” + “world”);
Ans.
Incredible
world
(f) Convert the following if else if construct into switch case: [2]
if(var == 1)
System.out.println("good");
else if(var == 2)
System.out.println("better");
else if(var == 3)
System.out.println("best");
else
System.out.println("invalid");Ans.
view sourceprint?
switch(var) {
case 1:
System.out.println("good");
break;
case 2:
System.out.println("better");
break;
case 3:
System.out.println("best");
break;
default:
System.out.println("invalid");
}
(g) Give the output of the following string functions: [2]
(i) “ACHIEVEMENT”.replace(‘E’, ‘A’)
(ii) “DEDICATE”.compareTo(“DEVOTE”)
Ans. i) ACHIAVEMANT
ii) The first two characters are same. So, we take the ASCII values of the third characters and subtract to get the result.
D – V = 68 – 86 = 18
(h) Consider the following String array and give the output: [2]
String arr[] = {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};
System.out.println(arr[0].length() > arr[3].length());
System.out.print(arr[4].substring(0, 3));Ans.
System.out.println(arr[0].length() > arr[3].length());
arr[0].length() > arr[3].length()
“DELHI”.length() > “LUCKNOW”.length()
5 > 7
false
Output is false
System.out.print(arr[4].substring(0, 3));
JAIPUR.substring(0, 3)
JAI
Output is JAI
(i) Rewrite the following using ternary operator: [2]
if(bill > 10000)
discount = bill * 10.0 / 100;
3else
discount = bill * 5.0 / 100;Ans.
1discount = bill > 10000 ? (bill * 10.0 / 100) : (bill * 5.0 / 100);
(j) Give the output of the following program segment and also mention how many times the loop is executed: [2]
int i;
for(i = 5; i > 10; i++)
System.out.println(i);
System.out.println(i * 4);Ans.
Loop condition i > 10 evaluates to 5 > 10 which is false. So, loop is not executed even once
System.out.println(i * 4) prints 5 * 4 = 20
SECTION B (60 Marks)
Attempt any four questions from this Section.
The answers in this section should consist of the programs in either BlueJ environment or any program environment with Java as the base.
Each program should be written using Variable descriptions/Mneumonic codes so that the logic of the program is clearly depicted.
Flow-charts and algorithms are not required.
Question 4
Design a class RailwayTicket with the following description:
Instance variables/data members:
String name: to store the name of the customer.
String coach: to store the type of coach customer wants to travel.
long mobno: to store customer’s mobile number.
int amt: to store basic amount of ticket.
int totalamt: to store the amount to be paid after updating the original amount.
Methods:
void accept(): to take input for name, coach, mobile number and amount.
void update(): to update the amount as per the coach selected. Extra amount to be added in the amount as follows:
Type of coaches Amount
First_AC 700
Second_AC 500
Third_AC 250
sleeper None
void display(): To display all details of a customer such as name, coach, total amount and mobile number.
Write a main() method to create an object of the class and call the above methods.
Ans.
import java.util.Scanner;
public class RailwayTicket {
String name;
String coach;
long mobno;
int amt;
int totalamt;
void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
name = scanner.nextLine();
System.out.print("Enter coach: ");
coach = scanner.nextLine();
System.out.print("Enter mobno: ");
mobno = scanner.nextLong();
System.out.print("Enter amt: ");
amt = scanner.nextInt();
}
void update() {
if (coach.equals("First_AC")) {
totalamt = amt + 700;
} else if (coach.equals("Second_AC")) {
totalamt = amt + 500;
} else if (coach.equals("Third_AC")) {
totalamt = amt + 250;
} else if (coach.equals("sleeper")) {
totalamt = amt;
}
}
void display() {
System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Mobile Number: " + mobno);
System.out.println("Amount: " + amt);
System.out.println("Total Amount: " + totalamt);
}
void main(String ss[]) {
RailwayTicket railwayTicket = new RailwayTicket();
railwayTicket.accept();
railwayTicket.update();
railwayTicket.display();
}
}Sample output:
Enter name: Akash
Enter coach: First_AC
Enter mobno: 9999999988
Enter amt: 3000
Name: Akash
Coach: First_AC
Mobile Number: 9999999988
Amount: 3000
Total Amount: 3700
Question 5
Write a program to input a number and check and print whether it is a Pronic number or not. Pronic number is the number which is the product of two consecutive integers.
Examples:
12 = 3 × 4
20 = 4 × 5
42 = 6 × 7
Ans.
import java.util.Scanner;
public class PronicNumber {
public static void main(String ss[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number: ");
int number = scanner.nextInt();
boolean pronic = false;
for (int i = 1; i < number; i++) {
int product = i * (i + 1);
if (product == number) {
pronic = true;
}
}
System.out.println("Pronic number = " + pronic);
}
}Sample output
Enter number: 42
Pronic number = true
Question 6
Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string.
Sample INPUT: we are in cyber world
Sample OUTPUT: We Are In Cyber World
Ans.
import java.util.Scanner;
public class SentenceCase {
public static void main(String ss[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = scanner.nextLine();
String output = "";
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (i == 0 || sentence.charAt(i - 1) == ' ') {
output = output + Character.toUpperCase(ch);
} else {
output = output + ch;
}
}
System.out.println("Output: " + output);
}
}Sample output
Enter a sentence: we are in cyber world
Output: We Are In Cyber World
Question 7
Design a class to overload a function volume() as follows:
(i) double volume(double r) – with radius ‘r’ as an argument, returns the volume of sphere using the formula:
v = 4 / 3 × 22 / 7 × r3
(ii)double volume(double h, double r) – with height ‘h’ and radius ‘r’ as the arguments, returns the volume of a cylinder using the formula:
v = 22 / 7 × r2 × h
(iii) double volume(double l, double b, double h) – with length ‘l’, breadth ‘b’ and height ‘h’ as the arguments, returns the volume of a cuboid using the formula:
v = l × b × h
Ans.
public class Overload {
double volume(double r) {
double vol = 4.0 / 3 * 22 / 7 * Math.pow(r, 3);
return vol;
}
double volume(double h, double r) {
double v = 22.0 / 7 * Math.pow(r, 2) * h;
return v;
}
double volume(double l, double b, double h) {
double v = l * b * h;
return v;
}
}
Question 8
Write a menu-driven program to display the pattern as per user’s choice:
Pattern 1 Pattern 2
ABCDE B
ABCD LL
ABC UUU
AB EEEE
A JJJJJ
For an incorrect option, an appropriate error message should be displayed.
Ans.
import java.util.Scanner;
public class Pattern {
public static void main(String ss[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter pattern number: ");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.print("Enter number of lines: ");
int n = scanner.nextInt();
for (int i = n; i >= 1; i--) {
for (int j = 0; j < i; j++) {
System.out.print((char) ('A' + j));
}
System.out.println();
}
} else if (choice == 2) {
System.out.print("Enter string: ");
String s = scanner.next();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
for (int j = 1; j <= (i + 1); j++) {
System.out.print(ch);
}
System.out.println();
}
} else {
System.out.println("Invalid choice");
}
}
}Sample output 1
Enter pattern number: 1
Enter number of lines: 5
ABCDE
ABCD
ABC
AB
ASample output 2
Enter pattern number: 2
Enter string: BLUE
B
LL
UUU
EEEE
JJJJJ
Sample output 3
Enter pattern number: 3
Invalid choice
Question 9
Write a program to accept name and total marks of N number of students in two single subscripts array name[] and totalmarks[].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students) / N]
(ii) Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average]
Ans.
import java.util.Scanner;
public class Student {
public static void main(String ss[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = scanner.nextInt();
String[] name = new String[n];
int[] totalmarks = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = scanner.next();
System.out.print("Enter marks: ");
totalmarks[i] = scanner.nextInt();
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + totalmarks[i];
}
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++) {
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
}
}
}Sample output
Enter number of students: 3
Student 1
Enter name: Akash
Enter marks: 95
Student 2
Enter name: Manu
Enter marks: 99
Student 3
Enter name: Rahul
Enter marks: 94
Average is 96.0
Deviation of Akash is -1.0
Deviation of Manu is 3.0
Deviation of Rahul is -2.0
(a) Define abstraction. [2]
Ans. Abstraction refers to specifying the behaviour of a class without going into the details of the implementation.
(b) Differentiate between searching and sorting. [2]
Ans. In searching, we are provided with an array which contains multiple elements and asked to find the index of a specific element.
In sorting, an array is provided and we are asked to order the elements in either ascending or descending order.
(c) Write a difference between the functions isUpperCase() and toUpperCase(). [2]
Ans. Both isUpperCase() and toUpperCase() are functions of the Character class.
isUpperCase() accepts a character and returns true if the provided character is uppercase. Else, it returns false.
Ex:
boolean b1 = Character.isUpperCase('a');
boolean b2 = Character.isUpperCase('A');
System.out.println(b1 + " " + b2);will print
false true
(d) How are private members of a class different from public members? [2]
Ans. Private members of a class can be accessed only by the methods of the class in which they are declared. While public members can be accessed outside of the class also.
(e) Classify the following as primitive or non-primitive data types: [2]
(i) char
(ii) arrays
(iii) int
(iv) classes
Ans. (i) char – Primitive
(ii) arrays – Non primitive
(iii) int – Primitive
(iv) Classes – Non proimitive
Question 2
(a) (i) int res = ‘A’;
What is the value of res?
(ii) Name the package that contains wrapper classes. [2]
Ans. (i) res will hold the ASCII value of ‘A’ which is 65.
(ii) java.lang package contains wrapper classes.
(b) State the difference between while and do-while loop. [2]
Ans. A while is an entry controlled loop i.e. the loop condition is checked before the loop is executed while do-while is an exit controlled loop i.e. the loop condition is checked after the loop is executed.
Because of this a do-while loop gets executed atleast once which is not true for a while loop.
(c) System.out.print(“BEST “);
System.out.println(“OF LUCK”);
Choose the correct option for the output of the above statements: [2]
(i) BEST OF LUCK
(ii) BEST
OF LUCK
Ans. (i) is the correct output.
System.out.print() does not add a new line at the end because of which ‘OF LUCK’ gets printed on the same line as ‘BEST’.
(d) Write the prototype of a function check which takes an integer as an argument and returns a character. [2]
Ans.
char function(int a)(e) Write the return data type of the following functions: [2]
(i) endsWith()
(ii) log()
Ans. i) boolean
ii) double
Question 3
(a) Write a Java expression for the following: [2]
√(3x + x2) / (a + b)
Ans. Math.sqrt(3 * x + Math.pow(x, 2)) / (a + b)
(b) What is the value of y after evaluating the expression given below? [2]
y += ++y + y– + –y; when int y = 8.
Ans.
y += ++y + y– + –y
y = 8 + ++y + y– + –y
y = 8 + 9 + 9 + 7
y = 33
(c) Give the output of the following: [2]
(i) Math.floor(-4.7)
(ii) Math.ceil(3.4) + Math.pow(2, 3)
Ans. i) -5.0
ii) 4.0 + 8.0 = 12.0
(d) Write two characteristics of a constructor. [2]
Ans. i) A constructor has the same name as that of the class
ii) A constructor does not have a return type
(e) Write the output for the following: [2]
System.out.println(“Incredible” + “\n” + “world”);
Ans.
Incredible
world
(f) Convert the following if else if construct into switch case: [2]
if(var == 1)
System.out.println("good");
else if(var == 2)
System.out.println("better");
else if(var == 3)
System.out.println("best");
else
System.out.println("invalid");Ans.
view sourceprint?
switch(var) {
case 1:
System.out.println("good");
break;
case 2:
System.out.println("better");
break;
case 3:
System.out.println("best");
break;
default:
System.out.println("invalid");
}
(g) Give the output of the following string functions: [2]
(i) “ACHIEVEMENT”.replace(‘E’, ‘A’)
(ii) “DEDICATE”.compareTo(“DEVOTE”)
Ans. i) ACHIAVEMANT
ii) The first two characters are same. So, we take the ASCII values of the third characters and subtract to get the result.
D – V = 68 – 86 = 18
(h) Consider the following String array and give the output: [2]
String arr[] = {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};
System.out.println(arr[0].length() > arr[3].length());
System.out.print(arr[4].substring(0, 3));Ans.
System.out.println(arr[0].length() > arr[3].length());
arr[0].length() > arr[3].length()
“DELHI”.length() > “LUCKNOW”.length()
5 > 7
false
Output is false
System.out.print(arr[4].substring(0, 3));
JAIPUR.substring(0, 3)
JAI
Output is JAI
(i) Rewrite the following using ternary operator: [2]
if(bill > 10000)
discount = bill * 10.0 / 100;
3else
discount = bill * 5.0 / 100;Ans.
1discount = bill > 10000 ? (bill * 10.0 / 100) : (bill * 5.0 / 100);
(j) Give the output of the following program segment and also mention how many times the loop is executed: [2]
int i;
for(i = 5; i > 10; i++)
System.out.println(i);
System.out.println(i * 4);Ans.
Loop condition i > 10 evaluates to 5 > 10 which is false. So, loop is not executed even once
System.out.println(i * 4) prints 5 * 4 = 20
SECTION B (60 Marks)
Attempt any four questions from this Section.
The answers in this section should consist of the programs in either BlueJ environment or any program environment with Java as the base.
Each program should be written using Variable descriptions/Mneumonic codes so that the logic of the program is clearly depicted.
Flow-charts and algorithms are not required.
Question 4
Design a class RailwayTicket with the following description:
Instance variables/data members:
String name: to store the name of the customer.
String coach: to store the type of coach customer wants to travel.
long mobno: to store customer’s mobile number.
int amt: to store basic amount of ticket.
int totalamt: to store the amount to be paid after updating the original amount.
Methods:
void accept(): to take input for name, coach, mobile number and amount.
void update(): to update the amount as per the coach selected. Extra amount to be added in the amount as follows:
Type of coaches Amount
First_AC 700
Second_AC 500
Third_AC 250
sleeper None
void display(): To display all details of a customer such as name, coach, total amount and mobile number.
Write a main() method to create an object of the class and call the above methods.
Ans.
import java.util.Scanner;
public class RailwayTicket {
String name;
String coach;
long mobno;
int amt;
int totalamt;
void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
name = scanner.nextLine();
System.out.print("Enter coach: ");
coach = scanner.nextLine();
System.out.print("Enter mobno: ");
mobno = scanner.nextLong();
System.out.print("Enter amt: ");
amt = scanner.nextInt();
}
void update() {
if (coach.equals("First_AC")) {
totalamt = amt + 700;
} else if (coach.equals("Second_AC")) {
totalamt = amt + 500;
} else if (coach.equals("Third_AC")) {
totalamt = amt + 250;
} else if (coach.equals("sleeper")) {
totalamt = amt;
}
}
void display() {
System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Mobile Number: " + mobno);
System.out.println("Amount: " + amt);
System.out.println("Total Amount: " + totalamt);
}
void main(String ss[]) {
RailwayTicket railwayTicket = new RailwayTicket();
railwayTicket.accept();
railwayTicket.update();
railwayTicket.display();
}
}Sample output:
Enter name: Akash
Enter coach: First_AC
Enter mobno: 9999999988
Enter amt: 3000
Name: Akash
Coach: First_AC
Mobile Number: 9999999988
Amount: 3000
Total Amount: 3700
Question 5
Write a program to input a number and check and print whether it is a Pronic number or not. Pronic number is the number which is the product of two consecutive integers.
Examples:
12 = 3 × 4
20 = 4 × 5
42 = 6 × 7
Ans.
import java.util.Scanner;
public class PronicNumber {
public static void main(String ss[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number: ");
int number = scanner.nextInt();
boolean pronic = false;
for (int i = 1; i < number; i++) {
int product = i * (i + 1);
if (product == number) {
pronic = true;
}
}
System.out.println("Pronic number = " + pronic);
}
}Sample output
Enter number: 42
Pronic number = true
Question 6
Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string.
Sample INPUT: we are in cyber world
Sample OUTPUT: We Are In Cyber World
Ans.
import java.util.Scanner;
public class SentenceCase {
public static void main(String ss[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = scanner.nextLine();
String output = "";
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (i == 0 || sentence.charAt(i - 1) == ' ') {
output = output + Character.toUpperCase(ch);
} else {
output = output + ch;
}
}
System.out.println("Output: " + output);
}
}Sample output
Enter a sentence: we are in cyber world
Output: We Are In Cyber World
Question 7
Design a class to overload a function volume() as follows:
(i) double volume(double r) – with radius ‘r’ as an argument, returns the volume of sphere using the formula:
v = 4 / 3 × 22 / 7 × r3
(ii)double volume(double h, double r) – with height ‘h’ and radius ‘r’ as the arguments, returns the volume of a cylinder using the formula:
v = 22 / 7 × r2 × h
(iii) double volume(double l, double b, double h) – with length ‘l’, breadth ‘b’ and height ‘h’ as the arguments, returns the volume of a cuboid using the formula:
v = l × b × h
Ans.
public class Overload {
double volume(double r) {
double vol = 4.0 / 3 * 22 / 7 * Math.pow(r, 3);
return vol;
}
double volume(double h, double r) {
double v = 22.0 / 7 * Math.pow(r, 2) * h;
return v;
}
double volume(double l, double b, double h) {
double v = l * b * h;
return v;
}
}
Question 8
Write a menu-driven program to display the pattern as per user’s choice:
Pattern 1 Pattern 2
ABCDE B
ABCD LL
ABC UUU
AB EEEE
A JJJJJ
For an incorrect option, an appropriate error message should be displayed.
Ans.
import java.util.Scanner;
public class Pattern {
public static void main(String ss[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter pattern number: ");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.print("Enter number of lines: ");
int n = scanner.nextInt();
for (int i = n; i >= 1; i--) {
for (int j = 0; j < i; j++) {
System.out.print((char) ('A' + j));
}
System.out.println();
}
} else if (choice == 2) {
System.out.print("Enter string: ");
String s = scanner.next();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
for (int j = 1; j <= (i + 1); j++) {
System.out.print(ch);
}
System.out.println();
}
} else {
System.out.println("Invalid choice");
}
}
}Sample output 1
Enter pattern number: 1
Enter number of lines: 5
ABCDE
ABCD
ABC
AB
ASample output 2
Enter pattern number: 2
Enter string: BLUE
B
LL
UUU
EEEE
JJJJJ
Sample output 3
Enter pattern number: 3
Invalid choice
Question 9
Write a program to accept name and total marks of N number of students in two single subscripts array name[] and totalmarks[].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students) / N]
(ii) Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average]
Ans.
import java.util.Scanner;
public class Student {
public static void main(String ss[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = scanner.nextInt();
String[] name = new String[n];
int[] totalmarks = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = scanner.next();
System.out.print("Enter marks: ");
totalmarks[i] = scanner.nextInt();
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + totalmarks[i];
}
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++) {
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
}
}
}Sample output
Enter number of students: 3
Student 1
Enter name: Akash
Enter marks: 95
Student 2
Enter name: Manu
Enter marks: 99
Student 3
Enter name: Rahul
Enter marks: 94
Average is 96.0
Deviation of Akash is -1.0
Deviation of Manu is 3.0
Deviation of Rahul is -2.0