Challenge your understanding of object-oriented programming fundamentals with questions on classes, objects, attributes, and methods. This quiz is perfect for those seeking to solidify core OOP concepts and terminology in programming.
In object-oriented programming, what is the main purpose of defining a class such as 'Car'?
Explanation: A class acts as a blueprint from which objects are created, specifying the properties and behaviors common to all objects of that type. Classes themselves do not perform operations immediately (option B) but are templates. Option C is incorrect because while classes can have variables, their purpose is not temporary storage. Option D is misleading, as classes define structure rather than provide global data access.
When you create an object named 'book1' from the 'Book' class, which concept is best illustrated?
Explanation: Instantiation is the process of creating an actual object from a class, as seen when making 'book1' from 'Book'. Inheritance (option A) involves acquiring properties from a parent class, which is not shown here. Encapsulation (option B) is about restricting access to certain internal parts, and abstraction (option D) simplifies complex reality by modeling classes. Instantiation specifically refers to object creation.
Given a 'Dog' class with 'name' and 'bark()', which of the following best describes 'bark()'?
Explanation: 'bark()' is a function defined within the class, representing a behavior or action the object can perform, making it a method. Option A confuses methods with attributes, which store data like 'name'. Option C refers to class variables, which is a different concept. Option D describes something like a primary key or unique ID, not a method.
How can you access the 'height' attribute of an object 'tree', assuming 'height' is public?
Explanation: In most object-oriented languages, the dot notation 'tree.height' is used to access a public attribute of an object. Option A uses an arrow notation found in some languages but not for public attribute access. Option B reverses the correct order. Option D mixes up array and object notation, which is incorrect in this context.
Which statement correctly describes the role of a constructor in a class?
Explanation: A constructor's primary role is to initialize new objects by assigning initial values to their attributes when an object is created. Option B refers instead to destructors, which handle object cleanup. Option C is incorrect because constructors do not control inheritance. Option D confuses constructors with error handling mechanisms.