Logix CA Answer Key Grade 9
Logix CA Answer Key Grade 9
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
© 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
© 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.
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.
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.
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( )
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( )
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.
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.
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.
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
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");
}
}
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.
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.*/
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.
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
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
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))";
}
}
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);
}
}
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.
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;
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") ;
}
}
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.");
}
}
}
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.
33
© Kips Learning Pvt. Ltd: 2023
4. Which looping construct can be used for a fixed number of iterations?
Ans. 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);
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);
}
}
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);
}
}
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);
}
}
3. What is cybercrime?
Ans. Cybercrime is the use of computer as an illegal end for committing frauds and
digital crimes.
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
52
© Kips Learning Pvt. Ltd: 2023