wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Understanding Java Classes and Inheritance - UNIT 1

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

What is an object in Java?

a)

A class is a blueprint for creating objects.

b)

An object is a static data type in Java.

c)

An object is a method that performs actions.

d)

An object in Java is an instance of a class that contains state and behavior.

2.

Define a constructor in Java.

a)

A constructor in Java is a method that initializes an object and has the same name as the class.

b)

A constructor is a type of variable in Java.

c)

A constructor is a method that returns a value.

d)

A constructor can have a different name than the class.

3.

What is method overloading?

a)

Method overloading is a way to override methods in subclasses.

b)

Method overloading requires methods to have the same parameters.

c)

Method overloading is when methods have different names.

d)

Method overloading allows multiple methods with the same name but different parameters.

4.

Explain access control in Java.

a)

Access control in Java is irrelevant to class design.

b)

Access control in Java is only managed through the 'public' modifier.

c)

Access control in Java is determined by the operating system settings.

d)

Access control in Java is managed through access modifiers: public, protected, private, and default.

5.

What is the difference between static and final methods?

a)

Static methods can be overridden; final methods are not associated with a class.

b)

Final methods are class-level methods; static methods cannot be overridden.

c)

Static methods can only be used in interfaces; final methods are used in abstract classes.

d)

Static methods are class-level methods; final methods cannot be overridden.

6.

What are inner classes in Java?

a)

Inner classes are classes defined within another class in Java.

b)

Inner classes are standalone classes in Java.

c)

Inner classes are only used for static methods in Java.

d)

Inner classes cannot access the outer class's members.

7.

Describe inheritance in Java.

a)

Inheritance in Java is a way to create multiple instances of a class.

b)

Inheritance allows classes to be combined into a single class.

c)

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.

d)

Inheritance is a method to hide data from other classes.

8.

How do you use the super keyword?

a)

Use 'super' to override child class properties.

b)

Use 'super' to delete parent class methods.

c)

Use 'super' to access parent class methods or properties in a child class.

d)

Use 'super' to create a new class instance.

9.

What is method overriding?

a)

Method overriding is a way to increase the performance of a method by optimizing its code.

b)

Method overriding is when a superclass redefines a method from its subclass.

c)

Method overriding is a feature in object-oriented programming where a subclass redefines a method from its superclass.

d)

Method overriding is a technique to create multiple methods with the same name in different classes without any relation.

10.

Explain dynamic method dispatch.

a)

Dynamic method dispatch enables runtime method resolution for overridden methods, facilitating polymorphism.

b)

Dynamic method dispatch is used to optimize memory usage in applications.

c)

Dynamic method dispatch is a compile-time feature for method resolution.

d)

Dynamic method dispatch prevents polymorphism in object-oriented programming.

11.

What is an abstract class?

a)

An abstract class must contain only concrete methods.

b)

An abstract class is a class that cannot be instantiated and may contain abstract methods that must be implemented by subclasses.

c)

An abstract class can be instantiated directly.

d)

An abstract class is a type of interface that cannot have any methods.

12.

How does an abstract class differ from an interface?

a)

An abstract class can have implemented methods and member variables, while an interface can only declare methods and constants.

b)

An interface can have implemented methods and member variables.

c)

An abstract class cannot have any methods or variables.

d)

An abstract class can only declare methods and constants.

13.

What is the purpose of the 'this' keyword in a constructor?

a)

The 'this' keyword is used to define global variables in a constructor.

b)

The 'this' keyword is used to create static methods in a class.

c)

The 'this' keyword is used to refer to the current instance of the object in a constructor.

d)

The 'this' keyword is used to refer to the parent class in a constructor.

14.

Can a constructor be overloaded?

a)

Yes, a constructor can be overloaded.

b)

No, a constructor cannot be overloaded.

c)

Overloading is only applicable to methods, not constructors.

d)

Constructors can only have one definition.

15.

What happens if you do not define a constructor in a class?

a)

Members are initialized to random values.

b)

The class cannot be instantiated without a constructor.

c)

A constructor must always be defined explicitly.

d)

A default constructor is automatically created, initializing members to default values.

16.

What is the significance of the 'final' keyword in a class?

a)

The 'final' keyword indicates that a class can have multiple subclasses.

b)

The 'final' keyword allows a class to be instantiated multiple times.

c)

The 'final' keyword is used to mark methods as private.

d)

The 'final' keyword in a class signifies that the class cannot be subclassed.

17.

How can you prevent method overriding in Java?

a)

Declare methods as 'abstract' in the class.

b)

Override methods in the superclass.

c)

Use the 'static' keyword for methods.

d)

Use the 'final' keyword for methods or classes.

18.

What is the role of the 'instanceof' operator?

a)

The 'instanceof' operator is used to compare two strings.

b)

The 'instanceof' operator is used to test the type of an object against a constructor or class.

c)

The 'instanceof' operator checks if a variable is null.

d)

The 'instanceof' operator is used to create new objects.

19.

What are the advantages of using inner classes?

a)

Limited access to outer class members

b)

Increased complexity in code management

c)

Advantages of using inner classes include improved encapsulation, better code organization, and enhanced readability.

d)

Reduced performance in execution

20.

How do you create an instance of an inner class?

a)

Create an instance of the inner class directly without the outer class.

b)

Create an instance of the outer class, then use it to create the inner class instance.

c)

Use a static method of the outer class to create the inner class instance.

d)

Instantiate the inner class using a separate constructor without the outer class.

21.

class Car {

String model;

int year;

public void Car(String m, int y) {

model = m;

year = y;

}

}

What is the issue?

a)

  1. Change void to int

b)

Remove void to define a proper constructor

c)

Add Static to the method

d)

Rename Class to match the constructor

22.

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?

a)

Rename second method

b)

  1. Add static to both methods

c)

Use different parameter count or types

d)

Declare return type as void

23.

class Bank {

private int balance = 1000;

int getBalance() {

return balance;

}

}

class Customer extends Bank {

void printBalance() {

System.out.println(balance);

}

}

Why does this fail?

a)

  1. balance is inaccessible due to private

b)

  1. getBalance() is missing static

c)

  1. printBalance() is not overridden properly

d)

  1. balance must be declared in Customer class

24.

class Sample {

static int count = 0;

void increment() {

count++;

}

static void display() {

System.out.println("Count: " + count);

increment(); // ❌

}

}

Whats wrong here?

a)

  1. count should be final

b)

  1. Static method cannot access instance method

c)

  1. increment() should be static

d)

  1. display() must be private

25.

abstract class Animal {

abstract void makeSound() {}

}

Why won’t this compile?

a)

Abstract methods must be static

b)

  1. Abstract class can't contain methods

c)

  1. Abstract methods should not have a body

d)

Abstract methods must be final