Practice 50 Swift Core Data and Persistence Questions
Q1. Write a Swift program to create a Core Data model with a single entity Person containing attributes name and age.
Input: Core Data model with Person entity.
Output: Core Data model with entity and attributes created.
Q2. Write a Swift program to save a Person object to Core Data with name "John" and age 30.
Input: name = "John", age = 30.
Output: "Data saved to Core Data" message printed.
Q3. Write a Swift program to fetch all Person objects from Core Data and print their names.
Input: Core Data with multiple Person entities.
Output: List of Person names printed.
Q4. Write a Swift program to update the age of a Person entity in Core Data.
Input: name = "John", update age to 31.
Output: "Age updated to 31" message printed.
Q5. Write a Swift program to delete a Person object from Core Data.
Input: name = "John".
Output: "Person deleted from Core Data" message printed.
Q6. Write a Swift program to fetch Person objects from Core Data where age is greater than 25.
Input: Core Data with Person entities.
Output: List of Person names with age > 25 printed.
Q7. Write a Swift program to save a Person object with name "Alice" and age 25 into Core Data.
Input: name = "Alice", age = 25.
Output: "Data saved to Core Data" message printed.
Q8. Write a Swift program to fetch Person objects from Core Data sorted by age.
Input: Core Data with Person entities.
Output: List of Person names sorted by age.
Q9. Write a Swift program to create a new Core Data entity called Book with attributes title and author.
Input: Book entity with title and author attributes.
Output: "Core Data entity Book created" message printed.
Q10. Write a Swift program to save a Book object to Core Data with title "Swift Programming" and author "John Doe".
Input: title = "Swift Programming", author = "John Doe".
Output: "Book saved to Core Data" message printed.
Q11. Write a Swift program to fetch all Book objects from Core Data and print their titles.
Input: Core Data with Book entities.
Output: List of Book titles printed.
Q12. Write a Swift program to fetch a Book object from Core Data where title is "Swift Programming".
Input: Core Data with Book entities.
Output: Book object with title "Swift Programming" printed.
Q13. Write a Swift program to update the author of a Book entity in Core Data.
Input: title = "Swift Programming", update author to "Jane Smith".
Output: "Author updated to Jane Smith" message printed.
Q14. Write a Swift program to delete a Book object from Core Data.
Input: title = "Swift Programming".
Output: "Book deleted from Core Data" message printed.
Q15. Write a Swift program to fetch all Book objects from Core Data sorted by title.
Input: Core Data with multiple Book entities.
Output: List of Book titles sorted alphabetically.
Q16. Write a Swift program to save multiple Book objects into Core Data.
Input: title = "Swift Programming", author = "John Doe"; title = "Core Data 101", author = "Jane Smith".
Output: "Multiple books saved to Core Data" message printed.
Q17. Write a Swift program to check if a Person object exists in Core Data with the name "John".
Input: name = "John".
Output: "Person exists in Core Data" message printed.
Q18. Write a Swift program to fetch Person objects from Core Data with age between 20 and 30.
Input: Core Data with Person entities.
Output: List of Person names with age between 20 and 30 printed.
Q19. Write a Swift program to save a Person object to Core Data with name "Chris" and age 28.
Input: name = "Chris", age = 28.
Output: "Data saved to Core Data" message printed.
Q20. Write a Swift program to create a relationship between Person and Book entities in Core Data.
Input: Person entity with name "John" and Book entity with title "Swift Programming".
Output: "Relationship between Person and Book created" message printed.
Q21. Write a Swift program to fetch all Book objects related to a specific Person in Core Data.
Input: Person name "John".
Output: List of Book titles associated with "John" printed.
Q22. Write a Swift program to fetch Person objects with their related Book entities using Core Data.
Input: Core Data with relationships between Person and Book.
Output: List of Person names with associated Book titles printed.
Q23. Write a Swift program to save a Person object to Core Data with name "Jane" and age 35, then fetch it by name.
Input: name = "Jane", age = 35.
Output: "Person Jane fetched from Core Data" message printed.
Q24. Write a Swift program to fetch Person objects from Core Data and print the total number of objects.
Input: Core Data with multiple Person entities.
Output: Total count of Person objects printed.
Q25. Write a Swift program to update a Person object’s age by incrementing it by 1 in Core Data.
Input: name = "John", increment age by 1.
Output: "Age incremented to 31" message printed.
Q26. Write a Swift program to fetch Person objects where name starts with "J".
Input: Core Data with multiple Person entities.
Output: List of Person names starting with "J" printed.
Q27. Write a Swift program to create a Core Data entity with attributes productName, price, and quantity.
Input: Core Data entity Product with productName, price, and quantity.
Output: Core Data model with Product entity created.
Q28. Write a Swift program to save a Product object to Core Data with productName "Laptop", price 999.99, and quantity 5.
Input: productName = "Laptop", price = 999.99, quantity = 5.
Output: "Product saved to Core Data" message printed.
Q29. Write a Swift program to fetch Product objects from Core Data with price greater than 500.
Input: Core Data with multiple Product entities.
Output: List of Product names with price > 500 printed.
Q30. Write a Swift program to update the price of a Product entity in Core Data.
Input: productName = "Laptop", update price to 899.99.
Output: "Price updated to 899.99" message printed.
Q31. Write a Swift program to delete a Product object from Core Data.
Input: productName = "Laptop".
Output: "Product deleted from Core Data" message printed.
Q32. Write a Swift program to fetch all Product objects from Core Data sorted by price.
Input: Core Data with multiple Product entities.
Output: List of Product names sorted by price.
Q33. Write a Swift program to create an Order entity with attributes orderId, orderDate, and totalAmount.
Input: Core Data entity Order with orderId, orderDate, and totalAmount.
Output: Core Data model with Order entity created.
Q34. Write a Swift program to save an Order object to Core Data with orderId 1001, orderDate "2025-01-01", and totalAmount 1500.
Input: orderId = 1001, orderDate = "2025-01-01", totalAmount = 1500.
Output: "Order saved to Core Data" message printed.
Q35. Write a Swift program to fetch Order objects from Core Data and print the orderId and totalAmount.
Input: Core Data with multiple Order entities.
Output: List of orderId and totalAmount values printed.
Q36. Write a Swift program to implement an undo and redo feature in Core Data.
Input: Modify an entity, undo/redo the operation.
Output: Entity updated or reverted as per the undo/redo action.
Q37. Write a Swift program to save a Customer entity to Core Data with attributes name, email, and phone.
Input: name = "Alice", email = "alice@example.com", phone = "123456789".
Output: "Customer saved to Core Data" message printed.
Q38. Write a Swift program to fetch all Customer objects from Core Data and sort by name.
Input: Core Data with multiple Customer entities.
Output: List of Customer names sorted alphabetically.
Q39. Write a Swift program to handle migrations in Core Data by adding a new attribute to an existing entity.
Input: Modify existing Core Data model.
Output: "Migration successful" message printed.
Q40. Write a Swift program to fetch Product objects that have a quantity of 0.
Input: Core Data with Product entities.
Output: List of Product names with quantity = 0 printed.
Q41. Write a Swift program to create a Transaction entity with attributes transactionId, date, and amount.
Input: Core Data entity Transaction with transactionId, date, and amount.
Output: Core Data model with Transaction entity created.
Q42. Write a Swift program to save a Transaction object to Core Data with transactionId 500, date "2025-01-01", and amount 2000.
Input: transactionId = 500, date = "2025-01-01", amount = 2000.
Output: "Transaction saved to Core Data" message printed.
Q43. Write a Swift program to fetch all Transaction objects from Core Data and print the transactionId and amount.
Input: Core Data with multiple Transaction entities.
Output: List of transactionId and amount printed.
Q44. Write a Swift program to implement a search functionality in Core Data for finding entities by name.
Input: name = "John".
Output: "Entity with name John found" message printed.
Q45. Write a Swift program to handle background data saving and fetching in Core Data.
Input: Core Data entity save/fetch operation.
Output: "Data saved/fetched in background" message printed.
Q46. Write a Swift program to create a Category entity with attributes categoryId and categoryName.
Input: Core Data entity Category with categoryId, categoryName.
Output: Core Data model with Category entity created.
Q47. Write a Swift program to fetch all Category entities from Core Data and print their names.
Input: Core Data with multiple Category entities.
Output: List of Category names printed.
Q48. Write a Swift program to add a Category to an existing Product in Core Data.
Input: Add Category to Product entity.
Output: "Category added to Product" message printed.
Q49. Write a Swift program to fetch Product entities from Core Data where categoryName is "Electronics".
Input: Core Data with Product and Category entities.
Output: List of Product names with categoryName = "Electronics" printed.
Q50. Write a Swift program to implement Core Data versioning and schema changes.
Input: Modify the schema of an existing Core Data model.
Output: "Schema changed successfully" message printed.
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!