
Practice 50 JavaScript Object-Oriented Programming Questions
Q1. Write a JavaScript program to create a Car class with a property model and a method drive that logs "Driving!".
Input: Create an instance of Car with model = "Tesla". Call the drive() method.
Expected Output: Logs "Driving!".
Q2. Write a JavaScript program to create a Person class with properties name and age. Initialize them using the constructor.
Input: Create an instance with name = "John", age = 30.
Expected Output: Logs name: John, age: 30.
Q3. Write a JavaScript program to create an Animal class with a method speak() that logs "Animal speaks!".
Input: Create an instance of Animal. Call speak().
Expected Output: Logs "Animal speaks!".
Q4. Write a JavaScript program to create a Rectangle class with properties length and width and a method area() that returns the area of the rectangle.
Input: Create an instance with length = 5, width = 3. Call area().
Expected Output: Returns 15.
Q5. Write a JavaScript program to create a Book class with properties title and author and a method getDetails() that returns a string Title: <title>, Author: <author>.
Input: Create an instance with title = "JavaScript Basics", author = "John Doe". Call getDetails().
Expected Output: Returns "Title: JavaScript Basics, Author: John Doe".
Q6. Write a JavaScript program to demonstrate inheritance by creating a Dog class that inherits from the Animal class and has an additional method bark().
Input: Create an instance of Dog and call bark().
Expected Output: Logs "Woof!".
Q7. Write a JavaScript program to create a Circle class with a property radius and a method circumference() that calculates the circumference.
Input: Create an instance with radius = 7. Call circumference().
Expected Output: Returns 43.982297150257104.
Q8. Write a JavaScript program to demonstrate polymorphism by creating a Shape class with a method area(), and override it in Circle and Rectangle classes.
Input: Create instances of Circle with radius = 5 and Rectangle with length = 4, width = 6. Call area().
Expected Output: Returns 78.53981633974483 for the circle, 24 for the rectangle.
Q9. Write a JavaScript program to create a Car class with a constructor and a method start() that logs "Starting the car!".
Input: Create an instance with model = "Toyota". Call start().
Expected Output: Logs "Starting the car!".
Q10. Write a JavaScript program to create a Person class with a greet() method that logs "Hello, <name>!".
Input: Create an instance with name = "Alice". Call greet().
Expected Output: Logs "Hello, Alice!".
Q11. Write a JavaScript program to create a BankAccount class with methods to deposit() and withdraw().
Input: Create an instance with initial balance 1000. Deposit 500, withdraw 200.
Expected Output: Final balance: 1300.
Q12. Write a JavaScript program to create a Student class with a method getGrade() that returns the student's grade based on the score.
Input: Create an instance with score = 85. Call getGrade().
Expected Output: Returns "A".
Q13. Write a JavaScript program to create an Employee class with properties name, position, and salary, and a method getDetails() that returns the employee details.
Input: Create an instance with name = "Bob", position = "Manager", salary = 5000. Call getDetails().
Expected Output: Returns "Name: Bob, Position: Manager, Salary: 5000".
Q14. Write a JavaScript program to create a Person class with a birthYear property and a method getAge() that calculates the age based on the current year.
Input: Create an instance with birthYear = 1990. Call getAge().
Expected Output: Returns the current age.
Q15. Write a JavaScript program to create a Product class with properties name, price, and quantity, and a method totalPrice() that returns the total price.
Input: Create an instance with name = "Laptop", price = 1000, quantity = 2. Call totalPrice().
Expected Output: Returns 2000.
Q16. Write a JavaScript program to create a Person class with a method introduce() that logs "Hi, my name is <name>".
Input: Create an instance with name = "Charlie". Call introduce().
Expected Output: Logs "Hi, my name is Charlie".
Q17. Write a JavaScript program to create a Student class with a method study() that logs "Studying...".
Input: Create an instance with name = "Tom". Call study().
Expected Output: Logs "Studying...".
Q18. Write a JavaScript program to create an Author class with properties name and books, and a method addBook() to add a book to the author's collection.
Input: Create an instance with name = "J.K. Rowling". Add books ["Harry Potter", "Fantastic Beasts"].
Expected Output: Logs the list of books.
Q19. Write a JavaScript program to create a Counter class with methods increment() and decrement().
Input: Create an instance with value = 0. Increment once, then decrement once.
Expected Output: Final value: 0.
Q20. Write a JavaScript program to create a Book class with properties title, author, and price, and a method applyDiscount() to apply a discount.
Input: Create an instance with title = "JS Guide", price = 100. Apply 10% discount.
Expected Output: Returns 90.
Q21. Write a JavaScript program to create a Person class with a fullName getter and setter.
Input: Create an instance with firstName = "John", lastName = "Doe". Access and modify fullName.
Expected Output: Logs full name correctly.
Q22. Write a JavaScript program to create an Employee class with properties id and name, and a method getInfo() that returns employee details.
Input: Create an instance with id = 123, name = "Steve". Call getInfo().
Expected Output: Returns "ID: 123, Name: Steve".
Q23. Write a JavaScript program to create a Person class with a method isAdult() that returns true if the person is 18 or older.
Input: Create an instance with age = 20. Call isAdult().
Expected Output: Returns true.
Q24. Write a JavaScript program to create a Book class with properties title, author, and price, and a method getPrice() to return the price.
Input: Create an instance with title = "JavaScript Guide", price = 50. Call getPrice().
Expected Output: Returns 50.
Q25. Write a JavaScript program to create a Car class with a method fuelEfficiency() that calculates fuel efficiency.
Input: Create an instance with mileage = 15, fuel = 5. Call fuelEfficiency().
Expected Output: Returns 3.
Q26. Write a JavaScript program to create a User class with a method isValidEmail() that checks if the email is valid.
Input: Create an instance with email = "user@example.com". Call isValidEmail().
Expected Output: Returns true.
Q27. Write a JavaScript program to create a Library class with a method addBook() that adds a book to the collection.
Input: Create an instance and add book = "JavaScript for Beginners".
Expected Output: Logs "Book added to library".
Q28. Write a JavaScript program to create a Movie class with a releaseYear property and a method isOld() that returns true if the movie is older than 10 years.
Input: Create an instance with releaseYear = 2005. Call isOld().
Expected Output: Returns true.
Q29. Write a JavaScript program to create a Person class with a method getGreeting() that returns a greeting message.
Input: Create an instance with name = "Mary". Call getGreeting().
Expected Output: Returns "Hello, Mary!".
Q30. Write a JavaScript program to create a Person class with a property age and a method isMinor() that checks if the person is under 18.
Input: Create an instance with age = 15. Call isMinor().
Expected Output: Returns true.
Q31. Write a JavaScript program to create a Circle class with a method area() to calculate the area of the circle.
Input: Create an instance with radius = 3. Call area().
Expected Output: Returns 28.274333882308138.
Q32. Write a JavaScript program to create a Shape class with a method getArea() and override it in Circle and Rectangle classes.
Input: Create instances of Circle with radius = 5 and Rectangle with length = 6, width = 4.
Expected Output: Returns 78.53981633974483 for the circle, 24 for the rectangle.
Q33. Write a JavaScript program to create a Product class with a method getInfo() that returns product details.
Input: Create an instance with name = "Laptop", price = 1500. Call getInfo().
Expected Output: Returns "Product: Laptop, Price: 1500".
Q34. Write a JavaScript program to create a Person class with a method isOlderThan() that compares the person's age with another person.
Input: Create two instances: one with age = 30, another with age = 25.
Expected Output: Returns true for the first person.
Q35. Write a JavaScript program to create a Customer class with properties name and purchaseAmount, and a method discount() that calculates a discount based on the purchase amount.
Input: Create an instance with purchaseAmount = 1000. Call discount().
Expected Output: Returns 100.
Q36. Write a JavaScript program to create a Vehicle class with a method start() and override it in Car and Truck classes.
Input: Create instances of Car and Truck. Call start().
Expected Output: Logs different messages for Car and Truck.
Q37. Write a JavaScript program to create an Account class with methods deposit() and withdraw() and a method getBalance() that returns the current balance.
Input: Create an instance with an initial balance of 500. Deposit 200, withdraw 100.
Expected Output: Final balance: 600.
Q38. Write a JavaScript program to create a Person class with a fullName method that returns the person's full name.
Input: Create an instance with firstName = "John", lastName = "Doe". Call fullName().
Expected Output: Returns "John Doe".
Q39. Write a JavaScript program to create a Task class with a property completed and a method markComplete() to mark the task as complete.
Input: Create an instance with completed = false. Call markComplete().
Expected Output: Final value of completed: true.
Q40. Write a JavaScript program to create a Date class with a method getDayOfWeek() that returns the day of the week.
Input: Create an instance with the current date. Call getDayOfWeek().
Expected Output: Returns the current day of the week.
Q41. Write a JavaScript program to create a Player class with a method play() that logs "Playing the game!".
Input: Create an instance with name = "Tom". Call play().
Expected Output: Logs "Playing the game!".
Q42. Write a JavaScript program to create an Employee class with a method getSalary() that returns the salary.
Input: Create an instance with salary = 4000. Call getSalary().
Expected Output: Returns 4000.
Q43. Write a JavaScript program to create a Song class with properties title, artist, and duration, and a method getDetails() that returns song details.
Input: Create an instance with title = "Shape of You", artist = "Ed Sheeran". Call getDetails().
Expected Output: Returns "Title: Shape of You, Artist: Ed Sheeran".
Q44. Write a JavaScript program to create a Item class with properties name, price, and a method applyTax() that applies a 5% tax to the price.
Input: Create an instance with name = "T-Shirt", price = 20. Call applyTax().
Expected Output: Returns 21.
Q45. Write a JavaScript program to create a MusicPlayer class with methods play(), pause(), and stop() that perform corresponding actions.
Input: Create an instance with currentTrack = "Track 1". Call play(), pause(), stop().
Expected Output: Logs corresponding actions for each method.
Q46. Write a JavaScript program to create a Movie class with properties title, director, and year, and a method getInfo() to return the movie details.
Input: Create an instance with title = "Inception", director = "Christopher Nolan", year = 2010. Call getInfo().
Expected Output: Returns "Title: Inception, Director: Christopher Nolan, Year: 2010".
Q47. Write a JavaScript program to create a Ticket class with properties event, date, and a method getTicketDetails() that returns the ticket details.
Input: Create an instance with event = "Concert", date = "2024-12-01". Call getTicketDetails().
Expected Output: Returns "Event: Concert, Date: 2024-12-01".
Q48. Write a JavaScript program to create a Product class with a method getDiscountPrice() that calculates a discount based on a given percentage.
Input: Create an instance with price = 100, discount = 10. Call getDiscountPrice().
Expected Output: Returns 90.
Q49. Write a JavaScript program to create a Movie class with properties name, director, and rating, and a method isHighRated() that checks if the movie has a rating greater than 8.
Input: Create an instance with rating = 9. Call isHighRated().
Expected Output: Returns true.
Q50. Write a JavaScript program to create a Task class with a method complete() that marks the task as complete and logs "Task completed!".
Input: Create an instance with status = "incomplete". Call complete().
Expected Output: Logs "Task completed!".