Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Applications, Class 10, ICSE
How does a class encapsulate state and behaviour?
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.
Name the access specifiers available in Java.
The access specifiers available in Java are:
Explain visibility in terms of the following access modifiers:
Why is it a good idea to make all instance variables private?
It is a good idea to make all instance variables private because:
What do you mean by the scope of variables in Java?
The scope of a variable refers to that part of the program in which the variable is accessible.
Explain the scope of the following variables in Java:
Explain the scope of variables in blocks and sub-blocks.
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.
Is it legal to define local variables with the same identifier in nested blocks?
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.
Why do you need to use static variables in Java?
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:
What is the mechanism that allows one class to extend another class?
Inheritance is the mechanism that allows one class to extend another class.
What do you call a class that is an extension of another class?
A class that is an extension of another class is known as a derived class or child class or sub-class.
What does the inheritance mechanism allow one class to acquire from another?
The inheritance mechanism allows the derived class to inherit the state and behaviour from the base class. The private data members and member methods are not inherited by the sub class.
How is inheritance transitive? Explain.
The transitive nature of inheritance means if class B is derived from class A, class B will inherit all the properties of class A. Now all the sub classes of class B will also be able to inherit properties of class A because of the transitive nature of inheritance.
Explain various types of inheritance.
The various types of inheritance are as follows:
Why do you need to use inheritance? Give two reasons.
We need to use inheritance for the following reasons:
Declare a public class CoolClass.
public class CoolClass
{
public void CoolMethodA() {
}
int CoolMethodB() {
}
private int CoolMethodC() {
}
protected char CoolMethodD() {
}
}
............... is the technique of binding both data and methods together to keep them safe from unauthorised access and misuse.
Encapsulation
Reason — Encapsulation is the technique of binding both data and methods together to keep them safe from unauthorised access and misuse.
Which of the following is an access specifier?
All of these
Reason — Public, private and protected are access specifiers.
A member variable declared with a public access specifier has visibility in ............... .
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.
A member variable declared with a private access specifier has visibility only in the ............... .
Class
Reason — A member variable declared with a private access specifier has visibility only in the class in which it is declared.
A member variable declared with no access specifier has visibility in ............... .
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.
An instance variable ............... .
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.
A static variable ............... .
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.
............... is the feature by means of which one class acquires the properties of another class.
Inheritance
Reason — Inheritance is the feature by means of which one class acquires the properties of another class.
The class that gets inherited is known as ............... .
All of these
Reason — The class that gets inherited is known as a parent class, base class and super class as well.
When many sub classes are inherited from a single base class, it is known as ............... .
Hierarchical inheritance
Reason — When many sub classes are inherited from a single base class, it is known as Hierarchical inheritance.