Encapsulation and Inheritance

ICSE Class 10 Computer Applications · Logix Computer Applications · 26 solved questions

Exercise
Marks
11 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

Encapsulation

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

21 Mark · Multiple Choice Questions

Which of the following is an access specifier?

Show SolutionHide Solution↓

All of these

Reason — Public, private and protected are access specifiers.

31 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

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.

41 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

Class

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

51 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

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.

61 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

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.

71 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

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.

81 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

Inheritance

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

91 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

All of these

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

101 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓

Hierarchical inheritance

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

1Assignment Questions

How does a class encapsulate state and behaviour?

Show SolutionHide Solution↓

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.

2Assignment Questions

Name the access specifiers available in Java.

Show SolutionHide Solution↓

The access specifiers available in Java are:

  1. public
  2. private
  3. protected
3Assignment Questions

Explain visibility in terms of the following access modifiers:

Show SolutionHide Solution↓
  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.
4Assignment Questions

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

Show SolutionHide Solution↓

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.
5Assignment Questions

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

Show SolutionHide Solution↓

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

6Assignment Questions

Explain the scope of the following variables in Java:

Show SolutionHide Solution↓
  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.
7Assignment Questions

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

Show SolutionHide Solution↓

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.

9Assignment Questions

Why do you need to use static variables in Java?

Show SolutionHide Solution↓

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.
10Assignment Questions

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

Show SolutionHide Solution↓

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

11Assignment Questions

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

Show SolutionHide Solution↓

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

12Assignment Questions

What does the inheritance mechanism allow one class to acquire from another?

Show SolutionHide Solution↓

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.

13Assignment Questions

How is inheritance transitive? Explain.

Show SolutionHide Solution↓

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.

14Assignment Questions

Explain various types of inheritance.

Show SolutionHide Solution↓

The various types of inheritance are as follows:

  1. Single Inheritance — When a class is derived from only one base class, it is known as single inheritance.
  2. Multiple Inheritance — When a sub class is inherited from multiple base classes, it is known as multiple inheritance.
  3. Hierarchical Inheritance — When many sub classes are inherited from a single base class, it is known as hierarchical inheritance.
  4. Hybrid Inheritance — When more than one type of inheritance forms are used together, it is known as hybrid inheritance.
  5. Multilevel Inheritance — When a sub class is inherited from a class that itself is being inherited from another class, it is known as multilevel inheritance.
15Assignment Questions

Why do you need to use inheritance? Give two reasons.

Show SolutionHide Solution↓

We need to use inheritance for the following reasons:

  1. Inheritance divides a program into useful and reusable set of classes.
  2. Changes made in the original class are reflected in all the inherited classes.
16Assignment Questions

Declare a public class CoolClass.

Show SolutionHide Solution↓
public class CoolClass
{
    public void CoolMethodA()   {

    }

    int CoolMethodB()   {

    }

    private int CoolMethodC()   {

    }

    protected char CoolMethodD() {

    }
}

Press ↓ to reveal a solution and move to the next question. Back to Logix Computer Applications.

Free sample bank