0% found this document useful (0 votes)
26 views17 pages

CLASS XI CS Notes

for 11th and 12th students

Uploaded by

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

CLASS XI CS Notes

for 11th and 12th students

Uploaded by

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

INTRODUCTION TO JAVA LANGUAGE

UNIT 1:OBJECT ORIENTED PROGRAMMING

Write short answers

Question 1

What do you understand by Object Oriented Programming? Name two OOP


languages.

Answer

Object Oriented Programming is a modular approach of programming that


gives stress to data over functions. It decomposes the program into a
number of entities called Objects. Each Object contains data and functions
to operate on that data. Objects cannot access each other's data, they only
communicate through functions.

Two OOP languages are:

1. Java
2. C++

Question 2

Name any two OOP's principles.


Answer
Two OOP's principles are:

1. Abstraction
2. Encapsulation

Question 3

What are the features of OOP?

Answer

Features of Object Oriented Programming are:

1. It gives stress on the data items rather than functions.


2. It makes the complete program/problem simpler by dividing into
number of objects.
3. The objects share information through functions.
4. Data remain secure from outer interference.
Question 4
Define the following:

(a) Inheritance
Answer

Inheritance enables new classes to receive or inherit the properties and


methods of existing classes. The class that acquires the properties is called
the child class or sub-class or target whereas the class that gets inherited to
another class is called the base class or super class.

(b) Polymorphism

Answer

Polymorphism is the process of using a function for more than one


purposes. It allows the use of different internal structure of the object by
keeping the same external interface.

(c) Encapsulation

Answer
The system of wrapping of data and functions of an object together as a unit
in such a way that the data items are accessible only within the functions of
the same object is known as Encapsulation.

(d) Data Hiding


Answer
The process of isolating the data such that it cannot be accessed directly
outside the class premises although they are available in the same program
is known as Data Hiding.

Question 5

Write two disadvantages of Procedure Oriented Programming.

Answer

1. As data values are global to all the functions, you may be required to
make necessary changes in the function due to any change in the data
values.
2. It is not suitable to solve complex problems in real situation.

Question 6
How can 'Data Abstraction' be made useful in the real world? Explain with an example.
Answer
Electrical switchboard is an example of Data Abstraction in real world. To
switch on or off a particular light or fan, we press the required switch to on
or off position. We don't worry about the things like how the the circuit is
affected by the switch, what is the internal wiring, etc. Switchboard hides all
such background details from us and provides a very simple way to operate
the lights, fans and other electrical appliances.

Question 7

What are the benefits of OOP?


Answer

1. An existing class can be extended through Inheritance.


2. Data hiding enables writing secured programs.
3. Multiple instances of an object can be generated to co-exist without any
interference.
4. Different modules can be created in the project using objects.
5. It is highly beneficial in solving complex problems.
6. OOP makes it easier to modify and maintain software complexity.

Question 8

TV set is a real world object. How?

Answer

A real world object is an entity having a specific identity, specific


characteristics and specific behavior. A television can be considered as an
object that posses the following characteristics and behaviours:

Characteristics Behaviours

It is rectangular in shape. It is used to view news.

It has a flat screen. It is also used to watch games.

It contains a number of It is used to view different entertainment


channels. channels.

Moreover a television set is owned by someone giving it an identity. Hence,


television set is a real world object.
Question 9
In what way Data Hiding is related to Data Abstraction?

Answer
Data Hiding and Data Abstraction are complementary concepts. Data
Abstraction focuses on the observable behaviour of an object, whereas Data
hiding or Data Encapsulation focuses upon the implementation that gives
rise to this behaviour. In other words, Data Abstraction cares about what
something does but not how it does it. Data Encapsulation cares about how
something does what it does such that others don't have to worry about the
implementation details. Hence, we can say that Encapsulation is a way to
implement Data Abstraction.

Question 10

Explain Polymorphism with an example.

Answer

In object-oriented programming, Polymorphism provides the means to


perform a single action in multiple different ways. Taking the real world
example of animals, if we ask different animals to speak, they respond in
their own way. Dog barks, duck quacks, cat says meow and so on. So the
same action of speaking is performed in different ways by different animals.

Question 11
Differentiate Encapsulation and Inheritance.

Answer

Encapsulation Inheritance

Encapsulation binds the data and Inheritance enables new classes to


the functions that operate on that receive or inherit the properties and
data into a single unit. methods of existing classes.

It supports Data Hiding. It supports code reusability.

It keeps data safe from outside It allows us to do hierarchical


interference. classification of data.
UNIT 2 : OBJECTS AND CLASSES
Answer the following

Question 1

What is the purpose of 'new' operator?

Answer

The purpose of new operator is to instantiate an object of the class by


dynamically allocating memory for it.

Question 2

In what way a class and an object are interrelated?

Answer
A Class is used to create various Objects that have different characteristics
and common behaviours. Each object follows all the features defined within
a class. Hence, class is also referred to as a blue print or prototype of an
object.

Question 3

Write a statement to create an object MP4 belonging to class 'Digital'.


Answer

Digital MP4 = new Digital();

Question 4
What do you mean by the following statement?
Employee staff = new Employee();
Answer

This statement creates a new object of class Employee. The newly created
object is assigned to a variable named staff which is of Employee type. The
object can be accessed using staff variable.

Question 5

Why is class referred to as object factory?


Answer
A class has the complete description of the data elements the object will
contain, the methods the object can do, the way these data elements and
methods can be accessed. A class can create objects of itself with different
characteristics and common behaviour just like a factory can produce similar
items based on a particular design. Hence, class is also referred to as 'Object
Factory'.

Question 6

An object is referred as an instance of a class. Explain.


Answer

A class can create objects of itself with different characteristics and common
behaviour. So, we can say that an Object represents a specific state of the
class. For these reasons, an Object is called an Instance of a Class.

Question 7
What do you mean by Abstract class?

Answer
A class declared using the abstract keyword is called an Abstract Class. We
cannot instantiate objects of Abstract Class. An Abstract Class can contain
abstract methods (methods without body) and concrete methods (normal
methods with body). The sub-class inheriting the abstract class needs to
provide the definition of abstract methods of the super-class otherwise sub-
class also needs to be abstract.

Question 8
In what way is a class helpful in creating a user defined data type?

Answer
We may need to use a group of primitive types so that a number of values
can be operated simultaneously by using a single interface. To do so, user
may define a class with a specific name that serves as a specific data type.
This is the reason, a class is referred to as user defined data type.

Question 9
What do you mean by instantiating a class?

Answer

Instantiating a class means creating an object of the class. A class is


instantiated with the help of new operator that allocates space in dynamic
memory for the storage of an object.

Question 10
Design a class 'Time' with the following specifications:
class Time

Data members/Instance variables


int hour, min, sec

Member Methods:
void get_time( ) : to accept a time in hour, minute and second.
void show_time( ) : to display the time in terms of hour, minute and second.

Write a main method to create an object of class 'Time' and call the member
methods.
Answer

import java.util.Scanner;

class Time {
int hour;
int min;
int sec;

void get_time() {
Scanner in = new Scanner(System.in);
System.out.print("Enter hours: ");
hour = in.nextInt();
System.out.print("Enter minutes: ");
min = in.nextInt();
System.out.print("Enter seconds: ");
sec = in.nextInt();
}

void show_time() {
System.out.println(hour + ":" + min + ":" + sec);
}

public static void main(String args[]) {


Time obj = new Time();
obj.get_time();
obj.show_time();
}
}

Question 11

Design a class 'Rectangle' with the following specifications:

class Rectangle
Data members/Instance variables
int length, breadth, area, perimeter

Member methods:
void input( ) : to accept length and breadth of a rectangle.
void calculate( ) : to calculate area and perimeter of rectangle.
void display( ) : to print area and perimeter of rectangle.
Write a main method to create an object of class 'Rectangle' and call the member
methods.

Answer
import java.util.Scanner;

class Rectangle {
int length;
int breadth;
int area;
int perimeter;

void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter length of rectangle: ");
length = in.nextInt();
System.out.print("Enter breadth of rectangle: ");
breadth = in.nextInt();
}

void calculate() {
area = length * breadth;
perimeter = 2 * (length + breadth);
}

void display() {
System.out.println("Area of Rectangle = " + area);
System.out.println("Perimeter of Rectangle = " + perimeter);
}

public static void main(String args[]) {


Rectangle obj = new Rectangle();
obj.input();
obj.calculate();
obj.display();
}
}
Question 12
Write a class template 'Calculator' with the following specifications:

class Calculator
Data members/Instance variables
int a,b,c

Member Methods:
void readdata( ) : to accept the values of a and b.
void add( ) : to add a and b and place the result in c.
void sub( ) : to subtract b from a and place the result in c. Display the result.
void mul( ) : to multiply a and b and place the result in c. Display the result.
void div( ) : to divide a by b and place the result in c. Display the result.

Write a main method to create an object of class 'Calculator' and call the member
methods to enable the task.

Answer

import java.util.Scanner;

class Calculator {
int a;
int b;
int c;

void readdata() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
a = in.nextInt();
System.out.print("Enter b: ");
b = in.nextInt();
}

void add() {
c = a + b;
System.out.println("Addition Result = " + c);
}

void sub() {
c = a - b;
System.out.println("Subtraction Result = " + c);

void mul() {
c = a * b;
System.out.println("Multiplication Result = " + c);

void div() {
c = a / b;
System.out.println("Division Result = " + c);

public static void main(String args[]) {


Calculator obj = new Calculator();
obj.readdata();
obj.add();
obj.sub();
obj.mul();
obj.div();
}
}
UNIT 3: HISTORY AND DEVELOPMENT OF JAVA

Write short answers

Question 1
What is Java? What was it called initially?
Answer
Java is an Object Oriented programming language developed in 1991 by
James Gosling at Sun Microsystems. It was initially called 'Oak'.

Question 2
What are the features of Java?
Answer
The features of Java are:

1. It is an Object Oriented Programming Language.


2. It is platform independent. It provides us Write Once, Run Anywhere
(WORA) feature.
3. It uses a compiler as well as an interpreter.
4. It is case sensitive.
5. It is a multithreaded language.

Question 3
Name a Java package, which is imported by default.
Answer
java.lang

Question 4
Explain briefly about the development of Java language.
Answer
In 1991, at Sun Microsystems, Green team led by James Gosling started
working on a new technology for consumer electronic devices. Over the
next 18 months, in 1992 the team created a new programming language
which they called “Oak”. By 1994 the team refocussed their efforts
towards internet programming with Oak as it didn't find much traction in
consumer electronics space. Oak was renamed to Java and on 23rd of
May 1995, Sun microsystems made its first public release.

Question 5
What are the different ways to give comments in Java Programming?
Answer
There are three ways to write comments in Java:

1. Single Line comment.


2. Multi line comment.
3. Documentation comment.

Question 6
Explain the significance of the following Java library packages.
(a) java.lang
Answer
It provides classes that are fundamental to the design of the Java
programming language. Some of the important classes it contains are
Object, which is the root of the class hierarchy, Class, instances of which
represent classes at run time, Wrapper classes and Math class, which
provides commonly used mathematical functions such as sine, cosine,
square root, etc.
(b) java.io
Answer
It contains the classes that handle fundamental input and output
operations in Java.
(c) java.math
Answer
java.math package provides classes for performing arbitrary-precision
integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic
(BigDecimal). Refer to the official documentation for more details.
Clarification: Mathematical Library Functions like min(), max(), abs(), avg(),
floor(), ceil(), round(), sqrt(), pow(), etc. are present in Math class of
java.lang package. java.lang.Math class should not be confused with
java.math package.
Question 7
Give an example of each with reference to Java programming:
(a) Single Line comment
Answer
// This is my first program.
(b) Multi Line comment
Answer
/* This is my first program.
It calculates the sum of two numbers.*/

Question 8
Define the terms:
(a) Source code
Answer
A set of statements written in a High-Level programming language like
Java, C++, Python, etc. is called as Source Code.
(b) Machine code
Answer
Machine code is a low-level programming language. Instructions in
machine code are written as a sequence of 0s and 1s. It is directly
understood and executed by the processor.
(c) Byte code
Answer
Java compiler converts Java source code into an intermediate binary code
called Bytecode. Bytecode can't be executed directly on the processor. It
needs to be converted into Machine Code first.

Question 9
Distinguish between:
(a) Compiler and Interpreter
Answer
Compiler Interpreter

It converts the whole source


It converts the source program into
program into object program at
object program one line at a time.
once.

It displays the errors for the It displays the errors of one line at a
whole program together after time and after debugging the control
compilation. goes to the next line.

(b) Java (JDK 1.5) and BlueJ


Answer

JDK 1.5 BlueJ

JDK or Java Development Kit is the BlueJ is an IDE or Integrated


set of tools required to compile and Development Environment for
run Java programs developing Java programs.

BlueJ provides tools like Code


JDK includes tools like Compiler,
Editor, Debugger, Syntax
Interpreter, Java libraries, etc.
Highlighting, etc.

IDE isn't essential for developing


JDK is essential for developing
Java programs but it makes the
Java programs.
process easier and efficient.

Question 10
What is BlueJ? What are the features of BlueJ?
Answer
BlueJ is an integrated development environment for Java. It was created
for teaching Object Oriented programming to computer science students.
Features of BlueJ are:

1. Simple beginner friendly graphical user interface.


2. It allows creating objects of the class dynamically, invoking their
methods and also supplying data to the method arguments if
present.
3. It supports syntax highlighting. (Syntax highlighting means showing
the different tokens of the program like keywords, variables,
separators, etc. in different colours so that they show up more
clearly.)
4. It facilitates easier debugging as lines causing compilation errors are
marked clearly and the error is displayed at the bottom of the
window.
5. It provides a code editor, compiler and debugger integrated into a
single tool.

Question 11
Write down the syntax of output statement in Java programming with an
example
Answer
We commonly use two output statements in Java. There syntax are:

1. System.out.println(<output value>); — This statement prints data on


the console. The data to be printed is passed to the println method as
a String. After printing the string, it places the cursor at the start of
the next line. So the next printing happens at the start of the next
line.
2. System.out.print(<output value>); — This statement also prints data
on the console. The data to be printed is passed to the print method
as a String. After printing the string, the cursor remains on the same
line at the end of the printed string. So the next printing starts in the
same line just after the end of the previous printed string.

As an example, the below statements will generate the following output:


System.out.println("JVM stands for");
System.out.print("Java ");
System.out.print("Virtual ");
System.out.print("Machine");

Output

JVM stands for


Java Virtual Machine

Question 12
What do you understand by Java reserved words? Name at least five Java
reserved words which are commonly used in Java programming.
Answer
In Java, a reserved word is a word that has a predefined meaning in the
language. Due to this, reserved words can’t be used as names for
variables, methods, classes or any other identifier. Reserved words are
also known as keywords. Five commonly used Java reserved words are:

1. public
2. class
3. int
4. double
5. char

Question 13
Write down all the steps to compile and execute a Java program.
Answer

1. Right click on the class icon in the BlueJ main window.


2. Select the compile option from the menu that appears.
3. Again right click on the class icon in the BlueJ main window and
select the option void main(String[] args).
4. Click OK on the method call window that appears on the screen.
5. The desired output of the program will be shown on the screen.

Question 14
'Java interpreter is also called Java Virtual Machine'. Justify the statement.
Answer
Java Virtual Machine takes Bytecode as input and converts it into Machine
Code one line at a time. This Bytecode can be generated by compiling
source code written in any JVM language like Scala, Kotlin, etc not just
Java. Hence, Java interpreter is called Java Virtual Machine.

Question 15
A Java program uses Compiler as well as Interpreter. Explain.
Answer
Java compiler compiles Java source code to Bytecode. Bytecode cannot
run on the processor directly as processor only understands Machine
Code. Java Virtual Machine (JVM) takes this Bytecode as input and
converts it into Machine Code line by line. So, JVM acts as an interpreter
for converting Bytecode to Machine Code. In this way, a Java program
uses both a Compiler as well as an Interpreter to get executed on the
processor.
Question 16
Distinguish between System.out.print() and System.out.println() with an
example.
Answer

System.out.println() System.out.print()

It prints data to the console but the


It prints data to the console and
cursor remains at the end of the data
places the cursor in the next line.
in the same line.

Next printing takes place from Next printing takes place from the
next line. same line.

Question 17
How can you modify a Java program on BlueJ platform?
Answer
To modify a Java program on BlueJ platform we will double click the class
icon to open the source code of the class. Now we will make the required
modification to this Java program.

You might also like