ICSE Examination 2017 – 18 (Solution)
COMPUTER APPLICATION
Class – X
SECTION – A (40 MARKS)
Attempt all questions
Class – X
SECTION – A (40 MARKS)
Attempt all questions
Question 1:
(a) What is Inheritance? [2]
Ans: - Inheritance is the process by which one class extends another class to inherit the
variables and functions and add any additional variables and methods.
(b) Name the operators listed below: [2]
i) <
ii) ++
iii) &&
iv)? :
Ans: - i) < – Less than relational operator
ii) ++ – increment operator
iii) && – Logical AND operator
iv) ? : – Ternary operator
(c) State the number of bytes occupied by char and int data types. [2]
Ans: - char occupies two bytes and int occupies 4 bytes.
(d) Writ one difference between / and % operator. [2]
Ans: - / division operator which gives quotient while % is modules operator which gives the
remainder on dividing two numbers.
(e) String x[] = {“SAMSUNG”, “NOKIA”, “SONY”, “MICROMAX”,
“BLAKBERRY”}; [2]
Give the output of the following statements:
i) System.out.println(x[1]);
ii) System.out.println(x[3].length());
Ans: - i) NOKIA
ii) “MICROMAX”.length()
= 8
Question 2:
(a) Name of the following:
i) A keyword used to call a package in the program.
ii) Any one reference data types.
Ans: - i) import
ii) String
(b) What are the two ways of invoking functions? [2]
Ans: - Call by value and Call by reference
(c) State the data type and value of res after the following is executed:
char ch = 't';
res = Character.toUpperCase(ch); [2]
Ans: - Data type of res is char and value is T
(d) Give the output of the following program segment and also mention the number of
times the loop is executed: [2]
int a,b;
for(a=6,b=4;a<=24;a=a+6)
{
if(a%b==0)
break;
}
System.out.println(a);
Ans: - The output is 12 and loop is executed 2 times.
(e) Write the output: [2]
char ch= 'F';
int m=ch;
m=m+5;
System.out.println(m+ " " +ch);
Ans: - 75 F
Question 3:
(a) Write a Java expression for the following:
[2]
Ans: - a * Math.pow(x, 5) + b *b Math.pow(x, 3) + c
(b) What is the value of x1 if x = 5? [2]
x1 = ++x – x++ + – –x
Ans: x1 = ++x – x++ + – –x
x1 = 6 – 6 + 6
x1 = 6
(c) Why is an object called an instance of a class? [2]
Ans: - An object is called an instance of a class as every object created from a class gets its
own instances of the variables defined in the class. Multiple objects can be created from the
same class.
(d) Convert following do-while loop into for loop. [2]
int i=1;
int d=5;
do{
d=d*2;
System.out.println(d);
i++;
}while(i<=5);
Ans: for(int i=1, d=5; i<=5; i++)
{
d = d * 2;
System.out.println(d);
}
(e) Differentiate between constructor and function. [2]
Ans: - (i) The constructor of a class is invoked automatically during object creation while a
function is invoked manually after object creation.
(ii)A constructor is invoked only once while a function can be invoked multiple times.
(iii) A constructor has the same name as the class in which it is defined while a function
can have any time.
(f) Write the output of the following: [2]
String s= "Today is Test";
System.out.println(s.indexOf('T'));
System.out.println(s.substring(0,7)+ " "+ "Holiday");
Ans: - 0
Today i Holiday
(g) What are the values stored in variables r1 and r2: [2]
i) double r1 = Math.abs(Math.min(–2.83, –5.83));
ii) double r2 = Math.sqrt(Math.floor(16.3));
Ans: - i) 5.83
ii) 4.0
(h) Give the output of the following code: [2]
String A= "26", B= "100" ;
String D=A+B+ "200";
int x= Integer.parseInt(A);
int y=Integer.parseInt(B);
int d=x+y;
System.out.println("Result 1=" +D);
System.out.println("Result 2="+d);
Ans: - Result 1 = 26100200
Result 2 = 126
(i) Analyze the given program segment and answer the following questions: [2]
for(int i=3;i<=4;i++){
for(int j=2;j<i;j++){
System.out.print(" ");
}
System.out.println("WIN");
}
(i) How many times does the inner loop execute?
(ii) Write the output of the program segment.
Ans: - i) 3 times
ii) WIN
WIN
(j) What is the difference between the Scanner class functions next() and nextLine()? [2]
Ans: - next() read a single token i.e. all characters from the current position till a whitespace
or tab or new line is encountered.
nextLine() reads an entire line i.e. all characters from the current position till a new
line is encountered.
SECTION – B (60 MARKS)
Attempt any four questions
Question 4:
Define a class ElectricBill with the following specifications:
class : ElectricBill
Instance variable / data members
String n – to store the name of the customer
int units – to store the number of units consumed
double bill – to store the amount to be paid
Methods
void accept(): to accept the name of the customer and number of units consumed.
void calculate(): to calculate the bill as per the following tariff:
Number of units Rate per unit
First 100 units ₹ 2.00
Next 200 units ₹ 3.00
Above 300 units ₹ 5.00
A surcharged of 2.5% charged if the number of units consumed is above 300 units.
void print(): to print the details as follows:
Name of the customer: ___________
Number of units consumed: ________
Bill amount: __________
Write a main() method to create an object of the class and call the above member
functions. [15]
Ans: -
import java.util.*;
class ElectricBill
{
private String n;
private int units;
private double bill;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
n = sc.next();
System.out.print("Enter units: ");
units = sc.nextInt();
}
void calculate()
{
if (units <= 100)
bill = units * 2;
else if (units <= 300)
bill = 100 * 2 + (units - 100) * 3;
else
{
bill = 100 * 2 + 200 * 3 + (units - 300) * 5;
double surcharge = bill * 2.5 / 100;
bill = bill + surcharge;
}
}
public void print()
{
System.out.println("Name of the customer: " + n);
System.out.println("Number of units consumed: " + units);
System.out.println("Bill amount: " + bill);
}
public static void main()
{
ElectricBill ob= new ElectricBill();
ob.accept();
ob.calculate();
ob.print();
}
}
Question 5:
Write a program to accept a number and check and display whether it is a spy number
or not. [15]
A number is spy if the sum of its digits equals the product of its digits.
Example: Consider the number 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 × 1 × 2 × 4 = 8
Ans: -
class SpyNumber
{
public static void main(int n)
{
int sumOfDigits = 0;
int productOfDigits = 1;
while (n > 0)
{
int remainder = n % 10;
sumOfDigits = sumOfDigits + remainder;
productOfDigits = productOfDigits * remainder;
n = n / 10;
}
if (sumOfDigits == productOfDigits)
System.out.println("Spy number");
else
System.out.println("Not a spy number");
}
}
Question 6:
Using switch statement, write a menu driven program for the following: [15]
i) To find and display the sum of the series given below:
ii) To display the following series:
1 11 111 1111 11111
For an incorrect option, an appropriate error message should displayed.
Ans: -
import java.util.*;
class Menu
{
public static void main()
{
System.out.println("1. Sum of series");
System.out.println("2. Display Series");
System.out.print("Enter your choice: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (choice)
{
case 1:
double sum = 0;
for (int i = 1; i <= 20; i++)
{
if (i % 2 == 1)
sum = sum + Math.pow(2, i);
else
sum = sum - Math.pow(2, i);
}
System.out.println("Sum = " + sum);
break;
case 2:
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
System.out.print("1");
System.out.print(" ");
}
break;
default:
System.out.println("Invalid choice");
}
}
}
Question 7:
Write a program to input integer elements into an array of size 20 and perform the
following operations: [15]
i) Display largest number from the array
ii) Display smallest number from the array
iii) Display sum of all the elements of the array
Ans: -
import java.util.*;
class ArrayOperations
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int[] numbers = new int[20];
System.out.print("Enter 20 numbers: ");
for (int i = 0; i < 20; i++)
numbers[i] = sc.nextInt();
int smallest = numbers[0];
int largest = numbers[0];
int sum = 0;
for (int i = 0; i < 20; i++)
{
if (numbers[i] < smallest)
smallest = numbers[i];
if (numbers[i] > largest)
largest = numbers[i];
sum = sum + numbers[i];
}
System.out.println("Smallest = " + smallest);
System.out.println("Largest = " + largest);
System.out.println("Sum = " + sum);
}
}
Question 8:
Design a class to overload a function check() as follows: [15]
i) void check(String str, char ch) – to find and print the frequency of a
character in a String.
Example:
str = “success”;
ch = 's';
Output: Number of s present is = 3
ii) void check(String s1) – to display only the vowels from string s1, after
converting it to lower case.
Example:
s1 = “computer”;
Output – o u e
Ans: -
class Overload
{
public void check(String str, char ch)
{
int result = 0;
for (int i = 0; i < str.length(); i++)
{
char currentChar = str.charAt(i);
if (ch == currentChar)
result++;
}
System.out.println("number of s present is = " + result);
}
public void check(String s1)
{
s1 = s1.toLowerCase();
for (int i = 0; i < s1.length(); i++)
{
char currentChar = s1.charAt(i);
if (currentChar == 'a' || currentChar == 'e' || currentChar == 'i' ||
currentChar == 'o' || currentChar == 'u')
System.out.print(currentChar + " " );
}
}
}
Question 9:
Write a program to input forty words in an array. Arrange these words in descending
order of alphabets, using selection sort technique. Print the sorted array. [15]
Ans: -
import java.util.*;
class SelectionSort
{
public static void main()
{
Scanner sc = new Scanner(System.in);
String[] words = new String[40];
System.out.println("Enter 40 words: ");
for (int i = 0; i < 40; i++)
words[i] = sc.nextLine();
for (int i = 0; i < words.length - 1; i++)
{
int largestWordIndex = i;
for (int j = i + 1; j < words.length; j++)
{
if (words[j].compareTo(words[largestWordIndex]) > 0)
largestWordIndex = j;
}
String temp = words[largestWordIndex];
words[largestWordIndex] = words[i];
words[i] = temp;
}
for (int i = 0; i < 40; i++)
System.out.println(words[i]);
}
}
0 Comments:
Post a Comment