Ix Revision Worksheet
Ix Revision Worksheet
REVISION WORKSHEET IX
1
L-2 Values and Data Types
Character Sets used in Java
Java language uses letters, digits, operators and delimiters. It uses Unicode to represent characters.
Unicode is a standard encoding system created by Unicode consortium that is used to encode a
character in any computer language.
Unicode uses 16 bits to represent a character.
ADVANTAGES OF UNICODE
It is the universal coding scheme.
More efficient than ISO or IEC.
Supports uniform coding width for all characters.
Character codes are unique for each character.
UNICODE ASCII
It is a generalized
form of coding
scheme for It is a specific coding
numerous scheme used for
characters of limited number
different scripts. of characters.
ESCAPE SEQUENCES
They are the non-graphic characters, used as commands to direct the cursor while printing. Escape
sequences always begin with a backslash (\) character.
TOKENS
Tokens are the smallest individual components of a Java statement.
Following are the various types of tokens:
Literals or Constants: These are the fixed values that do not change during program
execution.
Identifiers: These are names given to different parts in a Java program. example name of the
variables ,class etc.
Assignments: The = sign is an assignment as it helps to store values in a variable.
2
Punctuators: Special characters like question mark (?), dot (.), semicolon (;) are punctuators.
Separators: Special characters used to separate various parts of a Java program are
separators.
Operators: These are symbols that perform some calculation or operation with values.
Keywords: These are reserved words which have special meaning for the compiler.
Primitive Data Type: These are the predefined data types. Following are the primitive data types:
byte (1 byte)
short (2 bytes)
char (2 bytes)
int (4 bytes)
long (8 bytes)
float (4 bytes)
double (8 bytes)
boolean (1 byte)
Type Conversion
1. Implicit: Conversion that takes place automatically into higher type is implicit type conversion.
It is also known as coercion.
2. Explicit: Conversion that takes place by the user by force is explicit type conversion.
3
L-3 Elementary Concept of Objects and Classes
Objects
Object is a fundamental unit of Object Oriented Programming. They are referred to as entities. Objects
are categorized in two ways:
These objects are the ones that we experience in our day-to-day life. Following are some of its
features:
1. It is visible to us.
2. They have a definite shape and size.
3. It can be brought in thoughts and figures.
Each real world object contains characteristics and behaviour.
Software Objects
It is an object that is created while writing a Java program. It contains data members and methods.
The state or characteristics of the real world objects are considered to be data members in the
software objects.
The behaviours in the real world objects are considered to be the methods in the software objects.
Message Passing
Objects can interact with each other. They interact through behaviors or methods, which are better
known as message passing.
Classes
A class is a blueprint or template for its objects. It is simply a representation of similar types of
objects.
CLASS OBJECT
It is a representation
of an abstraction. It is a real and unique entity.
It is an object
producer. It is created using “new” operator.
4
It is known as an It is known as the instance of a
object factory. class.
Exercises
5
L-4 Operators in Java
6
a = 9 + 40 + 9
= 58.
10. What will be the output of the following, if x = 5;?
a. 5 * ++x
=5*6
= 30.
b. 5 * x++
=5*5
= 25.
11. Evaluate the following expressions, if the values of the variables are:
a = 2, b = 3, and c = 9
a. a - (b++) * (--c)
=2–3*8
= 2 – 24
= -22.
b. a * (++b) % c
=2*4%9
=2*4
= 8.
12. If a = 5, b = 9, calculate the value of:
a += a++ - ++b + a;
a = a + a++ - ++b + a;
= 5 + 5 – 10 + 6
= 6.
13. Give the output of the program snippet:
int a = 10, b = 12;
if(a >= 10)
a++;
else
++b;
System.out.println("a = " + a + " and b = " + b);
a = 11 and b = 12
14. Rewrite the following using ternary operator:
if(income <= 100000)
tax = 0;
else
tax = (0.1 * income);
tax = (income <= 100000)? 0 : (0.1 * income);
15. Rewrite the following using ternary operator:
if(p > 5000)
d = p * 5 / 100;
else
d = 2 * p / 10;
d = (p > 5000)? p * 5 / 100 : 2 * p / 10;
7
Conditional Statements in Java
Answer the following questions:
(b) if-else
It is used to execute a set of statements when a condition is true, and execute another set of statements when the
condition is false.
if(condition){
statement(s)
}
else{
statement(s)
}
(c) if-else-if
if(condition 1){
statement(s)
}
else if(condition 2){
statement(s)
}
else{
statement(s)
}
8
8. Explain ‘fall through’ with reference to a switch case statement.
When we do not provide the break statement in switch statement, it leads to fall through, in which the flow of control
moves from one case to another.
9. What is a compound statement? Give an example.
A statement that includes a set of statements within it under opening and closing curly braces is a compound
statement.
Example:
if(a > b){
System.out.println(a + " is greater");
System.out.println(b + " is smaller");
}
In the above example, the if-else-if construct provides a series of conditions and the one that matches gets executed
while the other conditions and statements are skipped.
Q1.
int a = 1, b = 1, m = 10, n = 5;
if((a == 1) && (b == 0)){
System.out.println((m + n));
System.out.println((m - n));
}
if((a == 1) && (b == 1)){
System.out.println((m * n));
System.out.println((m % n));
}
OUTPUT:
50
0
Q2.
int x = 1, y = 1;
if(n > 0){
x = x + 1;
y = y + 1;
}
Q3.
int b = 3, k, r;
float a = 15.15F, c = 0;
if(k == 1){
r = (int)a / b;
System.out.println(r);
}
else{
c = a / b;
System.out.println(c);
}
OUTPUT:
variable k might not have been initialized.
Q4.
switch(opn){
case 'a':
9
System.out.println("Platform Independent");
break;
case 'b':
System.out.println("Object Oriented");
case 'c':
System.out.println("Robust and Secure");
break;
default:
System.out.println("Wrong Input");
}
When (i) opn = ‘b’ (ii) opn = ‘x’ (iii) opn = ‘a’
OUTPUT i:
Object Oriented
Robust and Secure
OUTPUT ii:
Wrong Input
OUTPUT iii:
Platform Independent
10
III. WRITE DOWN THE SYNTAX FOR THE FOLLOWING FUNCTIONS:
1. To find the smaller between two numbers p and q.
Math.min(p, q)
2. To find the absolute value of a number m.
Math.abs(m)
3. To find the exponent of a number k.
Math.exp(k)
4. To find the square root of a number d.
Math.sqrt(d)
5. To find the rounded-off value of a number b.
Math.round(b)
11