Computer App With BlueJ-cl9
Computer App With BlueJ-cl9
Class IX
Contents
Chapter 1
Introduction to Object Oriented
Programming Concepts
1
6. Procedure Oriented Programming
Ans. Procedure Oriented Programming basically consists of a list of instructions
for the computer to follow and these are organised into groups known
as functions. In Procedure Oriented Programming, most of the functions
share global data and this data moves more openly around the system
from one function to the other.
7. Inheritance
Ans. The term 'Inheritance' means to link and share some common properties
of one class with the other class. This can be done by extending a class
into another class and thus using both of them.
8. Assembly Language
Ans. In assembly language, the instructions are written more in English like
words knowns as mnemonics. These instructions are not understood
by the processor directly. They are converted into equivalent machine
code through a translator program called Assembler. Assembly language
is machine dependent that makes it unsuitable for writing portable
programs that can execute across machines.
V. Distinguish between:
1. Object Oriented Programming and Procedure Oriented Programming
Ans. Object Oriented Programming Procedure Oriented
Programming
The stress is put on data rather than The stress is put on function rather
functions. than data.
The data is restricted and used in a It allows data to flow freely
specific program area. throughout the program.
It follows bottom-top programming It follows top-down programming
approach. approach.
9
(d) class Book
Ans. Characteristics Methods
Name Input( )
ISBN Number Read( )
Price
Author
Publisher
(e) class Park
Ans. Characteristics Methods
Name OpenPark( )
Address ClosePark( )
ENtry
Opening Time
Closing Time
(f) class Medicine
Ans. Characteristics Methods
Name Purchase( )
Quantity Sell( )
Price
Manufacturing Date
Expiry Date
(g) class Computer
Ans. Characteristics Methods
Company Input( )
Model Display( )
Processor
RAM
Hard Disk
(h) class Camera
Ans. Characteristics Methods
Company Input( )
Model Display( )
Serial Number
Price
Resolution
3. What is an Object? Give five examples of real world objects.
Ans. An object is an entity having some specific characteristics and specific
behaviour.
For example, car, mobile phone, computer, student, camera, etc.
4. How will you define a software object?
Ans. A software object replaces the characteristics and behaviours of a real
world object with data members and member methods respectively.
5. Class and objects are inter-related. Explain.
Ans. A class is used to create various objects having different characteristics
and common behaviours. Further, each object follows all the features
defined within a class. This is why class is also referred to as a blue print or
prototype of an object. In this way, we can say that they are inter-related.
6. A class is also referred to as 'Object Factory'. Comment.
Ans. A class can create objects with different characteristics and common
behaviour just like a factory where it produces similar items (objects)
based on a particular design (class). Hence, class is also referred to as
'Object Factory'.
7. What does the following statement mean?
Employee staff = new Employee ( );
Ans. This statement means that the staff is an object created of class Employee.
Further, this object of the class 'Employee' will possess different
characteristics but will have common behaviour.
8. Why is an object called an 'Instance' of a class? Explain.
Ans. The data members declared within a class are also known as instance
variables.
When an object of a class is created, it includes instance variables
described within the class. This is the reason why an object is called as
an instance of a class.
9. Why is a class known as composite data type?
Ans. When the user creates a class, it becomes a data type for his/her program.
Thus, class is referred to as a user defined data type. This data type
includes different primitive data types such as int, float, char, etc. Hence,
it is also said to be composite data type.
10. Write a statement to create an object 'Keyboard' of the class 'Computer'.
Ans. Computer Keyboard = new Computer( );
11. Consider a real world object as 'Cricket Ball'. Now, mention two behaviour
and methods each by taking the 'Cricket Ball' as a software Object.
Ans. Behaviour : colour, weight
Methods : Throw( ), Display( )
12. Refer the class structure as shown below:
class MySchool
{
Name
Address
Principal's name
14. Write a program by using a class 'Picnic' without any data members but
having only functions as per the specifications given below:
class name : Picnic
void display1( ) : to print venue, place and reporting time
void display2( ) : to print number of students, name of the teacher
accompanying and bus number
Write a main class to create an object of the class 'Picnic' and call the
functions display1( ) and display2( ) to enable the task.
Ans. class Picnic
{
public void display1( )
{
System.out.println("Venue: Dimna Lake");
System.out.println("Place: Jamshedpur");
System.out.println("Reporting Time: 9:00 am");
}
public void display2( )
{
System.out.println("Number of students: 50");
System.out.println("Name of the teacher: Mr. Abhinandan Kishore");
System.out.println("Bus Number: JH 05 DH 2445");
}
public static void main(String args[ ])
{
Picnic ob = new Picnic( );
ob.display1( );
ob.display2( );
}
}
14
7. Explain the term 'type casting'?
Ans. The process of converting one pre-defined data type into another after
the user's intervention, is called type casting.
8. Perform the following:
Ans. (a) Assign the value of pie (3.142) to a variable with the requisite data
type.
double pi = 3.142;
(b) Assign the value of 3 (1.732) to a variable with the requisite data type.
double x = 1.732;
9. Distinguish between:
Ans. (a) Integer and floating constant
Integer Constant Floating Constant
Integer constants represent whole Floating constants represent
number values like 2, –16, 18246, fractional numbers like 3.14159,
24041973, etc. –14.08, 42.0, 675.238, etc.
Integer constants are assigned Floating constants are assigned
to variables of data type such as to variables of data type such as
byte, short, int, long, char. float, double.
(b) Token and Identifier
Token Identifier
A token is the smallest element of Identifiers are used to name
a program that is meaningful to things like classes, objects,
the compiler. variables, arrays, etc.
Tokens in Java are categorised Identifier is a type of token in
into various types such as Java.
Keywords, Identifiers, Literals,
Operators, etc.
(c) Character and String constant
Character Constant String Constant
These constants are written by They are written by enclosing a
enclosing a character within a set of characters within a pair of
pair of single quotes. double quotes.
Character constants are assigned String constants are assigned to
to variables of type char. variables of type String.
(d) Character and Boolean literal
Character Literal Boolean Literal
Character literals are written by A boolean literal can take only
enclosing a character within a one of the two boolean values
pair of single quotes. represented by the words true or
false.
Character literals can be assigned Boolean literals can only be
to variables of any numeric data assigned to variables declared as
type such as byte, short, int, boolean.
double, char, etc.
Operators in Java
19
4. What is a Ternary operator? Explain with the help of an example.
Ans. Ternary operators deal with three operands and evaluates the condition.
Syntax: variable = (test expression)? Expression 1: Expression 2;
If the condition is true then the result of ternary operator is the value of
expression 1. Otherwise, the result is the value of expression 2.
For example,
int a = 5; int b = 3;
int Max = (a>b)? a : b;
Here, the value 5 is stored in Max, as a>b is true.
5. Differentiate between the following:
Ans. (a) Arithmetical operator and Logical operator
Arithmetical Operator Logical Operator
Arithmetic operators are used They operate on boolean
to perform mathematical expressions to combine the results
operations. into a single boolean value.
It results in int, float, double data It results in true or false.
types.
(b) Binary operator and Ternary operator
Binary operator Ternary operator
Binary operators work on two Ternary operators work on three
operands. operands.
It is an arithmetical operator. It is a conditional operator.
Operators in Java 21
(b) a * (++b) % c;
Ans. a * (++b) % c
⇒ a * (++b) % c
⇒ 2 * (4) % 9
⇒8%9
⇒8
12. If a = 5, b = 9, calculate the value of:
a += a++ – ++b + a;
Ans. a += a++ – ++b + a
⇒ a = a + (a++ – ++b + a)
⇒ a = 5 + (5 – 10 + 6)
⇒a=5+1
⇒a=6
13. Give the output of the program snippet.
int a = 10, b =12;
if(a>=10)
a++;
else
++b;
System.out.println(" a = " + a + " and b = " +b);
Output: a = 11 and b = 12
14. Rewrite the following using ternary operator.
(a) if(income<=100000)
tax = 0;
else
tax = (0.1*income);
Ans. tax = income <= 100000 ? 0 : (0.1*income);
(b) if(p>5000)
d = p*5/100;
else
d = p*2/100;
Ans. d = p > 5000 ? p * 5 / 100 : p * 2 / 100;
IV. Unsolved Java Programs
1. Write a program to find and display the value of the given expressions:
(a) (x + 3) / 6 – (2x + 5) / 3; taking the value of x = 5
Prog. public class Expression
{
public static void main(String args[ ])
{
int x = 5;
double value = ((x + 3) / 6.0) – ((2 * x + 5) / 3.0);
System.out.println("Result = " + value);
}
}
(b) a2 + b2 + c2 / abc; taking the values a=5, b=4,c=3
Prog. public class Expression
{
public static void main(String args[ ])
{
int a = 5, b = 4, c = 3;
double value = (a * a + b * b + c * c) / (double)(a * b * c);
System.out.println("Result = " + value);
}
}
2. A person is paid ` 350 for each day he works and fined ` 30 for each day
he remains absent. Write a program to calculate and display his monthly
income, if he is present for 25 days and remains absent for 5 days.
Prog. public class Salary
{
public static void main(String args[ ])
{
int salary = 25 * 350;
int fine = 5 * 30;
int netSalary = salary – fine;
System.out.println("Monthly Income = " + netSalary);
}
}
3. In a competitive examination, there were 150 questions. One candidate
got 80% correct and the other candidate 72% correct. Write a program
to calculate and display the correct answers each candidate got.
Prog. public class Exam
{
public static void main(String args[ ])
{
int tques = 150;
int c1 = (int)(80 / 100.0 * tques);
int c2 = (int)(72 / 100.0 * tques);
System.out.println("Correct Answers of Candidate 1 = " + c1);
System.out.println("Correct Answers of Candidate 2 = " + c2);
}
}
4. Write a program to find and display the percentage difference, when:
(a) a number is updated from 80 to 90
public class Increase
{
public static void main(String args[ ])
{
int num = 80;
int newnum = 90;
int inc = newnum – num;
double p = inc / (double)num * 100;
Operators in Java 23
System.out.println("Percentage Increasee = " + p + "%");
}
}
(b) a number is updated from 7.5 to 7.2
Prog. public class Decrease
{
public static void main(String args[ ])
{
double num = 7.5;
double newnum = 7.2;
double inc = num – newnum;
double p = inc / num * 100;
System.out.println("Percentage Decrease = " + p + "%");
}
}
5. The normal temperature of human body is 98.6°F. Write a program to
convert the temperature into degree Celsius and display the output.
[Hint: c / 5 = f – 32 / 9]
Prog. public class Temp
{
public static void main(String args[ ])
{
double f = 98.6;
double c = 5 * (f – 32) / 9.0;
System.out.println("Temperature in Degree Celsius = " + c);
}
}
6. The angles of a quadrilateral are in the ratio 3:4:5:6. Write a program to
find and display all of its angles.
[Hint: The sum of angles of a quadrilateral = 360°]
Prog. public class Display
{
public static void main(String args[ ])
{
int r1 = 3, r2 = 4, r3 = 5, r4 = 6, total;
double a, b, c, d;
total = r1 + r2 + r3 + r4;
a = (double)r1/total * 360;
b = (double)r2 /total * 360;
c = (double)r3 /total * 360;
d = (double)r4 /total * 360;
System.out.println("Angle A = " + a);
System.out.println("Angle B = " + b);
System.out.println("Angle C = " + c);
System.out.println("Angle D = " + d);
}
}
Chapter 5
Input in Java
25
3. Testing and Debugging
Ans. Testing Debugging
In this process, we check the validity In this process, we correct the errors
of a program. that were found during testing.
Testing is complete when all Debugging is finished when there
the desired verifications against are no errors and the program is
specifications have been performed. ready for execution.
Input in Java 27
System.out.print("Enter length: ");
l = in.nextDouble( );
System.out.print("Enter the value g: ");
g = in.nextDouble( );
t = 2 * (22.0/7.0) * Math.sqrt(l/g);
System.out.println("Time Peroid = " + t);
}
}
2. Write a program by using class 'Employee' to accept basic pay of an
employee. Calculate the allowances/deductions as given below.
Finally, find and display the gross and net pay.
Input in Java 31
Prog. import java.util.*;
public class Difference
{
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
int p;
double si, amt, ci;
System.out.print("Enter principal : ");
p = in.nextInt( );
si = p * 10 * 3/100;
amt = p * Math.pow(1+(10/100.0), 3);
ci = amt – p;
System.out.print("Difference between CI and SI: " + (ci – si));
}
}
10. A shopkeeper sells two calculators for the same price. He earns 20%
profit on one and suffers a loss of 20% on the other. Write a program
to find and display his total cost price of the calculators by taking their
selling price as input.
Hint: CP = (SP/(1 + (profit/100))) (when profit)
CP = (SP/(1 – (loss/100))) (when loss)
Prog. import java.util.*;
public class Calculate
{
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
int sp;
double cp1, cp2, totalcp;
System.out.print("Enter the selling price: ");
sp = in.nextInt( );
cp1 = (sp/(1 + (20/100.0)));
cp2 = (sp/(1 – (20/100.0)));
totalcp = cp1 + cp2;
System.out.println("Cost Price of first article = " + cp1);
System.out.println("Cost Price of second article = " + cp2);
System.out.println("Total Cost Price = " + totalcp);
}
}
Chapter 6
33
5. Math.log( )
Ans. Returns the natural logarithm value of its argument. Both return type
and argument are of double data type.
VI. Distinguish between them with suitable examples:
1. Math.ceil( ) and Math.floor( )
Ans. Math.ceil( ) Math.floor( )
This function is used to return This function is used to return
the higher integer for the given the lower integer for the given
argument. argument.
double a = Math.ceil(65.5); double b = Math.floor(65.5);
It will result in 66.0. It will result in 65.0.
2. Math.rint( ) and Math.round( )
Ans. Math.rint( ) Math.round( )
Rounds off its argument to the Rounds off its argument to the
nearest mathematical integer and nearest mathematical integer and
returns its value as a double type. returns its value as a long type.
At mid-point, it returns the integer At mid-point, it returns the higher
that is even. integer.
double a = Math.rint(1.5); long a = Math.round(1.5);
double b =Math.rint(2.5); long b = Math.round(2.5);
Both, a and b will have a value of 2.0 a will have a value of 2 and b will
have a value of 3
37
(b) if - else
if - else statement is used to execute one set of statements when the
condition is true and another set of statements when the condition is
false.
Syntax:
if (condition 1)
{
Statement 1;
Statement 2;
………………
}
else
{
Statement 3;
Statement 4;
………………
}
(c) if - else - if
if - else - if ladder construct is used to test multiple conditions and then
take a decision. It provides multiple branching of control.
Syntax:
if (condition)
statement;
else if (condition)
statement;
else if (condition)
statement;
……………….
……………....
else
statement;
4. Differentiate between if and switch statement.
Ans. if switch
if can test for any Boolean switch can only test for equality.
expression.
if doesn't have such limitations. tests the same expression against
constant values.
5. What is the purpose of the switch statement in a program?
Ans. Switch statement is used for multi-way branching. It compares its
expression with multiple case values for equality and executes the case
whose value is equal to the expression of switch. If none of the cases
match, default case is executed.
6. Explain with the help of an example, the purpose of default in a switch
statement.
Ans. When none of the case values are equal to the expression of switch
statement then default case is executed.
For example,
int num = 3;
switch(num)
{
case 1:
System.out.println("Value of number is one");
break;
case 2:
System.out.println("Value of number is two");
break;
default:
System.out.println("Incorrect Input!");
}
7. Is it necessary to use 'break' statement in a switch case statement?
Explain.
Ans. Use of break statement in a switch case statement is optional. The
omission of break statement will lead to fall through where program
execution continues into the next case and onwards till end of switch
statement is reached.
8. Explain 'Fall through' with reference to a switch case statement.
Ans. The use of break statement at the end of each case is an optional. The
omission of break statement will continue the execution into the next
case and onwards till a break statement is encountered or end of switch
is reached. This is termed as 'Fall Through' in a switch case statement.
9. What is a compound statement? Give an example.
Ans. When two or more statements can be grouped together by enclosing
them between opening and closing curly braces, it is called a compound
statement.
For example,
a= 10; b=20;
if (a < b)
{
d = a – b;
System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);
System.out.println("The difference is " + d);
}
10. Explain with an example the if-else-if construct.
Ans. The if - else - if ladder construct is used to test multiple conditions and
then take a decision. It provides multiple branching of control.
For example,
if (marks < 35)
System.out.println("No Grade");
else if (marks < 60)
System.out.println("Grade: C");
else if (marks < 80)
System.out.println("Grade: B");
Distance Rate
Up to 5 km ` 100
For the next 10 km ` 10/km
For the next 10 km ` 8/km
More than 25 km ` 5/km
Write a program to input the distance covered and calculate the amount
to be paid by the passenger. The program displays the printed bill with
the details given below:
Taxi No. : …………………………
Distance covered : …………………………
Amount : …………………………
Prog. import java.util.*;
public class PrePaidTaxi
{
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
int dist, fare;
String tn;
System.out.print("Enter Taxi Number: ");
tn = in.nextLine( );
System.out.print("Enter distance travelled: ");
dist = in.nextInt( );
fare = 0;
if (dist <= 5)
fare = 100;
else if (dist <= 15)
fare = 100 + (dist – 5) * 10;
else if (dist <= 25)
fare = 100 + 100 + (dist – 15) * 8;
else
fare = 100 + 100 + 80 + (dist – 25) * 5;
Write a program to input the name, age and taxable income of a person. If
the age is more than 60 years then display the message "Wrong Category".
If the age is less than or equal to 60 years then compute and display the
income tax payable along with the name of tax payer, as per the table
given above.
Prog. import java.util.*;
public class IncomeTax
{
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
int age;
double ti, tax=0.0;
String name;
System.out.print("Enter Name: ");
name = in.nextLine( );
System.out.print("Enter taxable income: ");
ti = in.nextDouble( );
System.out.print("Enter age: ");
age = in.nextInt( );
if (age > 60)
{
System.out.print("Wrong Category");
System.exit(0);
}
else
{
if (ti <= 250000)
tax = 0;
else if (ti <= 500000)
tax = (ti – 160000) * 10/100;
else if (ti <= 1000000)
tax = 34000 + ((ti – 500000) * 20/100);
61
(b) Exit controlled loop
Ans. An exit-controlled loop checks the condition after executing its body. If
the condition is true, the loop will perform the next iteration, otherwise
the program control will exit the loop.
For example, do-while loop
6. Write down the general format of:
(a) for loop
Ans. for (initial value; test condition; update)
{
//loop-body
}
(b) do - while loop
do
{
//loop-body
} while (condition);
(c) while loop
while (condition)
{
//loop-body
}
7. What is the purpose of using:
(a) break statement?
Ans. The 'break' statement is also used in looping construct for unnatural
termination of the loop. It can be used in all the loops.
(b) continue statement?
Ans. The continue statement is used to unconditionally jump to the next
iteration of the loop, skipping the remaining statements of the current
iteration.
8. What do you understand by inter-conversion of loops?
Ans. We can convert the repetitive statement using one type of loop into other
type of loops. Thus, the process of converting of some repetitive logic is
coded using a loop into other type of loop is termed as inter-conversion
of loops.
9. What are the different ways to inter-convert the loops? Name them.
Ans. The different ways to inter-convert the loops are:
• for loop to while loop
• for loop to do-while loop
• do-while loop to while loop
• do-while loop to for loop
• while loop to do-while loop
• while loop to for loop
10. Define the following:
(a) Finite loop
Ans. A loop which iterates for a finite number of iterations is termed as a finite
loop.
(b) Delay loop
Ans. A loop which is used to pause the execution of the program for some
amount of time, is termed as delay loop.
(c) Infinite loop
Ans. A loop which continues iterating indefinitely and never stops, is termed
as infinite loop.
(d) Null loop
Ans. A loop that does not include any statement to be repeated, is known as
null loop.
Basically, a null loop is used to create a delay in the execution.
11. Distinguish between:
(a) for and while loop
Ans. for loop while loop
It is suitable when the number of It is helpful in situations where
iterations are known. number of iterations are not known.
The loop will repeat infinite times, if If the condition is not defined then
the condition is not defined. an error will be shown.
(b) while and do-while loop
Ans. while loop do-while loop
It is an entry-controlled loop. It is an exit-controlled loop.
The loop will not execute at all, if This loop will execute at least once,
the condition is not satisfied. whether the condition is true or
false.
12. State one difference and one similarity between while and do-while loop.
Ans. Similarity: Both while and do-while loops are suitable in situations where
numbers of iterations is not known.
Difference: The while is an entry-controlled loop whereas do-while is an
exit-controlled loop.
13. State one similarity and one difference between while and for loop.
Ans. Similarity: Both for and while are entry-controlled loops.
Difference: The for loop is a suitable choice when the number of iterations
are known. Wheras, while loop is helpful in situations where number of
iterations is not known.
14. Distinguish between Step loop and Continuous loop.
Ans. In Continuous loop, loop control variable is incremented or decremented
by one in each iteration. Whereas in Step loop, the loop control variable
is incremented or decremented by more than one in each iteration.
III. Predict the output of the following program snippets:
1. class dkl
{
public static void main(String args[ ])
{
int i;
for(i = –1;i<10;i++)
92
3. How will you terminate outer loop from the block of the inner loop?
Ans. We can terminate outer loop from the block of the inner loop by using
Labelled break statement (break outer).
4. What do you mean by labelled break statement? Give an example.
Ans. Sometimes, it may happen that the outer loop is to be terminated, if a
condition is met in the inner loop. In this situation, the labelled break
statement will be used to serve the purpose.
For example,
int i, j;
outer:
for(i=1;i<=5;i++)
{
for(j=1;j<=3;j++)
{
System.out.println(i*j);
if (j ==2)
break outer; //Labelled break
}
}
5. Write down the syntax of the nested loop.
Ans. The syntax of the nested loop:
executable statement(s)
………………………………………………………………………………………………
}
}
1
12
1234
5. int i, j;
first:
for (i=10; i>=5; i– –)
{
for (j= 5; j<=i; j++)
{
if (i*j <40)
continue first;
System.out.print(j);
}
System.out.println( );
}
Output: 5678910
56789
5678
VI. Solutions to Unsolved Java Programs:
1. Write a program to display the Mathematical Tables from 5 to 10 for 10
iterations in the given format:
Sample Output: Table of 5
5*1 = 5
5*2 =10
5*10 = 50
Prog. public class Tables
{
public static void main(String args[ ])
{
for (int i = 5; i <= 10; i++)
{
System.out.println("Table of " + i);
for (int j = 1; j <= 10; j++)
System.out.println(i + "*" + j + " = " + (i*j));
}
}
}
2. Write a program to accept any 20 numbers and display only those
numbers which are prime.
[Hint: A number is said to be prime, if it is only divisible by 1 and the
number itself.]
Prog. import java.util.*;
public class Prime
{
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
int n, i, j, c;
System.out.println("Enter any 20 numbers");
for (i = 1; i <= 20; i++)
{
n = in.nextInt( );
c=0;
for (j = 1; j <= n; j++)
{
if (n % j == 0)
c++;
}
if (c==2)
System.out.println(n + " is a Prime Number");
}
}
}
110
common issues of computer ethics include intellectual property rights
(such as copyrighted electronic content), privacy concerns, and how the
computers affect society.
Five ethical values related to computers are as follows:
• Do not install or uninstall any software without prior permission.
• Never move the peripherals from one place to other unnecessarily.
• While working in a computer lab/room, you must maintain the rules
and regulations of the lab.
• Do not try to steal the secret information of any other computer.
• Do not copy or use proprietary software for which you have not paid.
2. How will you protect your data on the Internet?
Ans. The different ways to protect data on the internet are as follows:
• Do not disclose your passwords to your friends. They may open
different websites and misuse your accounts.
• Do not download unwanted software/free software from the web. It
may corrupt your system.
• Do not read unwanted e-mails, as they might be carrying viruses.
• Install high quality antivirus programs or signatory antivirus of a
reputed company in your computer to protect against viruses.
3. What is meant by malicious code?
Ans. Malicious code refers to a new kind of threat, which corrupts the system
and leaks personal data in due course of time. It is virtually impossible
to recognise such malicious code attacks.
Malicious code is any program that acts in unexpected and potentially
damaging ways (e.g., Trojan Horse). Malicious code can attack
institutions at either the server or the client level. It can also attack into
the infrastructure of a system (i.e., it can change, delete, insert or even
transmit data outside the institution).
Malicious code may be downloaded by the user in the following ways:
• While working on e-mails
• At the time of web surfing
• While downloading files/documents/software
Protection from malicious code involves limiting the capabilities of the
servers and web applications to only functions necessary to support the
operations. User of firewalls and other such network protection devices
prevents ingress of malicious code through the network. There must be
user awareness and training programs to overcome such situations.
4. How will you protect your system from malicious intent and malicious
code?
Ans. You can protect your computer system by using it in a proper and
systematic way. The following precautions can protect your system from
attackers:
• You should use high quality antivirus/signature-based antivirus on
your computer system.
• If you have started downloading a software that you suspect to be
malicious then stop the downloading immediately.
• You should avoid the use of pirated software or unauthorised software
which might cause great damage to your system in due course.
113
IV. Differentiate between the following:
1. Scanner object and BufferedReader object
Ans. Scanner Object BufferedReader Object
It works on the principle of 'Tokens' The buffered allows storing of input
into specific types like int, float, so that the instant supply of data
boolean, etc. as an input. can be done to the CPU without
any delay.
It works in JDK 1.5 version or more. It works in JDK 1.1 version onwards.
V. Unsolved Programs:
1. Using Scanner class, write a program to input temperatures recorded in
different cities in °F (Fahrenheit). Convert and print each temperature
in °C (Celsius). The program terminates when the user enters a non-
numeric character.
c F – 32
Formula: = =
5 9
Prog. import java.util.Scanner;
public class Convert
{
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter temperature in Fahrenheit: ");
while (in.hasNextDouble( ))
{
double t = in.nextDouble( );
double ct = 5/9.0 * (t – 32);
System.out.println("Temperature in Celsius: " + ct);
System.out.println("Enter temp. in Fahrenheit to continue or a
character to quit: ");
}
}
}
2. Write a program to accept a set of 50 integers. Find and print the greatest
and the smallest numbers by using Scanner class method.
Prog. import java.util.Scanner;
public class MaxMin
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
int max, min, n, i, num;
System.out.print("Enter first number: ");
n = sc.nextInt( );
max = n;
min = n;
for(i = 2; i<=50 ; i++)
{
System.out.print("Enter remaining numbers one by one : ");
num = sc.nextInt( );
if(num>max)
max = num;
if(num < min)
min = num;
}
System.out.println("Maximum of fifty numbers : " + max);
System.out.println("Minimum of fifty numbers : " + min);
}
}
3. Write a program (using Scanner class) to generate a pattern of a token in
the form of a triangle or in the form of an inverted triangle depending
upon the user's choice.