ICSE COMPUTER APPLICATION 2018

SECTION A

(Attempt all questions.)

Question 1:

a. Define abstraction.

Answer: Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.

b. Difference between searching and sorting.

Answer:

Searching AlgorithmSorting Algorithm
1.Searching Algorithms are designed to retrieve an element from any data structure where it is used.A Sorting Algorithm is used to arrange the data of a list or array into some specific order.
2.These algorithms are generally classified into two categories i.e. Sequential Search and Interval Search.There are two different categories in sorting. These are Internal and External Sorting.
3.The worst-case time complexity of a searching algorithm is O(N).The worst-case time complexity of many sorting algorithms like Bubble Sort, Insertion Sort, Selection Sort, and Quick Sort is O(N2).
4.There are no stable and unstable searching algorithms.Bubble Sort, Insertion Sort, Merge Sort, etc are the stable sorting algorithms whereas Quick Sort, Heap Sort, etc are the unstable sorting algorithms.
5.The Linear Search and the Binary Search are examples of Searching Algorithms.The Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort, etc are examples of Sorting Algorithms.

c. Write a Difference between the function isUpperCase() and toUpperCase().

Answer: The isuppercase( ) method is used to check whether the given character is in upper case or not. It returns a Boolean data type.

The toUpperCase( ) method is used to convert a character or string into upper case. It returns char or String type.

d. How are private members of a class different from public members?

Answer: The scope of the private members is within the class whereas the scope of the public members is global.

e. Classify the following as primitive or non-primitive data types :
i.char

ii. arrays

iii. int

iv. classes

Answer: i. char is a primitive data type.
ii. arrays are non-primitive data types.
iii. int is the primitive data type.
iv. classes are non-primitive data types.

Question 2:

a. i. int res = ‘A’;
What is the value of res?
ii. Name the package that contains wrapper classes.

Answer:

i. Value of res is 65.

ii. Java.lang

b. State the difference between while and do-while loop.

Answer:

 WhileDo-While
The statements can be executed.The loop executes the statement at least once.
The condition is tested before execution.The condition is tested after execution.
The loop terminates if the condition becomes false.If the condition is false, the computer keeps executing the loop.

c. System.out.print(“BEST “);
System.out.println(“OF LUCK”);
Choose the correct option for the output of the above statements
i. BEST OF LUCK
ii. BEST
OF LUCK

Answer: i. BEST OF LUCK is correct.

d. Write the prototype of a function check which takes an integer as an argument and returns a character.

Answer: char check(int x).

e. Write the return data type of the following function:

i. endsWith( )

ii. log( )

Answer: i. Boolean

ii. double

Question 3:

a. Write a Java expression for the following : 

Answer: Math.sqrt (3 * x + x*x) / (a + b);

What is the value of y after evaluating the expression given below?

y + = + +y+y–+ –y; when int y=8

Answer: 33

c. Give the output of the following :

i. Math.floor (- 4.7)

ii. Math.ceil(3.4) + Math.pow(2,3)

Answer:

i. -5.0

ii. 12.0

d. Write two characteristics of a constructor.

Answer: i. A constructor has the same name as of class.

ii. Constructor gets invoked when an object is created.

e. Write the output for the following :

System.out.printIn(“Incredible” + “\n” + “world”);

Answer:

Incredible
world

f. Convert the following if else if construct into switch case 

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”);

Answer:

switch ( ) 
{
case 1:
System.out .println( “good”);
break; .
case 2:
System.out .println( “better”);
break;
case 3:
System.out.println( “invalid”);
break;
}

g. Give the output of the following string functions : 

i. “ACHIEVEMENT” .replace(‘E’, ‘A’)

ii. “DEDICATE”. compareTo(“DEVOTE”)

Answer:

i. ACHIAVAMANT

ii. – 18

h. Consider the following String array and give the output 

String arr[]= {“DELHI”, “CHENNAI”, “MUMBAI”, “LUCKNOW”, “JAIPUR”};

System.out.println(arr[0].length( )> arr[3].length( );

System.out.print(arr[4].substring(0,3));

Answer: false (Because arr[0] is DELHI consists of 5 characters but arr[3] is LUCKNOW consists of 7 characters. Therefore 5 > 7 is false)
JAI (because arr[4] is JAIPUR exists and extracts its three characters)

i. Rewrite the following using ternary operator : 

if(bill > 10000)
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;

Answer: discount = bill > 100 ? 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 : 

int i;
for (i = 5; i > 10; i++)
System.out.println(i);
System.out.println(i*4);

Answer: Output: 20, Loop will be executed 0 times.

Section B

Question 4:

Design a class Railway Ticket with 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 mob no: To store customer’s mobile number
int amt: To store a basic amount of ticket
int total amt: To store the amount to be paid after updating the original amount

Member methods
void accept ( )-To take input for a 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 CoachesAmount
First_ AC700
Second_AC500
Third _AC250
sleeperNone

void display( ) — To display all details of a customer such as a name, coach, total amount and mobile number.

Write a main method to create an object of the class and call the above member methods.

import java.util.*;
import java.io.*;
class Main {
String name, coach;
long mobno;
int amt, totalamt;
void accept()throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Passenger’s Name: ");
name = sc.next();
System.out.print("Enter Mobile Number: ");
mobno = sc.nextLong();
System.out.println("Enter Coach (First_AC/Second_AC/Third_AC/sleeper):");
coach = sc.next();
System.out.println("Enter basic amount of ticket: ");
amt = sc.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
totalamt = amt;
}
void display() {
System.out.println("\n\n Name :" +name);
System.out.println("Coach :" +coach);
System.out.println("Total Amount:" +totalamt);
System.out.println("Mobile No.:" +mobno);
}
public static void main (String args[ ])throws IOException
{
Main t = new Main();
t.accept();
t.update();
t.display();
}
}

Output:

Enter Passenger’s Name: 
Manoj
Enter Mobile Number: 9876543210
Enter Coach (FirstAC/SecondAC/ThirdAC/sleeper):
First_Ac C
Enter basic amount of ticket: 
1000


 Name :Manoj
Coach :First_AC
Total Amount:1700
Mobile No.:9876543210

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

import java.util.*;

class Main
{

  public static void main (String args[])
  {

    Scanner sc = new Scanner (System.in);

    System.out.print ("Enter the number: ");

    int n = sc.nextInt ();

    int i = 0;

    while (i * (i + 1) < n)

    {

	i++;

    }

    if (i * (i + 1) == n)
    System.out.println(n + " is a Pronic Number.");
    else
    System.out.println (n + " is not a Pronic Number.");

  }

}

Output:

Enter the number: 12
12 is a Pronic Number.

Question 6:

Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string. 

Sample input: we are in a cyber world

Sample output: We Are In Cyber World

import java.util.*;
class Main 
{
    public static void main(String args[ ]) 
    {
        Scanner sc = new Scanner(System.in);
        int i,l;
        char ch,ch1;
        System.out.print("Enter String in lowercase:");
        String str = sc.nextLine();
        str = ' ' +str;
        str= str.toLowerCase();
        String str2 = "";
        l=str.length();
        for (i = 0; i<l-1; i++) 
        {
           ch= str.charAt(i);
           ch1= str.charAt(i+1);
           if(str.charAt(i) == ' ') 
           {
                str2 = str2 + ch + (char)(ch1-32);
                i++;
            }
            else
            str2= str2 + ch;
        }
    str2 = str2 + str.charAt(i);// Last Character to Store
    System.out.println("New String = " + str2);
    }
}

Output:

Enter String in lowercase:we are in a cyber world
New String =  We Are In A Cyber World

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 a 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.

Answer:

import java.util.*;
class Main 
{
double volume (double R) 
{
    double V = 4.0 / 3 * 22.0 / 7 * R*R*R;
    return V;
}
double volume(double H, double R) 
{
    double V = 22.0 / 7 * R * R * H ;
    return V;
}
double volume (double L, double B, double H) 
{
    double V = L * B * H;
    return V;
}
public static void main(String args[])
{
    Scanner in = new Scanner (System.in);
    double sr,cr,ch,l,b,h;
    System.out.println("Enter The Value Of Radius of Sphere");
    sr=in.nextDouble(); // Radius of Sphere
    System.out.println("Enter The Value Of Radius and Height of Cylinder");
    cr=in.nextDouble(); //Radius of Cylinder
    ch=in.nextDouble(); //Height of Cylinder
    System.out.println("Enter The Value Of Length, Breadth and Height of Cuboid");
    l=in.nextDouble(); //Length of Cuboid
    b=in.nextDouble(); //Breadth of Cuboid
    h=in.nextDouble(); //Height of Cuboid
    Main ob = new Main();
    System.out.println("Volume Of Sphere = " + ob.volume(sr));
    System.out.println("Volume Of Cylinder = " +ob.volume(ch,cr));
    System.out.println("Volume Of Cuboid = " +ob.volume(l,b,h));
}
}

Output:

Enter The Value Of Radius of Sphere
3
Enter The Value Of Radius and Height of Cylinder
4
5
Enter The Value Of Length, Breadth and Height of Cuboid
3
5
4
Volume Of Sphere = 113.14285714285717
Volume Of Cylinder = 251.42857142857142
Volume Of Cuboid = 60.0

Write a menu driven program to display the pattern as per user’s choice.
Pattern 1
ABCDE
ABCD
ABC
AB
A

Pattern 2
B
LL
UUU
EEEE
For an incorrect option, an appropriate error message should be displayed.

Answer

import java.util.*;
class Main 
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s;
int i,j,l,ch;
System.out.println("::::MENU::::");
System.out.println(" 1. To display ABCD Pattern");
System.out.println(" 2. To display Word Pattern");
System.out.println("Enter your choice:");
ch= sc.nextInt();
switch(ch) 
{
case 1:
for (i=65;i<=69;i++)
{
for(j=65;j<=i;j++)
{
System.out.print(((char)j));
}
System.out.println();
}
break;
case 2:
System.out.print("Enter your String:");
s= sc.next();
l=s.length();
for (i = 0; i <=l;i++) 
{
System.out.println(s.substring(0,i));
}
break;
default:
System.out.println("Invalid Input");
break;
}
}
}

Output:

::::MENU::::
 1. To display ABCD Pattern
 2. To display Word Pattern
Enter your choice:
1
A
AB
ABC
ABCD
ABCDE

::::MENU::::
 1. To display ABCD Pattern
 2. To display Word Pattern
Enter your choice:
2
Enter your String:BLUE

B
BL
BLU
BLUE

Question 9:

Write a program to accept a name and total marks of N number of students in two single subscript 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]

Answer:

import java. util.*;
class Main 
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double average;
System.out.print("Enter number of students:");
int n = sc.nextInt( );
String name[] = new String[n];
int totalmarks[] = new int[n];
double deviation[] = new double[n];
double s = 0;
for (int i = 0; i < n; i++) 
{
System.out.print("Enter Name of the Student:");
name[i] = sc.next( );
System.out.print("Enter Marks:");
totalmarks[i] = sc.nextInt();
s = s + totalmarks[i];
}
average = s / n;
System.out.println("The average of the total marks of " +n+" number of students:" +average);
for (int i = 0; i < n; i++) {
deviation[i] = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + "/'s marks with the average:" +deviation[i]);
}
}
}

Output:

Enter number of students:5
Enter Name of the Student:Manoj
Enter Marks:98
Enter Name of the Student:Abhijeet
Enter Marks:98
Enter Name of the Student:Avinash
Enter Marks:93
Enter Name of the Student:Bikash
Enter Marks:94
Enter Name of the Student:Jayshree
Enter Marks:75
The average of the total marks of 5 number of students:91.6
Deviation of Manoj's marks with the average:6.400000000000006
Deviation of Abhijeet's marks with the average:6.400000000000006
Deviation of Avinash's marks with the average:1.4000000000000057
Deviation of Bikash's marks with the average:2.4000000000000057
Deviation of Jayshree's marks with the average:-16.599999999999994
Share your love