0% found this document useful (0 votes)
38 views17 pages

ICSE Class X Java String Handling Worksheet

This document is a worksheet for ICSE Class X focusing on string handling and constructors in Java. It includes syntax questions, output questions, string function outputs, exception handling, and programming exercises related to string manipulation and constructors. The document also contains example programs demonstrating various concepts such as constructor overloading, copy constructors, and user input handling.

Uploaded by

20s41a6603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views17 pages

ICSE Class X Java String Handling Worksheet

This document is a worksheet for ICSE Class X focusing on string handling and constructors in Java. It includes syntax questions, output questions, string function outputs, exception handling, and programming exercises related to string manipulation and constructors. The document also contains example programs demonstrating various concepts such as constructor overloading, copy constructors, and user input handling.

Uploaded by

20s41a6603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ICSE Class X - String Handling Worksheet (Java Answers)

1. Syntax Answers:

a) [Link](chr);

b) [Link](str2);

c) [Link]([Link](1));

2. Output Questions:

a) "Computer".substring(1,5) -> ompu

b) "Computer Knowledge".substring(0,8).concat("Computer Applications".substring(9)) ->


ComputerApplications

"Computer Knowledge".endsWith("e") -> true

c) x[1] -> NOKIA

3. String Function Outputs:

"ACHIEVEMENT".replace('E','A') -> ACHIAVAMANT

"DEDICATE".compareTo("DEVOTE") -> -18

4. Array/String Outputs:

false

JAI

5. Exception and Blocks:

Exception: runtime error disrupting program flow.

Blocks: try, catch, finally.

6. Difference:

char[].length vs [Link]()

7. Library Functions Return Types:

charAt -> char

substring -> String

length() -> int

indexOf -> int

equals -> boolean


8. Trim:

[Link]();

9. Return Types:

[Link] -> boolean

[Link] -> String

10. startsWith vs endsWith:

Prefix vs Suffix check.

Java Programs Included:

- Longest Word Finder

- Replace Vowels with Next Letter

- Letter Frequency Counter

- Double Letter Sequence Counter

- Special Word / Palindrome Checks

- Word Triangle / Inverted Triangle

- Bubble Sort for 20 Names


Nalanda Public School
Nanal Nagar.
Class-X Sub: Computer Application Teacher:Bushra Sultana
Worksheet- STRINGS

[Link] the syntax to


a) To check whether a character(chr) is in upper case or not.
b) To compare two Strings(str1, str2) are same or not.
c) To check if the second character of a String(str) is in upper case.
2. Give the output:
a) If:
String x = "Computer";
String y = "Applications";
What do the following functions return?
(i) [Link]([Link](1,5));
b) Give the output of the following:
String n = "Computer Knowledge";
String m = "Computer Applications";
[Link]([Link](0,8).concat([Link](9)));
[Link]([Link]("e"));
c) Give the output of the following statements:
String x[] = {"SAMSUNG", "NOKIA", "SONY", "MICROMAX", "BLACKBERRY"};
[Link](x[1]);
3. Give the output of the following string functions:
(i) "ACHIEVEMENT".replace('E', 'A')
(ii) (ii) "DEDICATE".compareTo("DEVOTE")

4. Consider the following String array and give the output


String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};
[Link](arr[0].length() > arr[3].length());
[Link](arr[4].substring(0,3));

5. What is exception? Name two exception handling blocks.


6. Write a statement to show how finding the length of a character array chr[] differs from
finding the length of String object str..
7. What is the data type that the following library functions return?
8. Name the function which removes the spaces provided in the prefix and suffix of a string.
9. Write the return type of the following library functions.
(i) isLetterOrDigit(char) (ii) replace (char,char)
10 How does endsWith() and startsWith() differ? Explain with an example.

PROGRAMS

[Link] a program in Java to enter a String/Sentence and display the longest word and the
length of the longest word present in the String.
Sample Input: “TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN
BAGAN”
Sample Output: The longest word: FOOTBALL: The length of the word: 8
2. Write a program to accept a word and convert it into lower case, if it is in upper case.
Display the new word by
replacing only the vowels with the letter following it.
Sample Input: computer
Sample Output: cpmpvtfr
3. Write a program to input a sentence. Count and display the frequency of each letter of the
sentence in
alphabetical order.
Sample Input: COMPUTER APPLICATIONS
Sample Output: CHECK TABLE IN TEXTBOOK
4. Write a program to accept a string. Convert the string into upper case letters. Count and
output the number of
double letter sequences that exist in the string.
Sample Input: "SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE"
Sample Output: 4
5. Special words are those words which start and end with the same letter.
Example: EXISTENCE, COMIC, WINDOW
Palindrome words are those words which read the same from left to right and vice-
versa.
Example: MALYALAM, MADAM, LEVEL, ROTATOR, CIVIC
All palindromes are special words but all special words are not palindromes
6. Write a program to generate a triangle or an inverted triangle based upon User‟s choice.
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter a word : BLUEJ
Sample Output:
B
LL
UUU
EEEE
JJJJJ
Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter a word : BLUEJ
Sample Output:
BLUEJ
BLUE
BLU
BL
B
7. Write a program to input twenty names in an array. Arrange these names in ascending
order of letters, using the
bubble sort technique.
Sample Input:
Rohit, Devesh, Indrani, Shivangi, Himanshu, Rishi, Piyush, Deepak, Abhishek, Kunal, .....
Sample Output:
Abhishek, Deepak, Devesh, Himanshu, Indrani, Kunal, Piyush, Rishi, Rohit, Shivangi, .....
Constructors-ch.7
1. Which unit of the class gets called when the object of the class is
created?

2. Explain the concept of constructor overloading with an example.

3. What is parameterised constructor?

4. What is a constructor? When is it invoked?

5. Explain the features of a constructor.

6. Create a class with one integer instance. Initialize the variable using:
(i) default constructor (ii) parameterised constructor.

7. Give differences between a constructor and a method.

8. Name types of constructors.

9. Explain about copy constructor.

10. Name the keyword that:


(i) Is used for allocating memory to an array.
(ii) Causes the control to transfer back to the method call.

1
Programs:constructors
1. Define a class named MovieMagic with the following description:
Instance variables/data members:
int year : to store the year of release of the movie
String title : to store the title of the movie
float rating : to store the popularity rating of the movie
(minimum rating = 0.0 and maximum rating = 5.0)
Member method:
(i) MovieMagic () : default constructor to initialize numeric data
members to 0 an string data members to “ “
(ii) void accept () : to input and store year, title and rating.
(iii) void display () : to display the title of the movie and a message
based on the rating as per the tale given below.
Rating Message to
be displayed
0.0 to 2.0 Flop
2.1 to 3.4 Semi-Hit
3.5 to 4.5 Hit
4.6 to 5.0 Super Hit
Write the main method to create an object of the class and call the
above member methods.
2. Define a class called BookFair with the following description:
Instance variables/data members:
String Bname : stores the name of the book
double price : stores the price of the book
Member Method :
(i) BookFair() :constructor to initialize data members
(ii) void input() : to input and store the name and price of the book

2
(iii) void calculate(): to calculate the price after discount. Discount is
calculated based on the following criteria:
price Discount
Less than or equal to Rs. 1000 2%of price
More than Rs.1000 and less 10% of price
than or equal to Rs.3000
More than Rs. 3000 15% of price
(iv) void display () : to display the name and price of the book after
discount. Write a main method to create an object of the class and call
the above member methods.
3. design a class to overload a function polygon area() as follows:
(i) double area(double a, double b, double c) with three double
arguments, returns the area of a scalene triangle using the formula:
area= √s(s − a)(s − b)(s − c)
Where s= a+b+c/2
(ii) double area(int a,int b,int height) with three integer arguments,
returns the area of a trapezium using the formula:
area= ½ height(a+b)
(iii) double area(double diagonal1,double diagonal2) with two double
arguments,returns the area of a rhombus using the formula:
area= ½ (diagonal1*diagonal2)

3
Answer Key – Constructors (Java)

Q1. Book Class – Parameterized Constructor

class Book {
String title;

String author;

double price;

Book(String t, String a, double p) {


title = t;

author = a;

price = p;
}

void display() {
[Link]("Title: " + title);

[Link]("Author: " + author);

[Link]("Price: " + price);


}

public static void main(String[] args) {


Book b1 = new Book("Java Basics", "Herbert Schildt", 499.50);

[Link]();
}
}

Explanation: Uses a parameterized constructor to initialize object data members. Each value is assigned
through the constructor.

Sample Output: Title: Java Basics Author: Herbert Schildt Price: 499.5

Q2. Rectangle Class – Constructor Overloading

class Rectangle {
int length;

int breadth;

Rectangle(int side) {
length = breadth = side;
}
Rectangle(int l, int b) {
length = l;

breadth = b;
}

int area() {
return length * breadth;
}

public static void main(String[] args) {


Rectangle sq = new Rectangle(5);

Rectangle rect = new Rectangle(4, 6);

[Link]("Square Area: " + [Link]());

[Link]("Rectangle Area: " + [Link]());


}
}

Explanation: Shows constructor overloading by changing parameter lists.

Sample Output: Square Area: 25 Rectangle Area: 24

Q3. Student Class – Copy Constructor

class Student {
String name;

int marks;

Student(String n, int m) {
name = n;

marks = m;
}

Student(Student s) {
name = [Link];

marks = [Link];
}

void show() {
[Link](name + " - " + marks);
}

public static void main(String[] args) {


Student s1 = new Student("Ravi", 85);

Student s2 = new Student(s1);


[Link]();

[Link]();
}
}

Explanation: Demonstrates how a copy constructor duplicates the state of another object.

Sample Output: Ravi - 85 Ravi - 85

Q4. Default Constructor Message

class Welcome {
Welcome() {
[Link]("Welcome to Constructors!");
}

public static void main(String[] args) {


Welcome obj = new Welcome();
}
}

Explanation: Shows how a default constructor runs automatically when an object is created.

Sample Output: Welcome to Constructors!

Q5. GCD Calculation Constructor

class GCD {
int a;

int b;

GCD(int x, int y) {
a = x;

b = y;
}

int findGCD() {
while(a != b) {
if(a > b)
a = a - b;
else
b = b - a;
}
return a;
}

public static void main(String[] args) {


GCD g = new GCD(12, 8);

[Link]("GCD: " + [Link]());


}
}

Explanation: Uses a parameterized constructor and method to calculate the GCD.

Sample Output: GCD: 4

Q6. Inheritance Constructor Order

class Person {
Person() {
[Link]("Person Constructor Called");
}
}

class Employee extends Person {


Employee() {
[Link]("Employee Constructor Called");
}

public static void main(String[] args) {


Employee e = new Employee();
}
}

Explanation: Demonstrates constructor calling order in inheritance — superclass constructor runs first.

Sample Output: Person Constructor Called Employee Constructor Called

Q7. Box Volume using this Keyword

class Box {
double length;

double breadth;

double height;

Box(double length, double breadth, double height) {


[Link] = length;

[Link] = breadth;

[Link] = height;
}

double volume() {
return length * breadth * height;
}

public static void main(String[] args) {


Box b = new Box(2.5, 3.0, 4.0);

[Link]("Volume: " + [Link]());


}
}

Explanation: Shows use of 'this' keyword to refer to instance variables.

Sample Output: Volume: 30.0

Q8. Prime Check Constructor

class PrimeCheck {
int num;

PrimeCheck(int n) {
num = n;

boolean flag = true;

for(int i = 2; i <= num / 2; i++) {


if(num % i == 0) {
flag = false;

break;
}
}

if(flag)
[Link](num + " is Prime");
else
[Link](num + " is not Prime");
}

public static void main(String[] args) {


PrimeCheck p = new PrimeCheck(17);
}
}

Explanation: Checks if a number is prime using a constructor-based logic.

Sample Output: 17 is Prime

Q9. Scanner Input in Constructor

import [Link].*;
class StudentInput {
String name;

int marks;

StudentInput() {
Scanner sc = new Scanner([Link]);

[Link]("Enter name: ");

name = [Link]();

[Link]("Enter marks: ");

marks = [Link]();
}

void display() {
[Link](name + " scored " + marks + " marks.");
}

public static void main(String[] args) {


StudentInput s = new StudentInput();

[Link]();
}
}

Explanation: Shows how constructor can take user input directly when object is created.

Sample Output: Enter name: Ravi Enter marks: 90 Ravi scored 90 marks.

Q10. BankAccount Constructor

class BankAccount {
String name;

int accNo;

double balance;

BankAccount(String n, int a, double b) {


name = n;

accNo = a;

balance = b;
}

void display() {
[Link](name + " | " + accNo + " | " + balance);
}
public static void main(String[] args) {
BankAccount c1 = new BankAccount("Ravi", 1001, 25000);

BankAccount c2 = new BankAccount("Priya", 1002, 30000);

[Link]();

[Link]();
}
}

Explanation: Initializes customer details using parameterized constructor and prints them using a
method.

Sample Output: Ravi | 1001 | 25000.0 Priya | 1002 | 30000.0


Nalanda Public School – Class X (ICSE Board)

Worksheet – Descriptive Program Questions (Constructors in


Java)

Q1. Write a Java program to create a class Book with data members title, author, and price. Use
a parameterized constructor to initialize these values and a method display() to print them.
Explain how the constructor initializes the object.

Q2. Define a class Rectangle to calculate the area. Use constructor overloading — one
constructor to calculate the area of a square, and another to calculate the area of a rectangle.
Explain how Java distinguishes between the two constructors.

Q3. Create a class Student with members name and marks. Define one parameterized
constructor and another copy constructor to duplicate the data of an existing object.
Demonstrate the concept using two Student objects.

Q4. Write a program using a default constructor that displays a welcome message when an
object is created. Explain why a constructor is called automatically and how it differs from a
normal method call.

Q5. Write a program using a parameterized constructor that accepts two numbers and
calculates their GCD (Greatest Common Divisor). Display the result using a method show().

Q6. Create two classes Person and Employee where Employee inherits from Person. Use
constructors in both classes to initialize data and demonstrate the order of constructor invocation
in inheritance.

Q7. Write a program Box with data members length, breadth, and height. Use the this keyword
inside the constructor to differentiate between instance variables and parameters. Calculate and
display the volume of the box.

Q8. Write a Java program that uses a constructor to check whether a number passed as an
argument is prime. Display appropriate messages.

Q9. Write a program using Scanner class where the constructor reads the student’s name and
marks, then displays them. Explain how taking input inside a constructor differs from input inside
a normal method.
Q10. Create a class BankAccount with instance variables name, accNo, and balance. Use a
parameterized constructor to initialize details of each customer. Create multiple objects and
display all account details using a loop.

You might also like