← Back to quizzesFree quiz

Fundamentals of Object-Oriented Programming

Object‑oriented programming (OOP) is a cornerstone of modern software development. This course breaks down the essential concepts that appear in typical quiz questions, offering clear…

11 questions~6 min
Fundamentals of Object-Oriented Programming — Qwi
0 / 11
Score: 0%
1

Which programming technique first introduced the concept of extracting repeated code into separate procedures?

2

In Java, why does a class variable declared as static belong to the class rather than 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 will move the point to coordinates (5,7) assuming its current position is (2,3)?

4

Which inheritance type can cause the "diamond problem" if not handled properly?

5

In a statically typed language, what determines whether a message can be sent to a variable at compile time?

6

Which of the following statements correctly describes method overriding in Java?

7

When using a generic Stack class, which of the following is a valid explicit specialization?

8

In the Struts framework, which component is primarily responsible for mapping a URL to a specific Action class?

9

Which OOP principle is directly supported by declaring an interface that lists only method signatures without data members?

10

Consider two classes, Animal (base) and Dog (derived). If a variable of type Animal holds a Dog object, which of the following statements is true in Java?

11

What is the main advantage of using a copy constructor in a class like TimeOfDay?

Fundamentals of Object‑Oriented Programming

Object‑oriented programming (OOP) is a cornerstone of modern software development. This course breaks down the essential concepts that appear in typical quiz questions, offering clear explanations, practical examples, and SEO‑friendly language to help you master the topic.

1. The Evolution of Code Reuse: From Procedural to Modular Programming

Early software was written in unstructured programming style, where code flowed without clear boundaries. The first major shift toward reuse came with procedural programming. By grouping related statements into procedures (also called functions or subroutines), developers could extract repeated logic into a single location and call it from multiple places.

Later, modular programming refined this idea by organizing procedures into separate files or modules, each with its own namespace. While modular programming improves maintainability, the concept of extracting repeated code originated with procedural programming.

  • Procedural programming – introduces reusable procedures.
  • Modular programming – groups procedures into distinct modules.
  • Object‑oriented programming – encapsulates data and behavior together.

2. Class Variables and the static Keyword in Java

In Java, a variable declared with the static modifier belongs to the class itself, not to any individual instance. This happens because static fields are allocated when the class is loaded by the JVM, creating a single shared copy for all objects.

Key points to remember:

  • Memory allocation: static fields are stored in the method area of the JVM, not on the heap per object.
  • Shared state: changes made by one instance are visible to all other instances.
  • Access: static members can be accessed via the class name (e.g., MyClass.counter) as well as through an instance reference.

3. Understanding Method Calls: moveto vs. rmoveto

Consider a Point class with two methods:

  • moveto(int a, int b) – moves the point directly to the absolute coordinates (a, b).
  • rmoveto(int dx, int dy) – moves the point relatively by adding dx and dy to the current coordinates.

If the point starts at (2, 3) and you want it to end at (5, 7), the correct call is:

p.moveto(5, 7);

Using rmoveto(3,4) would also work because 2+3=5 and 3+4=7, but the quiz expects the direct absolute method.

4. Inheritance Patterns and the Diamond Problem

Multiple inheritance allows a class to inherit from more than one superclass. When two parent classes share a common ancestor, the subclass can end up with two copies of the ancestor’s members – a situation known as the diamond problem. Languages like C++ provide virtual inheritance to resolve this, while Java avoids the issue by allowing only single class inheritance (multiple interfaces are safe because they contain no implementation).

  • Multiple inheritance: prone to the diamond problem.
  • Single class inheritance: safe from the diamond issue.
  • Hierarchical & multilevel inheritance: do not create the diamond shape.

5. Compile‑Time Type Checking in Statically Typed Languages

In a statically typed language like Java, the compiler determines whether a method call is valid based on the static (declared) type** of the variable**. This static type is known at compile time and dictates which methods are accessible.

For example:

Animal a = new Dog(); // static type is Animal
  a.makeSound(); // compiler checks Animal for makeSound()

If makeSound exists in Animal, the call compiles, even though the runtime object is a Dog. The actual method executed is determined later via dynamic dispatch.

6. Method Overriding Rules in Java

Overriding occurs when a subclass provides its own implementation of a method declared in a superclass. The overriding method must:

  • Have the exact same signature (name, parameter types, and order).
  • Return a type that is the same or a covariant return type (a subclass of the original return type).
  • Be marked with @Override (optional but recommended) to signal intent and enable compile‑time checking.
  • Not be more restrictive in access (e.g., a public method cannot be overridden by a private one).

Static methods cannot be overridden; they are hidden instead.

7. Generic Class Specialization in C++

When working with templates, you can provide explicit specializations for particular type arguments. For a generic Stack<T> class, a valid explicit specialization for int looks like:

template<>
void Stack::push(int val) {
    // custom implementation for integers
}

This syntax tells the compiler to use a distinct implementation for the int version of push. Other options shown in the quiz are syntactically incorrect or represent partial specializations, which require a different form.

8. Request Mapping in the Struts Framework

Apache Struts follows the Model‑View‑Controller (MVC) pattern. The component that maps an incoming URL to a specific Action class is the ActionServlet. When a request arrives, the ActionServlet consults the struts-config.xml file, finds the matching ActionMapping, and forwards the request to the appropriate Action implementation.

  • ActionServlet: central controller, handles URL routing.
  • ActionMapping: configuration entry linking a URL pattern to an Action class.
  • ActionForm: JavaBean that holds form data.
  • ActionForward: determines the next view (JSP) after the Action executes.

9. Summary of Core OOP Concepts Covered

By mastering the topics above, you will be equipped to answer quiz questions confidently and apply these principles in real‑world programming:

  • Procedural programming introduced reusable procedures.
  • Static class variables belong to the class because they are allocated at load time.
  • Absolute vs. relative method calls (moveto vs. rmoveto).
  • Multiple inheritance can cause the diamond problem.
  • Static type determines compile‑time method availability.
  • Method overriding requires identical signatures and the @Override annotation.
  • Explicit template specialization syntax in C++.
  • Struts’ ActionServlet maps URLs to Action classes.

Understanding these fundamentals not only prepares you for quizzes but also builds a solid foundation for designing robust, maintainable, and scalable software systems.