NOTE FOR JAVA BY MUKUL SINHA SIR
Revision of Class IX Syllabus
Unit1: INTRODUCTION TO OBJECT-ORIENTED CONCEPTS
VERY SHORT & SHORT ANSWER TYPE QUESTIONS Q1. Name two OOP principles.
Ans:-
Encapsulation and Abstraction are two OOP's principles.
Other principles are
Polymorphism, Inheritance, Dynamic Binding and Message Passing
Q2. What is an Object?
Ans:- An object
is an identifiable entity, which contains characteristics and behaviors. To create an object in Java we write
Employee e=new Employee();
Q3. What is Polymorphism?
Ans:-
Polymorphism is an act of using a function for more than one purpose.
Example of Polymorphism
public class Addition
{
int sum;
public void Add(int a, int b)
{
sum=a+b;
}
public void Add(int a, int b,int c) //this method overloads previous Add method
{
sum=a+b+c;
}
}
Here Add function/method is defined twice with a different set of signatures in first method two parameters are defined whereas in the second method three parameters(a, b, c) are defined but the name of both the methods are of same name.So here we are doing function overloading(polymorphism).
Explanation:-Polymorphism, as its name implies, comes in a variety of forms. OOP allows for the use of a variety of techniques I. Simply expressed, they have to do with how methods are used, and how different methods operate for different objects. Method Overriding and Method Overloading are two ways that polymorphic methods allow for different actions to be done.
Q4. Name two OOP languages.
Ans:- Java and
C++ are object-oriented programming languages.
Q5. What is byte code?
Ans:- Byte code
is the end product of the compiler(software to convert a source code into byte
code). Java Interpreter further converts byte code to machine code.
Q6. What is JVM?
Ans:- JVM
stands for Java Virtual Machine also called Java interpreter. It converts byte
code into machine code.
Q7. Name two packages used in Java.
Ans- java.io
and java. lang are two packages in java.(Java Packages is a collection of pre-defined or built-in classes in a single folder)
Other java packages are
lang, util, io, applet, net, swing etc.
Q8. Name a package, which java language is used by default.
Ans- java. lang
is a package, in which Java language used by default
Q9. Name a keyword applied to include a package.
Ans:-"import" is a keyword applied to include a package.
Q10. What is a keyword? Give example.
Ans:- A keyword is a reserved word that carries a special meaning for the
compiler. A keyword cannot be used as a variable.
Q11. Name two types of comments used in Java.
Ans:- Single line comment(//) , multiline comment(/*.........*/ and document comments )
Q12. Name a windows based environment in Java.
Ans:- Blue J is a Windows based
environment in Java.
Q13. Who developed Java?
Ans:- Java was initially developed Sir James Gosling.
Q14. What is the
hypothetical name of java?
Ans:- OAK is the hypothetical name of java.
Q15. What is a Source Code?
Ans:- A Source code is a program in a high-level language (say Java). entered
through the console is known as a source code.
Q16. What do you mean by Applet?
Ans:- Java Applets are Java programs, which can be downloaded from the Internet.
Q17. What is an Object? Give examples of some real-world objects.
Ans:- An object
is an instance of a class. Basically, an object is a run-time entity that works for the implementation of a class.
Q18. What is a class How it is related to objects?
Ans:- A class
is a collection of objects, objects are created when class is formed. Actually, the object implements class. It(object) works as it is defined in the class.
Q19. Define Data Abstraction. Explain with example. (ICSE
2010,2016)
Ans:-An
abstraction is an act of representing essential features without including
background details. For an example, an abstraction is like a switchboard, you only
press certain switches according to your requirement.
Q20. Define Encapsulation.
Ans:-Encapsulation
is an act of combining both data and the functions that operate on the data
under a single unit. encapsulation, in a way, implements abstraction. In other
words, wrapping up data and its member functions into a single unit.
Q21. What is message Passing amongst objects?
Ans:-A way of
sending/ receiving information to/from another object.
Q22. What is the method? What do they represent?
Ans:-A member
function/methods representing some behavior of an object.
Q23. What is Information Hiding(Data Hiding)?
Ans:- An
abstraction is an act of representing essential features without including
background details. For example, an abstraction is like a switchboard, you only
press certain switches according to your requirement.
This phenomenon is called information
hiding.
Q24.Name two elements that are essential to define a class. [ICSE
2001]. .
Ans:-Two
elements which are essential to define a class are (1)
Data Members (2) Member Methods
Q25.Predict the output when
int a=5, b=18,c=6;
(i) a-=(++a + --b)*c++; Ans (-133)
(ii) c=c*(a-- + b++)+2*++a; Ans(148)
Solution:-
(i)
Here a=5 b=18 and c=6
first calculate (++a + --b)*c++
(++a + --b
) + c++
(6 + 17
) + 6(as c incremented after calculation post
incremented so taken as c=6 not c=7)
=23+6=29
second calculate a-=(calculated part) which is equal to a=a-(calculated
part)
a=a-29
a=5-29=-24
(ii) Here again
a=5 b=18 and c=6
first calculate c*(a-- + b++)+2*++a;
c * (
a-- + b++ )
+ 2 *
++a;
6
x ( 5
+ 18 )
+ 2 *
6;
6 x (23)+12=138+12=150
Q26. What is JDK?
Ans:- JDK stands for Java Development
Tool Kit contains various library classes which in turn possess different packages.
Q27.Name any two versions of JDK?
Ans: Two
version of JDK are
1. JDK1.0(Oldest)
2. JDK 8 or JDK 9(Latest)
Q28. What are Compilers?
Ans: Compiler
is the soft wares that convert the
complete program to byte code
[Source Code(Code in High Level
Language)->Byte Code(Code in 0101010)]
Like Turbo C or Turbo C++ for (C and C
++ Language)
and JDK for Java Language
Q29.What are Interpreters?
Ans: Interpreters
are again software that converts line by line to machine code, not the whole
program.
Like typing MS-DOS commands
Q30.Mention Two features of Java
Language.
Ans: Two
features of Java Language are
(1) Robust -WORA(Write Once and Run
anywhere)
(2) Java is an Object-Oriented Programming
Language.
Chapter 1(Introduction to classes)
LONG ANSWER TYPE QUESTIONS
Q1. How real-world objects implemented/represented in software terms
Ans:- The
object is implemented in software terms as follows:-
1. Characteristics /attributes are implemented
through member variables or data items of the object.
2. Behaviours is implemented through
member functions called methods.
3. Data and methods are encapsulated
into one unit and given a unique name to give it an identity.
Q2. What do you mean by abstraction and encapsulation? How are these two
terms interrelated?
Or
Define Encapsulation. (ICSE 2016)
Ans:-Abstraction An
abstraction is an act of representing essential features without including
background details. For example, an abstraction is like a switchboard, you only
press certain switches according to your requirement.
This phenomenon is
called information hiding.
Encapsulation is an act
of combining both data and the functions that operate on the data under a
single unit. encapsulation, in a way, implements abstraction. In other words, wrapping up of data and its member functions into a single unit.
Q3. Encapsulation is major properties of OOP. How is it implemented in
software terms?
Ans:- Encapsulation is an act of combining both data and the functions that
operate on the data under a single unit. encapsulation, in a way, implements
abstraction. In other words, wrapping up of data and its member functions into a
single unit. An object binds together its associated functions under one unit thereby enforcing
encapsulation. It means wrapping up of data and its associated functions
together into a single unit.
Q4. Describe OOP's How is it advantageous over conventional POP?
Ans: - POP is
a Procedural oriented language. In this system of programming, the stress is on
functions other than data. Data keep floating throughout the program. Hence, to
rectify any error you may need to scan the whole program from beginning to end.
On the other hand, OOP stresses data
rather than function. The data is confined and used within the specified part
of the program. Errors caused can be rectified by scanning specific parts of the
program only.
Q5. Mention four features f OOP's.
Ans: -Four
Features of OOP's are:-
1. It gives stress on data rather
than procedures.
2. The object can be used as a bridge to have data flow from one function to
another.
3. It makes the program simple by
dividing it into a number of objects.
4. Data can easily be modified without
any change in the function.
Q6. Name four basic principles of OOP.
Ans:- Four basic principles of OOP are:-
1)Object:- An instance of a class
2)Class:
- A prototype definition having Data Members and Member Methods
3) Encapsulation:- Encapsulation is an act of combining both data and
the functions that operate on the data under a single unit.
4) Abstraction:- An abstraction is an act of representing essential
features without including background details.
Q7.Mention Four Features of Blue J.
Ans:- Four
Features of Blue J are:-
1) Windows-based platform
2) Provides a sample program as it is
activated.
3) Menu-Driven Approach.
4) Comparatively easier to use as
compared to JDK.
Q8.Differentiate b/w Byte Code and Source code.
Ans: Source code
is the program developed in java Language, which is input to a computer through a keyboard.
Compiler converts source code to Byte
Code.
Source code is written in any High-Level Language(C, C++, and Java)
Whereas Byte Code is a machine
convertible code done by Compiler of any Language.
Q9. Distinguish b/w Java Application and Java Applet.
Ans:- Java Application is a program developed by users (in and High-Level
languages) where Java Applets are downloaded from the Internet(or Internet-based
programs embedded in HTML page).
Q10. Write down the historical evolution of JAVA.
Ans:-Java is an Object-Oriented Programming Language, developed by Sir James
Gosling at Sun Micro System. This language was initially called Oak (named
after the tree outside Gosling's Office) >Later the language was professionally
named by Java.
Q11. How is a class referred to as Abstract Data Type?
Ans:- An abstraction is an act of representing essential features without
including background details. A class deals with the data through functions. The
data follows the features of abstraction in its termed as abstract data type.
Q12. "Encapsulation promotes data hiding". Comment.
Ans:- Encapsulation is a feature to wrap data and function together as a unit
in such a way that the data members are only applied within the functions.
These data members are not accessed outside the class. Hence, it is promoted
data hiding.
Q13. Mention four benefits of OOP's.
Ans:- Four benefits of OOP's are:-
1) Use of existing
class can be extended through inheritance.
2) Using data hiding can generate secured
program concepts.
3) Modular programming
approach can be generated through objects.
4) Highly beneficial to
solve complex problems.
Q14. Definition of the class by code:
class ABC
{
//Data Members
int a, b, c;
double v1, v2, v3;
char ch1, ch2, ch3;
float f1, f2, f3;
// Member Methods
public void Add()
{
//statements
}}}
Chapter 2
DATA TYPES IN JAVA
VERY SHORT & SHORT ANSWER TYPE QUESTIONS
Q1.What do you mean by data type?
Ans:- It is a
variable that is used throughout the programming as a constant to give meaningful results.
Q2.What are tokens?
Ans:- Each individual character used in a statement is termed as token.
Q3. What do you mean by identifiers?
Ans Identifiers
are the name given to different part of the program in other words
identifiers(variables ) is the name of the memory location which contains a
constant.
Q4. What do mean by assignment of a variable?
Ans: Assignment is the store of a
constant value to a variable.
e.g. int a=4; char ch='@' double b=7.78585;
Q5. How to declare a variable? Show with the Help of an example.
Ans: Syntax to declare a variable is as follows:-
<data
type><space><variable name>
e.g. int x;
Q6. Differentiate b/w static and dynamic initialization.
Ans: When a variable is initialized with a specific constant before its use in
the Operation it is termed as static initialization whereas, a variable that
is initialized with the outcome of any function is known as dynamic
initialization;
e.g.
//static initialization
int x=10; char ch='@';
double d=8.7478;
//dynamic
initialization
int x=Math.round(8.6); double d=Math.sqrt(3)
Q7. What do you mean by Boolean data type?
Ans: Boolean data type represents the value true or false.
Q8. What do you mean by Charter Data Type?
Ans: A character data type represents a character used in a Program. It may be
an alphabet, a digit, or any special character.
Q9. What do you mean by mixed expression?
Ans: An Expression, which means contains values of different data types is
known as mixed expression.
Q10. Arrange the following according to ascending order of
their usage. long, double, char, float,
int.
Ans: char, int, long, float, double.
Q11. What do you mean by coercion?
Ans:
Implicit type conversion in which the data gets converted by default to its
higher type without any intervention of the user is called coercion.
Q12. What do you mean by library package?
Ans: A package is a collection of Classes, e.g. java.io, java.lang, java.util
etc.
Q13.What is the function to round a given fractional number?
Ans: Math. round(4.9); p is a variable
whose value is to be round.
Q14. What is the function to find absolute value?
Ans:
Math.abs(-266);
Q15. Differentiate between 'A' and "A".
Ans:
'A' is a character and "A" is a String.
Q16. Differentiate between "true" and true.
Ans: "true" is enclosed under double quotes is a string 'literal'
where are true without quotes is a boolean liberal.
Q17. Why is need of using data type?
Ans: Compiler allocates memory for the storage of data. It must know in
advance .how much space has to be presented for a specific value. Hence it is
needed to declare a variable of a specific type.
Q18. What are tokens?
Ans: a token is an object that represents something else, such as
another object (either physical or virtual), or an abstract concept as, for
example, a gift is sometimes referred to as a token of the giver's
esteem for the recipient. In computers, there are a number of types
of tokens.
Q19. Mention different types of tokens used in java.
Ans: The Tokens are classified as follows:-
Literals
Integer Literal int x=10 "10"
is Integer Literal..
String Literal str="Hello"
"Hello" is a String Literal.
char
Literal char ch='@' '@' is character
Literal.
double d=8.278; 8.278 is Double Literal.
float ft=9.78; 9.78 is
float literal
long
lg=8437473; 8437473 is long
Literal.
short sht=87; 87 is Short
Literal
boolean b=true true is boolean Literal
byte bt =4; 4 is byte Literal.
(b) Identifiers(variables) Name given to
diifferent parts of a program is called Identifiers.
(c) Operators
i) Relational Operators(>, <, >=,
<=, !=)
ii) Mathematical Operators(+,-,*,/,%)
iii) Ternary Operators(?)
iii) Unary Operators(& | !=).
iv) Binary Operators(&&, ||,
<>)
v)
(d) Separators {} [] ()
(e) Punctuators . ; :
Q20. Distinguish b/w Implicit and Explicit type conversion.
Ans: In a mixed expression the data type gets converted automatically to its
higher type without any intervention of the user. It is called implicit type
conversion.
e.g. int a, char b;
int c=a+b;
The sum will be converted
into int type.
Inexplicit type
conversion the data type gets converted to another type based on the user's intervention.
int a; char b;
char c=(char)a+b;
Here result obtained
will be character type.
Q21. What is the rule of a Naming variable?
Ans:
Rules for naming
variables are:-
1) The variable may
have any number of characters.
2) It may contain an alphabet, numbers, and characters.
3) The underscore may
be applied in between the character to increase the length of a variable.
4) Variable name should
meaningful which can be easily depicted the logic.
Q22. What are Data Types?
Or
Distinguish between Primitive and Non-Primitive Data Type.
Ans: Primitive data types are the fundamental data types that are
independent.
e.g. int, char, float,
double.
Non Primitive or
reference data types are derived data types which originated from the Primitive
Data type.
e.g. class, array, interface, etc.
Q23. Write down the data types of the following :
(a) Whole Number (b)
Long Integer
(c) Fractional Number (d)
A capital letter
Ans:
(a) int (b)
long
(c) float or double (d) char
Q24. Write down the
data types of the following:-
(a) To find the square root of x.
(b) To find a round of number n.
(c) To find out x raised to the power y.
(d) To find an absolute number of p.
Ans:-
(a) Math.sqrt(x)
(b) Math.round(n)
(c) Math.pow(x,y)
(d) Math.abs(p)
Q25 Indicate whether the following variables are valid or
Invalid.
(a) Name (b)
42Pay (c) ABC4CG (d) Pay-to-date (e)
delhi-6
Ans: (a) Valid (b) Invalid (c)
Invalid (d) Valid (e) Valid
Q26. Give reasons for the following invalid variable names.
(a) xy,pq (b) a+b (c)
14 years (d) cd7p
Ans:
(a) comma is not
allowed in between
(b) + operator canot be
used.
(c) 14 as digits cannot
be used as variable name.
(d) Digit 7 is not
applicable in between the character.
POINTS TO REMEMBER CHAPTER 1
1. OOPs stands for
Object-Oriented Programming System.
2. In OOP's stress is
laid on data rather than functions.
3. OOP's support
modular programming approach.
4. C++, Java, Small
talk, Stimula-67, Effilel are some object-oriented programming languages.
5. Object is an entity,
which possesses some characteristics and behavior.
6. Class is a
collection of similar objects.
7. Each object of a
class contains the same attributes and behaviors.
8. Wrapping up data and
function into a single unit is called Encapsulation.
9. Encapsulation
promotes data hiding and data abstraction facilitates OOP's(class object,
inheritance encapsulation, Dynamic Binding Message Passing).
10. A class uses the
property of Abstraction; hence it is also called Abstract Data Type.
11. Class contains
various primitives data types. Hence, it is known as the Composite Data type.
12. A property
according to which an object of a class can acquire some characteristics from the object of another class is known as Inheritance.
13. Reusability is the process of adding some
additional features to a class without modifying its contents. It can be
attained through Inheritance.
14. Polymorphism is the
principle of OOP, which allows the function to be used for more than one purpose.
15. Polymorphism can be
produced through Function Overloading.
16. Dynamic Binding is
the process to link a function Overloading.
17. Java Language is
developed by James Gosling at Sun Micro System, USA.
18. Initially java was
named Oak (named after Oaktree outside Gosling's Office.
19. Java uses a compiler
as well as an interpreter.
20. Java programming
can be done in two ways viz., Java
Application and Java Applet.
21. Java language in
case sensitive, robust, platform-independent, and secure.
22. Java Compiler
convert Java Source Code to Byte Code.
23. Java Interpreter
converts byte code to machine code, suitable to a specific.
24. JDK stands for Java
Development Tool Kit, which contains various
library classes in the platform.
25. Reserved Words or
keywords are those words, which are reserved for the system and cannot be used
as variable names.
26. Comment
Statements are used in a program to
indicate that the action is being taken in a programming step.
27. There are two
versions of Java i.e., JDK 1.3/JDK 1.5 and Blue J JDK 1.3 is DOS-based whereas
Blue J is a Windows-based platform.
28. Blue J is the
product of Monash University Southern Denmark.
POINTS TO REMEMBER CHAPTER 2
1. Types of data used
for storage in memory is called Data
type.
2. Tokens are referred to as each individual character available in Java Statement.
3. Literals are
constants that remains unchanged throughout the discussion of a program
4. Identifiers are a
variable that can depend upon circumstances or problems.
5. Punctuators refer to
the punctuation signs like "", ' ', :,; etc
6. Literals are four
types Integer, Real, Character and String.
7. Fundamentals data
types such as int, float, char, double, etc are called Primitive data types.
8. Non Primitive data
types are derived from primitives data types e.g. class, array & interface.
9. Boolean is a specific
data type that represents true or false.
10. Pure expression
contains data of similar types.
11. Mixed Expressions
Contains Different types of data.
12. Typecasting is the
process to convert a data type to another as per user intervention or
demand.
13. A Package is a
collection of classes.
___________________________
Chapter 3
Conditional Statements
Control Structure
1. Overview
There are three kinds of control structures:
Sequential Control Structure:- In the sequential control, the statements are executed one after the other linearly in the sequence they are written). In Java the first statement to be executed in a program in the main() function. this execution ends with the last statement of the main() function.
Conditional Branches, These are special control structures that execute a set of statement(s) based on the result of a single condition. So they are also known as a selection control structure. Every condition will always result in either a true or false value. if, if-else,switch-case statement of the main() function.
Iteration (Loops) Control Structure that is used to iterate through multiple values/objects and repeatedly run specific code blocks. The basic loop types in Java are for, while, and do-while.
Branching Statements, which are used to alter the flow of control in loops. There are two types in Java: break and continue.
Short Answer Type Questions
Q1, Give the output of the following code fragment: [ICSE 2009]
when (i) option='b' (ii) option='x' (iii) option='a'
switch(option)
{
case 'a':
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");
}
Ans (i)
when (i) option='b'
Output: Robust and Secure
Robust and Secure
when (ii) option='x'
Output: Wrong Input
when (iii) option='a'
Output: Wrong Input
when (iii) option='a'
Output: Platform Independent
Q2. Give the difference between the if else-if and switch-case construct. [ICSE 2006]
Ans:
Q3. If((p>q)&&(q>r)) then
- q is the smallest number
- q is the greatest number
- p is the greatest number ✓ Ans.
Q4. if(a<b)
c=a;
else
c=b;
It can be written as:
1. c= (b<a)?a:b;
2. c= (a!=b)?a:b;
3. c= (a<b)?a:b; ✓
int m=3,n=5,p=4;
if(m==n && n!=p)
{
System.out.println(m*n);
System.out.println(n%p);
}
if((m!=n) || (n==p))
{
System.out.println(m+n);
System.out.println(m-n);
}
Output
8
-2
Explanation
The first if condition — if(m==n && n!=p), tests false as m is not equal to n. The second if condition — if((m!=n) || (n==p)) tests true so the statements inside its code block are executed printing 8 and -2 to the console.
Q6. Predict the output
int a=1,b=2,c=3;
switch(p)
{
case 1: a++;
case 2: ++b;
break;
case 3: c--;
}
System.out.println(a + "," + b + "," +c);
(i) p=1
Ouptput 2, 3, 3
Q7. Convert the following constructs as directed
(a) switch case constructs into if-else-if :
switch (n)
{
case 1:
s=a+b;
System.out.println("Sum="+s);
break;
case 2:
d=a-b;
System.out.println("Difference="+d);
break:
case 3:
p=a*b;
System.out.println("Product="+p);
break;
default:
System.out.println("Wrong Choice!");
}
Ans:
if (n == 1)
{
s = a + b;
System.out.println("Sum="+s);
}
else if (n == 2)
{
d = a - b;
System.out.println("Difference="+d);
}
else if (n == 3)
{
p = a * b;
System.out.println("Product="+p);
}
else
{
System.out.println("Wrong Choice!");
}
LOOPING CONTROL STRUCTURE
Class X: Java Programming
LOOPING [Pattern Printing]
Program 1: Print the following Pattern
(a)
*
**
***
****
*****
******
Solution:
class Pattern1
{
public static void main(String args[])
{
int i, j;
for(i=1;i<7;i++)
{
for(i=1;i<7;i++)
{
System.out.print("*");
}
System.out.println();
}
}
}
(b)
1
13
135
1357
13579
Solution:
class Pattern2
{
public static void main(String args[])
{
int i, j,x=1;
for(i=1;i<7;i++)
{ x=1;
for(i=1;i<7;i++)
{
System.out.print(x);
x=x+2;
}
System.out.println();
}
}
}
(c)
2
24
246
2468
Solution:
class Pattern3
{
public static void main(String args[ ])
{
int i, j,x=2;
for(i=1;i<5;i++)
{ x=2;
for(i=1;i<7;i++)
{
System.out.print(x);
x=x+2;
}
System.out.println();
}
}
}
(d)
1
22
333
4444
55555
666666
Solution:
class Pattern4
{
public static void main(String args[])
{
int i, j;
for(i=1;i<7;i++)
{
for(j=1;j<=i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
(e)
1
21
321
4321
54321
654321
Solution:-
class Pattern5
{
public static void main(String args[])
{
int i, j;
for(i=1;i<=6;i++)
{
for(j=i; j>0;j--)
{
System.out.print(j);
}
System.out.println();
}
}













Comments
Post a Comment