File
File
2024-25
SAVIOUR OF
COMPUTERS
THEORY
NOTES
CHAPTER
OBJECT OBJECT
Methods
Car benz = new car () ;
Data Member
Car swift = new swift () ;
CLASS
DEFINING A CLASS AND
Blueprint of the Object ITS SYNTAX
Syntax of class :
Representation of Similar objects class classname
{
type instance - variable;
Car (Class)
type methodname1(parameter-list)
{
Swift Object 1
//body of method
Benz Object 2 }
Endeavour Object 3 type methodname2(parameter-list)
Verna Object 4 {
//body of method
}
}
CHAPTER
ACCESS SPECIFIER
OBJECT
Decides how parts of the classes can be accessed by other classes in other
parts of the program // defines the scope of visibility of the particular code.
Visibility Rules
PRIMITIVE DATATYPES
[PRE DEFINED]
BYTE INT
1. Byte Datatype is an 8-bit signed two 1. int datatype is a 32-bit signed two's
complement Integer. complement integer.
2. Minimum value is -128 or (-2)^7. 2. Minimum value is - 2,147,483,648 or
3. Maximum value is 127 (inclusive) or (-2)³¹.
(2^7-1). 3. Maximum value is
4. Default value is 0. 2,147,483,647(inclusive) or (2³¹– 1).
5. Example, Byte a = 100 and Byte b = 4. The default value is 0.
-50. 5. Example, int a = 100000, int b =
-200000.
SHORT LONG
PRIMITIVE DATATYPES
[PRE DEFINED]
DOUBLE CHAR
BOOLEAN
non-primitive Data types are called reference types because they refer to objects. The
main difference between primitive and non-primitive data types are:
Primitive types are predefined (already
defined) in Java. Non-primitive types are
created by the programmer and is not
defined by Java (except for String).
The variables declared within the class on which some operations are to be performed
are known as data or data members or instance variables or attributes or fields.
A method is a block of code that only runs when it is called. You can pass data, known as
parameters, into a method. Methods are used to perform specific actions, and they are also
known as functions.
CHAPTER
Classes are required in OOPs The variables declared using short, int,
because It provides a template for long, float, double, char, byte, and
creating objects, which can bind boolean are called primitive data types
code into data. It has definitions of
methods and data. It supports the
inheritance property of Object
Oriented Programming and hence
can maintain the class hierarchy.
METHODS
INTRODUCTION METHODS
2. Code reusability: Once a problem has been defined as a method, then these
methods can be called anywhere in the program repeatedly. Thus, in helps to
reduce the duplication of the program. As add() method can be defined only once
and used repeatedly just by calling it.
4. Hides the complexity: Methods are just like black boxes where a user need not
to be aware of the logic/code running behind the application.
4. Hides the complexity: Methods are just like black boxes where a user need not
to be aware of the logic/code running behind the application.
CHAPTER
METHODS
1. if method prototype is
public void dispMessage() then calling statement is
dispMessage(); // calling a method with no parameters.
METHODS
CALL BY VALUE
SYNTAX:
CALL BY REFERENCE
All the data is stored in the memory which is divided into cells. Each cell has a unique address.
The term reference, stores memory location of a variable.
Points to Ponder:
When a method is called by reference, the actual parameters are not copied to the formal
parameters.
The memory is not allocated separately for actual and formal parameters.
Any change made to the formal parameters will automatically update or change the value of
the actual parameters.
While calling by reference it is mandatory that only object or array variables should be passed.
The actual parameters cannot be constants and expressions.
SYNTAX:
METHOD OVERLOADING
In Java, two different methods can have the same name if the parameter types or number of
parameter passed are different. The parameter types and number of parameter are called the
signature of methods. When more than one method with the same name exist in the same
program with different signatures, this is called method overloading.
CHAPTER
CONSTRUCTOR
INTRODUCTION
So far, you have studied the methods and the way they
work in Java. Now you will be learning a different type
of method. It is known as a constructor and its most
distinguishing feature is that it is named after the class it
is in. Let us find out more about the constructor. CHARACTERISTICS
Being a method, it also gives the programmer the ability to define, declare and modify. Since, a
constructor has no return type (not even void), it also provides the programmer with the liberty to
define a constructor with or without a parameter. A simple constructor is created using the new
operator followed by the name of the class.
Syntax:
<access specifier> class_name (parameter list/void)
{
Data_member1 = value1 ; Data_member2 = value2;
}
CHAPTER
CONSTRUCTORS
EXAMPLE:
class Book
{ int bookno;
String name;
float price;
public Book()
//constructor is created, having same name Book as that of
{ bookno = 0;
its class name Book, with public access specifier and no
name = "ABC";
return type.
price=0.0
} }
TYPES OF CONSTRUCTORS
The main function of a constructor is to initialise the data members of an object while it is
created. Constructors can be broadly categorised into two categories based on the parameters
it takes:
A constructor that takes no parameters is called a default constructor. It will have the name of the
class and have no return type. If you do not write a constructor in your program, the compiler will
supply a default constructor and initialise values with the default values of the primitive data
types i.e. integer variables to zero, floating point variables to 0.0 and String to null.
Syntax:
class_name()
{ Data_member1 = value1;
Data_member2 = value2;
}
CHAPTER
CONSTRUCTOR
PARAMETERISED CONSTRUCTOR
'this' keyword is used within a constructor to refer to the current object. It is actually a
reference to the object that invokes the constructor. In fact, even if you do not use the this
keyword, the compiler normally implicitly converts it by prefixing this to the data members.
LIBRARY CLASSES
DATA TYPE
The data type refers to the identification of the type of data(or variable) which is used to
perform some operations.
COMPOSITE DATA TYPE
The data type which combines primitive data types is known as composite data type.
USER DEFINED DATA TYPE
The data type created by the user to perform some task is known as user defined data
type.
class is a composite data type.
WRAPER CLASS
The class that converts primitive or fundamental data types into its
corresponding object types and vice-versa is known as wrapper class.
short Short
int Integer
long Long
double Double
AUTO BOXING
It converts primitive data type into its corresponding wrapper class object.
UNBOXING
It converts wrapper class into its corresponding primitive data type.
CHAPTER
LIBRARY CLASSES
DATA TYPE
Java has some functions which converts numeric data into string and vice -versa.
toString( )
parse
valueOf( )
CHARACTER FUNCTIONS
ENCAPSULATION &
INHERITANCE
INTRODUCTION
VISIBILITY MODIFIERS
In Java, the visibility modifiers (access specifiers) allows the user to control which members of a class
can be accessed by others or other classes can have access to. Java specifies different levels of
access to each of these specifiers. It is through these access specifiers that Java implements data
abstraction. The most commonly used are public and private. public-the member is accessible from
all classes inside or outside the package. private-the member is accessible only within its own class.
protected-the member is accessible by any subclasses or other classes in the same package or any
other package. This is normally used with inherited classes. default(friendly)-the member is accessible
by any subclasses or other classes in the same package. It will not be accessible outside the package
even if they are subclasses.
CHAPTER
ENCAPSULATION &
INHERITANCE
ENCAPSULATION &
INHERITANCE
Subclass in
in the same other Everywhere/ Outside
Modifiers Class
package packages package
private Yes No No No
Yes (with related
protected Yes Yes Yes
subclasses only)
DATA HIDING.
You might be wondering, how does specifying the access permissions of the class
implement encapsulation?
That's because the aim behind the concept to encapsulation was to hide the details of the
implementation procedure. The user should only know how to use the program not the
intricacies. This is why encapsulation is also known as data hiding.
A function which is used to access private data members is called an Accessor ar Getter
method.
A function which is used to change the value of a private data member is called a Mutator
or Setter method. Such methods keep the instance variables safe and secure from the
outside interference and implement encapsulation.
CHAPTER
ENCAPSULATION &
INHERITANCE
The goal of object oriented programming is to make the data secure. In that interest,
accessor and mutator play a very important role. If the variables of a class are not private
then they can be accessed and modified from anywhere. This is a very dangerous
scenario as it can lead to massive data loss. To prevent such a thing, the access to data is
kept limited and setter or getter methods are used to access the data. These methods
help the user to alter the data in a controlled manner. Hence, only the user can change
the data.
ADVANTAGES OF ENCAPSULATION
Flexibility: It gives more versatility to user as the implementation details are hidden. So,
the logic can be changed easily. In other words encapsulation helps us create flexible
codes that can be easily changed and maintained. Program
Logic is hidden: When the user uses an encapsulated code, he/she is only capable of
updating and viewing the data through setter and getter methods. The working of
these methods are completely hidden from the user.
Data Hiding: Encapsulation enables the programmer to hide valuable data by making it
private.
Programmers become more powerful: The programmer decides what to show and
what not to. If a programmer decides not to allow any setter methods then the user can
only view the data without any modification
INHERITANCE
Inheritance is the process by which objects of one class acquire/inherits the properties of
objects of another class.
For example, let us consider a class Plant. Three classes: class Herb, class Shrub and class
Tree are all parts of the class Plant and have a few common properties. So the uppermost
class is called the Base class and classes that acquire its properties are called Derived
classes. Each derived class shares common characteristics with the base class and will
have the combined features of both the classes.
CHAPTER
ENCAPSULATION &
INHERITANCE
INHERITANCE
Here, class Tomato, Mustard, Sunflower are the derived classes of the class Herb.
Class Herb is the base class of the class Tomato, Mustard, Sunflower.
Inheritance provides the facility of reusability. Which means that additional
features can be added to an already existing class without modifying it. This is
possible when a new class is derived from the existing one.
The new class will have the combined features of both the classes
Each derived class should define only those features that are unique to it.
NOTE:
Inheritance is the process by which objects of one class acquire the properties of the
objects of another class.
Inheritance provides the facility of reusability.
CHAPTER
STRINGS
CHARACTER
STRING
STRING CONSTANT
STRING VARIABLE
STRINGS
STRING INPUT
To input a string from the input device string class is used. The string class creates a string
of fixed length which can not be altered or modified.
The following functions can be used to input a string :
readLine()
nextLine()
next()
readLine()
The readLine() method of Console class in Java is used to read a single line
of text from the console.
syntax: string_variable/string_object = input_buffer_variable.readLine( );
nextLine()
This function is used to input a line of text or string from input using scanner
class.
syntax: string_variable/string_object = scanner_object.nextLine( );
next()
This is used to input a string without any space from the input using
Scanner class
syntax: string_variable/string_object = scanner_object.next( );
CHAPTER
STRINGS
ARRAYS
INTRODUCTION
TYPES OF ARRAY
ARRAYS
ARRAYS
This is the simplest form of the array. They are also known as 1-D arrays.
For example: int A[10] represents the array of the integer data type that can hold 10
values. This memory location of A[10] will be A[0], A[1], A[2], . . A[9] .
These are also known as 2-dimensional array. A 2-D array will be a table, or a matrix.
It would look like this:
or
Sometimes you may need to search an element in an array. Then you can use different
searching techniques Searching an array involves traversing the array and searching for
a particular element. There are two main methods for searching Linear Search or
Sequential Search for unsorted arrays Binary Search for sorted arrays
BINARY SEARCH
You must be aware of the phrase 'Divide and Conquer'. Binary search uses this very
technique to find the given item in the array. Binary search technique is quite efficient but
it works on sorted arrays only, either ascending or descending. Theoretically, this
technique divides the array to form two arrays of equal size. It then looks for the element
in the first array, if found, returns the value, otherwise repeats the process on the next
array.
CHAPTER
ARRAYS
BUBBLE SORT
This technique is mostly used for sorting the elements in a single dimensional array. In
bubble sort. as the elements are sequentially scanned and sorted (in
ascending/descending order), they gradually move to their proper location in the array like
bubbles. During each iteration, the pair of consecutive elements are compared and
interchanged if out of order. This sorting process continues until the last two elements of
the array are compared and swapped if out of order.It is an easy technique but it
consumes a lot of time when the number of exchanges are more. If there are 6 elements,
then five iteration are required. If there are 9 elements, then eight iterations are required.
The Bubble sort comes to an end when it examines the entire array and no more swapping
is needed.
SELECTION SORT
It is a combination of searching and sorting. Assume that we wish to sort the array in
increasing order. During each pass, the unsorted element with the smallest value is moved
to its proper position in the array (for ascending order). If an array has n elements, then the
number of passes will be n-1. In selection sort, the inner loop sends the next smallest value
and the outer loop swaps that values into proper index positions.
SAVIOUR OF
COMPUTERS
50 MOST
IMPORTANT
PROGRAMS
CHAPTER
CONDITIONAL
CONSTRUCTS
import java.util.Scanner;
public class NumberAnalysis
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
int greatestNumber = a;
if ((a == b) && (b == c)) {
System.out.println("Entered numbers are equal.");
}
else {
if (b > greatestNumber) {
greatestNumber = b;
}
if (c > greatestNumber) {
greatestNumber = c;
}
System.out.println("The greatest number is " + greatestNumber);
if ((a >= 0) && (b >= 0) && (c >= 0)) {
System.out.println("Entered numbers are all positive numbers.");
}
else if((a < 0) && (b < 0) && (c < 0)) {
System.out.println("Entered numbers are all negative numbers.");
}
else {
System.out.println("Entered numbers are mixed numbers.");
}
}
}
}
CHAPTER
CONDITIONAL
CONSTRUCTS
iimport java.util.Scanner;
public class EquableTriangle
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the 3 sides of the triangle.");
System.out.print("Enter the first side: ");
double a = in.nextDouble();
System.out.print("Enter the second side: ");
double b = in.nextDouble();
System.out.print("Enter the third side: ");
double c = in.nextDouble();
double p = a + b + c;
double s = p / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
if (area == p)
System.out.println("Entered triangle is equable.");
elseSystem.out.println("Entered triangle is not equable.");
}
}
}
CHAPTER
CONDITIONAL
CONSTRUCTS
import java.util.Scanner;
public class SpecialNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a 2 digit number: ");
int orgNum = in.nextInt();
if (orgNum < 10 || orgNum > 99) {
System.out.println("Invalid input. Entered number is not a 2 digit
number");
System.exit(0);
}
int num = orgNum;
int digit1 = num % 10;
int digit2 = num / 10;
num /= 10;
int digitSum = digit1 + digit2;
int digitProduct = digit1 * digit2;
int grandSum = digitSum + digitProduct;
if (grandSum == orgNum)
System.out.println("Special 2-digit number");
elseSystem.out.println("Not a special 2-digit number");
}
}
CHAPTER
CONDITIONAL
CONSTRUCTS
import java.util.Scanner;
public class BusFare
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter distance travelled: ");
int dist = in.nextInt();
int fare = 0;
if (dist <= 0)
fare = 0;
else if (dist <= 10)
fare = 80;
else if (dist <= 20)
fare = 80 + (dist - 10) * 6;
else if (dist <= 30)
fare = 80 + 60 + (dist - 20) * 5;
else if (dist > 30)
fare = 80 + 60 + 50 + (dist - 30) * 4;
System.out.println("Fare = " + fare);
}
}
CHAPTER
CONDITIONAL
CONSTRUCTS
import java.util.Scanner;
public class CourierCompany
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter weight of parcel: ");
double wt = in.nextDouble();
System.out.print("Enter type of booking: ");
char type = in.next().charAt(0);
double charge = 0;
if (wt <= 0)
charge = 0;
else if (wt <= 100 && type == 'O')
charge = 80;
else if (wt <= 100 && type == 'E')
charge = 100;
else if (wt <= 500 && type == 'O')
charge = 150;
else if (wt <= 500 && type == 'E')
charge = 200;
else if (wt <= 1000 && type == 'O')
charge = 210;
else if (wt <= 1000 && type == 'E')
charge = 250;
else if (wt > 1000 && type == 'O')
charge = 250;
else if (wt > 1000 && type == 'E')
charge = 300;
System.out.println("Parcel charges = " + charge);
}
}
CHAPTER
CONDITIONAL
CONSTRUCTS
import java.util.Scanner;
public class Series
{
public void computeSeriesSum() {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
int a = 1, b = 1;
int sum = a + b;
for (int i = 3; i <= n; i++) {
int term = a + b;
sum += term;
a = b;
b = term;
}
System.out.println("Sum=" + sum);
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class Series
{
public void computeSeriesSum() {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
int sum = 0;
for (int i = 1, j = 2; i <= n; i++, j = j + 2) {
if (i % 2 == 0) {
sum -= j;
}
else {
sum += j;
}
}
System.out.println("Sum=" + sum);
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class Series
{
public void computeSeriesSum() {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
int a = 1, b = 1;
int sum = a + b;
for (int i = 3; i <= n; i++) {
int term = a + b;
sum += term;
a = b;
b = term;
}
System.out.println("Sum=" + sum);
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class Series
{
public void computeSeriesSum() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
double sum = 0;
for (int i = 2; i <= 20; i = i + 3) {
sum += a / (double)i;
}
System.out.println("Sum=" + sum);
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class Coprime
{
public void checkCoprime() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter b: ");
int b = in.nextInt();
int hcf = 1;
for (int i = 1; i <= a && i <= b; i++) {
if (a % i == 0 && b % i == 0)
hcf = i;
}
if (hcf == 1)
System.out.println(a + " and " + b + " are co-prime");
elseSystem.out.println(a + " and " + b + " are not co-prime");
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class PronicNumber
{
public void pronicCheck() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();
boolean isPronic = false;
for (int i = 1; i <= num - 1; i++) {
if (i * (i + 1) == num) {
isPronic = true;
break;
}
}
if (isPronic)
System.out.println(num + " is a pronic number");
elseSystem.out.println(num + " is not a pronic number");
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.*;
public class HarshadNumber
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();
int x = num, rem = 0, sum = 0;
while(x > 0) {
rem = x % 10;
sum = sum + rem;
x = x / 10;
}
int r = num % sum;
if(r == 0)
System.out.println(num + " is a Harshad Number.");
elseSystem.out.println(num + " is not a Harshad Number.");
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class PrimeCheck
{
public void primeCheck() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(num + " is a prime number");
}
else {
for (int newNum = num + 1; newNum <= Integer.MAX_VALUE; newNum++)
{
isPrime = true;
for (int i = 2; i <= newNum / 2; i++) {
if (newNum % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println("Next prime number = " + newNum);
break;
}
}
}
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class FibonacciNDigitSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Fibonacci Series");
System.out.println("2. Sum of digits");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
int a = 0, b = 1;
System.out.print(a + " " + b);
/*
* i is starting from 3 below
* instead of 1 because we have
* already printed 2 terms of
* the series. The for loop will
* print the series from third
* term onwards.
*/
for (int i = 3; i <= 10; i++) {
int term = a + b;
System.out.print(" " + term);
a = b;
b = term;
}
break;
CHAPTER
ITERATIVE
CONSTRUCTS
case 2:
System.out.print("Enter number: ");
int num = in.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of Digits " + " = " + sum);
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class FibonacciNDigitSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Fibonacci Series");
System.out.println("2. Sum of digits");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
int a = 0, b = 1;
System.out.print(a + " " + b);
/*
* i is starting from 3 below
* instead of 1 because we have
* already printed 2 terms of
* the series. The for loop will
* print the series from third
* term onwards.
*/
for (int i = 3; i <= 10; i++) {
int term = a + b;
System.out.print(" " + term);
a = b;
b = term;
}
break;
CHAPTER
ITERATIVE
CONSTRUCTS
case 2:
System.out.print("Enter number: ");
int num = in.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of Digits " + " = " + sum);
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
CHAPTER
ITERATIVE
CONSTRUCTS
import java.util.Scanner;
public class SeriesSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Sum of x^1 - x^2 + .... - x^20");
System.out.println("2. Sum of 1/a^2 + 1/a^4 + .... to n terms");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
System.out.print("Enter the value of x: ");
int x = in.nextInt();
long sum1 = 0;
for (int i = 1; i <= 20; i++) {
int term = (int)Math.pow(x, i);
if (i % 2 == 0)
sum1 -= term;
else
sum1 += term;
}
System.out.println("Sum = " + sum1);
break;
CHAPTER
ITERATIVE
CONSTRUCTS
case 2:
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
System.out.print("Enter the value of a: ");
int a = in.nextInt();
int c = 2;
double sum2 = 0;
for(int i = 1; i <= n; i++) {
sum2 += (1/Math.pow(a, c));
c += 2;
}
System.out.println("Sum = " + sum2);
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
CHAPTER
NESTED
LOOPS
import java.util.Scanner;
public class DigitSort
{
public void sortDigits() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number having 3 or more digits: ");
int OrgNum = in.nextInt();
for (int i = 0; i <= 9; i++) {
int num = OrgNum;
int c = 0;
while (num != 0) {
if (num % 10 == i)
c++;
num /= 10;
}
for (int j = 1; j <= c; j++) {
System.out.print(i + ", ");
}
}
System.out.println();
}
}
CHAPTER
NESTED
LOOPS
import java.util.Scanner;
public class MultipleHarshad
{
public void checkMultipleHarshad() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number to check: ");
int num = in.nextInt();
int dividend = num;
int divisor;
int count = 0;
while (dividend > 1) {
divisor=0;
int t = dividend;
while (t > 0) {
int d = t % 10;
divisor += d;
t /= 10;
}
if (dividend % divisor == 0 && divisor != 1) {
dividend = dividend / divisor;
count++;
}
else {
break;
}
}
if (dividend == 1 && count > 1)
System.out.println(num + " is Multiple Harshad Number");
else
System.out.println(num + " is not Multiple Harshad Number");
}
}
CHAPTER
NESTED
LOOPS
import java.util.Scanner;
public class MultipleHarshad
{
public void checkMultipleHarshad() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number to check: ");
int num = in.nextInt();
int dividend = num;
int divisor;
int count = 0;
while (dividend > 1) {
divisor=0;
int t = dividend;
while (t > 0) {
int d = t % 10;
divisor += d;
t /= 10;
}
if (dividend % divisor == 0 && divisor != 1) {
dividend = dividend / divisor;
count++;
}
else {
break;
}
}
if (dividend == 1 && count > 1)
System.out.println(num + " is Multiple Harshad Number");
else
System.out.println(num + " is not Multiple Harshad Number");
}
}
CHAPTER
NESTED
LOOPS
NESTED
LOOPS
NESTED
LOOPS
15 14 13 12 11
10 9 8 7
654
32
1
NESTED
LOOPS
(D)
1
10
101
1010
10101
NESTED
LOOPS
*
* #
* #*
* #*#
* #*#*
NESTED
LOOPS
1
23
456
7 8 9 10
11 12 13 14 15
import java.util.Scanner;
public class Pattern
{
public void choosePattern() {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Floyd's triangle");
System.out.println("Type 2 for an ICSE pattern");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
CHAPTER
NESTED
LOOPS
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
CHAPTER
CHARACTER
MANIPULATION
import java.util.Scanner;
public class Integer2Letter
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter integer: ");
int n = in.nextInt();
if (n > 0 && n < 27) {
char ch = (char)(n + 64);
System.out.println("Corresponding letter = " + ch);
}
else {
System.out.println("Please enter a number in 1 to 26 range");
}
}
}
CHAPTER
CHARACTER
MANIPULATION
import java.util.Scanner;
public class ASCIIReverse
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a letter: ");
char l = in.next().charAt(0);
int a = (int)l;
System.out.println("ASCII Code = " + a);
int r = 0;
while (a > 0) {
int digit = a % 10;
r = r * 10 + digit;
a /= 10;
}
System.out.println("Reversed Code = " + r);
System.out.println("Equivalent character = " + (char)r);
}
}
CHAPTER
CHARACTER
MANIPULATION
import java.util.Scanner;
public class MenuUpLowCase
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter '1' to display upper case letters");
System.out.println("Enter '2' to display lower case letters");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
for (int i = 65; i <= 69; i++)
System.out.println((char)i);
break;
case 2:
for (int i = 118; i <= 122; i++)
System.out.println((char)i);
break;
default:
break;
}
}
}
CHAPTER
CHARACTER
MANIPULATION
CHARACTER
MANIPULATION
ARRAYS
import java.util.Scanner;
public class SDAOddEvenSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[ ] = new int[20];
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
int oddSum = 0, evenSum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0)
evenSum += arr[i];
elseoddSum += arr[i];
}
System.out.println("Sum of Odd numbers = " + oddSum);
System.out.println("Sum of Even numbers = " + evenSum);
}
}
CHAPTER
ARRAYS
import java.util.Scanner;
public class SDAF2C
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
System.out.println("Enter 20 temperatures in degree Fahrenheit");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextDouble();
}
System.out.println("Temperatures in degree Celsius");
for (int i = 0; i < arr.length; i++) {
double tc = 5 * ((arr[i] - 32) / 9);
System.out.println(tc);
}
}
}
CHAPTER
ARRAYS
import java.util.Scanner;
public class SDAGrades
{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 100;
Scanner in = new Scanner(System.in);
int rollNo[ ]= new int[TOTAL_STUDENTS];
String name[ ]= new String[TOTAL_STUDENTS];
int s1[ ]= new int[TOTAL_STUDENTS];
int s2[ ]= new int[TOTAL_STUDENTS];
int s3[ ]= new int[TOTAL_STUDENTS];
int s4[ ]= new int[TOTAL_STUDENTS];
int s5[ ]= new int[TOTAL_STUDENTS];
int s6[ ]= new int[TOTAL_STUDENTS];
double p[ ] = new double[TOTAL_STUDENTS];
char g[ ] = new char[TOTAL_STUDENTS];
for (int i = 0; i < TOTAL_STUDENTS; i++)
{
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
System.out.print("Subject 4 Marks: ");
s4[i] = in.nextInt();
System.out.print("Subject 5 Marks: ");
s5[i] = in.nextInt();
System.out.print("Subject 6 Marks: ");
s6[i] = in.nextInt();
CHAPTER
ARRAYS
import java.util.Scanner;
public class SDAGrades
{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 100;
Scanner in = new Scanner(System.in);
int rollNo[ ]= new int[TOTAL_STUDENTS];
String name[ ]= new String[TOTAL_STUDENTS];
int s1[ ]= new int[TOTAL_STUDENTS];
int s2[ ]= new int[TOTAL_STUDENTS];
int s3[ ]= new int[TOTAL_STUDENTS];
int s4[ ]= new int[TOTAL_STUDENTS];
int s5[ ]= new int[TOTAL_STUDENTS];
int s6[ ]= new int[TOTAL_STUDENTS];
double p[ ] = new double[TOTAL_STUDENTS];
char g[ ] = new char[TOTAL_STUDENTS];
for (int i = 0; i < TOTAL_STUDENTS; i++)
{
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
System.out.print("Subject 4 Marks: ");
s4[i] = in.nextInt();
System.out.print("Subject 5 Marks: ");
s5[i] = in.nextInt();
System.out.print("Subject 6 Marks: ");
s6[i] = in.nextInt();
CHAPTER
ARRAYS
ARRAYS
import java.util.Scanner;
public class SDABubbleSort
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
//Sort first half in ascending orderfor (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
//Sort second half in descending orderfor (int i = 0; i < arr.length / 2 - 1;
i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
//Print the final sorted arraySystem.out.println("\nSorted Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
CHAPTER
ARRAYS
import java.util.Scanner;
public class SDAEvenOdd
{
public static void main(String args[]) {
final int NUM_COUNT = 20;
Scanner in = new Scanner(System.in);
int i = 0;
int arr[] = new int[NUM_COUNT];
int even[] = new int[NUM_COUNT];
int odd[] = new int[NUM_COUNT];
System.out.println("Enter 20 numbers:");
for (i = 0; i < NUM_COUNT; i++) {
arr[i] = in.nextInt();
}
int eIdx = 0, oIdx = 0;
for (i = 0; i < NUM_COUNT; i++) {
if (arr[i] % 2 == 0)
even[eIdx++] = arr[i];
elseodd[oIdx++] = arr[i];
}
System.out.println("Even Numbers:");
for (i = 0; i < eIdx; i++) {
System.out.print(even[i] + " ");
}
System.out.println("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
System.out.print(odd[i] + " ");
}
}
}
CHAPTER
ARRAYS
import java.util.Scanner;
public class DDAEvenOdd
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N[][] = new int[4][4];
long evenSum = 0, oddProd = 1;
System.out.println("Enter the elements of 4x4 DDA: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
N[i][j] = in.nextInt();
if (N[i][j] % 2 == 0)
evenSum += N[i][j];
else
OddProd *= N[i][j];
}
}
System.out.println("Sum of all even numbers = " + evenSum);
System.out.println("Product of all odd numbers = " + oddProd);
}
}
CHAPTER
STRINGS
import java.util.Scanner;
public class WordsNLetters
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
int wCount = 0, lCount = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ')
wCount++;
elselCount++;
}
System.out.println("No. of words = " + wCount);
System.out.println("No. of letters = " + lCount);
}
}
CHAPTER
STRINGS
import java.util.Scanner;
public class VowelRemoval
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word or sentence:");
String str = in.nextLine();
int len = str.length();
String newStr = "";
for (int i = 0; i < len; i++) {
char ch = Character.toUpperCase(str.charAt(i));
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
continue;
}
newStr = newStr + ch;
}
System.out.println("String with vowels removed");
System.out.println(newStr);
}
}
CHAPTER
STRINGS
import java.util.Scanner;
public class NameInitials
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a name of 3 or more words:");
String str = in.nextLine();
int len = str.length();
System.out.print(str.charAt(0) + " ");
for (int i = 1; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
char ch2 = str.charAt(i + 1);
System.out.print(ch2 + " ");
}
}
}
}
CHAPTER
STRINGS
import java.util.Scanner;
public class SurnameFirst
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a name of 3 words:");
String name = in.nextLine();
Int lastSpaceIdx = name.lastIndexOf(' ');
String surname = name.substring(lastSpaceIdx + 1);
String initialName = name.substring(0, lastSpaceIdx);
System.out.println(surname + " " + initialName);
}
}
CHAPTER
STRINGS
import java.util.Scanner;
public class LongestWord
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word or sentence:");
String str = in.nextLine();
str += " "; //Add space at end of stringString word = "", lWord = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
if (word.length() > lWord.length())
lWord = word;
word = "";
}
else {
word += ch;
}
}
System.out.println("The longest word: " + lWord +
": The length of the word: " + lWord.length());
}
}
CHAPTER
STRINGS
STRINGS
import java.util.Scanner;
public class VowelReplace
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string in uppercase:");
String str = in.nextLine();
String newStr = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
newStr = newStr + '*';
}
else {
newStr = newStr + ch;
}
}
System.out.println(newStr);
}
}
CHAPTER
STRINGS
import java.util.Scanner;
public class FrameWord
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
String word = "" + str.charAt(0);
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ')
word += str.charAt(i + 1);
}
System.out.println(word);
}
}
SAVIOUR OF
COMPUTERS
LAST 10 YEARS
SOLUTIONS
SAVIOUR OF COMPUTERS
2013 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
Answer
Precedence of operators refers to the order in which the operators are applied to the operands in an
expression.
Answer
Literals are data items that are fixed data values. Java provides different types of literals like:
Integer Literals
Floating-Point Literals
Boolean Literals
Character Literals
String Literals
null Literal
Answer
Inheritance
Abstraction
Answer
Constructor has the same name as class name whereas function should have a different name than
class name.
(e) What are the types of casting shown by the following examples?
double x = 15.2;
int y = (int) x;
int x = 12;
long y = x;
Answer
2013 COMPUTER
APPLICATION PAPER
SECTION A
Question 2
Answer
(b) What is the difference between a break statement and a continue statement when they
occur in a loop?
Answer
When the break statement gets executed, it terminates its loop completely and control reaches to
the statement immediately following the loop. The continue statement terminates only the current
iteration of the loop by skipping rest of the statements in the body of the loop.
(c) Write statements to show how finding the length of a character array char[] differs from
finding the length of a String object str.
Answer
Answer
1. void
2. this
Answer
An exception is an abnormal condition that arises in a code sequence at run time. Exceptions indicate
to a calling method that an abnormal condition has occurred.
SAVIOUR OF COMPUTERS
2013 COMPUTER
APPLICATION PAPER
Question 3
Answer
(b) State the values stored in the variables str1 and str2:
String s1 = "good";
String s2 = "world matters";
String str1 = s2.substring(5).replace('t', 'n');
String str2 = s1.concat(str1);
Answer
Value stored in str1 is " manners" and str2 is "good manners". (Note that str1 begins with a
space.)
Explanation
s2.substring(5) gives a substring from index 5 till the end of the string which is " matters".
The replace method replaces all 't' in " matters" with 'n' giving " manners". s1.concat(str1)
joins together "good" and " manners" so "good manners" is stored in str2.
Answer
(d) Rewrite the following program segment using the if..else statement:
Answer
(e) How many times will the following loop execute? What value will be returned?
int x = 2, y = 50;
do{
++x;
y -= x++;
}while(x <= 10);
return y;
Answer
The loop will run 5 times and the value returned is 15.
SAVIOUR OF COMPUTERS
2013 COMPUTER
APPLICATION PAPER
(f) What is the data type that the following library functions return?
1. isWhitespace(char ch)
2. Math.random()
Answer
1. boolean
2. double
ut + 1/2 ft^2
Answer
u * t + 1 / 2.0 * f * t * t
(h) If int n[] = {1, 2, 3, 5, 7, 9, 13, 16}, what are the values of x and y?
x = Math.pow(n[4], n[2]);
y = Math.sqrt(n[5] + n[7]);
Answer
x = Math.pow(n[4], n[2]);
⇒ x = Math.pow(7, 3);
⇒ x = 343.0;
y = Math.sqrt(n[5] + n[7]);
⇒ y = Math.sqrt(9 + 16);
⇒ y = Math.sqrt(25);
⇒ y = 5.0;
(i) What is the final value of ctr when the iteration process given below, executes?
int ctr = 0;
for(int i = 1; i <= 5; i++)
for(int j = 1; j <= 5; j += 2)
++ctr;
Answer
The outer loop executes 5 times. For each iteration of outer loop, the inner loop executes 3
times. So the statement ++ctr; is executed a total of 5 x 3 = 15 times.
Answer
1. nextInt()
2. nextLine()
SAVIOUR OF COMPUTERS
2013 COMPUTER
APPLICATION PAPER
SECTION B
Question 4
Define a class named FruitJuice with the following description:
String flavour. stores the flavour of the juice (e.g., orange, apple, etc.)
String pack_type. stores the type of packaging (e.g., tera-pack, PET bottle, etc.)
int pack_size. stores package size (e.g., 200 mL, 400 mL, etc.)
void input(). to input and store the product code, flavour, pack type, pack size and product price
void display(). to display the product code, flavour, pack type, pack size and product price
SAVIOUR OF COMPUTERS
2013 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
2013 COMPUTER
APPLICATION PAPER
Question 5
The International Standard Book Number (ISBN) is a unique numeric book identifier
which is printed on every book. The ISBN is based upon a 10-digit code.
Example:
For an ISBN 1401601499
Sum = 1 × 1 + 2 × 4 + 3 × 0 + 4 × 1 + 5 × 6 + 6 × 0 + 7 × 1 + 8 × 4 + 9 × 9 + 10 × 9 = 253 which is
divisible by 11.
Answer
import java.util.Scanner;
if (count != 10) {
System.out.println("Illegal ISBN");
}
else if (sum % 11 == 0) {
System.out.println("Legal ISBN");
}
else {
System.out.println("Illegal ISBN");
}
}
}
SAVIOUR OF COMPUTERS
2013 COMPUTER
APPLICATION PAPER
Question 6
Write a program that encodes a word into Piglatin. To translate word into Piglatin
word, convert the word into uppercase and then place the first vowel of the
original word as the start of the new word along with the remaining alphabets.
The alphabets present before the vowel being shifted towards the end followed by
"AY".
Answer
import java.util.Scanner;
word=word.toUpperCase();
String piglatin="";
int flag=0;
if(flag == 0)
{
piglatin = word + "AY";
}
System.out.println(word + " in Piglatin format is " + piglatin);
}
}
SAVIOUR OF COMPUTERS
2013 COMPUTER
APPLICATION PAPER
Question 7
Write a program to input 10 integer elements in an array and sort
them in descending order using bubble sort technique.
Answer
import java.util.Scanner;
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
SAVIOUR OF COMPUTERS
2013 COMPUTER
APPLICATION PAPER
Question 8
Design a class to overload a function series( ) as follows:
1. double series(double n) with one double argument and returns
the sum of the series sum = (1/1) + (1/2) + (1/3) + .......... + (1/n)
2. double series(double a, double n) with two double arguments
and returns the sum of the series. sum = (1/a^2) + (4/a^3)+
(7/a^8)+ (10/a^11) + .......... to n terms
Answer
2013 COMPUTER
APPLICATION PAPER
Question 9
Using the switch statement, write a menu driven program:
1. To check and display whether a number input by the user is a composite
number or not. A number is said to be composite, if it has one or more than one
factors excluding 1 and the number itself. Example: 4, 6, 8, 9...
2. To find the smallest digit of an integer that is input: Sample input: 6524 Sample
output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.
Answer
import java.util.Scanner;
switch (ch) {
case 1:
System.out.print("Enter Number: ");
int n = in.nextInt();
int c = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0)
c++;
}
if (c > 2)
System.out.println("Composite Number");
elseSystem.out.println("Not a Composite Number");
break;
case 2:
System.out.print("Enter Number: ");
int num = in.nextInt();
int s = 10;
while (num != 0) {
int d = num % 10;
if (d < s)
s = d;
num /= 10;
}
System.out.println("Smallest digit is " + s);
break;
default:
System.out.println("Wrong choice");
}
}
}
SAVIOUR OF COMPUTERS
2014 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
Answer
(b) What is meant by a package ? Name any two java Application Programming
Interface packages.
Answer
A package is a named collection of Java classes that are grouped on the basis of their
functionality. Two java Application Programming Interface packages are:
1. java.util
2. java.io
Answer
1. long
2. char
(d) State one difference between the floating point literals float and double.
Answer
2014 COMPUTER
APPLICATION PAPER
(e) Find the errors in the given program segment and re-write the statements correctly to assign
values to an integer array.
int a = new int (5);
for (int i=0; i<=5; i++) a[i]=i;
Answer
Corrected Code:
int a[] = new int[5];
for (int i = 0; i < 5; i++)
a[i] = i;
Question 2
(a) Operators with higher precedence are evaluated before operators with relatively lower
precedence. Arrange the operators given below in order of higher precedence to lower
precedence:
1. &&
2. %
3. >=
4. ++
Answer
++
%
>=
&&
(b) Identify the statements listed below as assignment, increment, method invocation or object
creation statements.
1. System.out.println("Java");
2. costPrice = 457.50;
3. Car hybrid = new Car();
4. petrolPrice++;
Answer
1. Method Invocation
2. Assignment
3. Object Creation
4. Increment
(c) Give two differences between the switch statement and the if-else statement
switch if-else
2014 COMPUTER
APPLICATION PAPER
Answer
A loop which continues iterating indefinitely and never stops is termed as infinite
loop. Below is an example of infinite loop:
for (;;)
System.out.println("Infinite Loop");
Answer
A constructor is a member method that is written with the same name as the class
name and is used to initialize the data members or instance variables. A constructor
does not have a return type. It is invoked at the time of creating any object of the
class.
Question 3
(a) List the variables from those given below that are composite data types:
1. static int x;
2. arr[i]=10;
3. obj.display();
4. boolean b;
5. private char chr;
6. String str;
Answer
Answer
The output of the above code is:
grinds
WHEAT
Explanation:
1. str1.substring(0,2) returns "gr". str2.substring(1) returns "inds". Due to concat both
are joined together to give the output as "grinds".
2. str1.substring(2) returns "eat". It is converted to uppercase and joined to "WH" to
give the output as "WHEAT".
SAVIOUR OF COMPUTERS
2014 COMPUTER
APPLICATION PAPER
(c) What are the final values stored in variable x and y below?
double a = -6.35;
double b = 14.74;
double x = Math.abs(Math.ceil(a));
double y = Math.rint(Math.max(a,b));
Answer
Explanation:
1. Math.ceil(a) gives -6.0 and Math.abs(-6.0) is 6.0.
2. Math.max(a,b) gives 14.74 and Math.rint(14.74) gives 15.0.
(d) Rewrite the following program segment using if-else statements instead of the
ternary operator:
String grade = (marks>=90)?"A": (marks>=80)? "B": "C";
Answer
String grade;
if (marks >= 90)
grade = "A";
else if (marks >= 80)
grade = "B";
elsegrade = "C";
2014 COMPUTER
APPLICATION PAPER
Answer
1. int
2. boolean
(g) State the value of characteristic and mantissa when the following code
is executed:
String s = "4.3756";
int n = s.indexOf('.');
int characteristic=Integer.parseInt(s.substring(0,n));
int mantissa=Integer.valueOf(s.substring(n+1));
Answer
Answer
2014 COMPUTER
APPLICATION PAPER
1. Name the variables for which each object of the class will have its
own distinct copy.
2. Name the variables that are common to all objects of the class.
Answer
1. a, b
2. x, y
(j) What will be the output when the following code segments are
executed?
(i)
String s = "1001";
int x = Integer.valueOf(s);
double y = Double.valueOf(s);
System.out.println("x="+x);
System.out.println("y="+y);
(ii)
System.out.println("The king said \"Begin at the beginning!\" t
Answer
2014 COMPUTER
APPLICATION PAPER
SECTION B
Question 4
Define a class named movieMagic with the following description:
void display(). To display the title of the movie and a message based on the rating as
per the table given below
Ratings table
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
public movieMagic() {
year = 0;
title = "";
rating = 0.0f;
}
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Title of Movie: ");
title = in.nextLine();
System.out.print("Enter Year of Movie: ");
year = in.nextInt();
System.out.print("Enter Rating of Movie: ");
rating = in.nextFloat();
}
SAVIOUR OF COMPUTERS
2014 COMPUTER
APPLICATION PAPER
System.out.println(title);
System.out.println(message);
}
Question 5
A special two-digit number is such that when the sum of its digits is
added to the product of its digits, the result is equal to the original
two-digit number.
2014 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
}
if (count != 2)
System.out.println("Invalid input, please enter a 2-digit number");
else if ((digitSum + digitProduct) == orgNum)
System.out.println("Special 2-digit number");
elseSystem.out.println("Not a special 2-digit number");
}
}
Question 6
Write a program to assign a full path and file name as given below. Using
library functions, extract and output the file path, file name and file
extension separately as shown.
Input
C:\Users\admin\Pictures\flower.jpg
Output
Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg
SAVIOUR OF COMPUTERS
2014 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
Question 7
Design a class to overload a function area( ) as follows:
1. double area (double a, double b, double c) with three double
arguments, returns the area of a scalene triangle using the
formula: area = √(s(s-a)(s-b)(s-c)) where s = (a+b+c) / 2
2. double area (int a, int b, int height) with three integer arguments,
returns the area of a trapezium using the formula: area =
(1/2)height(a + b)
3. double area (double diagonal1, double diagonal2) with two double
arguments, returns the area of a rhombus using the formula: area
= 1/2(diagonal1 x diagonal2)
SAVIOUR OF COMPUTERS
2014 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
Question 8
Using the switch statement, write a menu driven program to
calculate the maturity amount of a Bank Deposit.
The user is given the following options:
1. Term Deposit
2. Recurring Deposit
For option 1, accept principal (P), rate of interest(r) and time period in
years(n). Calculate and output the maturity amount(A) receivable
using the formula:
A = P[1 + r / 100]^n
For option 2, accept Monthly Installment (P), rate of interest (r) and
time period in months (n). Calculate and output the maturity amount
(A) receivable using the formula:
A = P x n + P x (n(n+1) / 2) x r / 100 x 1 / 12
2014 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
switch (ch) {
case 1:
System.out.print("Enter Principal: ");
p = in.nextDouble();
System.out.print("Enter Interest Rate: ");
r = in.nextDouble();
System.out.print("Enter time in years: ");
n = in.nextInt();
a = p * Math.pow(1 + r / 100, n);
System.out.println("Maturity amount = " + a);
break;
case 2:
System.out.print("Enter Monthly Installment: ");
p = in.nextDouble();
System.out.print("Enter Interest Rate: ");
r = in.nextDouble();
System.out.print("Enter time in months: ");
n = in.nextInt();
a = p * n + p * ((n * (n + 1)) / 2) * (r / 100) * (1 / 12.0);
System.out.println("Maturity amount = " + a);
break;
default:
System.out.println("Invalid choice");
}
}
}
SAVIOUR OF COMPUTERS
2014 COMPUTER
APPLICATION PAPER
Question 9
Write a program to accept the year of graduation from school as an integer
value from the user. Using the binary search technique on the sorted array of
integers given below, output the message "Record exists" if the value input is
located in the array. If not, output the message "Record does not exist".
Sample Input:
n[0]. 1982
n[1]. 1987
n[2]. 1993
n[3]. 1996
n[4]. 1999
n[5]. 2003
n[6]. 2006
n[7]. 2007
n[8]. 2009
n[9]. 2010
Answer
import java.util.Scanner;
if (idx == -1)
System.out.println("Record does not exist");
elseSystem.out.println("Record exists");
}
}
SAVIOUR OF COMPUTERS
2015 COMPUTER
APPLICATION PAPER
SECTION A
(a) What are the default values of the primitive data type int and
float?
Answer
Default value of int is 0 and float is 0.0f.
Answer
1. Encapsulation
2. Inheritance
Answer
Answer
1. Real Literal
2. Character Literal
3. Boolean Literal
4. String Literal
(e) Name the wrapper classes of char type and boolean type.
Answer
2015 COMPUTER
APPLICATION PAPER
Answer
int n = (q – p) > (p – q) ? (q – p) : (p – q)
⇒ int n = (19 - 5) > (5 - 19) ? (19 - 5) : (5 - 19)
⇒ int n = 14 > -14 ? 14 : -14
⇒ int n = 14 [∵ 14 > -14 is true so ? : returns 14]
Final value of n is 14
Answer
byte, char, int, double
Answer
Value of res is 8.0
Index of '5' in "345" is 2. Math.pow(2, 3) means 23 which is equal to 8
Answer
Parameterized constructors and Non-Parameterized constructors.
(e) What are the values of a and b after the following function is
executed, if the values passed are 30 and 50:
void paws(int a, int b)
{
a=a+b;
b=a–b;
a=a–b;
System.out.println(a+ "," +b);
}
Answer
2015 COMPUTER
APPLICATION PAPER
(e) (i) Name the mathematical function which is used to find sine of an angle
given in radians.
(ii) Name a string function which removes the blank spaces provided in the
prefix and suffix of a string.
Answer
(i) Math.sin() (ii) trim()
(ii) Name the keyword which is used- to resolve the conflict between
method parameter and instance variables/fields.
Answer
this keyword
1. java.io
2. java.util
2015 COMPUTER
APPLICATION PAPER
(a) State the data type and value of y after the following is executed:
char x='7';
y=Character.isLetter(x);
Answer
Data type of y is boolean and value is false. Character.isLetter() method
checks if its argument is a letter or not and as 7 is a number not a letter
hence it returns false.
(c) State the output when the following program segment is executed:
String a ="Smartphone", b="Graphic Art";
String h=a.substring(2, 5);
String k=b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
Answer
The output of the above code is:
art
true
Explanation
(d) The access specifier that gives the most accessibility is ________ and
the least accessibility is ________.
Answer
The access specifier that gives the most accessibility is public and the least
accessibility is private.
SAVIOUR OF COMPUTERS
2015 COMPUTER
APPLICATION PAPER
SECTION B
Question 4
Member Methods:
void input() — To input and store the vno and hours.
void calculate() — To compute the parking charge at the rate of ₹3
for the first hour or part thereof, and ₹1.50 for each additional hour
or part thereof.
void display() — To display the detail.
Write a main() method to create an object of the class and call the
above methods.
SAVIOUR OF COMPUTERS
2015 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
2015 COMPUTER
APPLICATION PAPER
Question 5
*
*#
*#*
*#*#
*#*#*
(b)
5 4321
5 432
5 43
5 4
5
Answer
2015 COMPUTER
APPLICATION PAPER
(B)
Answer
public class Pattern
{
public static void main(String args[]) {
for (int i = 1; i <=5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Question 6
Write a program to input and store roll numbers,
names and marks in 3 subjects of n number of students
in five single dimensional arrays and display the
remark based on average marks as given below:
2015 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
System.out.println("Roll No\tName\tRemark");
for (int i = 0; i < n; i++) {
String remark;
if (avg[i] < 40)
remark = "Poor";
else if (avg[i] < 60)
remark = "Pass";
else if (avg[i] < 75)
remark = "First Class";
else if (avg[i] < 85)
remark = "Distinction";
elseremark = "Excellent";
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ remark);
}
}
}
SAVIOUR OF COMPUTERS
2015 COMPUTER
APPLICATION PAPER
Question 7
2015 COMPUTER
APPLICATION PAPER
Answer
public class StringOverload
{
public void joystring(String s, char ch1, char ch2) {
String newStr = s.replace(ch1, ch2);
System.out.println(newStr);
}
Question 8
2015 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
System.out.println("\nSorted Names");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}
}
Question 9
Using switch statement, write a menu driven program to:
(i) To find and display all the factors of a number input by the user (
including 1 and the excluding the number itself).
Example:
Sample Input : n = 15
Sample Output : 1, 3, 5
(ii) To find and display the factorial of a number input by the user (the
factorial of a non-negative integer n, denoted by n!, is the product of all
integers less than or equal to n.)
Example:
Sample Input : n = 5
Sample Output : 5! = 1*2*3*4*5 = 120
For an incorrect choice, an appropriate error message should be displayed.
SAVIOUR OF COMPUTERS
2015 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
switch (choice) {
case 1:
System.out.print("Enter number: ");
num = in.nextInt();
for (int i = 1; i < num; i++) {
if (num % i == 0) {
System.out.print(i + " ");
}
}
System.out.println();
break;
case 2:
System.out.print("Enter number: ");
num = in.nextInt();
int f = 1;
for (int i = 1; i <= num; i++)
f *= i;
System.out.println("Factorial = " + f);
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
SAVIOUR OF COMPUTERS
2016 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
1. Runtime Error
2. Syntax Error
Answer
1.6
2.q = x[2] + x[5] * x[1]
q = 7 + 10 * 3
q = 7 + 30
q = 37
SAVIOUR OF COMPUTERS
2016 COMPUTER
APPLICATION PAPER
Answer
equals() ==
It is a method It is a relational operator
It is used to check if the It is used to check if two
contents of two strings are variables refer to the same
same or not Example: String s1 object in memory
= new String("hello");String s2 Example:String s1 = new
= new String("hello");boolean
String("hello");String s2 =
res
s1.equals(s2);System.out.printl new
n(res); String("hello");boolean res
The output of this code = s1 ==
snippet is true as contents of s2;System.out.println(res);
s1 and s2 are the same. The output of this code
snippet is false as s1 and s2
point to different String
objects.
(b) What are the types of casting shown by the following examples:
1. char c = (char) 120;
2. int x = ‘t’;
Answer
1. Explicit Cast.
2. Implicit Cast.
Answer
2016 COMPUTER
APPLICATION PAPER
1. public
2. private
Question 3
(a) Give the output of the following string functions:
1. "MISSISSIPPI".indexOf('S') + "MISSISSIPPI".lastIndexOf('I')
2. "CABLE".compareTo("CADET")
Answer
1. 2 + 10 = 12
2. ASCII Code of B - ASCII Code of D
⇒ 66 - 68
⇒ -2
1. 5.0
2. 4
2016 COMPUTER
APPLICATION PAPER
int m = 5;
for (int n = 10; n >= 1; n--) {
System.out.println(m*n);
}
2016 COMPUTER
APPLICATION PAPER
(h) Analyze the given program segment and answer the following
questions:
Output
5
10
Loop executes 3 times
1. boolean
2. String
SAVIOUR OF COMPUTERS
2016 COMPUTER
APPLICATION PAPER
(h) Analyze the given program segment and answer the following
questions:
Output
5
10
Loop executes 3 times
1. boolean
2. String
SAVIOUR OF COMPUTERS
2016 COMPUTER
APPLICATION PAPER
SECTION B
Question 4
Define a class named BookFair with the following description:
Instance variables/Data members:
String Bname — stores the name of the book
double price — stores the price of the book
Member methods:
Price. Discount
Less than or equal to ₹1000. 2% of price
More than ₹1000 and less than or equal to ₹3000. 10% of price
More than ₹3000. 15% of price
(iv) void display() — To display the name and price of the book
after discount.
Write a main method to create an object of the class and call the
above member methods.
SAVIOUR OF COMPUTERS
2016 COMPUTER
APPLICATION PAPER
import java.util.Scanner;
public BookFair() {
bname = "";
price = 0.0;
}
price -= disc;
}
2016 COMPUTER
APPLICATION PAPER
Question 5
Using the switch statement, write a menu driven program for the
following:
2016 COMPUTER
APPLICATION PAPER
import java.util.Scanner;
switch (ch) {
case 1:
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
SAVIOUR OF COMPUTERS
2016 COMPUTER
APPLICATION PAPER
Question 6
Special words are those words which start and end with the same
letter.
Example: EXISTENCE, COMIC, WINDOW
Palindrome words are those words which read the same from left
to right and vice-versa.
All palindromes are special words but all special words are not
palindromes.
2016 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
if (isPalin) {
System.out.println("Palindrome");
}
else {
System.out.println("Special");
}
}
else {
System.out.println("Neither Special nor Palindrome");
}
}
}
SAVIOUR OF COMPUTERS
2016 COMPUTER
APPLICATION PAPER
question7
Answer
public class Overload
{
void sumSeries(int n, double x) {
double sum = 0;
for (int i = 1; i <= n; i++) {
double t = x / i;
if (i % 2 == 0)
sum -= t;
elsesum += t;
}
System.out.println("Sum = " + sum);
}
void sumSeries() {
long sum = 0, term = 1;
for (int i = 1; i <= 20; i++) {
term *= i;
sum += term;
}
System.out.println("Sum=" + sum);
}
}
SAVIOUR OF COMPUTERS
2016 COMPUTER
APPLICATION PAPER
Question 8
(i) void sumSeries(int n, double x): with one integer argument and one
double argument to find and display the sum of the series given
below:
s=x/1-x/2+x/3-x/4+x/5……….to n terms
(ii) void sumSeries(): to find and display the sum of the following
series:
s=1+(1×2)+(1×2×3)+... ... ... +(1×2×3×4... ... ... ×20)s=1+(1×2)+(1×2×3)+... ... ... +
(1×2×3×4... ... ... ×20)
Answer
import java.util.Scanner;
int digitSum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
}
2016 COMPUTER
APPLICATION PAPER
Question 9
Write a program to initialize the seven Wonders of the World along with their
locations in two different arrays. Search for a name of the country input by the
user. If found, display the name of the country along with its Wonder, otherwise
display "Sorry not found!".
Seven Wonders:
CHICHEN ITZA, CHRIST THE REDEEMER, TAJ MAHAL, GREAT WALL OF CHINA,
MACHU PICCHU, PETRA, COLOSSEUM
Locations:
MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Examples:
Country name: INDIA
Output: TAJ MAHAL
Country name: USA
Output: Sorry Not found!
Answer
import java.util.Scanner;
if (i == locations.length)
System.out.println("Sorry Not Found!");
}
}
SAVIOUR OF COMPUTERS
2017 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
(a) What is inheritance?
Answer
(c) State the number of bytes occupied by char and int data types.
Answer
2017 COMPUTER
APPLICATION PAPER
Question 2
(a) Name the following:
1. A keyword used to call a package in the program.
2. Any one reference data type.
Answer
1. import
2. Array
(c) State the data type and value of res after the following is executed:
char ch='t';
res= Character.toUpperCase(ch);
Answer
(d) Give the output of the following program segment and also mention the
number of times the loop is executed:
int a,b;
for (a = 6, b = 4; a <= 24; a = a + 6)
{
if (a%b == 0)
break;
}
System.out.println(a);
Answer
2017 COMPUTER
APPLICATION PAPER
Question 3
(a) Write a Java expression for the following:
ax^5+ bx^3 + c
Answer
a * Math.pow(x, 5) + b * Math.pow(x, 3) + c
Constructor Function
2017 COMPUTER
APPLICATION PAPER
Explanation
As the first T is present at index 0 in string s so s.indexOf('T') returns 0.
s.substring(0,7) extracts the characters from index 0 to index 6 of string s. So its
result is Today i. After that println statement prints Today i followed by a space
and Holiday
Explanation
1. Math.min(-2.83, -5.83) returns -5.83 as -5.83 is less than -2.83. (Note that these
are negative numbers). Math.abs(-5.83) returns 5.83.
2. Math.floor(16.3) returns 16.0. Math.sqrt(16.0) gives square root of 16.0 which is
4.0.
Explanation
1. As A and B are strings so String D=A+B+"200"; joins A, B and "200" storing the
string "26100200" in D.
2. Integer.parseInt() converts strings A and B into their respective numeric values.
Thus, x becomes 26 and y becomes 100.
3. As Java is case-sensitive so D and d are treated as two different variables.
4. Sum of x and y is assigned to d. Thus, d gets a value of 126.
SAVIOUR OF COMPUTERS
2017 COMPUTER
APPLICATION PAPER
next() nextLine
2017 COMPUTER
APPLICATION PAPER
SECTION B
Member methods:
void accept( ) — to accept the name of the customer and
number of units consumed
void calculate( ) — to calculate the bill as per the following
tariff:
2017 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
2017 COMPUTER
APPLICATION PAPER
Question 5
Write a program to accept a number and check and display
whether it is a spy number or not. (A number is spy if the sum of its
digits equals the product of its digits.)
Example: consider the number 1124.
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 x 1 x 2 x 4 = 8
Answer
import java.util.Scanner;
sum += digit;
prod *= digit;
num /= 10;
}
if (sum == prod)
System.out.println(orgNum + " is Spy Number");
elseSystem.out.println(orgNum + " is not Spy Number");
}
}
SAVIOUR OF COMPUTERS
2017 COMPUTER
APPLICATION PAPER
Question 6
Using switch statement, write a menu driven program for the
following:
1. To find and display the sum of the series given below:
S = x^1- x^2 + x^3 - x^4 + x^5 .......... - x^20 (where x = 2)
To display the following series: 1 11 111 1111 11111
For an incorrect option, an appropriate error message should
be displayed.
Answer
import java.util.Scanner;
switch (choice) {
case 1:
int sum = 0;
for (int i = 1; i <= 20; i++) {
int x = 2;
int term = (int)Math.pow(x, i);
if (i % 2 == 0)
sum -= term;
elsesum += term;
}
System.out.println("Sum=" + sum);
break;
case 2:
int term = 1;
for (int i = 1; i <= 5; i++) {
System.out.print(term + " ");
term = term * 10 + 1;
}
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
SAVIOUR OF COMPUTERS
2017 COMPUTER
APPLICATION PAPER
Question 7
Write a program to input integer elements into an array of size
20 and perform the following operations:
1. Display largest number from the array.
2. Display smallest number from the array.
3. Display sum of all the elements of the array
Answer
import java.util.Scanner;
sum += arr[i];
}
2017 COMPUTER
APPLICATION PAPER
Question8
Design a class to overload a function check( ) as follows:
void check (String str , char ch ) — to find and print the frequency of a
character in a string.Example:
Input:
str = "success"
ch = 's'
Output:
number of s present is = 3
void check(String s1) — to display only vowels from string s1, after converting
it to lower case.Example:
Input:
s1 ="computer"
Output : o u e
Answer
2017 COMPUTER
APPLICATION PAPER
Question 9
Write a program to input forty words in an array. Arrange these words in
descending order of alphabets, using selection sort technique. Print the sorted
array.
Answer
import java.util.Scanner;
System.out.println("Sorted Names");
for (int i = 0; i < n; i++) {
System.out.println(a[i]);
}
}
}
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
Searching Sorting
isUpperCase( ) toUpperCase( )
isUpperCase( ) toUpperCase( )
function checks if a function converts
given character is in a given character
uppercase or not. to uppercase.
Its return type is Its return type is
boolean. char.
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
(d) How are private members of a class different from public members?
Answer
private members of a class can only be accessed by other member methods of the
same class whereas public members of a class can be accessed by methods of
other classes as well.
1. Primitive
2. Non-Primitive
3. Primitive
4. Non-Primitive
Question 2
(a) (i) int res = 'A';
What is the value of res?
Answer
Value of res is 65 which is the ASCII code of A. As res is an int variable and we are
trying to assign it the character A so through implicit type conversion the ASCII
code of A is assigned to res.
java.lang
while do-while
It is an entry- it is an exit-
controlled loop. controlled loop.
It is helpful in It is suitable when
situations where we need to display
numbers of a menu to the user.
iterations are not
known.
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
char check(int a)
1. boolean
2. double
Question 3
Answer
Math.sqrt(3 * x + x * x) / (a + b)
(b) What is the value of y after evaluating the expression given below?
y+= ++y + y-- + --y; when int y=8
Answer
2018 COMPUTER
APPLICATION PAPER
1. -5.0
2. 12.0
\n is the escape sequence for newline so Incredible and world are printed on two
different lines.
switch (var) {
case 1:
System.out.println("good");
break;
case 2:
System.out.println("better");
break;
case 3:
System.out.println("best");
break;
default:
System.out.println("invalid");
}
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
1. ACHIAVAMANT
2. -18
Explanation
1. "ACHIEVEMENT".replace('E', 'A') will replace all the E's in
ACHIEVEMENT with A's.
2. The first letters that are different in DEDICATE and DEVOTE are D
and V respectively. So the output will be: ASCII code of D - ASCII
code of V
⇒ 68 - 86
⇒ -18
(h) Consider the following String array and give the output
String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW",
"JAIPUR"};
System.out.println(arr[0].length() > arr[3].length());
System.out.print(arr[4].substring(0,3));
Answer
2018 COMPUTER
APPLICATION PAPER
(j) Give the output of the following program segment and also
mention how many times the loop is executed:
int i;
for ( i = 5 ; i > 10; i ++ )
System.out.println( i );
System.out.println( i * 4 );
Answer
In the loop, i is initialized to 5 but the condition of the loop is i > 10. As 5
is less than 10 so the condition of the loop is false and it is not executed.
There are no curly braces after the loop which means that the
statement System.out.println( i ); is inside the loop and the statement
System.out.println( i * 4 ); is outside the loop. Loop is not executed so
System.out.println( i ); is not executed but System.out.println( i * 4 );
being outside the loop is executed and prints the output as 20 (i * 4 ⇒ 5
* 4 ⇒ 20).
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
SECTION B
Question 4
Design a class RailwayTicket with following description:
Instance variables/data members:
Member methods:
void accept() — To take input for name, coach, mobile number and
amount.
void update() — To update the amount as per the coach selected (extra
amount to be added in the amount as follows)
2018 COMPUTER
APPLICATION PAPER
Answer
import java.util.Scanner;
2018 COMPUTER
APPLICATION PAPER
Question 5
import java.util.Scanner;
if (isPronic)
System.out.println(num + " is a pronic number");
elseSystem.out.println(num + " is not a pronic number");
}
}
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
Question 6
Write a program in Java to accept a string in lower case and change
the first letter of every word to upper case. Display the new string.
Sample input: we are in cyber world
Sample output: We Are In Cyber World
Answer
import java.util.Scanner;
System.out.println(word);
}
}
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
Question 7
Design a class to overload a function volume() as follows:
double volume (double R) – with radius (R) as an argument,
returns the volume of sphere using the formula.
V = 4/3 x 22/7 x R^3
double volume (double H, double R) – with height(H) and
radius(R) as the arguments, returns the volume of a cylinder
using the formula. V = 22/7 x R^2 x H
double volume (double L, double B, double H) – with length(L),
breadth(B) and Height(H) as the arguments, returns the volume
of a cuboid using the formula. V = L x B x H
Answer
2018 COMPUTER
APPLICATION PAPER
Question 8
Write a menu driven program to display the pattern as per
user’s choice.
Pattern 1
ABCDE
ABCD
ABC
AB
A
Pattern 2
B
LL
UUU
EEEE
For an incorrect option, an appropriate error message should
be displayed.
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
import java.util.Scanner;
case 2:
String word = "BLUE";
int len = word.length();
for(int i = 0; i < len; i++) {
for(int j = 0; j <= i; j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
SAVIOUR OF COMPUTERS
2018 COMPUTER
APPLICATION PAPER
Question 9
Write a program to accept name and total marks of N number of
students in two single subscript array name[] and totalmarks[].
Calculate and print:
1. The average of the total marks obtained by N number of
students.
2. [average = (sum of total marks of all the students)/N]
3. Deviation of each student’s total marks with the average.
4. [deviation = total marks of a student – average]
Answer
import java.util.Scanner;
2019 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
(a) Name any two basic principles of Object-oriented Programming.
Answer
1. Encapsulation
2. Inheritance
(d) Write the memory capacity (storage size) of short and float data
type in bytes.
Answer
1. short — 2 bytes
2. float — 4 bytes
2019 COMPUTER
APPLICATION PAPER
Question 2
(a) Differentiate between if else if and switch-case statements
Answer
if else if switch-case
2019 COMPUTER
APPLICATION PAPER
(d) State the data type and value of res after the following is
executed:
char ch = '9';
res= Character.isDigit(ch);
Answer
(e) What is the difference between the linear search and the binary
search technique?
Answer
Question 3
(a) Write a Java expression for the following:
|x^2+ 2xy|
Answer
Math.abs(x * x + 2 * x * y)
1. boolean
2. double
SAVIOUR OF COMPUTERS
2019 COMPUTER
APPLICATION PAPER
(c) If the value of basic=1500, what will be the value of tax after the
following statement is executed?
tax = basic > 1200 ? 200 :100;
Answer
(d) Give the output of following code and mention how many times
the loop will execute?
int i;
for( i=5; i>=1; i--)
{
if(i%2 == 1)
continue;
System.out.print(i+" ");
}
Answer
2019 COMPUTER
APPLICATION PAPER
v = x + --z + y++ + y
⇒v=2+0+3+4
⇒v=9
(i) String x[] = {"Artificial intelligence", "IOT", "Machine
learning", "Big data"};
Give the output of the following statements:
1. System.out.println(x[3]);
2. System.out.println(x.length);
Answer
1. Big data
2. 4
2019 COMPUTER
APPLICATION PAPER
SECTION B
Question 4
Design a class name ShowRoom with the following description:
Instance variables / Data members:
Member methods:
ShowRoom() — default constructor to initialize data members
void input() — To input customer name, mobile number, cost
void calculate() — To calculate discount on the cost of purchased items, based
on following criteria
2019 COMPUTER
APPLICATION PAPER
import java.util.Scanner;
public ShowRoom()
{
name = "";
mobno = 0;
cost = 0.0;
dis = 0.0;
amount = 0.0;
}
2019 COMPUTER
APPLICATION PAPER
Question 5
Using the switch-case statement, write a menu driven program to do the
following:
(a) To generate and print Letters from A to Z and their Unicode
Letters. Unicode
A. 65
B. 66
.
.
.
Z 90
(b) Display the following pattern using iteration (looping) statement:
1
12
123
1234
12345
Answer
import java.util.Scanner;
public class CK
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 for letters and Unicode");
System.out.println("Enter 2 to display the pattern");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch(ch) {
case 1:
System.out.println("Letters\tUnicode");
for (int i = 65; i <= 90; i++)
System.out.println((char)i + "\t" + i);
break;
case 2:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
break;
default:
System.out.println("Wrong choice");
break;
}
}
}
SAVIOUR OF COMPUTERS
2019 COMPUTER
APPLICATION PAPER
Question 6
Write a program to input 15 integer elements in an array and
sort them in ascending order using the bubble sort technique.
Answer
import java.util.Scanner;
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
SAVIOUR OF COMPUTERS
2019 COMPUTER
APPLICATION PAPER
Question 7
Design a class to overload a function series( ) as follows:
(a) void series (int x, int n) – To display the sum of the series
given below:
x^1+ x^2+ x^3 + .......... x^n terms
(b) void series (int p) – To display the following series:
0, 7, 26, 63 .......... p terms
(c) void series () – To display the sum of the series given below:
1/2 + 1/3 + 1/4 + .......... 1/10
Answer
void series(int p) {
for (int i = 1; i <= p; i++) {
int term = (int)(Math.pow(i, 3) - 1);
System.out.print(term + " ");
}
System.out.println();
}
void series() {
double sum = 0.0;
for (int i = 2; i <= 10; i++) {
sum += 1.0 / i;
}
System.out.println("Sum = " + sum);
}
}
SAVIOUR OF COMPUTERS
2019 COMPUTER
APPLICATION PAPER
Question 8
Write a program to input a sentence and convert it into uppercase
and count and display the total number of words starting with a
letter 'A'.
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION
TECHNOLOGY ARE EVER CHANGING.
Sample Output: Total number of words starting with letter 'A' = 4
Answer
import java.util.Scanner;
Question 9
A tech number has even number of digits. If the number is split in
two equal halves, then the square of sum of these halves is equal to
the number itself. Write a program to generate and print all four
digits tech numbers.
Example:
Consider the number 3025
Square of sum of the halves of 3025 = (30 + 25)^2
(55)^2
= 3025 is a tech number.
SAVIOUR OF COMPUTERS
2019 COMPUTER
APPLICATION PAPER
Answer
2022 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
(i) Return data type of isLetter(char) is ............... .
1. Boolean
2. boolean
3. bool
4. char
Answer
boolean
toUpperCase(char
Float
Wrapper
SAVIOUR OF COMPUTERS
2022 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
(i) Return data type of isLetter(char) is ............... .
1. Boolean
2. boolean
3. bool
4. char
Answer
boolean
toUpperCase(char
Float
Wrapper
SAVIOUR OF COMPUTERS
2022 COMPUTER
APPLICATION PAPER
2022 COMPUTER
APPLICATION PAPER
SECTION B
Question 2
Define a class to perform binary search on a list of integers
given below, to search for an element input by the user, if it is
found display the element along with its position, otherwise
display the message "Search element not found".
2, 5, 7, 10, 15, 20, 29, 30, 46, 50
import java.util.Scanner;
if (index == -1) {
System.out.println("Search element not found");
}
else {
System.out.println(n + " found at position " + index);
}
}
SAVIOUR OF COMPUTERS
2022 COMPUTER
APPLICATION PAPER
Question 3
Define a class to declare a character array of size ten. Accept the
characters into the array and display the characters with highest and
lowest ASCII (American Standard Code for Information Interchange)
value. EXAMPLE :
INPUT:
'R', 'z', 'q', 'A', 'N', 'p', 'm', 'U', 'Q', 'F'
OUTPUT :
Character with highest ASCII value = z
Character with lowest ASCII value = A
import java.util.Scanner;
System.out.println("Enter 10 characters:");
for (int i = 0; i < len; i++)
{
ch[i] = in.nextLine().charAt(0);
}
char h = ch[0];
char l = ch[0];
if (ch[i] < l)
{
l = ch[i];
}
}
}
SAVIOUR OF COMPUTERS
2022 COMPUTER
APPLICATION PAPER
Question 4
Define a class to declare an array of size twenty of double
datatype, accept the elements into the array and perform
the following :
Calculate and print the product of all the elements.
Print the square of each element of the array.
import java.util.Scanner;
System.out.println("Enter 20 numbers:");
for (int i = 0; i < l; i++)
{
arr[i] = in.nextDouble();
}
2022 COMPUTER
APPLICATION PAPER
Question 5
Define a class to accept a string, and print the characters with the
uppercase and lowercase reversed, but all the other characters
should remain the same as before.
EXAMPLE:
INPUT : WelCoMe_2022
OUTPUT : wELcOmE_2022
import java.util.Scanner;
System.out.println(rev);
}
}
SAVIOUR OF COMPUTERS
2022 COMPUTER
APPLICATION PAPER
Question 6
Define a class to declare an array to accept and store ten words.
Display only those words which begin with the letter 'A' or 'a' and
also end with the letter 'A' or 'a'.
EXAMPLE :
Input : Hari, Anita, Akash, Amrita, Alina, Devi Rishab, John, Farha,
AMITHA
Output: Anita
Amrita
Alina
AMITHA
import java.util.Scanner;
2022 COMPUTER
APPLICATION PAPER
Question 7
Define a class to accept two strings of same length and form a
new word in such a way that, the first character of the first
word is followed by the first character of the second word and
so on.
Example :
Input string 1 – BALL
Input string 2 – WORD
OUTPUT : BWAOLRLD
import java.util.Scanner;
if(s2.length() == len)
{
for (int i = 0; i < len; i++)
{
char ch1 = s1.charAt(i);
char ch2 = s2.charAt(i);
str = str + ch1 + ch2;
}
System.out.println(str);
}
else
{
System.out.println("Strings should be of same length");
}
}
}
SAVIOUR OF COMPUTERS
2022 COMPUTER
APPLICATION PAPER
Question 7
Define a class to accept two strings of same length and form a
new word in such a way that, the first character of the first
word is followed by the first character of the second word and
so on.
Example :
Input string 1 – BALL
Input string 2 – WORD
OUTPUT : BWAOLRLD
import java.util.Scanner;
if(s2.length() == len)
{
for (int i = 0; i < len; i++)
{
char ch1 = s1.charAt(i);
char ch2 = s2.charAt(i);
str = str + ch1 + ch2;
}
System.out.println(str);
}
else
{
System.out.println("Strings should be of same length");
}
}
}
SAVIOUR OF COMPUTERS
2023 COMPUTER
APPLICATION PAPER
SECTION A
Question 1
(I) A mechanism where one class acquires the properties of another
class:
1. Polymorphism
2. Inheritance
3. Encapsulation
4. Abstraction
Answer
Inheritance
(iii) The Scanner class method used to accept words with space:
1. next()
2. nextLine()
3. Next()
4. nextString()
Answer
nextLine()
2023 COMPUTER
APPLICATION PAPER
2023 COMPUTER
APPLICATION PAPER
2023 COMPUTER
APPLICATION PAPER
(XVI) Consider the following program segment and select the output
of the same when n = 10 :
switch(n)
{
case 10 : System.out.println(n*2);
case 4 : System.out.println(n*4); break;
default : System.out.println(n);
}
1. 20
40
2.10
4
3.20,40
4. 10, 10
Answer
20
40
2023 COMPUTER
APPLICATION PAPER
(XX) Method which is a part of a class rather than an instance of the class is termed as:
1. Static method
2. Non static method
3. Wrapper class
4. String method
Answer
Static method
Question 2
2023 COMPUTER
APPLICATION PAPER
(b) Character.isLetterOrDigit('#')
Output
false
(VIII) Consider the given array and answer the questions given below:
int x[ ] = {4, 7, 9, 66, 72, 0, 16};
(a) What is the length of the array?
(b) What is the value in x[4]?
Answer
(a) 7
(b) 72
SAVIOUR OF COMPUTERS
2023 COMPUTER
APPLICATION PAPER
SECTION B
Question 3
Design a class with the following specifications:
Class name: Student
Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated
Member methods:
void accept() — Accept name, age and marks using methods of Scanner
class.
void allocation() — Allocate the stream as per following criteria:
mks. Stream
>= 300. Science and Computer
>= 200 and < 300. Commerce and Computer
>= 75 and < 200. Arts and Animation
< 75. Try Again
void print() – Display student name, age, mks and stream allocated.
Call all the above methods in main method using an object.
SAVIOUR OF COMPUTERS
2023 COMPUTER
APPLICATION PAPER
import java.util.Scanner;
2023 COMPUTER
APPLICATION PAPER
Question 4
Define a class to accept 10 characters from a user. Using bubble
sort technique arrange them in ascending order. Display the
sorted array and original array.
Answer
import java.util.Scanner;
System.out.println("Original Array");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}
System.out.println("\nSorted Array");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}
}
}
SAVIOUR OF COMPUTERS
2023 COMPUTER
APPLICATION PAPER
Question 5
Define a class to overload the function print as follows:
void print() - to print the following format
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
void print(int n) - To check whether the number is a lead number.
A lead number is the one whose sum of even digits are equal to
sum of odd digits.
e.g. 3669
odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.
SAVIOUR OF COMPUTERS
2023 COMPUTER
APPLICATION PAPER
import java.util.Scanner;
if(evenSum == oddSum)
System.out.println("Lead number");
elseSystem.out.println("Not a lead number");
}
System.out.println("Pattern: ");
obj.print();
2023 COMPUTER
APPLICATION PAPER
Question 6
Define a class to accept a String and print the number of digits,
alphabets and special characters in the string.
Example:
S = "KAPILDEV@83"
Output:
Number of digits – 2
Number of Alphabets – 8
Number of Special characters – 1
import java.util.Scanner;
Answer
int ac = 0;
int sc = 0;
int dc = 0;
char ch;
}
}
SAVIOUR OF COMPUTERS
2023 COMPUTER
APPLICATION PAPER
Question 7
Define a class to accept values into an array of double data type of size 20.
Accept a double value from user and search in the array using linear search
method. If value is found display message "Found" with its position where it
is present in the array. Otherwise display message "not found".
Answer
import java.util.Scanner;
if (i == l)
{
System.out.println("Not found");
}
else
{
System.out.println(n + " found at index " + i);
}
}
}
SAVIOUR OF COMPUTERS
2023 COMPUTER
APPLICATION PAPER
Question 8
Define a class to accept values in integer array of size 10. Find sum
of one digit number and sum of two digit numbers entered.
Display them separately.
Example:
Input: a[ ] = {2, 12, 4, 9, 18, 25, 3, 32, 20, 1}
Output:
Sum of one digit numbers : 2 + 4 + 9 + 3 + 1 = 19
Sum of two digit numbers : 12 + 18 + 25 + 32 + 20 = 107
Answer
import java.util.Scanner;
}
}
SAVIOUR OF
COMPUTERS
PROGRAMMING
NOTES
CHAPTER
CLASS 9TH
REVISION
Variable
[String]
CHAPTER
CLASS 9TH
REVISION
HOW TO PRINT ?
OBJECT
IMPLICIT TYPE
System.out.print() : This statement prints the sentence CONVERSION
and keeps the controller in the same line.
System.out.println() : This statement prints the
sentence and brings the controller in the next line. byte char short int long float double
TYPE CONVERSION
CLASS 9TH
REVISION
The process of assigning data or actual values to a declared variable is known as initialization.
This process is basically not taking input from the user but assigning the values in the variables
during writing the code.
The class that allows to input or read primitive data types(int,short,float,etc.) and strings is known
as Scanner Class.
The increment operator increases the value by 1 and the decrement operator decreases the value by 1.
Prefix Notation
The presence of an increment or decrement operator before the operand or variable is known as prefix
notation. For eg: ++A or --A
It first changes the value then uses it. (change-then-use rule)
Postfix Notation
The presence of an increment or decrement operator after the operand or variable is known as postfix
notation. For eg: A++ or A--
It first uses the original value then changes it. (use-then-change rule)
Step 1: Step 2:
CREATE A CLASS CREATE A SCANNER OBJECT
CHAPTER
CLASS 9TH
REVISION
Step 3:
CREATE A SCANNER OBJECT
Step 4:
ASK FOR USER'S CHOICE
Step 5:
ASK FOR NUMBERS
Step 6:
CREATE CONDITIONS FOR PROPER WORKING
Step 7:
JUST PRINT THE RESULT NOW
CHAPTER
CLASS 9TH
REVISION
Math.cbrt(125) ->
Returns the cube root of the 5.0
cbrt() Math.cbrt(argument)
number Math.cbrt(-8) ->
-2.0
Math.random()
Generates a random number
random() Math.random() ->
between 0.0 to 1..0
0.7134361215296077
CHAPTER
CLASS 9TH
REVISION
IF ELSE
SIMPLE IF STATEMENT
The process of assigning data or actual values to a declared variable is known as initialization.
This process is basically not taking input from the user but assigning the values in the variables
during writing the code.
IF ELSE STATEMENT
Syntax:-
if(condition-1)
{
if(condition-2)
{
Statement-2;
}
else()
{
Statement-3;
}
}
else()
{
Statement-3;
}
CHAPTER
CLASS 9TH
REVISION
NESTED IF ELSE
NESTED IF STATEMENT
True
Syntax:- Condition-1 Statement-1
if(condition-1)
{
False
Statement-1
if(condition-2) False
Exit Condition-2
{
Statement-2;
True
}
}
Statement-2
Syntax:-
if(condition or relational statements)
{
if-code;
}
else(conditional or relational statements)
{
else-code;
}
CHAPTER
CLASS 9TH
REVISION
LOOPS
For Loop :
The loop that executes one or more statements upto a finite limit is known as for ( ) loop.
SYNTAX :
for ( Initial-Value ; Test Condition ; Update-statement )
{
Statement-blocks;
}
While Loop :
The loop that executes one or more statements till the condition is TRUE
and terminates the iteration when the condition is FALSE .
SYNTAX :
while ( expression or condition or relational expression)
{
Statements-block
}
Do - While Loop :
The loop that executes one or more statements till the condition is TRUE and terminates the
iteration when the condition is FALSE, is known as do-while( ) loop.
SYNTAX :
do
{
Statement-block
}
while( condition or relational expression);
CHAPTER
CLASS 9TH
REVISION
LOOPS
For Loop :
The loop that executes one or more statements upto a finite limit is known as for ( ) loop.
SYNTAX :
for ( Initial-Value ; Test Condition ; Update-statement )
{
Statement-blocks;
}
While Loop :
The loop that executes one or more statements till the condition is TRUE
and terminates the iteration when the condition is FALSE .
SYNTAX :
while ( expression or condition or relational expression)
{
Statements-block
}
Do - While Loop :
The loop that executes one or more statements till the condition is TRUE and terminates the
iteration when the condition is FALSE, is known as do-while( ) loop.
SYNTAX :
do
{
Statement-block
}
while( condition or relational expression);
CHAPTER
FUNCTION
AND CONSTRUCTOR
FUNCTIONS
A method is a named piece of code within a program and executes when it is called from another
section of code. In JAVA, the methods can exists only inside class.
class abc
{
void main ()
{
}
}
Note - : While naming a method, just make sure that it should be a legal identifier and should be
meaningful.
METHOD TYPES
ACCESSING A
METHOD/FUNCTION
JAVA METHODS
A method is called or invoked by
providing the method name,
followed by parameters being
STATIC NON STATIC sent enclosed in paranthesis.
They are created The are created by
float area (float a, float b)
by keyword static absence of area (x,y);
in their prototype. keyword static in
their prototype.
CHAPTER
FUNCTION
AND CONSTRUCTOR
FUNCTIONS
SCOPE OF VARIABLE
CONSTRUCTOR
A constructor is a special member method of a class without a return type. It is used for
initialising and constructing the data members of an object when it is created. It is
automatically called (invoked) when the object is created using new operator. It cannot be
invoked by the user like normal methods.
Characteristics
A constructor will have the same name as that of the class.
A constructor does not have a return type not even void.
A constructor cannot be static or final.
A constructor must be declared public (if it has to be accessed outside the class).
It is automatically called (invoked) when an object is created.
CHAPTER
FUNCTION
AND CONSTRUCTOR
NEED OF A CONSTRUCTOR
A constructor is used for initialising and constructing the data members of an object with legal
values when it is created. Data members are mostly declared as private members, to
implement data hiding and abstraction. Due to this, data members cannot be initialised
outside the class with values. So, constructors are used for initialising the objects implicitly as
they are created.
Example :
class Book
{ int bookno;
String name;
float price;
public Book() //constructor is created, having same name Book as
{ bookno = 0; that of its class name Book, with public access specifier
name = "ABC"; and no return type.
TYPES OF CONSTRUCTORS
The main function of a constructor is to initialise the data members of an object while it is created.
Constructors can be broadly categorised into two categories based on the parameters it takes:
FUNCTION
AND CONSTRUCTOR
Parameterised Constructor
Constructor that takes one or more parameters or arguments is called as a parameterised
constructor. Such a constructor will accept values and initialise the data members with the
corresponding values. There is no limitation to number of parameters.
Syntax:
class_name (type1 val 1, type2 val 2...)// parameter list
{
Data_member1 = val 1;
Data_member2 = val 2;
}
FUNCTION
AND CONSTRUCTOR
'This' keyword
'This' keyword is used within a constructor to refer to the current object. It is actually a reference to
the object that invokes the constructor. In fact, even if you do not use the this keyword, the compiler
normally implicitly converts it by prefixing this to the data members.
DOUBLE
DIMENSIONAL ARRAY
[ ]
SINGLE
OBJECTDIMENSION AL ARRAY:
00 0 1 201 02
It has a single subscript subscript
int ar[]=new int[3]; of array 10 3 4 511 12
DOUBLE
OBJECT DIMENSION AL ARRAY: 20
6 7 821 22
Row column
[ ] [ ]
00
10
20
0 1 2
01
3 4 5
11
6 7 8
21
02
12
22
00
10
20
0 1 2
01
3 4 5
11
6 7 8
21
02
12
22
DOUBLE
DIMENSIONAL ARRAY
[ ] [ ]
LEFT DIAGONAL RIGHT DIAGNOAL
00 0 1 2
01 02 00 0 1 2
01 02
10 3 4 5
11 12 10 3 4 5
11 12
20
6 7 8
21 22 20
6 7 8
21 22