WorksheetsMastering OOPS Concepts in Java
Total questions: 30
Worksheet time: 15mins
What is encapsulation in Java?
Encapsulation allows unrestricted access to all object components.
Encapsulation is the separation of data from methods in Java.
Encapsulation is the process of converting data types in Java.
Encapsulation is the bundling of data and methods that operate on that data, restricting direct access to some of the object's components.
How does encapsulation enhance security in Java?
Encapsulation enhances security by restricting direct access to an object's data and allowing controlled access through methods.
Encapsulation improves performance by optimizing memory usage.
Encapsulation simplifies code by reducing the number of classes needed.
Encapsulation allows multiple inheritance in Java, enhancing flexibility.
What are the access modifiers used in encapsulation?
Static, Dynamic, Local, Global
ReadOnly, WriteOnly, Internal, External
Public, Private, Protected, Default
Static, Final, Abstract, Virtual
Define inheritance in Java.
Inheritance in Java is a way to create interfaces without implementation.
Inheritance in Java restricts access to superclass methods.
Inheritance in Java allows multiple classes to share a single method.
Inheritance in Java allows a subclass to inherit properties and behaviors (methods) from a superclass.
What is the purpose of the 'extends' keyword in Java?
To implement multiple classes at once.
To create an interface in Java.
To indicate class inheritance from a superclass.
To define a new method in a class.
Explain the concept of method overriding in inheritance.
Method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass, enabling customized behavior.
Method overriding is a way to hide methods in the superclass.
Method overriding allows a subclass to inherit all methods without changes.
Method overriding prevents a subclass from changing inherited methods.
What is the difference between single and multiple inheritance?
Single inheritance allows for multiple parent classes, while multiple inheritance restricts to one.
Single inheritance uses interfaces, while multiple inheritance uses abstract classes.
Single inheritance involves one parent class, while multiple inheritance involves multiple parent classes.
Single inheritance is when a class inherits from multiple classes, while multiple is from one.
What is polymorphism in Java?
Polymorphism is the ability of a single interface to represent different underlying forms (data types).
Polymorphism allows methods to be executed in a sequential manner.
Polymorphism is the ability to create multiple instances of a class.
Polymorphism is the process of combining multiple classes into one.
How does method overloading demonstrate polymorphism?
Method overloading is a form of compile-time polymorphism where the method invoked is based on the method signature.
Method overloading is a feature that enables method execution at runtime based on parameters.
Method overloading allows multiple methods with the same name but different return types.
Method overloading is a runtime polymorphism based on object type.
What is the difference between compile-time and runtime polymorphism?
Runtime polymorphism is determined at compile-time (e.g., method overloading).
Compile-time polymorphism occurs at runtime (e.g., method overriding).
The difference between compile-time and runtime polymorphism is that compile-time polymorphism is resolved during compilation (e.g., method overloading), while runtime polymorphism is resolved during program execution (e.g., method overriding).
Both compile-time and runtime polymorphism are resolved during execution.
Define abstraction in Java.
Abstraction in Java is the process of hiding the implementation details and showing only the essential features of an object.
Abstraction in Java is the process of implementing all features of an object.
Abstraction in Java involves exposing all details of an object to the user.
Abstraction in Java is the technique of combining multiple classes into one.
How do abstract classes differ from interfaces?
Abstract classes cannot have any methods; interfaces can have both abstract and concrete methods.
Abstract classes are used for data storage; interfaces are used for defining data types.
Abstract classes support multiple inheritance; interfaces only allow single inheritance.
Abstract classes can have implemented methods and support single inheritance; interfaces only declare methods and support multiple inheritance.
What is the purpose of the 'abstract' keyword?
To create a class that can be instantiated multiple times.
To specify a class that can only contain static methods.
To define a class that cannot be instantiated and to enforce method implementation in subclasses.
To define a method that can be called without implementation.
What are classes and objects in Java?
Classes are types of variables; objects are collections of data.
Classes are functions in Java; objects are methods of classes.
Classes are blueprints for objects; objects are instances of classes.
Classes are templates for functions; objects are variables of templates.
How do you create an object from a class in Java?
ClassName objectName = ClassName();
objectName = new ClassName();
new ClassName objectName = new();
ClassName objectName = new ClassName();
What is the significance of the 'this' keyword in a class?
The 'this' keyword refers to the parent class in inheritance.
The 'this' keyword is used to define static methods in a class.
The 'this' keyword is significant as it allows access to the current object's properties and methods within a class.
The 'this' keyword is a way to create new instances of a class.
Explain the concept of constructors in Java.
Constructors in Java are special methods used to initialize objects, sharing the class name and having no return type.
Constructors in Java are optional methods that can be omitted in classes.
Constructors in Java are used to define class variables and methods.
Constructors in Java are regular methods that return a value.
What is the difference between a constructor and a method?
A constructor creates a class; a method defines its properties.
A constructor initializes an object; a method performs actions on an object.
A constructor is used for inheritance; a method is for encapsulation.
A constructor is a type of method; a method is a special function.
How can you achieve encapsulation using getters and setters?
Use public variables to allow unrestricted access.
Implement only private methods without any accessors.
Create global variables for direct manipulation.
Use private variables with public getters and setters to control access and modification.
What role do interfaces play in Java's OOPS design?
Interfaces in Java provide a way to implement multiple inheritance and enhance performance.
Interfaces in Java restrict class inheritance and enforce strict data types.
Interfaces in Java are used to create abstract classes and manage memory allocation.
Interfaces in Java enable polymorphism, define contracts for classes, and promote loose coupling.
What is the role of the 'super' keyword in Java inheritance?
The 'super' keyword allows a subclass to override methods from the parent class.
The 'super' keyword is used to access private methods of the parent class.
The 'super' keyword is used to call the constructor of the parent class.
The 'super' keyword is used to create an instance of the parent class.
What is the difference between an abstract class and a concrete class?
An abstract class is used for data storage, while a concrete class is used for method implementation.
An abstract class can have only abstract methods, while a concrete class can have both abstract and concrete methods.
An abstract class can implement interfaces, while a concrete class cannot.
An abstract class cannot be instantiated, while a concrete class can be.
How does the 'final' keyword affect inheritance in Java?
The 'final' keyword prevents a class from being subclassed.
The 'final' keyword allows a class to be inherited but restricts method overriding.
The 'final' keyword can only be applied to methods, not classes.
The 'final' keyword has no effect on inheritance.
What is the purpose of the 'static' keyword in Java?
The 'static' keyword allows a method to be overridden in subclasses.
The 'static' keyword is used to define instance variables in a class.
The 'static' keyword restricts access to a method or variable to only one instance of a class.
The 'static' keyword allows a method or variable to be shared among all instances of a class.
How does the 'final' keyword affect method overriding in Java?
The 'final' keyword can only be applied to classes, not methods.
The 'final' keyword has no effect on method overriding.
The 'final' keyword prevents a method from being overridden in subclasses.
The 'final' keyword allows a method to be overridden in subclasses.
What is the significance of the 'interface' keyword in Java?
The 'interface' keyword is used to define a class that cannot be instantiated.
The 'interface' keyword allows the creation of a contract that classes can implement.
The 'interface' keyword restricts the use of inheritance in Java.
The 'interface' keyword is used to create abstract classes.
class Test {
int x;
Test() {
x = 10;
}
Test(int x) {
this.x = x;
}
public static void main(String[] args) {
Test t = new Test();
System.out.print(t.x);
}
}
Garbage Value
Compilation Error
0
class Demo {
Demo() {
System.out.print("A ");
}
Demo(int x) {
this();
System.out.print("B ");
}
public static void main(String[] args) {
new Demo(10);
}
}
A B
B A
A
Compilation Error
class Parent {
void show() {
System.out.print("Parent ");
}
}
class Child extends Parent {
void show() {
System.out.print("Child ");
}
}
class Test {
public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}
Parent
Child
Parent Child
Compilation Error
class A {
static void show() {
System.out.print("A ");
}
}
class B extends A {
static void show() {
System.out.print("B ");
}
}
class Test {
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
A
B
A B
Compilation Error
