quiz Programming · 10 questions

Fundamentals of Object-Oriented Programming

help_outline 10 questions
timer ~5 min
auto_awesome AI-generated
0 / 10
Score : 0%
1

Which programming paradigm introduces the concept of objects that encapsulate both data and behavior, unlike procedural programming?

2

In Java, why does a class variable declared as static belong to the class rather than to each instance?

3

Given a class Point with methods moveto(int a, int b) and rmoveto(int dx, int dy), which of the following calls correctly moves a point from (2,3) to (5,7)?

4

Which of the following statements best describes the difference between static (compile‑time) and dynamic (run‑time) polymorphism?

5

In a statically typed language, why is the statement 'Mammal pet = new Dog; pet.speak();' illegal if speak() is defined only in Dog?

6

When using a template class Stack<T>, what is the term for the concrete class generated after substituting int for T?

7

Which inheritance type can cause the 'diamond problem' and therefore requires careful handling in languages like C++?

8

In Java, what is the primary purpose of an interface?

9

Consider the following class hierarchy: class Animal { public void speak() { System.out.println("generic"); } } class Dog extends Animal { public void speak() { System.out.println("woof"); } } If we write Animal a = new Dog(); a.speak(); which version of speak() is executed and why?

10

Why might a developer choose to overload constructors rather than provide default values for parameters in a single constructor?

menu_book

Fundamentals of Object-Oriented Programming

Review key concepts before taking the quiz

Introduction to Object‑Oriented Programming (OOP)

Object‑oriented programming is a programming paradigm that models software as a collection of objects—self‑contained units that combine data (state) and behavior (methods). Unlike procedural programming, which separates data structures from functions, OOP encourages encapsulation, inheritance, and polymorphism, making code more reusable and easier to maintain.

Core Concepts of OOP

Encapsulation

Encapsulation hides the internal representation of an object behind a well‑defined interface. By keeping fields private and exposing public methods, developers protect invariants and reduce coupling.

Inheritance

Inheritance allows a new class (subclass) to acquire the attributes and methods of an existing class (superclass). This promotes code reuse and establishes an is‑a relationship.

Polymorphism

Polymorphism lets a single interface represent different underlying forms (data types). It appears in two main flavors: static (compile‑time) and dynamic (run‑time) polymorphism.

Static Members and Class Variables

What is a static variable?

In Java, a variable declared with the static keyword belongs to the class itself, not to any individual instance. The JVM allocates the static field once when the class is loaded, and every object of that class shares the same memory location.

  • Memory allocation: The static field resides in the method area of the JVM, not on the heap for each object.
  • Shared state: Changing the value through one instance instantly reflects in all other instances.
  • Use cases: Constants, counters, configuration settings, or any data that must be common across all objects.

Why static belongs to the class

The class loader creates a single runtime representation of the class. During this process, static fields are initialized exactly once. Because they are part of the class metadata, they are accessible without creating an object, e.g., Math.PI.

Method Overloading vs. Method Overriding

Static (compile‑time) polymorphism: Overloading

Method overloading occurs when multiple methods share the same name but differ in parameter lists (type, number, or order). The compiler determines which method to invoke based on the compile‑time types of the arguments.

Dynamic (run‑time) polymorphism: Overriding

Method overriding happens when a subclass provides its own implementation of a method declared in a superclass. The decision of which method to execute is deferred until run time, based on the actual (dynamic) type of the object.

Key distinction: Overloading is resolved during compilation, while overriding is resolved during execution.

Static Typing and Method Visibility

In statically typed languages like Java, the compiler checks that every method call is valid for the variable's declared (static) type. Consider the statement:

Mammal pet = new Dog();
pet.speak();

If speak() is declared only in Dog and not in Mammal, the compiler rejects the call because pet is statically typed as Mammal. The runtime type (Dog) is irrelevant at compile time; the method must be part of the static type's contract.

Generics, Templates, and Specialization

What is template specialization?

When a generic class such as Stack<T> is instantiated with a concrete type (e.g., int), the compiler generates a specific version of the class. This process is called template specialization (or instantiation) in C++ and type erasure in Java.

Specialization creates a new class named something like Stack_int that contains all the methods of the generic template, but with T replaced by int. This enables type‑safe collections without sacrificing performance.

Inheritance and the Diamond Problem

Multiple inheritance and ambiguity

Multiple inheritance allows a class to inherit from more than one direct superclass. While powerful, it can introduce the infamous diamond problem:

   A
  / \
 B   C
  \ /
   D

Class D inherits from both B and C, which themselves inherit from A. If A defines a member variable or method, D may receive two copies—one via B and another via C. Languages like C++ resolve this with virtual inheritance, ensuring only a single shared base subobject.

Interfaces as Contracts in Java

Purpose of an interface

An interface in Java defines a contract: a set of method signatures without implementation. Any class that implements the interface promises to provide concrete bodies for those methods.

  • Decoupling: Code can depend on the interface rather than a concrete class, enabling flexible substitution.
  • Multiple inheritance of type: A class can implement multiple interfaces, achieving polymorphic behavior without inheriting implementation code.
  • Default methods (Java 8+): Interfaces can now include default implementations, blending the benefits of abstract classes and pure contracts.

Practical Example: Moving a Point

Consider a class Point with two methods:

  • void moveto(int a, int b) – sets the absolute coordinates.
  • void rmoveto(int dx, int dy) – moves the point relative to its current position.

To move a point from (2,3) to (5,7), the correct call is p.moveto(5,7);. Using rmoveto(5,7) would incorrectly add the offsets to the current coordinates, resulting in (7,10).

Summary of Key Takeaways

  • Object‑oriented programming introduces objects that encapsulate data and behavior.
  • Static class variables are allocated once when the class loads and are shared by all instances.
  • Method overloading provides static polymorphism; method overriding provides dynamic polymorphism.
  • In a statically typed language, a method must be declared in the variable's static type for the call to compile.
  • When a generic or template class is instantiated with a concrete type, the resulting class is called a specialization.
  • Multiple inheritance can cause the diamond problem; languages use mechanisms like virtual inheritance to mitigate it.
  • Java interfaces define contracts, enabling multiple inheritance of type and promoting loose coupling.

Further Reading and Resources

To deepen your understanding of the concepts covered, explore the following resources:

By mastering these foundational ideas, you will be equipped to design robust, maintainable, and scalable software systems using object‑oriented principles.

Stop highlighting.
Start learning.

Join students who have already generated over 50,000 quizzes on Quizly. It's free to get started.