Wednesday, 19 June 2024

Descriptive Questions - ICSE

Question 1:

Define a class called LMS with the following description:

Instance Variables/Data Members:
int acc_num — stores the accession number of the book.
String title — stores the title of the book.
String author — stores the name of the author.

Member methods:

1.     void input() — To input and store the accession number, title and author.

2.     void compute() — To accept the number of days late, calculate and display the fine charged at the rate of Rs. 2 per day.

3.     void display() — To display the details in the following format:

Fine is Rs

Accession Number    Title   Author


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


Solution:

import java.util.Scanner;

class LMS

{

    int acc_num;

    String title, author;

    void input()

    {

        Scanner Sr = new Scanner(System.in);

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

        acc_num = Sr.nextInt();

        System.out.print("Enter title: ");

        title = Sr.nextLine();

        System.out.print("Enter author: ");

        author = Sr.nextLine();

    }

    void compute()

    {

        Scanner Sr = new Scanner(System.in);

        System.out.print("Enter number of days late: ");

        int daysLate = Sr.nextInt();

        int fine = 2 * daysLate;

        System.out.println("Fine is Rs " + fine);

    }

    void display()

    {

        System.out.println("Accession Number\tTitle\tAuthor");

        System.out.println(acc_num + "\t\t " + title + "\t" + author);

    }

    public static void main(String[] args)

    {

        LMS libms = new LMS();

        libms.input();

        libms.compute();

        libms.display();

    }

}

VARIABLE DESCRIPTION TABLE

Name of Variable

Data Type

Purpose/Description

args

String[]

To accept command line arguments

acc_num

int

To store the accession number of the book

title

String

To store the title of the book

author

String

To store the author of the book

daysLate

int

To store the number of days late

fine

int

To store the fine amount


Question 2:

Define a class with the following specifications:
Class name: Bank
Member variables:
double p – stores the principal amount
double n – stores the time period in years
double r – stores the rate of interest
double a – stores the amount
Member methods:
void accept() – input values for p and n using Scanner class methods only.
void calculate() – calculate the amount based on the following conditions:

Time in (Years)Rate %
Up to 1/29
> 1/2 to 1 year10
> 1 to 3 years11
> 3 years12


void display() – displays the details in the given format:

Principal    Time    Rate    Amount
xxx          xxx     xxx     xxx

Write a main() method to create an object and call the above methods.


Solution:

import java.util.Scanner;
class bank
{
    double p,n,r,a;
    Scanner sc = new Scanner(System.in);
    void accept()
    {
       System.out.println("Enter Principal");
       p = sc.nextDouble();
       System.out.println("Enter number of years");
       n = sc.nextDouble();
    }
    void calculate()
    {
        if(n<=0.5)
        {
            r = 9;
        }
        else if(n>0.5 && n<=1)
        {
            r = 10;
        }
        else if(n>1 && n<=3)
        {
            r=11;
        }
        else
        {
            r=12;
        }
        a = p*(Math.pow(1+r/100,n));
    }
    void display()
    {
        System.out.println("Principal\tRate\tTime\tAmount");
        System.out.println(p+"\t\t"+r+"\t"+n+"\t"+a);
    }
    public static void main(String[]args)
    {
        bank bnk = new bank();
        bnk.accept();
        bnk.calculate();
        bnk.display();
    }
}

VARIABLE DESCRIPTION TABLE

Name of Variable

Data Type

Purpose/Description

args

String[]

To accept command line arguments

p

double

To store the Principal amount

n

double

To store the time period in years

r

double

To store the rate of interest

a

double

To store the amount



Monday, 17 June 2024

Operators - ICSE Very Important

Operators

There are several operators available in Java and some of the important operators in Java are as follows:

Arithmetic Operators:

These operators are used for performing mathematical calculations such as Addition, subtraction, multiplication, division and remainder.

Arithmetic Operators

Operation

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus


Increment/Decrement Operators

Increment operator is represented as ++ and Decrement operator is represented as --. These operators work in two forms: 

Prefix form: When used in Prefix form, it follows the rule Change-then-use. For example, ++x

Postfix form: When used in Postfix form, it follows the rule Use-then-Change. For example, x++

Example:

int S = 5,a = 10;

S = S + ++a;

 

Value of S will be 16.

int S = 5,a = 10;

S = S + a++;

 

Value of S will be 15.


Relational Operators

These operators are also called comparison operators. They compare the values of two variables. They are:

Relational Operators

Description

> 

Greater than

< 

Less than

==

Equal to

>=

Greater than or equal to

<=

Less than or equal to

!=

Not Equal to


Logical Operators

These operators are used to combine relational operators to make complex expressions for decision making.

Logical Operators

Description

&&

AND

||

OR

!

NOT


Conditional Operator: Java offers a shortcut conditional operator (?:) that stores a value depending on a condition. This condition is also known as Ternary Operator, i.e., it requires three operands:

expression1? expression2: expression3

int marks = 56;

String result = marks>35? “Pass”:”Fail”;

Here, Answer will be Pass because test expression marks>35. Here, value stored within marks namely variable is 56 and it is clear that 56>35 and that is the reason due to which, the identifier result will have value “Pass” or else, result will have value “Fail”.

Practice 1:

Evaluate expression when value of X = 5

X+=X++ + ++X + --X +X

Solution:

X = 5+(5+7+6+6)

X = 5+(24)

X = 5+24

X = 29


Practice 2:

Evaluate expression when value of a = 7

a+=a++ + ++a + --a + a--

Solution:

a = 7+(7+9+8+8)

a = 7+(32)

a = 7+32

a = 39


Practice 3:

Evaluate the value of n if value of p = 5, q = 19

int n = (q-p)>(p-q)?(q-p):(p-q)

Solution:

int n = (q-p)>(p-q)?(q-p):(p-q)

int n = (19-5)>(5-19)?(19-5):(5-19)

int n = 14>-14?14:-14

int n = 14


Practice 4:

Assume value of basic = 1500, what will be the value of tax after the following statement is executed?

tax = basic>1200? 200:100;

Solution: 200

Answer is 200 as test expression  is basic>1200. Here, value of basic is 1500 and it is clear that 1500>1200 due to which, the identifier tax will have value 200.

Practice 5:

Give the output of the following:

int x = 2, y = 4, z = 1;

int result = (++z)+y+(++x)+(z++);

Solution:

int result = (2)+4+(3)+(2)

int result = 2+4+3+2

int result = 6+5

int result = 11


Practice 6:

What is the value of x1 if x = 5?

x1 = ++x - x++ + --x

x1 = 6-6+6

x1 = 0+6

x1 = 6


Practice 7:

Evaluate the following if the value of x = 7, y = 5

x+ = x++ +x+ ++y

x = x+(x++ +x+ ++y)

x = 7 + (7+8+6)

x = 7+21

x = 28


Practice 8:

Give the output of the following:

int i = 10, m = 9;

String e = (m%i ==9)?"YES":"NO ";

System.out.println(e);

Answer is YES as test expression  is 9%10==9. Here, values of i and m are 10 and 9 respectively and it is clear that remainder value is coming to 9 due to which, the identifier e will have value YES.


Practice 9:

Rewrite the following using ternary operator:

if(bill>10000)

  discount = bill * 10.0/100;

else

   discount = bill * 5.0/100;

Solution:

discount = (bill>10000)?(bill*10.0/100): (bill*5.0/100)


Practice 10:


Rewrite the following using ternary operator:

if(n1>n2)
   r = true;
else
   r = false;

Solution:
r = (n1>n2)?true;false;

Practice 11:


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

y+ = ++y +y-- + --y, when int y = 8

Solution:

y = y+(++y + y-- +--y)
y = 8+(9+9+7)
y = 8 + (25)
y = 8+25
y = 33

Practice 12:


If int y = 10, then find
int z = (++y*(y++ +5))

Solution:

int z = (++y*(y++ +5))
           (11*(11+5))
           (11*16)
           176

Practice 13:


char ch = 'F';
int m = ch;
m = m+5;
System.out.println(m+ "" +ch);

Solution:

char ch = 'F' [ASCII code of F is 70]
int m = 70;
m = 70+5;
m = 75

Output will be:
75 F

Practice 14:


What is the output of the following program snippet?
char x = 'A';
int m;
m = (x=='a')?'A':'a';
System.out.println("m= "+m);

Solution:

m = 97

Practice 15:


What is the output of the following method?

public static void main(String[]args)
{
    int a = 5;
    a++;;
    System.out.println(a);
    a-=(a--)-(--a);
    System.out.println(a);
}

Solution:

First statement will produce 6
Second statement calculation is as follows:
a = a-(a--)-(--a)
a = 6-(6)-(4)
a = 6-6-4
a = 6-2
a = 4

Final output shall be

6
4

Practice 16:


Write a java expression for:







Solution:

u*t+0.5*f*Math.pow(t,2)

Practice 17:


Evaluate the expression:

z=++x*(y--)-y
where x = 20, y = 10

Solution:

z = 21*(10)-9
z = 21*10-9
z = 210-9
z = 201

Practice 18:


What will be the value of x after executing the following statement?

x=(x<y)?(y+x):(y-x);
If int x = 15, y = 20;

Solution:

x = (15<20)?35:5
As condition is correct, hence 2nd expression will be executed i.e. 35

Practice 19:


Write java expression for the following:

|x2+2xy|

Solution:

Math.abs(x*x+2*x*y)


Practice 20:


Give the output of the following code:

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

Solution:

Result 1 = 26100200
Result 2 = 126

Practice 21:


Rewrite the following code segment using if-else statements instead of the ternary operator.

String grade = (mark>=90)? "A":(mark>=80)? "B":"C";

Solution:

if(mark>=90)
{
    grade = "A";
}
else if(mark>=80)
{
    grade = "B";
}
else
{
    grade = "C";
}

Practice 22:


Evaluate the following expression. Given a=2,b=3,c=9.

a) a-(b++)*(--c)

Solution:

=2-(3)*(8)
=2-3*8
=2-24
=-22

b) a*(++b)%c
=2*(4)%9
=2*4%9
=8%9
=8

Practice 23:


Give value of x for the given expression:

a) int x = 50;
    x%=4

Solution:

x = x%4
x = 50%4
x = 2

b) int a = 10;b=20;
    boolean x;
    x=a>5 && b>5;

Solution:

x = 10>5 && 20>5;
As it is seen that both the expressions are correct and hence, answer will come to true.

c) int a =5, b = 10;
    boolean x;
    x = a>10!!(b+5>=15 && b-5<7)

Solution:

The output will be true.