0% found this document useful (0 votes)
191 views52 pages

Logix CA Answer Key Grade 9

The document covers fundamental concepts of Object Oriented Programming (OOP) and Java, including definitions of key terms, features of OOP, and the differences between classes and objects. It also addresses Java's platform independence, the role of the Java Virtual Machine (JVM), and the significance of encapsulation and inheritance. Additionally, it discusses data types, variables, and typecasting in Java programming.
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)
191 views52 pages

Logix CA Answer Key Grade 9

The document covers fundamental concepts of Object Oriented Programming (OOP) and Java, including definitions of key terms, features of OOP, and the differences between classes and objects. It also addresses Java's platform independence, the role of the Java Virtual Machine (JVM), and the significance of encapsulation and inheritance. Additionally, it discusses data types, variables, and typecasting in Java programming.
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
You are on page 1/ 52

Logix 2.

0 with BlueJ

Computer Applications
CLASS IX

1
© Kips Learning Pvt. Ltd: 2023
Chapter 1: Object Oriented Programming Concepts
[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. Expand the following terms: JDK, JVM, IDE.
Ans.
 JDK: Java Development Kit
 JVM: Java Virtual Machine
 IDE: Integrated Development Environment

2. What was the earlier name of Java?


Ans. The earlier name of Java was Oak.

3. Who developed Java?


Ans. Java was developed by James Gosling.

4. Define source code.


Ans. Source code is a set of instructions and statements written by a programmer
using a computer programming language.

5. Define objects. Also, give a real-life example.


Ans. An object is a unique entity, which has some characteristics and behaviours. For
example, a student can be considered as an object because he/she has few
characteristics like name, class, age, etc.

6. Why is Java referred to as platform-independent?


Ans. Java is referred to as platform-independent because the java compiled code
(byte code) can run on all operating systems.
B. Short answer type questions.
1. Describe POP.
Ans. POP stands for Procedural Oriented Programming. It deals with programs and
functions. Programs are divided into functions and data is global. In these
programming languages, more stress is given on process rather than objects.

2. What do you understand by OOP? Write any four features of OOP.


Ans. OOP is an Object Oriented Programming Language. It is a modular approach,
which allows the data to be applied within the stipulated program area. It also provides
reusability features to develop productivity logic.

2
© Kips Learning Pvt. Ltd: 2023
Features of OOP:
 It gives stress on data rather than function.
 It makes the program simple by dividing it into a number of objects.
 The objects can be used as a bridge to have data flow from one function to
another.
 Data can be easily modified without any change in the function.

3. Class is termed as an object factory. Explain.


Ans. A class is known as an object factory because it contains all the statements needed
to create an object, its attributes as well as the statements to describe the operations
that the object will be able to perform.

4. What is the difference between an object and a class?


Ans.
Object Class
 Object is an instance of a class.  Class is a blueprint or template
from which objects are created.
 Object is a real-world entity, such  Class is a group of similar objects.
as student, book.
 Object is a physical entity.  Class is a logical entity.
 Object is created mainly through  Class is declared using class
new keyword. keyword.

5. Mention any four advantages of OOPs.


Ans. The following are the advantages of OOPs:
 We can extend the use of existing class through inheritance.
 Using the concept data hiding can generate secured program.
 We can create different modules in our project through objects.
 Multiple instances of an object can be generated to co-exist without any
interference.

6. Write any four features of Java.


Ans. The basic features of Java are as follows:
 Java is platform independent.
 It follows the concept of Write Once Run Anywhere (WORA).
 It offers many security features to make its program safe and secure.
 Java contains built-in graphics and supports multimedia.

3
© Kips Learning Pvt. Ltd: 2023
7. What is a package? Name any two packages used in Java.
Ans. A package is a group of related classes and interfaces. It provides a convenient
mechanism for managing a large set of classes and interfaces and avoiding naming
conflicts. Some Java packages are java.io, java.util, java.awt, and java.net.

8. "Java uses both a compiler and an interpreter." Explain the statement.


Ans. In java compilation, first, the compiler converts the source code to an
intermediate language (Java Byte Code), which is further converted to machine code
by the interpreter (Java Virtual Machine).

9. Why is Java called a case-sensitive language?


Ans. Java is a case-sensitive language because it treats uppercase and lowercase
differently.

10. What is class? Give a real-life example.


Ans. A class represents a set of objects that share common characteristics and
behaviour. For example, you can consider 'fruits' as a class in which you can keep
various fruits that have characteristics, like shape, colour, etc., and their behaviour is
sweet and/or sour.

11. Why is Java called a robust language?


Ans. Java is called a robust language because it is capable of handling run-time errors,
supports automatic garbage collection and exception handling, and avoids explicit
pointer concept. Java has a strong memory management system. It helps in
eliminating errors as it checks the code during both compile and runtime.

12. Describe the role of JVM.


Ans. JVM or Java Virtual Machine is an abstract machine designed to be implemented
on top of existing processors. It hides the underlying OS from Java application.
Programs written in Java are compiled into Java byte-code, which is then interpreted
by a special Java interpreter for a specific platform, known as Java Virtual Machine
(JVM).

13. Distinguish between a compiler and interpreter.


Ans. A compiler translates the whole program in one go. Whereas, an interpreter
translates the source code instructions into machine code, line-by-line. As compile
translates the code in one go, the errors (if any) are shown at the end together.
Considering the interpreter scans the code one line at a time, errors are shown line by
line.
4
© Kips Learning Pvt. Ltd: 2023
14. How can you say that encapsulation and abstraction are related terms?
Ans. Abstraction means giving only essential things and hiding unnecessary details.
Encapsulation is the binding of data members and methods together in a capsule to
avoid accidental changes to data from external users, i.e., encapsulation is the
bundling of related algorithms and data.

15. Explain the element of OOP which allows the properties of one class to be used by
another class.
Ans. In Java, this feature can be obtained using inheritance. It is the process by which
objects of one class acquire the properties of objects of another class. Inheritance
supports the concept of hierarchical representation. In OOP, the concept of
inheritance provides the idea of reusability.

C. Application-based questions.
1. Give a real-life example to explain the term encapsulation.
Ans. A capsule is a real-life example of encapsulation. Basically, a capsule
encapsulates several combinations of medicine. If combinations of medicine are
variables and methods, then the capsule will act as a class and the whole process is
called encapsulation.

2. Explain the concept of class and objects by providing a real-life example.


Ans. A class is a group of objects that have some common characteristics and
behaviours. A class is a blueprint from which individual objects are created. In other
words, a class is a concept, and the object is the implementation of that concept. We
need to have a class before creating the objects. Let us consider a car as a class that
has characteristics, like steering wheel, seats, brakes, etc., and its behaviour is
mobility. However, you can say BMW, having a registered number of 4284, is an
'object’ that belongs to the class 'car'.

Chapter 2: Elementary Concept of Objects and Classes


[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. Define objects with an example.
Ans. An object is an instance of a class.
Example: 'SXD' is an instance of the class 'School'

2. 'A class a is blueprint'. Define the statement.


Ans. A class is a blueprint since it specifies the attributes and behaviours that a set of
objects should possess. Objects are created as per the specifications of the class.
5
© Kips Learning Pvt. Ltd: 2023
3. List any two features of class.
Ans. Two features of a class are as follows:
 Classes support a powerful programming model by encapsulating data and
functions into objects.
 Classes increase the reusability of code.

4. What does the following mean: Mission chandrayaan = new Mission();


Ans. The given statement implies that a new object, named 'chandrayaan', is being
created in the memory using the constructor 'Mission( )'. The created object would
have the properties as defined in the class 'Mission'.

5. Write a statement to create an object 'MousePad' of the class 'Laptop'.


Ans. Laptop MousePad = new Laptop( );

B. Short answer type questions.


1. Describe the concept of class. Give a real-life example.
Ans. A class is a collection of objects, which possess similar attributes and behaviour.
Example:
class Car
{
// Attributes
String model;
int no_of_wheels;
int no_of_gears;
// Behavious
void acceptData( )
{
.............
}
void disp( )
{
............
}
}

2. Class and objects are inter-related. Explain.


Ans. Class and objects are inter-related. An object is created using a class and it is
the actual physical representation of the class. All objects of a particular class have
the same properties as defined in the class.

6
© Kips Learning Pvt. Ltd: 2023
3. Why a class is referred to as a user-defined data type? Give reason.
Ans. A class is referred to as a user-defined data type since it allows users to define a
blueprint so as to represent real-world entities. By using a class, it becomes possible
to precisely define the attributes and behaviours of a set of real-world entities in a
program.

4. What do you mean by message passing?


Ans. The system of communication among the objects of a program during program
execution is known as message passing.

C. Application-based questions.
1. Kavita wants to define some classes in Java. Help her by mentioning the five states
(characteristics) and two methods for the following classes:
a. Class Employee b. Class Book
c. Class Bank d. Class Park
e. Class School f. Class Medicine
Ans. The following are the five states (characteristics) and two methods for the
following classes:
a. Class Employee b. Class Book
Attributes: Attributes:
Emp_ID Title
Emp_Name Author
Emp_Dept Publisher
Emp_Designation No_of_Pages
Emp_Salary Price
Methods: Methods:
AcceptData( ) InputData( )
CalBonus( ) CalDiscount( )

c. Class Bank d. Class Park


Attributes: Attributes:
Branch_Code Park_Name
Brance_Name Park_Timings
Manager_Name Manager_Name
Branch_Ph Manager_Ph
Branch_Fax Ticket_Price
Methods: No_of_Visitors
AcceptData( ) Methods:
Display( ) AcceptData( )
CalTotalAmount( )

7
© Kips Learning Pvt. Ltd: 2023
e. Class School f. Class Medicine
Attributes: Attributes:
Sch_Code Product_Code
Sch_Name Product_Name
Sch_Board Product _ManufacturingDate
Sch_Addr Product ExpiryDate
Sch_Tel Methods:
Methods: AcceptData( )
AcceptData( ) DisplayData( )
DisplayData( )

2. Explain how objects communicate with each other.


Ans. Objects can communicate with one another by passing arguments and
returning values. They communicate with each other in a way that allows them to
specify what they want to be done, but leaves the implementation of that behaviour
to the receiving object. Passing a message is one way of achieving that separation of
the interface from the implementation. Another way is to call a (virtual) method in
the receiving object.

Chapter 3: Values and Data Types


[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. Define variables with an example.
Ans. A variable is a named location of the memory used to store a value during
program execution.
Example: N = 10; Where, ‘N’ is a variable and 10 is the value.

2. State any two primitive data types.


Ans. 'int' and 'double'

3. Mention any two composite data types.


Ans. 'class' and 'array'

4. What are the default values of the primitive data types 'int' and 'float'?
Ans. The default value of 'int' is 0 and 'float' is 0.0f.

8
© Kips Learning Pvt. Ltd: 2023
5. Define boolean data type.
Ans. The boolean data type is used to declare variables that store a logical value of
either 'true' or 'false' obtained as a result of a relational and/or logical operation in a
program.

6. What is UNICODE?
Ans. UNICODE or Universal Coding Scheme is a character representation standard
that provides a unique number to every character irrespective of the platform,
device, application, or language.

7. Explain the term typecasting.


Ans. Typecasting refers to the concept/technique of temporarily changing the data
type of a value or variable.

8. State the use of operators.


Ans. Operators are used to perform certain pre-defined tasks on data elements of a
program.

9. Define non-primitive data types.


Ans. Non-primitive data types are composed of various primitive data types and are
used in a program for advanced data processing tasks not supported by primitive
types.

10. What are constants?


Ans. Constants are data values or elements that do not change during program
execution.

B. Short answer type questions.


1. What is the need of using data types?
Ans. A data type is used to specify the type of data that is to be stored/processed and
how much memory would be required for it.

2. List the use of \\ and \r escape sequences.


Ans. The '\\' is used to print a back-slash and '\r' is used to represent a carriage-
return in a program.

3. What are tokens? List different types of tokens.


Ans. A token is the smallest unit of a program. The various types of tokens are:
keywords, literals, identifiers, operators, punctuators, and separators.
9
© Kips Learning Pvt. Ltd: 2023
4. Determine whether the following variables are valid or invalid.
a. name b. 24june c. a+b d. ur_name
Ans.
a. Valid b. Invalid c. Invalid d. Valid

5. Explain the difference between implicit and explicit type conversions.


Ans. An implicit type conversion is that in which the data type of the result of a
computation is automatically converted to the higher type without the intervention
of the programmer.
Whereas, an explicit type conversion is that in which the type of a data element is
converted into a higher or lower type through an instruction given by the
programmer in the code.

6. Arrange the following data types in ascending order of hierarchy.


a. float b. int c. double d. Long
Ans. b. int  d. long  a. float  c. double

7. Define keywords. Give any two examples.


Ans. A keyword is a reserved word, which has a special meaning for the language
translator.
Example: public, void

8. State the values of 'n' and 'ch'.


char c='A';
int n=c+1;
char ch=(char)n;
Ans.
n=66andch ='B'

C. Application-based questions.
Observe the following code segments carefully and predict the data type of 'r' and 'p'.
a. int p; double q; b. float m;
r = p+q; p=m/3*4.5;
System.out.println(r); System.out.println(p);
Ans.
a. double b. float

10
© Kips Learning Pvt. Ltd: 2023
Chapter 4: Operators in Java
[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. Describe the use of operators in Java.
Ans. Operators in java help to perform different operations while in a program from
simple arithmetic to complex functions.

2. What are operands?


Ans. Operands specify the manipulation or operations of different computer
operations while representing the data as well.

3. What are the three main types of operators?


Ans. The three main types of operators are arithmetical, logical, and relational.

4. Describe the use of shorthand operators. Also, give any one example.
Ans. Shorthand operators provide a short way of assigning an expression to a
variable. For example, A=a+6 can be written as a+=6.

5. Explain the following operators:


a. Logical AND
b. Greater than Equal To
c. Logical NOT
Ans.
a. It returns true when both expressions are true. If any of the two expressions is
false, it returns false.
b. It returns true if the left operand is greater than or equal to the right.
c. It reverses the result of an expression.

6. State the values of 'n' and 'ch':


Char c='B';
int n=c+1;
char ch=(char)n;
Ans. n=67, ch=C

7. What is the purpose of new operator?


Ans. The new operator is used in java to create new objects.

8. If a = 2, b = 5 then what will be the value of x in the following statement?


x = (a > b) ? a : b;
Ans. 5
11
© Kips Learning Pvt. Ltd: 2023
9. What will be the output of the following, if x = 5 initially?
a. 5 * ++x b. 5 * x++
Ans.
a. 30 b. 25

10. If m = 5 and n = 2, what will be the output of the values of 'm' and 'n' on execution
of the following statements.
a. m – = n b. n = m + m / n
Ans.
a. 3 b. 7

11. Explain the function of the dot (.) operator.


Ans. The dot (.) operator, also known as separator or period, is used to separate a
variable or method from a reference variable.

12. Differentiate between the println( ) and print( ) functions.


Ans. The print ( ) method prints the string, but it does not move the cursor to a new
line. Whereas, the println ( ) method prints the string and moves the cursor to a new
line.

B. Short answer type questions.


1. Differentiate between the increment and decrement operators.
Ans. The increment operator is used for incrementing the value by 1, whereas, the
decrement operator is used for decrementing the value by 1.

2. What is operator precedence and associativity? Explain with example.


Ans. Operator precedence is the order of evaluation of the expressions, while
associativity is the order of the execution of operands. For example, in the
expression, (2+1) * 3, the addition will be performed first as it is of higher priority
than multiplication and will be solved from left to right.

3. Differentiate between a counter and an accumulator.


Ans. A counter is a variable that indicates the number of times a particular situation
occurs in a program. Whereas, an accumulator is a special variable that stores some
partial results while some repeated tasks are performed. Accumulators and counters
have the same initial value, but accumulators do not have the same step value as
counters.

4. Explain the following:


a. Postfix and Prefix increment b. Postfix and Prefix decrement
12
© Kips Learning Pvt. Ltd: 2023
Ans.
a. Postfix and Prefix increment: In postfix increment, the original value is returned
first, and then the value is incremented by 1. On the other hand, in prefix
increment, the value is incremented by 1 first, after that the computation is
performed.
b. Postfix and Prefix decrement: In postfix decrement, the original value is returned
first, and then the value is decremented by 1. On the other hand, in prefix
decrement, the value is decremented by 1 first, after that the computation is
performed.

5. If a = 30, then evaluate the following:


a. ++a b. a++ c. – –a d. a– –
Ans.
a. 31 b. 30 c. 29 d. 30

6. What will be the output of the following code?


int a, b;
a = 6;
b = 8;
b = ++a;
System.out.println(+a);
Ans. 7

C. Application-based questions.
1. Neha has learnt the use of various operators. However, she is unable to develop a
Java code to calculate the area of a rectangle with a length of 5.6 cm and a width of
4.5 cm. Assist her in developing the code.
Ans.
import java.util.*;
class rectangle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double len=5.6, width = 4.5;
System.out.println("Area of rectangle: " + len * width);
}
}

13
© Kips Learning Pvt. Ltd: 2023
2. Paras wants to convert 75 rupees to paise. Write a program that does the same to
assist him.
Ans.
import java.util.*;
class rupee_conversion
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int rupee=75;
int paisa=75 * 100;
System.out.println ("75 rupees equals to: " + paisa);
}
}

3. Monika wants to check whether the current year is a leap year or not. But, she is
unfamiliar with the if-else construct. Write a program using the ternary operator to
assist her.
Ans.
import java.util.*;
class leap_year
{
public static void main(String args [])
{
Scanner sc = new Scanner(System.in);
System.out.print ("Enter year to check:");
int year = sc.nextInt ();
if (year % 400==0 || year % 4==0 && year%100!=0)
System.out.println (year + " is a leap year");
else
System.out.println (year + " is not a leap year");
}
}

Chapter 5: Input in Java


[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. Name the package that contains the Scanner class.
Ans. java.util
14
© Kips Learning Pvt. Ltd: 2023
2. Give an example of logical error.
Ans. When we use an incorrect operator on the variables to perform an operation.
For example, using ‘/’ operator to get module instead using ‘%’ is an example of
logical error.

3. Name the next methods of the Java Scanner class that are used to read or scan the
input as:
a. An integer b. A String
Ans.
a. in.nextInt() b. in.next();

4. What is the difference between the nextFloat( ) and nextInt( ) methods of the
Scanner class?
Ans. The nextFloat( ) method reads the next float value, whereas the nextInt( )
method reads the next integer value.

5. What are comments in Java?


Ans. Comments in Java are the statements that are not executed by the compiler or
interpreter.

6. How do you write single-line and multiline comments in a Java program? Give an
example of each.
Ans. Single-line Comment: A single-line comment is started with double backslash.
Example: //This is an example of a single-line comment.
Multiline Comment: A multiline comment is enclosed within /*.........*/.
Example: /*This is an example of multiline comment.*/

7. Write syntax to call a package in a Java program.


Ans. import java.util.*;

B. Short answer type questions.


1. What are tokens?
Ans. The smallest individual unit of a program is known as token.

2. What are delimiters?


Ans. These are the characters that split or separate the string into token.

3. Write any four advantages of taking input of data using the Scanner class.
Ans.
 There is no need of exception handling.
 It accepts data in token form, so there is no need to mention the type of data.
15
© Kips Learning Pvt. Ltd: 2023
 InputStreamReader and BufferedReader are not needed.
 It can be used for powerful pattern matching.

4. What do you mean by initialising a variable?


Ans. Initialising a variable means specifying an initial value to assign to it.

5. Name the following:


a. A package that is invoked by default
b. A keyword to use the classes defined in a package
Ans.
a. java.lang b. import

6. Correct the following codes:


a. Scanner ss=new Scanner(); b.int a=intNext.ss();
c. String nm=ss.nextString();
Ans.
a. Scanner ss=new Scanner(System.in); b.int a=in.nextInt();
c. String nm()=in.next();

7. Identify the types of errors (syntax/runtime/logical) in the following conditions:


a. Division by zero
b. b. Square root of a negative number
c. Using '–' in an arithmetic operation instead of '+'
d. Missing semicolon ( ; )
e. Using a variable that has not been previously defined
Ans.
a. Runtime error b. Logical error c. Runtime error
d. Syntax error e. Syntax error

8. Differentiate between syntax and runtime errors.


Ans.

Syntax Errors Runtime Errors


 An error that occurs while  An error in the syntax of a
program is running sequence of character or token
 Occurs due to one or more  Occurs due to performing an illegal
grammatical mistakes operation
 Computer detects the syntax  Computer detects the runtime
error at the time of compiling the error at the time of running a
program program

16
© Kips Learning Pvt. Ltd: 2023
9. Does an error free program after compilation ensure that will not be any runtime
errors? Justify your statement along with a proper example.
Ans. No, an error free program, after compilation, does not ensure that there will be
any runtime error.
Reason: This is because, after a program is compiled, it does not have any error. An
error is shown before the runtime.

10. What are comments? How many types of comments are there in Java?
Ans. Comments in Java are the statement that are not executed by the compiler or
interpreter. There are two types of comments: single-line comment and multiline
comment.

C. Application-based questions.
1. Write a program to input the name of a student and his marks in three subjects.
Print the total marks obtained along with the percentage.
Ans.

import java.util.*;
class total_marks
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
int m1, m2, m3, tm, p;
String ns;
System.out.println("Enter the name of students");
ns= in.next();
System.out.println("Enter the marks of first subject");
m1= in.nextInt();
System.out.println("Enter the marks of second subject");
m2= in.nextInt();
System.out.println("Enter the marks of third subject");
m3 = in.nextInt();
tm= m1+ m2+ m3;
p= tm/3*100;
System.out.println("Name of student="+ns);
System.out.println ("Total marks obtained="+ tm);
System.out.println ("Percentage ="+p);
}
}

17
© Kips Learning Pvt. Ltd: 2023
2. Write a program to input the temperature in Celsius. Convert it to Fahrenheit.
Ans.
import java.util.*;
class celsius_to_fahrenheit
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the temperature in Celsius");
float c=in.nextFloat();
float f;
f=(float)((c * 9 / 5) + 32);
System.out.print("\n");
System.out.println(c+" Celsius= "+ f + "Fahrenheit");
}
}

3. Write a program to input the length and breadth of a rectangle using Scanner Class.
Find its area and perimeter.
Ans.
import java.util.*;
class rectangle
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
float l,b,ar,p;
System.out.println("Enter length and breadth");
l= in.nextFloat();
b=in.nextFloat();
ar=l*b;
P=2*(l+b);
System.out.println ("Area="+ ar);
System.out.println("Perimeter="+p);
}
}

18
© Kips Learning Pvt. Ltd: 2023
4. Write a program to input the distance travelled by a car in kilometres and the time
taken in hours. Find the speed of the car. Convert the speed into metres/sec.
Ans.
import java.util.*;
class kilometres_conversion
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
double s,d,t;
System.out.println ("Enter the speed int kilometres");
s = in.nextDouble();
System.out.println ("Enter the time in hour");
t = in.nextDouble();
d = s*t;
System.out.println ("Distance in kilometres ="+ d + "km");
System.out.println ("Distance in metre ="+d * 1000+ "m");
}
}

5. Write a program to input a two-digit number using the Scanner class methods and
find the number obtained by reversing its digits.
Ans.
import java.util.*;
class reverse_number
{
public static void main(String args[])
Scanner in = new Scanner(System.in);
int n= 0 ,rn=0;
System.out.println("Enter the number to be reversed");
n= in.nextInt();
While (n!=0)
{
rn= rn*10;
rn=rn+n%10;
n= n/10;
System.out.println ("Reverse number="+rn);
}
}
}

19
© Kips Learning Pvt. Ltd: 2023
Chapter 6: Mathematical Library Methods
[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. What will the following statements return?
a. Math.max(–17, –19);
b. Math.ceil(7.8);
c. int p = Math.abs(Math.max(–91, –97));
d. double m = Math.cbrt(9.261);
Ans.
a. -17
b. 8.0
c. 91
d. 2.1

2. Give the output of the following expression:


a. If x = –9.99, calculate Math.abs(x);
b. If x = 9.0, calculate Math.sqrt(x);
Ans.
a. 9.99
b. 3.0

3. Predict the return data type of the following functions:


a. Math.sqrt( )
b. Math.pow( )
c. Math.round( )
d. Math.ceil( )
e. Math.floor( )
Ans.
a. double
b. double
c. int/long
d. double
e. double

20
© Kips Learning Pvt. Ltd: 2023
B. Short answer type questions.
1. Distinguish between the following methods with suitable examples:
a. Math.rint( )and Math.round( )
b. Math.ceil( ) and Math.floor( )
Ans.
a. Math.rint (): This method returns the integer part of the input in double form.
Math.round(): This method basically rounds off the input to give the nearest integer
in integer form.

b. Math.ceil(): This function returns the next higher integer value of a number given
within brackets. It returns a double data type.
Example:
Input: 7.5
Output: 8
Math.floor(): This method returns the previous lower integer value of a number
given within brackets. Its return type is double.
Example:
Input: 7.5
Output:7.0

2. Predict the output of the following statements:


a. Math.max(Math.min(3,5),Math.max(2,1));
b. Math.sqrt(Math.floor(7.3)+Math.pow(3,2));
c. Math.ceil(9.9)+Math.ceil(9.0);
d. Math.abs(12.35);
e. Math.round(Math.abs(13.67));
Ans.
a. 3
b. 4
c. 18
d. 12.35
e. 14

3. Predict the output of the following statements:


a. System.out.println(Math.round(2.4));
b. System.out.println(Math.round(–2.4));
c. System.out.println(Math.ceil(2.6));
d. System.out.println(Math.cbrt(216));
21
© Kips Learning Pvt. Ltd: 2023
e. System.out.println(Math.pow(2, 5));
Ans.
a. 2
b. 2
c. 3
d. 6
e. 32

C. Application-based questions.
1. Write a program to input two numbers in Java and print the square root and cube
root of both.
Ans.
import java.util*;
class SquareandCube
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
int n;
System.out.println ("Enter the number");
n = in.nextInt();
System.out.println("Square root="+Math.sqrt(n));
System.out.println("Cube root="+Math.cbrt(n));
}
}

2. Write a program to input four numbers and print the biggest among them (use
Math.max( )).
Ans.
import java.util.*;
class BiggestNumber
{
public static void main(String args[])
{
Scanner in = new Scanner( System.in);
int a,b,c,d,g;
System.out.println("Enter the first number");
a=in.nextInt();
System.out.println("Enter the second number");
b=in.nextInt();
System.out.println("Enter the third number");
22
© Kips Learning Pvt. Ltd: 2023
c=in.nextInt();
System.out.println("Enter the fourth number");
d=in.nextInt();
System.out.println("Enter the biggest number");
g=in.nextInt();
g=Math.max(a,b,c);
g=Math.max(g,d);
System.out.println(“Biggest number=”+g);
}
}

3. Rohan wants to find the ceil and floor value of a variable. Which methods from the
Math class should he use to find the same.
Ans.
import java.util.*;
class math_operations
{
public static void main (String args[]);
{
Scanner in = new Scanner (System.in);
int a,b;
System.out.println("Enter the number");
a = in.nextInt();
b = in.nextInt();
System.out.println("Math.ceil(a,b))";
System.out.println("Math.floor(a,b))";
}
}

4. Priya'steacher has given her an assignment to write a program to calculate the


surface area of a cylinder using Math functions. Help her in doing so.
Ans.
import java.util.*;
class cylinder
{
public static void main (String args[]);
{
Scanner in = new Scanner (System.in);
double r,h,s;
System.out.println("Enter the radius");
r = in.nextDouble();
System.out.println("Enter the height");

23
© Kips Learning Pvt. Ltd: 2023
h = in.nextDouble();
s = ((2.0*22.0/7.0)*(r*h))+2.0*22.0/7.0(Math.sqrt(2));
System.out.println(“Surface area of cylinder=”+s);
}
}

Chapter 7: Conditional Constructs in Java


[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. Write the syntax of the 'if' statement.
Ans.
if (condition)
{
Statement(s)
}
2. Describe the use of break in the switch-case statement.
Ans. The 'break' keyword in a switch-case is used to specify the end of a particular
'case' and jump-out of the switch-case construct.
3. What will be the output of the following code segment?
int x=4, y=10;
if (x > n)
{
x = x + 1;
y = y + 1;
}
Ans. Error, since 'n' is not defined.
4. Predict the output of the following code:
int a=30, b=25;
if (a>10)
{
a++;
b++;

24
© Kips Learning Pvt. Ltd: 2023
}
System.out.println(a + " , " +b);
Ans. 31, 26
B. Short answer type questions.
1. Distinguish between if-else and switch-case statements.
Ans.
if-else switch-case
 It is a two-way decision-  It is a multi-way decision-
making construct. making construct.

 It can work with all types of  It can only work with


data. characters and integers.

 It results in a boolean-type  It results in a character or


value. integer value.

 It allows the use of Relational  It does not allow the use of


and Logical operators while Relational and Logical
specifying conditions. operators while specifying the
cases.

 The "default" operation is not  The "default" operation is


supported. supported.

 Use of "break" is not essential.  Use of "break" is largely


essential.

2. Write a statement using if-else to find the square of the bigger of two numbers.
Ans. if (a>b) s=a*a; else s= b*b;

3. What is the dangling-else problem?


Ans. It is a problem in programming in which an optional else in an if-else results
in nested conditions being ambiguous.

4. Explain the fall through situation in the switch-case.


Ans. When the variable being switched-on matches a ‘case’, the statements specified
for that ‘case’ are executed and, if a ‘break’ is not applied, the execution continues to
the following cases. This is referred-to as a fall through.

25
© Kips Learning Pvt. Ltd: 2023
5. What is the significance of the default statement in switch-case based programs?
Ans. It is used to specify statements that are to be executed if none of the given
cases match.

C. Application-based questions.
1. Write a program to input the age of a person and find whether he is eligible to vote
or not.
Ans.
import java.util.*;
class vote
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
int a;
System.out.println("Enter the Age: ");
a = sc.nextInt( );
if (a >= 18)
System.out.println("Can Vote");
else
System.out.println("Cannot Vote");
}
}

2. Write a program to input two numbers and find the cube of the bigger number.
Ans.
import java.util.* ;
class cube
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in) ;
int a, b;
System.out.println("Enter the two numbers:");
a = sc.nextInt( );
b = sc.nextInt( );
if (a > b)
System.out.println("Cube=" + a*a*a) ;
else
System.out.println("Cube=" + b*b*b) ;
}
}
26
© Kips Learning Pvt. Ltd: 2023
3. Write a program to input a year in yyyy format and determine if it is a leap year or
not.
Ans.
import java.util.* ;
class leap_year
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in) ;
int y;
System.out.println("Enter the Year: ") ;
y = sc.nextInt( );
if (y%4 == 0)
System.out.println("It is a leap year") ;
else
System.out.println("Not a leap year") ;
}
}

4. A shopkeeper offers discounts to customers as per the following purchase amount:

Goods Worth Discount


Upto 5000 10 %
>5000 15 % Write a program to input the
purchase amount, calculate the discount, and print the net payment after reducing
the discount.
Ans.
import java.util.*;
class net_payment
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
double p, d;
System.out.println("Enter the Purchase Amount:");
p = sc.nextInt( );
if (p <= 5000 )
d = 0.10 * p;
else
d = 0.15 * p;
System.out.println("Dis.=" + d );
27
© Kips Learning Pvt. Ltd: 2023
System.out.println("Net Amt.=" + (p – d));
}
}

5. Write a menu-based program using the switch-case statement that accepts two
numbers as input. Give the options to calculate the sum, difference, product, and
the result of the division operation (both quotient and remainder should be
printed).
Ans.
import java.util.*;
class arithmetic
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in) ;
int a, b, ch;
System.out.println("Enter the two numbers:");
a = sc.nextInt( );
b = sc.nextInt( );
System.out.println("Enter 1-Add, 2-Sub, 3-Mul, 4-Div: ");
ch = sc.nextInt( );
switch(ch)
{
case 1:
System.out.println("Addition="+(a+b));
break;
case 2:
System.out.println("Subtraction="+(a-b)); break;
case 3:
System.out.println("Product="+ (a*b)) ;
break;
case 4:
System.out.println("Quotient="+(a/b)) ;
break;
default:
System.out.println("Invalid Choice") ;
}
}
}

28
© Kips Learning Pvt. Ltd: 2023
6. Write a program to accept a number from the user and check whether it is a Buzz
number. [Hint: A buzz number is a number that either ends with 7 or is divisible by
7.
Ans.
import java.util.*;
class buzz_number
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
int N
if (N%7==0 || N%10==7);
{
System.out.println("Enter the Number : ");
N = sc.nextInt( );
System.out.println("It is a BUZZ Number ");
}
else
{
System.out.println("Not a BUZZ Number ");
}
}
}

7. Write a program to input the age and check if the person is a child, teenager, adult,
or old.
Age Type
Up to 12 Child
13-19 Teenager
20-50 Adult
Above 50 Old
Ans.
import java.util.*;
class check_age
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
int a;
String s;

29
© Kips Learning Pvt. Ltd: 2023
System.out.println("Enter the Age : ");
a = sc.nextInt( );
if (a<=12 )
s = "Child";
else if (a<=19)
s = "Teenager";
else if (a<=50)
s = "Adult";
else
s = "Old";
System.out.println(s);
}
}

8. Write a program to input the length and breadth of a rectangle and develop a
menu-based program which will be based upon the user's choice and find either of
the following:
a. The area of the rectangle l * b
b. Perimeter of the rectangle 2(l+b)
c. Diagonal of the rectangle √l2 +b2
Ans.
import java.util.*;
class rectangle
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
double l, b;
int ch;
System.out.println("Enter length & breadth: ");
l = sc.nextDouble( ) ;
b = sc.nextDouble( ) ;
System.out.println("Enter 1-Area, 2- Perimeter, 3-Diagonal");
ch =sc.nextInt( ) ;
switch(ch)
{
case 1:
System.out.println("Area=" + l*b);
break;
case 2:

30
© Kips Learning Pvt. Ltd: 2023
System.out.println("Perimeter=" + 2*(l+b));
break;
case 3:
System.out.println("Diagonal="+Math.sqrt(l*l + b*b));
break;
default: System.out.println("Invalid Choice.");
}
}
}

9. Write a program to enter the number of days. Use the switch statement to input an
option and convert it into weeks, months, or years.
7 days = 1 week
30 days = 1 month
365 days = 1 year
Ans.
import java.util.*;
class number_of_days
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int N, ch;
System.out.println("Enter the Number:");
N=sc.nextInt( );
System.out.println("Enter 1-Weeks, 2-Months, and 3-Years:");
ch=sc.nextInt( );
switch(ch)
{
case 1:
System.out.println("Weeks =" + N/7.0 );
break;
case 2:
System.out.println("Months =" + N/30.0 );
break;
case 3:
System.out.println("Years =" + N/365.0 );
break;
default: System.out.println("Invalid Choice.");
}
}
}

31
© Kips Learning Pvt. Ltd: 2023
10. Write a menu-driven program to calculate the following:
a. Square of the number
b. Cube of the number
c. Square root of the number
Ans.
import java.util.*;
class number
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System. in);
int N, ch;
System.out.println("Enter the Number: ");
N = sc.nextInt( );
System.out.println("Enter 1-Square, 2-Cube, 3-Square Root:");
ch =sc.nextInt( );
switch(ch)
{
case 1: System.out.println("Square =" + N*N);
break;
case 2: System.out.println("Cube =" + N*N*N);
break;
case 3:
System.out.println("Square Root =" + Math.sqrt(N));
break;
default: System.out.println("Invalid Choice.");
}
}
}

Chapter 8: Iterative Constructs in Java


[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. Name the various iterative statements used in Java.
Ans. Java uses mainly three types of iterative statements: for, while, do while. Java
also includes enhanced for loop in Java5.

2. Describe the entry-controlled loops in Java.


Ans. An entry-controlled loop checks the condition at the time of entry. Only if the
condition is true, the program control enters the body of the loop. For and while
loops are entry control loops.
32
© Kips Learning Pvt. Ltd: 2023
3. Write a for loop statement to display the even numbers from 1 to 20.
Ans.
for (int i=1; i<=20; i++)
{
if(i%2==0)
System.out.println(i);
}

4. What is the significance of using the break statement in a loop?


Ans. The break is a loop control statement, which is used to terminate the loop. As
soon as the break statement is encountered from within a loop, the loop iterations
stop and control returns from the loop immediately to the first statement after the
loop.

5. Which statement is used to execute a set of statements a fixed number of times?


Ans. For loop statement is used to execute a set of statements a fixed number of
times.

6. Which statement is used to come out of a loop without executing the further
statements in the loop?
Ans. Break statement is used to come out of a loop without executing the further
statements in the loop.

B. Short answer type questions.


1. What is a loop?
Ans. Looping in programming languages is a feature, which facilitates the execution
of a set of instructions repeatedly while some condition evaluates to true.

2. Write the syntax of a while loop.


Ans.
while( condition)
{
//statements
Update condition
}

3. What is meant by an infinite loop?


Ans. An infinite loop occurs when a condition always evaluates to true in looping
condition. For( ; ; ) and while(true) are examples of infinite loops.

33
© Kips Learning Pvt. Ltd: 2023
4. Which looping construct can be used for a fixed number of iterations?
Ans. for Loop

5. Which looping constructs executes at least once in a program?


Ans. do-while loop

6. Write syntax of a for loop.


Ans.
for(initialisation; boolean expression; update statement)
{
// Body of for loop
}

C. Application-based questions.
1. How many times the loop will be executed?
int x=1000;
while(true)
{
if(x<500)
break;
x=x–100;
}
Ans. 6 times
2. Rewrite the following program codes by using the while and do-while loops.
a. b.
int a; int p,q=1;
for(a=1;a<=50;a++) for(p=1;p<=5;p++)
System.out.println("OOPS"); q*=p;
System.out.println(q);
Ans.
a.
while loop do-while
int a; int a;
a=1; a=1;
while(a<=50) do
{ {
System.out.println("OOPS"); System.out.println("OOPS");
a++; a++;
} }
while(a<=50);
34
© Kips Learning Pvt. Ltd: 2023
b.
while loop do-while
int p, q = 1; int p, q = 1;
p = 1; p = 1;
while (p < 5) do {
{ p++;
p++; q *= p;
q *= p; }
} while (p < 5);

3. Rewrite the following program code by using a for loop.


int a=10;
while(a>0)
{
System.out.println("POP");
a– –;
}
Ans.
for (int a = 10; a > 0; a--)
{
System.out.println("POP");
}

4. Rewrite the following statements by using a while loop.


for(int n=1,int s=0;n<=10;n=n+1)
s+=n;
Ans.
int n = 1;
int s = 0;
while (n <= 10)
{
s += n;
n = n + 1;
}

5. Rewrite the following program code after correcting errors, if any.


int p; q;
for(p=1;p<=5;p++);
{
q=p*p*p
35
© Kips Learning Pvt. Ltd: 2023
System.out.println(q);
}
Ans.
int p, q;
for (p = 1; p <= 5; p++)
{
q = p * p * p;
System.out.println(q);
}

6. Rewrite the following program code after correcting errors, if any.


int x;
for(int i=1,i<=x, i++)
{
System.out.println(x);
}
Ans.
int x;
for (int i = 1; i <= x; i++)
{
System.out.println(x);
}

7. Write a program to find the sum of all odd numbers between 1 and 100.
Ans.
import java.util.*;
class Test
{
public static void main(String args[])
{
int sum = 0;
for (int i = 1; i <= 100; i++)
{
if (i % 2 != 0)
sum += i;
}
System.out.println(sum);
}
}

36
© Kips Learning Pvt. Ltd: 2023
8. Write a program to input the number of students in a class. For every student,
enter their height and find the average height of the students of the class.
Ans.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
int sum = 0;
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
sum += arr[i];
}
System.out.println(sum / n);
}
}

9. Write a menu-based program to input a number and perform the following:


a. Display the factors of the number
b. Print the first 5 multiples of the number
c. Find the sum of all numbers starting from 1 till that number
Ans.
import java.util.*;
class test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter any number:");
int n = sc.nextInt();
int x = 1;
while (x != 9)
{
37
© Kips Learning Pvt. Ltd: 2023
System.out.println("Enter 1 for find factors:");
System.out.println("Enter 2 for find multiples:");
System.out.println("Enter 3 for find sum:");
System.out.println(" Enter 9 for exit:");
int c = sc.nextInt();
switch (c)
{
case 1:
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
System.out.println(i);
}
break;
case 2:
for (int i = 1; i <= 5; i++)
{
System.out.println(i * n);
}
break;
case 3:
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i;
System.out.println(sum);
break;
case 9:
System.exit(0);
default:
System.out.println("Wrong input");
}
}
}
}

38
© Kips Learning Pvt. Ltd: 2023
10. Write a program to input 20 numbers and print the sum of even and odd numbers
separately.
Ans.
import java.util.*;
class even_odd
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr[] = new int[20];
int evensum = 0, oddsum = 0;
for (int i = 0; i < 20; i++)
arr[i] = sc.nextInt();
for (int i = 0; i < 20; i++)
{
if (arr[i] % 2 == 0)
evensum += arr[i];
else
oddsum += arr[i];
}
System.out.println(evensum);
System.out.println(oddsum);
}
}

Chapter 9: Nested for Loops


[Section 2: Unsolved Questions]
A. Short answer type questions.
1. What do you mean by nested loops?
Ans. Nested loop means a loop statement inside another loop statement.
2. Give an example of a nested loop.
Ans.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
System.out.print(i + "" + j + " ");
System.out.println( );
}

39
© Kips Learning Pvt. Ltd: 2023
3. Explain the use of the break statement in a nested loop.
Ans. The break statement takes the control out of the loop. In the case of nested
loops, the break statement will only terminate the loop where it is applied. For
example, if a break statement is written inside the inner loop then it will terminate
the inner loop only and the outer loop remains unaffected.

4. Write a program segment to show the use of the continue statement in a nested
loop.
Ans.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i == j)
continue;
System.out.print(i + "" + j + " ");
}
System.out.println( );
}
B. Application-based questions.
Series Based
1. Write a program to print first 10 terms of the following series:
a. 1,4,9,16,25, b. 1,8,27,64,125,
b. 0,7, 26,63,124, c. 1,–3,5,–7,9,
d. 2,5,10,17
Ans.
a.
import java.util.*;
class series1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 10; i++)
{
System.out.println((int)Math.pow(i, 2));
}
}
}

40
© Kips Learning Pvt. Ltd: 2023
b.
import java.util.*;
class series2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 10; i++)
{
System.out.println((int)Math.pow(i, 3) - 1);
}
}
}
c.
import java.util.*;
class series3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int j = 1;
for (int i = 1; i <= 20; i++)
{
if (i % 2 != 0)
{
System.out.println(i * j);
j = j * (-1);
}
}
}
}

41
© Kips Learning Pvt. Ltd: 2023
d.
import java.util.*;
class series4
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int j = 1;
for (int i = 1; i <= 10; i++)
{
System.out.println((int)Math.pow(i, 2) + 1);
}
}
}

e.
import java.util.*;
class series5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int d, i;
int n = 10;
for (int i = 1; i <= 10; i++)
{
d=i*i+1;
System.out.print(d+",");
}
}
}

42
© Kips Learning Pvt. Ltd: 2023
2. Find the sum of the following series:

a.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
int sum = (n * (n + 1)) / 2;
System.out.println(sum);
}
}
b.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i++)
{
int a = (i * 2) + 1;
sum += a;
}
System.out.println(sum);
}
}

43
© Kips Learning Pvt. Ltd: 2023
c.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++)
{
int a = (int)Math.pow(i, 2) + 1;
sum += a;
}
System.out.println(sum);
}
}

d.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
int sum = 0, j = 1;
for (int i = 2; i <= 2 * n; i += 2)
{
int a = i * j;
sum += a;
j = j * (-1);
}
System.out.println(sum);
}
}
44
© Kips Learning Pvt. Ltd: 2023
e.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
double sum = 0;
for (int i = 3; i <= (2 * n) + 1; i += 2)
{
double a = ((double)1 / i);
sum += a;
}
System.out.println(sum);
}
}

f.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
System.out.println("Enter value of x");
int x = sc.nextInt();
double sum = 1;
for (int i = 1; i < n; i ++)
{
double a = Math.pow(x, i);
a = ((double)a / (i + 1));
sum += a;
}
System.out.println(sum);
}
}

45
© Kips Learning Pvt. Ltd: 2023
g.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
System.out.println("Enter value of a");
int a = sc.nextInt();
double sum = 0;
for (int i = 1; i < n; i ++)
{
sum = Math.pow(a, n);
}
System.out.println(sum);
}
}

h.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
int sum = 0, j = 1;
for (int i = 0; i < n; i ++)
{
sum += j;
j = (j * 10) + 1;
}
System.out.println(sum);
}
}
46
© Kips Learning Pvt. Ltd: 2023
3. Write a program to print the following patterns:

Ans.
a.
import java.util.*;
class pattern1
{
public static void main(String args[])
{
for (int i = 1; i <= 4; i ++)
{
for (int j = 1; j <= 4; j++)
{
System.out.print("* ");
System.out.println();
}
}
}
}

47
© Kips Learning Pvt. Ltd: 2023
b.
import java.util.*;
class pattern2
{
public static void main(String args[])
{
for (int i = 1; i <= 4; i ++)
{
for (int j = 1; j <= 4; j++)
System.out.print(j + " ");
System.out.println();
}
}
}

c.
import java.util.*;
class pattern3
{
public static void main(String args[])
{
for (int i = 1; i <= 4; i ++)
{
for (int j = 1; j <= 4; j++)
{
System.out.print(i + " ");
System.out.println();
}
}
}

48
© Kips Learning Pvt. Ltd: 2023
d.
import java.util.*;
class pattern4
{
public static void main(String args[])
{
int k = 0;
for (int i = 1; i <= 4; i ++)
{
for (int j = 1; j <= 4; j++)
System.out.print(++k + " ");
System.out.println();
}
}
}

e.
import java.util.*;
class pattern5
{
public static void main(String args[])
{
for (int i = 1; i <= 4; i ++)
{
int k = 65;
for (int j = 1; j <= 4; j++)
System.out.print((char)k++ + " ");
System.out.println();
}
}
}
f.
import java.util.*;
class pattern6
{
public static void rightTriangle(int n)
49
© Kips Learning Pvt. Ltd: 2023
{
int i, j;
for(i=0; i<n; i++)
{
System.out.print(" ");
}
for(j=0; j<=i; j++)
{
System.out.print("* ");
}
System.out.println();
}
public static void main(String args[])
{
int n = 5;
rightTriangle(n);
}
}
g.
import java.util.*;
class pattern7
{
public static void printNums(int n)
{
int i, j,num;
for(i=0; i<n; i++)
{
num=1;
for(j=0; j<=i; j++)
{
System.out.print(num+ " ");
num++;
}
System.out.println();
}
public static void main(String args[])
{
int n = 5;
50
© Kips Learning Pvt. Ltd: 2023
printNums(n);
}
}

Chapter 10: Ethical Issues in Computing


[Section 2: Unsolved Questions]
A. Very short answer type questions.
1. What are computer ethics?
Ans. It is a part of decision-making regarding social and professional conduct for
computer professionals.

2. Write any five security threats.


Ans. Any five security threats are phishing, malware, viruses, computer worm, and
denial-of-service attack.

3. What is cybercrime?
Ans. Cybercrime is the use of computer as an illegal end for committing frauds and
digital crimes.

4. List any two measurers that provides protection against spam.


Ans.
 Not providing email address publicly.
 Downloading spam filtering tools and antiviruses.

5. Describe any two types of software piracy.


Ans. The following are the two types of software piracy:
Softlifting: It is the most prevalent sort of piracy.
Renting: It is done without the authorisation of the copyright owners. For example,
renting a movie from Blockbuster, is a violation of the software licence agreement.

B. Short answer type questions.


1. Describe any two preventions against software piracy.
Ans. The following are the two preventions against software piracy:
Always buy a licenced copy of the software.
Never make copies and circulate them.

2. Distinguish between black hat and white hat hackers.


51
© Kips Learning Pvt. Ltd: 2023
Ans. Black hat hackers are the bad guys who find flaws in computer systems to
exploit them for financial benefit. Whereas, white hat hackers are the good guys who
use proactive hacking to stop black hat hackers from succeeding.

3. Write any two safety measures against malicious intent and malicious code.
Ans. We should keep in mind the following two safety measures against malicious
intent and malicious:
 Use of antivirus
 Limitation on file-sharing

4. Describe any two ethical practices and etiquette.


Ans. Two ethical practices are as follows:
 Do not spy on another person's computer data.
 Do not use computer technology to steal information.
Two netiquette are as follows:
 Do not copy the work of the others without their permission as it leads to
copyright violation.
 Do not use sarcastic or disrespectful language in your communication. Treat
others the way you would like to be treated.

5. Define the following:


a. Spamming
b. Ransomware
c. Copyright
d. Phishing
Ans.
a. Spamming means sending the same message indiscriminately to a large number
of internet users.
b. Ransomware employs encryption to hold a victim's information at ransom.
c. Copyright gives its owner the exclusive right to copy and distribute a creative
work usually for a limited time.
d. Phishing is an attempt to steal an individual’s money or their identity by getting
them to reveal personal information.

52
© Kips Learning Pvt. Ltd: 2023

You might also like