0% found this document useful (0 votes)
8 views

Quiz 2

This document contains a quiz on object-oriented programming concepts in Java including polymorphism, encapsulation, abstraction, and object access. It includes 4 questions asking to implement various OOP concepts: 1. Polymorphism - Implement method overloading in Circle and Square classes for the draw method. 2. Encapsulation - Implement a Student class constructor, getters, and setters for private attributes with validation. 3. Abstraction - Declare an abstract calculateArea method in an abstract Shape class and implement a displayArea method. 4. Object Access - Override the makeSound method in a Dog class to print "Bark!" and access attributes and methods of a Dog instance.

Uploaded by

Hassam Shaykh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Quiz 2

This document contains a quiz on object-oriented programming concepts in Java including polymorphism, encapsulation, abstraction, and object access. It includes 4 questions asking to implement various OOP concepts: 1. Polymorphism - Implement method overloading in Circle and Square classes for the draw method. 2. Encapsulation - Implement a Student class constructor, getters, and setters for private attributes with validation. 3. Abstraction - Declare an abstract calculateArea method in an abstract Shape class and implement a displayArea method. 4. Object Access - Override the makeSound method in a Dog class to print "Bark!" and access attributes and methods of a Dog instance.

Uploaded by

Hassam Shaykh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Object-Oriented Programming in Java - Quiz

Question 1: Polymorphism (10 marks)


Consider the following Java code snippet:
1 class Shape {
2 void draw() {
3 System.out.println("Drawing a shape");
4 }
5 }
6
7 class Circle extends Shape {
8 void draw() {
9 System.out.println("Drawing a circle");
10 }
11
12 // Implement method overload here
13 }
14
15 class Square extends Shape {
16 // Implement method overload here

a) In the Circle class, implement method overloading for the draw method to accept an additional
parameter of type int representing the radius.
b) In the Square class, implement method overloading for the draw method to accept an additional
parameter of type double representing the side length.

Question 2: Encapsulation (15 marks)


Consider a Student class with private attributes name (String), age (int), and grade (double).
1 class Student {
2 // Declare private attributes: name, age, grade
3
4 // Implement constructor
5
6 // Implement getters and setters with validation
7 }

a) Implement the constructor for the Student class.


b) Implement getters and setters for each attribute with the following validation:
- Name: Should not be null or an empty string. - Age: Should be between 18 and 30 (inclusive). -
Grade: Should be between 0 and 100 (inclusive).

Question 3: Abstract Functionality (10 marks)


Consider an abstract class Shape with an abstract method calculateArea().
1 abstract class Shape {
2 // Declare abstract method calculateArea()
3
4 // Implement a non-abstract method displayArea() to print the calculated area
5 }

a) Declare an abstract method named calculateArea() in the Shape class.


b) Implement a non-abstract method named displayArea() in the Shape class to print the cal-
culated area.

1
Question 4: Object Access (15 marks)
Consider the following code snippet:
1 class Animal {
2 protected String name;
3
4 Animal(String name) {
5 this.name = name;
6 }
7
8 void makeSound() {
9 System.out.println("Animal sound");
10 }
11 }
12
13 class Dog extends Animal {
14 Dog(String name) {
15 super(name);
16 }
17
18 // Implement method overriding for makeSound()
19 }

a) In the Dog class, implement method overriding for the makeSound method to print ”Bark!”.
b) Create an instance of the Dog class named myDog with the name ”Buddy”. Access the name
attribute and invoke the makeSound method.

End of Quiz

You might also like