NEW
Font size
WorksheetsUnderstanding Java Classes and Inheritance - UNIT 1
Total questions: 25
Worksheet time: 13mins
What is an object in Java?
A class is a blueprint for creating objects.
An object is a static data type in Java.
An object is a method that performs actions.
An object in Java is an instance of a class that contains state and behavior.
Define a constructor in Java.
A constructor in Java is a method that initializes an object and has the same name as the class.
A constructor is a type of variable in Java.
A constructor is a method that returns a value.
A constructor can have a different name than the class.
What is method overloading?
Method overloading is a way to override methods in subclasses.
Method overloading requires methods to have the same parameters.
Method overloading is when methods have different names.
Method overloading allows multiple methods with the same name but different parameters.
Explain access control in Java.
Access control in Java is irrelevant to class design.
Access control in Java is only managed through the 'public' modifier.
Access control in Java is determined by the operating system settings.
Access control in Java is managed through access modifiers: public, protected, private, and default.
What is the difference between static and final methods?
Static methods can be overridden; final methods are not associated with a class.
Final methods are class-level methods; static methods cannot be overridden.
Static methods can only be used in interfaces; final methods are used in abstract classes.
Static methods are class-level methods; final methods cannot be overridden.
What are inner classes in Java?
Inner classes are classes defined within another class in Java.
Inner classes are standalone classes in Java.
Inner classes are only used for static methods in Java.
Inner classes cannot access the outer class's members.
Describe inheritance in Java.
Inheritance in Java is a way to create multiple instances of a class.
Inheritance allows classes to be combined into a single class.
Inheritance in Java is a mechanism where one class acquires the properties and behaviors of another class, enabling code reuse and establishing a parent-child relationship.
Inheritance is a method to hide data from other classes.
How do you use the super keyword?
Use 'super' to override child class properties.
Use 'super' to delete parent class methods.
Use 'super' to access parent class methods or properties in a child class.
Use 'super' to create a new class instance.
What is method overriding?
Method overriding is a way to increase the performance of a method by optimizing its code.
Method overriding is when a superclass redefines a method from its subclass.
Method overriding is a feature in object-oriented programming where a subclass redefines a method from its superclass.
Method overriding is a technique to create multiple methods with the same name in different classes without any relation.
Explain dynamic method dispatch.
Dynamic method dispatch enables runtime method resolution for overridden methods, facilitating polymorphism.
Dynamic method dispatch is used to optimize memory usage in applications.
Dynamic method dispatch is a compile-time feature for method resolution.
Dynamic method dispatch prevents polymorphism in object-oriented programming.
What is an abstract class?
An abstract class must contain only concrete methods.
An abstract class is a class that cannot be instantiated and may contain abstract methods that must be implemented by subclasses.
An abstract class can be instantiated directly.
An abstract class is a type of interface that cannot have any methods.
How does an abstract class differ from an interface?
An abstract class can have implemented methods and member variables, while an interface can only declare methods and constants.
An interface can have implemented methods and member variables.
An abstract class cannot have any methods or variables.
An abstract class can only declare methods and constants.
What is the purpose of the 'this' keyword in a constructor?
The 'this' keyword is used to define global variables in a constructor.
The 'this' keyword is used to create static methods in a class.
The 'this' keyword is used to refer to the current instance of the object in a constructor.
The 'this' keyword is used to refer to the parent class in a constructor.
Can a constructor be overloaded?
Yes, a constructor can be overloaded.
No, a constructor cannot be overloaded.
Overloading is only applicable to methods, not constructors.
Constructors can only have one definition.
What happens if you do not define a constructor in a class?
Members are initialized to random values.
The class cannot be instantiated without a constructor.
A constructor must always be defined explicitly.
A default constructor is automatically created, initializing members to default values.
What is the significance of the 'final' keyword in a class?
The 'final' keyword indicates that a class can have multiple subclasses.
The 'final' keyword allows a class to be instantiated multiple times.
The 'final' keyword is used to mark methods as private.
The 'final' keyword in a class signifies that the class cannot be subclassed.
How can you prevent method overriding in Java?
Declare methods as 'abstract' in the class.
Override methods in the superclass.
Use the 'static' keyword for methods.
Use the 'final' keyword for methods or classes.
What is the role of the 'instanceof' operator?
The 'instanceof' operator is used to compare two strings.
The 'instanceof' operator is used to test the type of an object against a constructor or class.
The 'instanceof' operator checks if a variable is null.
The 'instanceof' operator is used to create new objects.
What are the advantages of using inner classes?
Limited access to outer class members
Increased complexity in code management
Advantages of using inner classes include improved encapsulation, better code organization, and enhanced readability.
Reduced performance in execution
How do you create an instance of an inner class?
Create an instance of the inner class directly without the outer class.
Create an instance of the outer class, then use it to create the inner class instance.
Use a static method of the outer class to create the inner class instance.
Instantiate the inner class using a separate constructor without the outer class.
class Car {
String model;
int year;
public void Car(String m, int y) {
model = m;
year = y;
}
}
What is the issue?
Change void to int
Remove void to define a proper constructor
Add Static to the method
Rename Class to match the constructor
class MathOps {
int multiply(int a, int b) {
return a * b;
}
double multiply(double a, double b, double c) {
return a b c;
}
}
What's missing to make this method overload clear?
Rename second method
Add static to both methods
Use different parameter count or types
Declare return type as void
class Bank {
private int balance = 1000;
int getBalance() {
return balance;
}
}
class Customer extends Bank {
void printBalance() {
System.out.println(balance);
}
}
Why does this fail?
balance is inaccessible due to private
getBalance() is missing static
printBalance() is not overridden properly
balance must be declared in Customer class
class Sample {
static int count = 0;
void increment() {
count++;
}
static void display() {
System.out.println("Count: " + count);
increment(); // ❌
}
}
Whats wrong here?
count should be final
Static method cannot access instance method
increment() should be static
display() must be private
abstract class Animal {
abstract void makeSound() {}
}
Why won’t this compile?
Abstract methods must be static
Abstract class can't contain methods
Abstract methods should not have a body
Abstract methods must be final
