
Practice 50 Swift Object-Oriented Programming Questions
Q1. Write a Swift program to create a class called Person with properties name and age, and a method greet() that prints a greeting message.
Input:
name = "Alice", age = 25
Expected Output:
Hello, my name is Alice and I am 25 years old.
Q2. Write a Swift program to create a class called Circle with a property radius and a method area() to calculate the area of the circle.
Input:
radius = 5
Expected Output:
Area of the circle: 78.5398
Q3. Write a Swift program to create a Car class with properties model and color and a method displayInfo() to display the car's details.
Input:
model = "Tesla", color = "Red"
Expected Output:
Car Model: Tesla, Color: Red
Q4. Write a Swift program to create a Rectangle class with properties length and width, and a method area() to calculate the area of the rectangle.
Input:
length = 10, width = 5
Expected Output:
Area of the rectangle: 50
Q5. Write a Swift program to create a class Book with properties title, author, and pages, and a method printDetails() to print the book's details.
Input:
title = "Swift Programming", author = "John Doe", pages = 200
Expected Output:
Book Title: Swift Programming, Author: John Doe, Pages: 200
Q6. Write a Swift program to create a class Employee with properties name, position, and salary, and a method displayInfo() to display the employee's information.
Input:
name = "Bob", position = "Manager", salary = 5000
Expected Output:
Employee Name: Bob, Position: Manager, Salary: 5000
Q7. Write a Swift program to create a class Student with properties name, age, and marks, and a method calculateGrade() to calculate the grade based on marks.
Input:
marks = 85
Expected Output:
Grade: B
Q8. Write a Swift program to create a Person class with a method setDetails() and a method getDetails() to set and get the person's details.
Input:
name = "David", age = 30
Expected Output:
Name: David, Age: 30
Q9. Write a Swift program to create a class BankAccount with properties balance and accountNumber, and a method deposit() to deposit money into the account.
Input:
depositAmount = 1000
Expected Output:
New balance: 1000
Q10. Write a Swift program to create a Person class with properties firstName, lastName, and a method fullName() to return the full name.
Input:
firstName = "John", lastName = "Doe"
Expected Output:
Full Name: John Doe
Q11. Write a Swift program to create a class Animal with a method makeSound() that prints a sound based on the animal type.
Input:
animalType = "Dog"
Expected Output:
Woof! Woof!
Q12. Write a Swift program to create a class Shape with a method area() and a subclass Circle that implements the area() method to calculate the area of a circle.
Input:
radius = 7
Expected Output:
Area of Circle: 153.938
Q13. Write a Swift program to create a Rectangle class with a method perimeter() to calculate the perimeter of a rectangle.
Input:
length = 4, width = 6
Expected Output:
Perimeter of the rectangle: 20
Q14. Write a Swift program to create a Product class with properties name, price, and quantity, and a method totalPrice() to calculate the total price.
Input:
price = 100, quantity = 3
Expected Output:
Total Price: 300
Q15. Write a Swift program to create a Person class with a method isAdult() that returns true if the person's age is greater than or equal to 18.
Input:
age = 21
Expected Output:
True
Q16. Write a Swift program to create a class Shape with properties length and width, and a subclass Rectangle that calculates the area of the rectangle.
Input:
length = 5, width = 3
Expected Output:
Area of Rectangle: 15
Q17. Write a Swift program to create a class Product with properties name and price, and a method applyDiscount() to apply a discount to the price.
Input:
price = 200, discount = 10
Expected Output:
Discounted Price: 180
Q18. Write a Swift program to create a Car class with properties make, model, and a method startEngine() to print a message when the engine starts.
Input:
make = "Toyota", model = "Camry"
Expected Output:
The engine of Toyota Camry is now running.
Q19. Write a Swift program to create a class Person with properties age and name, and a method celebrateBirthday() that increases the age by one.
Input:
age = 25
Expected Output:
Happy 26th birthday!
Q20. Write a Swift program to create a class Student with properties name, grade, and a method study() to simulate studying.
Input:
name = "Charlie", grade = "A"
Expected Output:
Charlie is studying for the grade A.
Q21. Write a Swift program to create a class Car with a method drive() that prints a message when the car is driven.
Input:
carModel = "BMW"
Expected Output:
Driving a BMW!
Q22. Write a Swift program to create a class Employee with properties name and position, and a method promote() that changes the position of the employee.
Input:
position = "Junior Developer"
Expected Output:
Promoted to Senior Developer.
Q23. Write a Swift program to create a class Laptop with properties brand and model, and a method turnOn() to turn on the laptop.
Input:
brand = "Apple", model = "MacBook Pro"
Expected Output:
MacBook Pro is now powered on.
Q24. Write a Swift program to create a Vehicle class with a method start() and a subclass Truck that overrides start() with a specific message.
Input:
vehicleType = "Truck"
Expected Output:
The truck is starting.
Q25. Write a Swift program to create a class Movie with properties title, director, and year, and a method getDetails() to display the movie details.
Input:
title = "Inception", director = "Christopher Nolan", year = 2010
Expected Output:
Title: Inception, Director: Christopher Nolan, Year: 2010
Q26. Write a Swift program to create a Circle class with a method circumference() to calculate the circumference of the circle.
Input:
radius = 7
Expected Output:
Circumference of the circle: 43.9823
Q27. Write a Swift program to create a Team class with properties teamName and players, and a method addPlayer() to add a player to the team.
Input:
teamName = "Warriors", players = ["James", "Klay"]
Expected Output:
Player added: Curry
Q28. Write a Swift program to create a class Person with properties name and age, and a method isAdult() to return true if age is 18 or above.
Input:
age = 17
Expected Output:
False
Q29. Write a Swift program to create a Rectangle class with properties length and width, and a method resize() to update the size of the rectangle.
Input:
length = 5, width = 3
Expected Output:
New size: 6 x 4
Q30. Write a Swift program to create a Person class with a method updateName() that allows the name to be updated.
Input:
oldName = "David", newName = "Mike"
Expected Output:
Name updated from David to Mike.
Q31. Write a Swift program to create a Book class with properties title, author, and a method updateTitle() to change the book title.
Input:
oldTitle = "Old Book", newTitle = "New Book"
Expected Output:
Book title updated from Old Book to New Book.
Q32. Write a Swift program to create a class Employee with properties name and salary, and a method raiseSalary() to increase the salary.
Input:
salary = 50000
Expected Output:
New Salary: 55000
Q33. Write a Swift program to create a class Rectangle with methods setWidth() and setHeight() to modify the dimensions.
Input:
width = 10, height = 5
Expected Output:
Width set to 15, Height set to 8
Q34. Write a Swift program to create a class Person with properties firstName and lastName, and a method fullName() to return the full name.
Input:
firstName = "John", lastName = "Smith"
Expected Output:
Full Name: John Smith
Q35. Write a Swift program to create a class Rectangle with properties length and width, and a method perimeter() to calculate the perimeter.
Input:
length = 10, width = 2
Expected Output:
Perimeter of the rectangle: 24
Q36. Write a Swift program to create a Person class with properties firstName and lastName, and a method greet() to print a greeting message.
Input:
firstName = "Emma", lastName = "Stone"
Expected Output:
Hello, my name is Emma Stone.
Q37. Write a Swift program to create a class Car with properties speed and a method accelerate() to increase the speed.
Input:
speed = 50
Expected Output:
New speed: 60
Q38. Write a Swift program to create a Book class with properties title, author, and a method readBook() to simulate reading the book.
Input:
title = "Swift Programming", author = "Chris"
Expected Output:
Reading Swift Programming by Chris.
Q39. Write a Swift program to create a Person class with properties name and age, and a method celebrateBirthday() to simulate a birthday celebration.
Input:
name = "Alice", age = 29
Expected Output:
Happy 30th Birthday, Alice!
Q40. Write a Swift program to create a class Vehicle with properties model and color, and a method start() to simulate starting the vehicle.
Input:
model = "Honda", color = "Blue"
Expected Output:
The Honda Blue is now started.
Q41. Write a Swift program to create a Person class with a method changeAge() that updates the age of the person.
Input:
age = 25, newAge = 26
Expected Output:
Age updated from 25 to 26.
Q42. Write a Swift program to create a Shape class with properties width and height, and a subclass Rectangle that calculates the area.
Input:
width = 8, height = 6
Expected Output:
Area of Rectangle: 48
Q43. Write a Swift program to create a class Person with properties firstName, lastName, and a method initials() to return the initials.
Input:
firstName = "Sarah", lastName = "Johnson"
Expected Output:
Initials: SJ
Q44. Write a Swift program to create a Book class with a method read() that prints "Reading book..." when invoked.
Input:
bookTitle = "Swift in Depth"
Expected Output:
Reading Swift in Depth...
Q45. Write a Swift program to create a Student class with properties name and age, and a method study() to simulate studying.
Input:
name = "Mark", age = 20
Expected Output:
Mark is studying.
Q46. Write a Swift program to create a class Product with properties name and price, and a method applyDiscount() that applies a discount.
Input:
price = 120, discount = 20
Expected Output:
Price after discount: 96
Q47. Write a Swift program to create a class Account with properties balance and accountNumber, and a method withdraw() to withdraw money.
Input:
balance = 1000, amount = 200
Expected Output:
New balance: 800
Q48. Write a Swift program to create a Vehicle class with properties color and type, and a method info() to display the vehicle's details.
Input:
color = "Red", type = "Sedan"
Expected Output:
Vehicle Type: Sedan, Color: Red
Q49. Write a Swift program to create a class Student with properties name and marks, and a method isPassed() to check if the student passed based on marks.
Input:
marks = 60
Expected Output:
Passed
Q50. Write a Swift program to create a Teacher class with properties name and subject, and a method teach() that prints the subject being taught.
Input:
subject = "Mathematics"
Expected Output:
Teaching Mathematics