0% found this document useful (0 votes)
3 views11 pages

Ix Revision Worksheet

The document is a revision worksheet on Object Oriented Programming Concepts, specifically focusing on Java. It covers the history, features, and components of Java, including data types, classes, objects, and operators. Additionally, it provides exercises and questions to reinforce understanding of the material.

Uploaded by

arnavvrasal10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Ix Revision Worksheet

The document is a revision worksheet on Object Oriented Programming Concepts, specifically focusing on Java. It covers the history, features, and components of Java, including data types, classes, objects, and operators. Additionally, it provides exercises and questions to reinforce understanding of the material.

Uploaded by

arnavvrasal10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

HUTCHINGS SCHOLL TALEGAON

REVISION WORKSHEET IX

L-1 Introduction to Object Oriented Programming Concepts

1. Who developed Java? What was it initially called?


James Gosling developed Java. Java was initially called Oak.
2. Give a brief historical development of Java.
Sun Microsystems initially developed this language to develop software for consumer electronics. But its
platform independence feature made it popular as a general purpose programming language.
3. Mention at least four features of Java.
a) It is an object oriented programming language.
b) It uses both compiler and interpreter.
c) It is case-sensitive.
d) It is a platform independent programming language.
4. Define the following:
a) A compiler: Program that converts high-level language program into low-level language program at once.
b) An interpreter: Program that converts high-level language program into low-level language program line
by line.
c) Bytecode: Intermediate code that is generated after a Java source code is compiled. It is platform
independent, and only needs JVM for execution.
5. What is Java Virtual Machine (JVM)?
JVM is the interpreter that is responsible for executing the bytecode. It acts as a virtual processor for Java,
making it platform independent because the program directly doesn’t depend on the physical processor.
6. Name three packages of Java class library.
java.io, java.lang, java.util.
7. What are Java reserved words? Name any five.
Reserved words are the keywords that have a special meaning in a program. It cannot be used as an
identifier. Examples include class, public, static, void, return.
8. Distinguish between:
a) source code and object code: Source code is the program written in a high-level language. Object code
is the program that is in low-level language, generated after compiling the source code.
b) compiler and interpreter: Compiler is a program that converts high-level language program into low-level
language program at once. Interpreter is a program that converts high-level language program into low-level
language program line by line.
c) JDK 1.3 and BlueJ: JDK 1.3 is the toolkit that includes the compiler, interpreter and other libraries needed
to compile and run Java programs. BlueJ is an IDE that helps us in writing, compiling and executing Java
programs all in one place.
9. A compiler is specific to a language. Give your comments.
A compiler converts high-level language program into low-level language program. This low-level language
program consists of machine language which is specific to a given platform. It is machine dependent. This is
why a compiler is specific to a language.
10. What is the basic format of a Java program? Explain with an example.
The basic format is to enclose the code within a class. Inside a class, we provide data members, and methods
that work on those data members.
11. What is BlueJ?
BlueJ is an IDE that helps us in writing, compiling and executing Java programs all in one place.
12. Mention five features of BlueJ.
a) It is a window-based program, making it suitable to use.
b) It has facilities for easier debugging.
c) It highlights the syntax that makes the program easier to read.
d) It allows compilation, execution, editing of programs all in one place.
13. Name a package that is invoked by default.
java.lang package.
14. What are the points to be taken care while naming a class in a Java program?
The class name must not be a keyword. It should be meaningful and relevant to the program. It should follow
the naming rules that are used with identifiers.
15. Java is a case-sensitive language. Explain.
Java is a case-sensitive language because while naming identifiers, the uppercase and lowercase characters
are treated differently. For example, int num = 5; and int Num = 7; both the variables are different.
16. The main() function in a Java program is declared as:
public static void main(String args[])
What is the significance of the words public, static and void?
The keyword public makes the function main() accessible from anywhere. The static keyword makes it
possible to execute main() without the need of the object of the class in which it is defined. The void keyword
states that main() doesn’t return any value.
17. What does the term compilation mean?
Compilation is the process of converting a high-level language program into a low-level language program at
once.
18. Java programs uses compiler as well as an interpreter. Explain.
Java uses a compiler to generate an intermediate code called bytecode which becomes platform
independent. Then Java uses an interpreter to execute this bytecode.

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.

ASCII CHARACTER CODES


ASCII stands for American Standard Code for Information Interchange. It uses 7 bits to represent a
character.
ASCII code of white space blank is 32.

DIFFERENCE BETWEEN UNICODE AND ASCII

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.

It represents higher It represents limited


range of codes. range of codes.

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.

Following are some of the escape sequences:

 \t for Horizontal tab


 \v for Vertical tab
 \n for New line
 \’ for Single quote
 \” for Double quote
 \b for Backspace
 \f for Form feed
 \r for Carriage return
 \0 for Null
 \\ for Backslash

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.

DATA TYPES IN JAVA


There are two types of data types in Java: Primitive and Composite.

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

Converting one data type to another is called type conversion.

There are two types of 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:

1. Real World Objects


2. Software Objects
An object is also called an instance of a class.

Real World Objects

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.

CREATING OBJECTS OF A CLASS


Syntax:
<class name> <object name> = new <class name>();
Example:
Car bmw = new Car();
The new keyword is used to allocate space in the dynamic memory for creating an object.
Attributes are characteristics or data members of an object.
The state of an object is represented by the attributes of that object.

IMPORTANT TERMS RELATED TO CLASSES


 Class is an object factory.
 Class is a user defined data type.
 Object is an instance of a class.

DIFFERENCES BETWEEN A CLASS AND AN OBJECT:

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

FILL IN THE BLANKS:


1. A class is also considered as an object factory.
2. The objects of a class differs on various characteristics.
3. The object of a class is represented through the attributes.
4. The term instantiation is used for creating various objects.
5. Class is a blueprint/template of the objects.
6. new keyword indicates an operator for dynamic allocation.
7. Objects are also termed as class tags or entities.
8. Different objects of a class have common behavior.

ANSWER THE FOLLOWING QUESTIONS:


1. Class and objects are interrelated. Explain.
A class is the blueprint for an object, whereas an object is an instance of a class. Thus, classes
and objects are interrelated.
2. A class is also referred to as an object factory. Comment.
With one class, we can create several objects of similar kinds. Thus, a class is also referred to as
an object factory.
3. What does the following statement mean?
Employee staff = new Employee();
In the above statement, we are creating a object named “staff”, of class Employee, using the
new operator.
4. Why is an object called an instance of a class? Explain.
When we create several objects from a class, each object gets unique properties of their own.
And these objects actually exist in the dynamic memory of the computer. Thus, an object is
called an instance of a class.
5. Why is a class known as composite data type?
A class is known as a composite data type because classes are made up of primitive data types.
They are more complicated than primitive data types. They contain both characteristics and
behavior.
6. Write a statement to create an object “keyword” of the class “Composite”.
Composite keyword = new Composite();
7. What does the following statement mean?
Employee staff = new Employee();
In the above statement, an object named ‘staff’ is being created that belongs to the class
named ‘Employee’.
8. Why is an object called an instance of a class? Explain.
An object is an instance of a class because every object created has its own set of unique
properties and state, even though they belong to the same class.
9. Why is a class known as a composite data type?
A class is more complex than the available primitive data types. It can contain both data and
functions. Thus, a class is known as a composite data type.
10. Write a statement to create an object ‘keyboard’ of the class ‘Computer’.
Computer keyboard = new Computer();

5
L-4 Operators in Java

ANSWER THE FOLLOWING QUESTIONS:


1. What is an operator?
An operator is a symbol or token which performs arithmetical or logical operations to give
meaningful result.
2. Name the different types of operators.
The different types of operators are:
a. Unary
b. Binary
c. Ternary.
3. Explain the following:
a. Arithmetical operator: The operators used to perform arithmetical calculations in a
program are known as arithmetical operators.
b. Relational operator: The operators used to show the relationship between the operands by
comparing the values of the variables and give the result as true or false, are known as
relational operators.
c. Logical operator: AND, OR and NOT are logical operators, that yield true or false depending
upon the outcome of different expressions.
d. Unary operator: The operators applied to a single operand is known as unary operator.
e. new operator: The new operator is used to allocate space in the dynamic memory for the
storage of data and functions belonging to an object in Java programming.
f. Binary operator: The operators that deal with two operands is known as binary operator.
4. What is a ternary operator? Explain with the help of an example.
The ternary operator deals with three operands. It is also known as a conditional operator. In
this operator, the value of the variable depends on the logical expression.
Example:
int a = 5:
int b = 3;
int max = (a > b)? a : b;
5. Differentiate between the following:
a. Arithmetical operator and logical operator: The operators used to perform arithmetical
calculations in a program are known as arithmetical operators. AND, OR and NOT are logical
operators, that yield true or false depending upon the outcome of different expressions.
b. Binary operator and Ternary operator: The operators that deal with two operands is
known as binary operator. The ternary operator deals with three operands. It is also known as a
conditional operator. In this operator, the value of the variable depends on the logical
expression.
c. Logical AND and Logical OR: Logical AND combines two or more boolean expressions in
such a way that it results in true if all the expressions are true, otherwise it returns false. Logical
OR combines two or more boolean expressions in such a way that it will return true when any
one condition is true, otherwise it returns false.
d. Prefix operator and Postfix operator: Prefix means that the value of the variable changes
before the operation takes place. Postfix means that the operand will change after performing
the operation.
e. System.out.print() and System.out.println(): When we use print(), the cursor remains in
the same line after producing the result. But in println(), the cursor shifts to the next line after
producing the result.
6. Differentiate between an operator and an expression.
An operator is a symbol or token which performs arithmetical or logical operations to give
meaningful result. An expression is any meaningful combination of variables, constants and/or
operators, that, when calculated, yields a value.
7. If m = 5 and n = 2, then what will be the output of m and n after execution that will
store in (a) and (b)?
a. m -= n;
m = m – n;
=5–2
=3
b. n = m + m / n;
n=5+5/2
=5+2
=7
8. State the difference between = and ==.
= is an assignment operator. It assigns a value to a variable. == is a relational operator. It
compares two values for equality.
9. What will be the output of the following program segment?
int a = 0, b = 10, c = 40;
a = --b + c++ + b;
System.out.println("a = " + a);

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:

1. Name the different ways to manage the flow of control in a program.


Normal flow, Bi-directional flow, Multiple branching.
2. Mention one statement each to achieve:
(a) Bi-directional flow of control
if-else statement
(b) Multiple branching of control
switch case statement
3. Explain the following statements with their constructs:
(a) nested if
One if statement inside the other is termed as nested if statement.
if(condition 1){
if(condition 2){
statement(s)
}
}

(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)
}

4. Differentiate between if and switch statement.


The if statement works with all kinds of comparisons using a variety of relational and logical operators. The switch
statement on the other hand checks only for equality of two values.
5. What is the purpose of switch statement in a program?
The switch statement is a multi-branching statement where out of several options, any one gets executed and the
rest are skipped.
6. Explain with the help of an example, the purpose of default in a switch statement.
The default case is executed when none of the listed cases are matched.
switch(5){
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Other");
}

7. Is it necessary to use break statement in a switch case statement? Explain.


The break statement is optional in a switch statement. If not provided, then it may lead to fall through in switch
statement in which the flow of control can move from one case to another.

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");
}

10. Explain with an example the if-else-if construct.


if(marks >= 80)
System.out.println("A");
else if(marks >= 60)
System.out.println("B");
else if(marks >= 40)
System.out.println("C");
else
System.out.println("D");

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.

11. Name two jump statements and their use.


The break statement is used to exit from a loop or switch statement.
The continue statement is used to move to the next iteration of the loop.
12. Give two differences between the switch statement and the if-else statement.
The switch statement only checks for equality, whereas the if-else statement can do all kinds of comparisons. The
switch case is a multi-branching statement whereas the if-else statement is a bi-directional statement.
Predict the output of the given snippet, when executed:

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;
}

What will be the value of x and y if n assumes a value (i) 1 (ii) 0?


OUTPUT when n = 1:
x = 2, y = 2
OUTPUT when n = 0:
x = 1, 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

Mathematical Library Methods

I. CHOOSE THE CORRECT ANSWER:


1. Which of the following is false to find square of a number?
a) Math.pow(a, 2)
b) a * a
c) Math.sqrt(a, 2)
d) All of the above
2. What type of value is returned by Math.sqrt()?
a) int
b) float
c) double
d) All
3. Which of the following syntax is true to find the square root of a number?
a) sqrt(a)
b) Math.sqrt(a)
c) Squareroot(a)
d) None
4. Name the class that is used for different mathematical functions.
a) Java.Math
b) Java.Power
c) Java.Sqrt
d) None
5. Give the output of the Math.abs(x); when x = -9.99.
a) -9.99
b) 9.99
c) 0.99
d) None
6. Give the output of Math.sqrt(x); when x = 9.0.
a) 3
b) 3.0
c) 3.00
d) All

II. PREDICT THE OUTPUT:


1. System.out.println(Math.sqrt(10.24));
3.2
2. System.out.println(Math.rint(-99.4));
-99.0
3. System.out.println(Math.cbrt(42.875));
3.5
4. System.out.println(Math.min(-25.5, -12.5));
-25.5
5. System.out.println(Math.ceil(-0.95));
-0.0
6. System.out.println(Math.round(-18.51));
-19
7. System.out.println(Math.max(-77.6, -87.45));
-77.6
8. System.out.println(Math.floor(-0.88));
-1.0
9. System.out.println(Math.rint(98.5));
98.0
10. System.out.println(Math.ceil(65.5));
66.0

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)

IV. PREDICT THE RETURN DATA TYPE OF THE FOLLOWING FUNCTIONS:


1. Math.sqrt() – double
2. Math.rint() – double
3. Math.ceil() – double
4. Math.round() – int or long
5. Math.floor() – double
6. Math.log() – double

V. EXPLAIN THE FOLLOWING FUNCTIONS:


1. Math.random() – It returns a random number from 0 to less than 1. It always returns a value of
type double.
2. Math.max() – It returns the maximum of the given two arguments. Its return type depends on
the arguments passed.
3. Math.cbrt() – It returns the cube root of a given positive or negative number. It always returns
a value of type double.
4. Math.abs() – It returns the absolute value of a given number. Its return type depends on the
argument passed.
5. Math.log() – It returns the natural logarithmic value of a given number. It always returns a
value of type double.

VI. DISTINGUISH BETWEEN THEM USING SUITABLE EXAMPLES:


1. Math.ceil() and Math.floor()
a) Math.ceil() returns the smallest integer of type double which is greater than or equal to the
argument. Math.floor() returns the largest integer of type double that is less than or equal to the
given argument.
double a = Math.ceil(2.3); //a receives 3.0
double b = Math.floor(2.3); //b receives 2.0
2. Math.rint() and Math.round()
Math.rint() returns the integer part in type double of a given number if the fractional part is up
to 0.5, otherwise it returns the next higher integer value in type double. Math.round() returns
the integer part in type double if the fractional part is less than 0.5, otherwise it returns the next
higher integer value.
double p = Math.rint(2.5); //p receives 2.0
double q = Math.round(2.5); //q receives 3.0

11

You might also like