0% found this document useful (0 votes)
5 views

Class X Notes

Uploaded by

lakshay6547
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)
5 views

Class X Notes

Uploaded by

lakshay6547
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/ 14

Q. Explain the term ‘Encapsulation’ with an example?

Ans: The wrapping up to data and methods into a single units (called class) is known as
encapsulation. For example an engine of car or any vehicle contains many small
parts, which enables the entire machinery system to work. Encapsulation
property hides the inner working of
objects from the real world.
Q. What does a class encapsulate?
Ans: A class encapsulate Data Members that contains the information necessary to represent
the class and Member Functions that perform operations on the data member.
Q. How does a class enforce information hiding?
Ans: Classes enforce information hiding by means of access specifier.
Q. What is initial class?
Ans: A java program contains many classes. But one class in a Java program contains the
main() method. This class is called initial class.

Q. What is meant by private visibility of a method?


Ans: PRIVATE visibility of a Method means that only the methods in the same class are
permitted to use this method.

Functions/Methods
A. Fill in the blanks:

1. Polymorphism is encapsulated in java by function overloading.


2. The function name and the parameter list together is known as Function
Signature.
3. The access specifier, return type and the function signature together is
known as Function prototype.
4. The arguments of the function given in function prototype are called
Formal Parameters.
5. The arguments of the function given in statement that calls the function
are called Actual Parameters.
6. If a function does not return any value, the returning type in the
function prototype will be void.
7. Impure functions use Call by Reference.
8. Primitive Data Types are passed to a function by Call by Value
9. Non Primitive Data Types are passed to a function by Call by Reference.

1. What is Function? Why do we use functions while programs handling? Ans: A


named unit of a group of programs statements. This unit can be invoked from other
parts of the program.
2. Define Function prototype? Ans: The function prototype is the first line of the
function definition that tells the program about the type of the value returned by the
function and the number and types of arguments.
3. Write two advantages of using functions in a program. Ans. The Advantages of
using functions are:
a) Reduces Complexity: Breaking a large task into smaller tasks
automatically reduces complexity, thereby increasing the maintainability of the
program.
b) Reusability: Once a method is defined, it can be invoked again
and again from different segments of a program thus reusing
the method whenever required.
4. What is the use of void before function name? Ans: void data type specifies an
empty set of values and it is used as the return type for functions that do not return a
value. Thus a function that does not return a value is declared as follows. void
<functions name> (parameter list)
5. Explain the function prototype and the signature? Ans: The function prototype is
the first line of the function definitions, that tells the program about the type of the
value returned by the function and the number and type of the arguments. Function
signature basically refers to the number and types of the arguments, it is the part of
the prototype.
6. Explain the function of a return statement? Ans: The return statement is used to
return-back or exit from a function.
7. If a function contains several return statements, how many of them will be
executed? Ans One
8. Name the keyword that causes the control to transfer back to the method call.
Ans return
9. What are static members? Ans: The members that are declared static is called
static members. These members are associated with the class itself rather then
individual objects, the static members and static methods are often referred to as class
variables and methods.
10. Define an impure functionsAns: Impure Function change the state of the object
arguments they have received and then return. The following functions is the example
of an impure function:
public static void increment(Time obj, double secs)
{ time.seconds+=secs; return(Time); }

11. What is the difference between pure and impure functions?


Pure Impure
A function which does not change or
modify the state of the A function which change or modify the
argument/variable that are received state or the argument that are received
when invoked. when invoked
Pure function is also called accessor Impure function is also called Mutator
method method.
12. How are following passed in Java?
(i) primitive types  Call by Value
(ii) reference types  Call by Reference
13. What does function overloading mean? What is its significance? Ans: A Function
name having several definitions in the same scope that are differentiable by the
number or type of their arguments, is said to be an overloaded function. Function
overloading not only implements polymorphism but also reduce the number of
comparisons in a program and there by makes the programs run faster.
14. What is this keyword? What is its significance? Ans: The this keyword is used to
refer to currently calling objects.
It can be used to refer instance variable of current class
It can be used to invoke or initiate current class constructor
It can be passed as an argument in the method call
It can be passed as argument in the constructor call
Which OOP principle implements function overloading? Ans. The principle of
Polymorphism is implemented using Function Overloading.
15. When there are multiple definitions with the same function name, what makes
them different from each other? Ans. When there are multiple definitions with the
same name, the parameter-list makes them different from each other.
16. What are the different access specifiers available in Java? Ans. The different
access specifiers in Java are: default, public, protected and private.
Previous years board questions
17. Name the Java keyword that:
(i) indicates a method has no return type. void
(ii) stores the address of the currently calling object. this

18. Differentiate between private and protected visibility modifiers.


Ans. private members are accessible only in the class in which they have been
defined. protected members are accessible in the class in which they have been
defined as well in the sub classes of that class.

19. Differentiate between static and instance variables.


Static Variable Instance Variable

1. Declared with the keyword static. 1..Declared without static keyword.


2. Static member of a class is created 2.Instance member of a class comes into
when class comes into existance. existance when objects of class are created.
3. There is separate copies of instance
3.There is only one copy for all objects. member for each and every object.
4. Changes made in one variable do not affect
4. Changes made are visible to all the contents of the same variable owned by
instances. other instances.
5. Static members cannot have instance
variable. 5. Instance members can have static method.
.
1. Write the output for the following:
String s1 = "phoenix"; String s2 = "island";
System.out.println(s1.substring(0).concat(s2.substring(2)));
System.out.println(s2.toUpperCase());
OUTPUT: phoenixland ISLANDE
2. Write Java statement to create an object mp4 of class digital.
digital ob = new digital()
3. Name the keyword
i. Which indicate that a method has no return type  void
ii. Which declares a variable as class variable.  static
4. Write the prototype of a function check which takes an integer as an argument and
returns a character
Ans  char check(int a)
5. Give the prototype of a function search which receives a sentence sentnc and a
word wrd and returns 1 or 0 ?
int search(String sentnc, String wrd)
6. Write a function prototype of the following: [2]
A function PosChar which takes a string argument and a character argument and
returns an integer value.
int poschar(String a, char c)
7. Give the prototype of a function check which receives a character ch and an
integer n and returns true or false.
Ans: boolean check(char ch, int n)
8. Differentiate between private and protected visibility modifiers.
Ans. private members are accessible only in the class in which they have been
defined. protected members are accessible in the class in which they have been
defined as well in the sub classes of that class.
9. Write two advantages of using functions in a program.
Ans. i) Function make code reusable.
ii) Functions improve modularity and facilitate easy debugging
10. What is a parameterized constructor?
Ans. A parameterized constructor is a constructor that accepts arguments which are
used to initialize the object being created.
11. Name any two types of access specifiers.
Ans. public, private, protected and default/package access specifiers
12. How are private members .of a class different from public members?
Ans. Private members of a class can be accessed only by the methods of the class in
which they are declared. While public members can be accessed outside of the class
also.
13. Differentiate between Call by Value and Call by Reference
Call by Value Call by reference
3. Primitive data types are passed Non primitive data types are passed
to the formal parameter by actual to the formal paramter by actual
parameter. Eg int, float parameter. Eg arrays
2. The changes made in formal 2. The changes made in formal
arguments (if any) does not reflect arguments. (if any) are reflected in the
to actual argument.s actual argumants.
eg. void xyz (int a,int b); void xyz (abc ob)
14. What are the two ways of invoking functions?
Ans. Call by value and call by reference
15. What is the role of keyword void in declaring functions? [2]
Ans. void indicates that the function doesn’t return any value
16. What is call by value?
(ii) How are the following passed?
(1) Primitive types.  Call by value
(2) Reference types.  Call by Reference
17. Differentiate between pure and impure functions.
Ans. i) A pure function does not change the state of the object whereas an impure
function changes the state of the object by modifying instance variables.
ii) get functions are examples of pure functions and set functions are examples if
impure functions

18. .Differentiate between formal and Actual parameters


Formal Parameter Actual Parameter
The parameter which appear in the The parameter which appear in the
function definition are known as function call statement are known as
Formal parameters. Eg. void Formal parameters. Eg.
product(int x, int y) Ob.product(a,b);
A formal parameter is used along with The arguments/actual values are
the function name to receive the passed to the value to the formal
19. Con
value passed from the function call parameter at the time the function is side
statement. evoked. r
the following code;
static int x = 3,y=4; int a=2,b=3;
i. Name the variables for which each object of the class will have its own
distinct copy. a and b
ii. Name the variables that are common to all objects of the class.
x and y
20. Name the keyword which is used to resolve the conflict between method
parameter and instance variables/fields Ans. this keyword
21. When there are multiple definitions with the same function name, what makes
them different from each other?
Ans. The function prototype make multiple definitions of a function different from
each other. Either the number, type or order or arguments should be different for the
functions having identical names.
22. Explain the concept of constructor overloading [2]
Ans. A class can have more than one constructor provided that the signatures or
argument differ. This is known as constructor overloading.
27 Write prototype for a function called check which accept a char and return an
integer value Ans int check(char c)
Write prototype for a function called check which accept a double and a float and
return an boolean value Ans boolean check( double d, float f)
Write prototype for a function called abc which accept a char and return nothing
Ans void check(char c)
Write prototype for a function called xyz which accept nothing and return a double
value Ans double xyz( )
Write prototype for a function called check which accept nothing and return nothing
Ans void check( )
Write prototype for a function called study which accept a sentence and return an
word value Ans String check( Strings)
Write prototype for a function called decide which accept a short and return one or
twp Ans int check(short s)
Write prototype for a function called done which accept a double, and two integer
and return true or false Ans boolean done( double d, int a, int b)
Constructor
1. What is constructor? Ans: A constructor is a Member function that automatically
called, when the object is created of that class. It has the same name as that of the class
name and its primary job is to initialise the object to a legal value for the class.
2. Why do we need a constructor as a class member? Ans: Constructor is used
create an instance of of a class, This can be also called creating an object.
3. Why does a constructor should be define as public? Ans: A constructor should be
define in public section of a class, so that its objects can be created in any function.
4. Explain default constructor? [Ans: The constructor that accepts no parameter is called
the default constructor
5. Explain the Parameterised constructor?Ans: The constructor that accepts
6. parameter is called the default constructor
If no constructor is present in the class the compiler provides a default
constructor.
What are the types of Constructors used in a class? Ans: The different
types of constructors are as follows:Default Constructors. Parameterized
Constructor. iii. Copy Constructors.
Define Copy constructors.
Ans: A copy constructors initializes the instant variables of an object by
copying the initial value of the instant variables from another objects. e.g.
class xyz
{int a.b;
xyz(int x,int z) { a=x; b=y;}
xyz(xyz p) { a=p.x; b=p.y; } }
1. Mention some characteristics of constructors. Ans: The special characteristics of
constructors are:
(i) Constructors should be declared in the public section of the class. (ii) They
are invoked automatically when an object of the class is created. (iii) They
do not have any return type and cannot return any values (v) A class can
have more than one constructor.
State the difference between Constructor and Method. [2005]
Ans: The function has a return type like int. but the constructor has no return type. The
function must be called in programs where as constructor automatically called when the
object of that class is created. Constructr have same name as class name while function have
different name
Class as a User Defined Types
Q. What is data type? Ans: Data types are means to identify the type of data and
associated operations of handling it.
Q. What is composite (user define) data type? Explain with an example?
Ans: A composite data type is that data type that are based on fundamental or primitive data
types.
A ‘class’ is an example of composite data types.
class Date
{int dd, mm, yy; public Date() { dd=1; mm=1; yy=2005;}}
Q. What is user define datatype?Ans: A user defined datatype is a data type that is not
a part of the language and is created by a programmer.
Q. Can you refer to a class as a user defined (composite) data type? Ans:
Yes, we can refer to a class not having a main() method as user-defined data type.
Q. What is the difference between primitive data types and composite data
types?
Ans: (i) primitive data types are built-in data types. Java provides these data types.
User-defined data types are created by users.
(ii) The size of primitive data types are fixed. The size of user-defined data types are variable.
(iii) Primitive data types are available in all parts of Java programs. The availability of
user-defined data types depends upon their scope.
Q. Where can the following members of a class be accessed? private,
protected, public and
default access. Ans: Access specifier can be of following types:
(a) PUBLIC: Public members of a class are globally accessible.
(b) PRIVATE: Private members of a class be accessed by the member functions of the class
only.
(c) PROTECTED: Protected members of a class are accessible in the package where the class
is defined and in all the sub-classes of the class.
(d) Default (friendly) access: members with default (friendly) access can be used within
the package where the class is defined.
Q. How are private member different from public member of a class.
Ans: Private members of a class are accessible in the member function of the class only,
where as public members are accessible globally.
Q. How are protected members different from public and private members
of a class.
Ans: Protected members of a class are accessible in all the classes in the same package and
subclass in the other packages. private members of a class accessible in the
member functions in the class only. Where as public members are accessible
globally.
Q. Mention any two attributes required for class declaration.
Ans: The two attributes for class declaration are:
1. Access Specifier 2. Modifier 3. Class Name

Using Library Classes


Q. What is an Exception? Ans: Exception in general refers to some contradictory or
unusual situation which can be encountered while executing a program.
Q. What are the advantages of Exception Handling?
Ans: (i) Exception handling separates error handling code from normal code.
(ii) It clarifies the code and enhanced readability.
(iii) It stimulates consequences as the error handling takes place at one place and in one
manner.
(iv) It makes for clear, robust, fault tolerant programs.
Q. What do you mean by try block? How do you define it, give an example.
Ans: The try block is the one that contains the code that is to be monitored for the
occurrence of an exception.
Q. What do you mean by catch block? How do you define it, give an
example.
Ans: The catch block is the one that contains the code handle an exception. It must follow the
try block. i.e. there should be no statement between the try and the catch
blocks.
Q. What are wrapper classes?
Ans: Wrapper classes are the part or Java’s standard library *java.lang* and these convert
primitive data types into an object. to be more specific, a wrapper class wraps
a value of primitive types in an object.
Q Give the wrapper class for the following primitive data type
Primitive Data type Wrapper class Primitive Data type Wrapper class
byte Byte float Float
short Short double Double
int Integer char Character
long Long boolean Boolean
(The first letter will become capital in all types except char and int )

Q. Why do we need a wrapper class?


Ans: A wrapper class is needed to store primitive values in objects as well as in conversion
from string to to primitive type.
Q What is Autoboxing or boxing? The process of converting one primitive datatype into object is
known as Autoboxing. Eg. byte to Byte
QWhat is unboxing. The process of converting an object into primitive data type is known as
unboxing. Eg. Byte to byte

ENCAPSULATION

Question 1

............... is the technique of binding both data and methods together to keep them safe from
unauthorised access and misuse.

1. Abstraction

2. Inheritance

3. Encapsulation

4. Polymorphism

Answer

Encapsulation

Reason — Encapsulation is the technique of binding both data and methods together to keep them
safe from unauthorised access and misuse.

Question 2

Which of the following is an access specifier?

1. public

2. protected

3. private

4. All of these

Answer

All of these

Reason — Public, private and protected are access specifiers.

Question 3

A member variable declared with a public access specifier has visibility in ............... .

1. Class

2. Package

3. Subclass
4. All of these

Answer

All of these

Reason — A member variable declared with a public access specifier has visibility in the class, its sub
classes and the package as well.

Question 4

A member variable declared with a private access specifier has visibility only in the ............... .

1. Class

2. Package

3. Subclass

4. All of these

Answer

Class

Reason — A member variable declared with a private access specifier has visibility only in the class in
which it is declared.

Question 5

A member variable declared with no access specifier has visibility in ............... .

1. Class and package only

2. Class, package and subclass only

3. Class and subclass only

4. Class only

Answer

Class and package only

Reason — A member variable declared with no access specifier has visibility in the same class and
other classes of the same package only.

Question 6

An instance variable ............... .

1. needs an instance to access it

2. does not need an instance to access it

3. can be accessed using the class name

4. is declared with the static keyword

Answer
needs an instance to access it

Reason — An instance variable needs an instance (object) to access it as each instance of the class
has a separate copy of the instance variable.

Question 7

A static variable ............... .

1. is preceded by static keyword in the declaration

2. is accessed via the class name

3. is also known as a class variable

4. All of the above

Answer

All of the above

Reason — A static variable is preceded by static keyword in the declaration. It belongs to the class
and hence, it is called a class variable and is accessed via the class name.

Question 8

............... is the feature by means of which one class acquires the properties of another class.

1. Abstraction

2. Inheritance

3. Encapsulation

4. Polymorphism

Answer

Inheritance

Reason — Inheritance is the feature by means of which one class acquires the properties of another
class.

Question 9

The class that gets inherited is known as ............... .

1. Parent class

2. Base class

3. Super class

4. All of these

Answer

All of these

Reason — The class that gets inherited is known as a parent class, base class and super class as well.
Question 10

When many sub classes are inherited from a single base class, it is known as ............... .

1. Hierarchical inheritance

2. Multiple inheritance

3. Single inheritance

4. Multilevel inheritance

Answer

Hierarchical inheritance

Reason — When many sub classes are inherited from a single base class, it is known as Hierarchical
inheritance.

Assignment Questions

Question 1

How does a class encapsulate state and behaviour?

Answer

A class encapsulates state and behavior by combining data and functions into a single unit. The state
of an object is represented by its member variables and behaviour is represented by member
methods. By combining state and behavior within a single unit, the class encapsulates the
implementation details, allowing the outside world to interact with the object through a well-
defined interface.

Question 2

Name the access specifiers available in Java.

Answer

The access specifiers available in Java are:

1. public

2. private

3. protected

Question 3

Explain visibility in terms of the following access modifiers:

1. public

2. private

3. protected

4. no modifier specified

Answer
1. public — The class members declared with the public access specifier can be accessed from
outside the class.

2. private — The class members declared with the private access specifier can be accessed only
within the class in which they are declared.

3. protected — The class members declared with the protected access specifier can be
accessed within the same package only. They are not accessible from outside the package,
except if the sub class is in the other package.

4. no modifier specified — When no access specifier is mentioned, then by default that


members of a class are public within the package. They can be accessed within the same
package but not outside the package.

Question 4

Why is it a good idea to make all instance variables private?

Answer

It is a good idea to make all instance variables private because:

1. It hides the implementation details of the class from other objects, which makes it easier to
change the implementation without affecting the rest of the system.

2. It provides encapsulation as the class defines a clear and well-defined interface for
interacting with its state.

3. It protects the state of the object by preventing external objects from modifying the state in
an unintended way.

Question 5

What do you mean by the scope of variables in Java?

Answer

The scope of a variable refers to that part of the program in which the variable is accessible.

Question 6

Explain the scope of the following variables in Java:

1. Local variables

2. Parameter variables

3. Instance variables

4. Class variables

Answer

1. Local variables — The scope of local variables is limited to the method or the block they are
declared in. So, local variables are accessible only to the method or block in which they are
declared. They are not accessible to the rest of the class.
2. Parameter variables — The scope of parameter variables is limited to the method where
they are being used. Therefore, parameter variables are visible only to the method in which
they are used. They are not accessible to the rest of the class.

3. Instance variables — The scope of an instance variable is within the object that it belongs to,
and it can be accessed from any method of the class that it belongs to, within the visibility
restrictions defined by its access modifiers.

4. Class variables — A class variable can be accessed from any instance of the class using the
class name followed by the variable name. It is shared among all instances of a class. It is
available as a single copy to all instances of the class.

Question 7

Explain the scope of variables in blocks and sub-blocks.

Answer

A variable declared in a block is visible (accessible) in the block and in all the sub-blocks. However, it
is not visible outside the block.
Consider the given example:

void Add() {

int a = 10;

int b = 20;

if(a < b) { //Block 1

int sum = a + b;

System.out.println(sum);

else { //Block 2

int diff = a - b;

System.out.println(diff);

Here, the variables a and b will be accessible throughout the block and its sub-blocks (Block 1 and
Block 2) but the variable sum will be accessible only in Block 1 and the variable diff will be accessible
only in Block 2.

Question 8

Is it legal to define local variables with the same identifier in nested blocks?

Answer

No, it is illegal to define local variables with the same identifier in nested blocks
The local variable declaration space of a block includes any nested blocks. Thus, within a nested
block it is not possible to declare a local variable with the same name as a local variable in an
enclosing block.

Question 9

Why do you need to use static variables in Java?

Answer

Static variables in Java are used when we want to create a variable that is common to all objects of a
class, rather than having a separate instance of the variable for each object. This is useful in
situations where we need to keep track of information that applies to a class as a whole, rather than
to individual objects.

Some common use cases for static variables include:

1. Counter to keep track of the number of instances of a class that have been created.

2. To store a constant value that is common to all objects of a class.

3. To store configuration information that is common to all objects of a class.

Question 10

What is the mechanism that allows one class to extend another class?

Answer

Inheritance is the mechanism that allows one class to extend another class.

Question 11

What do you call a class that is an extension of another class?

Answer

A class that is an extension of another class is known as a derived class or child class or sub-class

You might also like