CLASS XI CS Notes
CLASS XI CS Notes
Question 1
Answer
1. Java
2. C++
Question 2
1. Abstraction
2. Encapsulation
Question 3
Answer
(a) Inheritance
Answer
(b) Polymorphism
Answer
(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.
Question 5
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
Question 8
Answer
Characteristics Behaviours
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
Answer
Question 11
Differentiate Encapsulation and Inheritance.
Answer
Encapsulation Inheritance
Question 1
Answer
Question 2
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
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
Question 6
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
Question 10
Design a class 'Time' with the following specifications:
class Time
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);
}
Question 11
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);
}
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);
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:
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:
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 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.
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:
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:
Output
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
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()
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.