EzDevInfo.com

oop interview questions

Top oop frequently asked interview questions

What is a metaclass in Python?

What are metaclasses? What do you use them for?


Source: (StackOverflow)

Constructors in JavaScript objects

Can JavaScript classes/objects have constructors? How are they created?


Source: (StackOverflow)

Advertisements

Prefer composition over inheritance?

Why prefer composition over inheritance? What trade-offs are there for each approach? When should you choose inheritance over composition?


Source: (StackOverflow)

Difference between a method and a function

Can someone provide a simple explanation of methods vs. functions?


Source: (StackOverflow)

What is the difference between an interface and abstract class?

What exactly is the difference between an interface and abstract class?


Source: (StackOverflow)

What is the difference between an abstract function and a virtual function?

In which cases is it recommended to use virtual or abstract? Which is the more correct approach?


Source: (StackOverflow)

What is the difference between old style and new style classes in Python?

What is the difference between old style and new style classes in Python? Is there ever a reason to use old-style classes these days?


Source: (StackOverflow)

Monad in plain English? (For the OOP programmer with no FP background)

In terms that an OOP programmer would understand (without any functional programming background), what is a monad?

What problem does it solve and what are the most common places it's used?

EDIT:

To clarify the kind of understanding I was looking for, let's say you were converting an FP application that had monads into an OOP application. What would you do to port the responsibilities of the monads to the OOP app?


Source: (StackOverflow)

Can you write object-oriented code in C?

Can you write object-oriented code in C? Especially with regard to polymorphism.


See also Stack Overflow question Object-orientation in C.


Source: (StackOverflow)

Why Doesn't C# Allow Static Methods to Implement an Interface?

Why was C# designed this way?

As I understand it, an interface only describes behaviour, and serves the purpose of describing a contractual obligation for classes implementing the interface that certain behaviour is implemented.

If classes wish to implement that behavour in a shared method, why shouldn't they?

Here is an example of what I have in mind:

// These items will be displayed in a list on the screen.
public interface IListItem {
  string ScreenName();
  ...
}

public class Animal: IListItem {
    // All animals will be called "Animal".
    public static string ScreenName() {
        return "Animal";
    }
....
}

public class Person: IListItem {

    private string name;

    // All persons will be called by their individual names.
    public string ScreenName() {
        return name;
    }

    ....

 }

Source: (StackOverflow)

Adding a Method to an Existing Object Instance

I've read that it is possible to add a method to an existing object (e.g. not in the class definition) in Python, I think this is called Monkey Patching (or in some cases Duck Punching). I understand that it's not always a good decision to do so. But, how might one do this?

UPDATE 8/04/2008 00:21:01 EST:

That looks like a good answer John Downey, I tried it but it appears that it ends up being not a true method. Your example defines the new patch function with an argument of self, but if you write actual code that way, the now patched class method asks for an argument named self (it doesn't automagically recognize it as the object to which it is supposed to bind, which is what would happen if defined within the class definition), meaning you have to call class.patch(obj) instead of just class.patch() if you want the same functionality as a true method. It looks like Python isn't really treating it as a method, but more just as a variable which happens to be a function (and as such is callable). Is there any way to attach an actual method to a class?

Oh, and Ryan, that isn't exactly what I was looking for (it isn't builtin functionality), but it is quite cool nonetheless.


Source: (StackOverflow)

How should a model be structured in MVC?

I am just getting a grasp on the MVC framework and I often wonder how much code should go in the model. I tend to have a data access class that has methods like this:

public function CheckUsername($connection, $username)
{
    try
    {
        $data = array();
        $data['Username'] = $username;

        //// SQL
        $sql = "SELECT Username FROM" . $this->usersTableName . " WHERE Username = :Username";

        //// Execute statement
        return $this->ExecuteObject($connection, $sql, $data);
    }
    catch(Exception $e)
    {
        throw $e;
    }
}

My models tend to be an entity class that is mapped to the database table.

Should the model object have all the database mapped properties as well as the code above or is it OK to separate that code out that actually does the database work?

Will I end up having four layers?


Source: (StackOverflow)

JavaScript: Class.method vs. Class.prototype.method

What is the difference between the following two declarations?

Class.method = function () { /* code */ }
Class.prototype.method = function () { /* code using this.values */ }

Is it okay to think of the first statement as a declaration of a static method, and the second statement as a declaration of an instance method?


Source: (StackOverflow)

What is the difference between class and instance methods?

What's the difference between a class method and an instance method?

Are instance methods the accessors (getters and setters) while class methods are pretty much everything else?


Source: (StackOverflow)

Interview: Can we instantiate abstract class?

The interviewer asked - Can we instantiate an abstract class? I said, No. He told me - Wrong, we can.

I argued a bit on this. Then he told me to try this yourself at your home.

abstract class my {
    public void mymethod() {
        System.out.print("Abstract");
    }
}

class poly {
    public static void main(String a[]) {
        my m = new my() {};
        m.mymethod();
    }
}

Here, I'm creating instance of my class and calling method of abstract class. Can anyone please explain this to me? Was I really wrong during my interview?


Source: (StackOverflow)