Sunday 13 April 2014

Viva and Basic Questions of OOPS Part-1


OOPS Lab Viva and Basic Questions Part-1



What are all the languages which support OOP?

There are several programming languages which are implementing OOP because of its close proximity to solve real life problems. Languages such as Python, Ruby, Ruby on rails, Perl, PHP, Cold fusion, etc use OOP. Still many languages prefer to use DOM based languages due to the ease in coding.


Explain the implementation phase with respect to OOP?

The design phase is followed by OOP, which is the implementation phase. OOP provides specifications for writing programs in a programming language. During the implementation phase, programming is done as per the requirements gathered during the analysis and design phases.


Explain about Object oriented programming?

Object oriented programming is one of the most popular methodologies in software development. It offers a powerful model for creating computer programs. It speeds the program development process, improves maintenance and enhances reusability of programs.


What are the characteristics of Object Oriented programming language?

Some key features of the Object Oriented programming are:

Emphasis on data rather than procedure
Programs are divided into entities known as objects
Data Structures are designed such that they characterize objects
Functions that operate on data of an object are tied together in data structures
Data is hidden and cannot be accessed by external functions
Objects communicate with each other through functions
New data and functions can be easily added whenever necessary
Follows bottom up design in program design

Explain about object oriented databases?

· Object oriented databases are very popular such as relational database management systems. Object oriented databases systems use specific structure through which they extract data and they combine the data for a specific output. These DBMS use object oriented languages to make the process easier.




Define OOPS. What are its benefits?

Object Oriented Programming Stuctures:
It is a programming methodology in which the programs are organized as collections of objects.Each object represents an instance of some class.The classes can be interrelated to each other through inheritance
OOPS revolves around these concepts:
1)Class
2)Object
3)Inheritance
4)Polymorphism
5)Abstraction
6)Encapsulation

Advantages:
1)Code reusability: define a class and n number of objects implement the class
logic: example: Button class and n number of Button objects
2)Inheritance : Eliminates redundant code and extend the use of existing classes.
example: 1)we can create our own TextBox by inheriting from the TextBox class.
2)We can inherit one Windows From into another.
3)Encapsulation: The programmer can hide the data and functions in a class from other classes.It is accomplished through modifiers like private, protected,
protected internal.
4)Easy to Maintain and Upgrade:
If we want to make changes in a class, we can make them and save the changes in the .dll This .dll can easily be updated in the client by using Update Reference.
5)Polymorphism provides us with methods extensibility.
we can have different methods with same name, but with different kinds of behavior

ex:
Console.WriteLine("welcome");
Console.WriteLine(100);
6)Abstraction allows us to define a common definition of a base class
that multiple derived classes can share.

For example,
create an abstract class bank with simple interest function
This function will be implemented in the derived classes of
the bank class.
ex: 1)icici class will have its own simple interest.
2)ABC class will have its own simple interest.
icici and ABC both are child classes of bank class.



What are the core OOP’s concepts?
Abstraction, Encapsulation,Inheritance and Polymorphism are the core OOP’s concepts.


Explain about a class?

Class describes the nature of a particular thing. Structure and modularity is provided by a Class in object oriented programming environment. Characteristics of the class should be understandable by an ordinary non programmer and it should also convey the meaning of the problem statement to him. Class acts like a blue print.

Class is a template for a set of objects that share a common structure and a common behaviour.



What is an Object? 

Object is an instance of a class. It has state,behaviour and identity. It is also called as an instance of a class.

An object is a combination of messages and data. Objects can receive and send messages and use messages to interact with each other. The messages contain information that is to be passed to the recipient object.



Difference between a Class and an object

Class:
1)It is a datatype that contains the programming logic.
2)Class is visible in the source code and resides in hard disk.
3)Class is like a template or blueprint of the object. It implements reusability,
encapsulation, inheritance
example:Button is a class with properties like Text,BackColor,
events like click, methods like Focus
Object:
1)it is a chunk of memory that implements the class logic.
2)Object is in the RAM and not visible in source code.
3)It is the real world implementation of the class.
Each object has its own copy of data.
example: Button1, Button2 are the objects of Button class.



What is an Instance? 

An instance has state, behaviour and identity. The structure and behaviour of similar classes are defined in their common class. An instance is also called as an object.



What is Polymorphisms?

Polymorphism means one interface and many forms. Polymorphism is a characteristics of being able to assign a different meaning or usage to something in different contexts specifically to allow an entity such as a variable, a function or an object to have more than one form.
There are two types of Polymorphism.
Compile time: function or operator overloading
Runtime: Inheritence & virtual functions


Explain about overriding polymorphism?

Overriding polymorphism is known to occur when a data type can perform different functions. For example an addition operator can perform different functions such as addition, float addition etc. Overriding polymorphism is generally used in complex projects where the use of a parameter is more.


Explain about parametric polymorphism?

Parametric polymorphism is supported by many object oriented languages and they are very important for object oriented techniques. In parametric polymorphism code is written without any specification for the type of data present. Hence it can be used any number of times.


What are advantages of polymorphism and dynamic binding?

Advantages of polymorphism:

– Same interface could be used for creating methods with different implementations
– Reduces the volume of work in terms of distinguishing and handling various objects
– Supports building extensible systems
– Complete implementation can be replaced by using same method signatures

Advantages of dynamic binding:

- Compiling time is much lower and allows faster development
- The current state of the program is better known to the runtime linker, so that the memory and its resources can easily be reordered
- The addresses symbols which may not be known at compile time, can be resolved by the runtime linker. This process is ideal for networked systems
- Common resources could well be shared instead of duplicating them in each time of execution.


What is Information Hiding in OOP?

Encapsulation supports information hiding by making use of the three access specifiers of a class.

Public:- If a class member is public, it can be used anywhere without the access restrictions.

Private: if a class member is private, it can be used only by the members and friends of class.

Protected : if a class member is protected, it can be used only by the members and friends of class and the members and friends of classes derived from class.


What is Association?

Association is a relationship between two classes. In this relationship the object of one instance perform an action on behalf of the other class. The typical behaviour can be invoking the method of other class and using the member of the other class.

public class MyMainClass{

public void init(){

new OtherClass.init();

}

}


What is meant by abstraction? 

Abstraction defines the essential characteristics of an object that distinguish it from all other kinds of objects. Abstraction provides crisply-defined conceptual boundaries relative to the perspective of the viewer. Its the process of focussing on the essential characteristics of an object. Abstraction is one of the fundamental elements of the object model.

What is meant by Encapsulation? 

Encapsulation is the process of compartmentalising the elements of an abtraction that defines the structure and behaviour. Encapsulation helps to separate the contractual interface of an abstraction and implementation.

What is meant by Inheritance? 

Inheritance is a relationship among classes, wherein one class shares the structure or behaviour defined in another class. This is called Single Inheritance. If a class shares the structure or behaviour from multiple classes, then it is called Multiple Inheritance. Inheritance defines “is-a” hierarchy among classes in which one subclass inherits from one or more generalised superclasses.




Explain about multiple inheritance?

Inheritance involves inheriting characteristics from its parents also they can have their own characteristics. In multiple inheritances a class can have characteristics from multiple parents or classes. A sub class can have characteristics from multiple parents and still can have its own characteristics.



What is multiple inheritance and does java support?

If a child class inherits the property from multiple classes is known as multiple inheritance.
Java does not allow to extend multiple classes but to overcome this problem it allows to implement multiple Interfaces.


What is Aggregation?

Aggregation has a relationship between two classes. In this relationship the object of one class is a member of the other class. Aggregation always insists for a direction.

public class MyMainClass{

OtherClass otherClassObj = new OtherClass();

}

What is an Abstract Class?

Abstract class is a class that has no instances. An abstract class is written with the expectation that its concrete subclasses will add to its structure and behaviour, typically by implementing its abstract operations.


What is an Interface?

Interface is an outside view of a class or object which emphaizes its abstraction while hiding its structure and secrets of its behaviour.

What is a base class?

Base class is the most generalised class in a class structure. Most applications have such root classes. In Java, Object is the base class for all classes.


Explain about the Design Phase?

In the design phase, the developers of the system document their understanding of the system. Design generates the blue print of the system that is to be implemented. The first step in creating an object oriented design is the identification of classes and their relationships.


Explain the mechanism of composition?

Composition helps to simplify a complex problem into an easier problem. It makes different classes and objects to interact with each other thus making the problem to be solved automatically. It interacts with the problem by making different classes and objects to send a message to each other.


Explain the rationale behind Object Oriented concepts?


Object oriented concepts form the base of all modern programming languages. Understanding the basic concepts of object-orientation helps a developer to use various modern day programming languages, more effectively.

Explain about instance in object oriented programming?

Every class and an object have an instance. Instance of a particular object is created at runtime. Values defined for a particular object define its State. Instance of an object explains the relation ship between different elements.




Explain about encapsulation?

Encapsulation passes the message without revealing the exact functional details of the class. It allows only the relevant information to the user without revealing the functional mechanism through which a particular class had functioned.


What is a constructor? 
Constructor is an operation that creates an object and/or initialises its state.

Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization.
If no constructor defined then the CLR will provide an implicit constructor which is called as Default Constructor.
A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.
Constructors do not return a value
Constructors can be overloaded



What are the various types of Constructors

Public : Accessible to All
Private: Those classes in which only static members are there and you don't want there objects to be created in any class.
Static: Used for initializing only the static members of the class. These will be invoked for the very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers.
Intern: implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly (Namespace).
and External


What is a destructor?
Destructor is an operation that frees the state of an object and/or destroys the object itself. In Java, there is no concept of destructors. Its taken care by the JVM.


What is meant by Binding?
Binding denotes association of a name with a class.


What is meant by static binding?
Static binding is a binding in which the class association is made during compile time. This is also called as Early binding.


What is meant by Dynamic binding?
Dynamic binding is a binding in which the class association is not made until the object is created at execution time. It is also called as late binding.


Define Modularity?
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.


What is meant by Persistence?

Persistence is the property of an object by which its existence transcends space and time.




What is collaboration?
Collaboration is a process whereby several objects cooperate to provide some higher level behaviour.

In Java, How to make an object completely encapsulated?
All the instance variables should be declared as private and public getter and setter methods should be provided for accessing the instance variables.



How is polymorphism acheived in java?
Inheritance, Overloading and Overriding are used to acheive Polymorphism in java.



What is Abstract method?

Abstract method doesn't provide the implementation & forces the derived class to override the method.



What is Virtual method?

Virtual Method has implementation & provide the derived class with the option to override it.



Can Struct be inherited?

No, Struct can't be inherited as this is implicitly sealed.



What is Static field?

To indicate that a field should only be stored once no matter how many instance of the class we create.



What is Static Method?

It is possible to declare a method as Static provided that they don't attempt to access any instance data or other instance methods.



What is Virtual keyword?

This keyword indicates that a member can be overridden in a child class. It can be applied to methods, properties, indexes and events.



What is a New modifier?
The new modifiers hides a member of the base class. C# supports only hide by signature.



What is Sealed modifiers?

Sealed types cannot be inherited & are concrete.
Sealed modifiers can also be applied to instance methods, properties, events & indexes. It can't be applied to static members.
Sealed members are allowed in sealed and non-sealed classes.



Difference between sealed and static classes

sealed classes:
1)we can create their instances, but cannot inherit them
ex:
sealed class demo
{
}
class abc:demo
{
--Wrong
}
2)They can contain static as well as nonstatic members.

static classes:
1)we can neither create their instances, nor inherit them
ex:
static class Program
{
}
2)They can have static members only.



What is an Interface?

An interface is a contract & defines the requisite behavior of generalization of types.
An interface mandates a set of behavior, but not the implementation. Interface must be inherited. We can't create an instance of an interface.
An interface is an array of related function that must be implemented in derived type. Members of an interface are implicitly public & abstract.
An interface can inherit from another interface.



When to use Interface over abstract class?

Abstract Classes: Classes which cannot be instantiated. This means one cannot make a object of this class or in other way cannot create object by saying ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class. Abstract classes contains have one or more abstarct methods, ie method body only no implementation.
Interfaces: These are same as abstract classes only difference is we can only define method definition and no implementation.
When to use wot depends on various reasons. One being design choice.
One reason for using abstarct classes is we can code common
functionality and force our developer to use it. I can have a complete
class but I can still mark the class as abstract.
Developing by interface helps in object based communication.



What is pure virtual function?

When you define only function prototype in a base class without and do the complete implementation in derived class. This base class is called abstract class and client won’t able to instantiate an object using this base class.

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be "pure" using the curious "=0"
syntax:
class Base {
public:
void f1(); // not virtual
virtual void f2(); // virtual, not pure
virtual void f3() = 0; // pure virtual
};



Can we specify the access modifier for explicitly implemented interface method?

No, we can't specify the access modifier for the explicitly implemented interface method. By default its scope will be internal.



What is Protected access modifier in C#?

The protected keyword is a member access modifier. It can only be used in a declaring a function or method not in the class ie. a class can't be declared as protected class.
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declare this member. In other words access is limited to within the class definition and any class that inherits from the class
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.



What is Public access modifier in C#?

The public keyword is an access modifier for types and type members ie. we can declare a class or its member (functions or methods) as Public. There are no restrictions on accessing public members.



What is Private access modifier in C#?

The private keyword is a member access modifier ie. we can't explicitly declare a class as Private, however if do not specify any access modifier to the class, its scope will be assumed as Private. Private access is the least permissive access level of all access modifiers.
Private members are accessible only within the body of the class or the struct in which they are declared. This is the default access modifier for the class declaration.



What is Internal access modifier in C#?

The internal keyword is an access modifier for types and type members ie. we can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes defined within the current project assembly.



What is Protected Internal access modifier in C#?

Protected Internal is a access modifiers for the members (methods or functions) ie. you can't declare a class as protected internal explicitly. The members access is limited to the current assembly or types derived from the containing class.
Protected Internal means the method is accessible by anything that can access the protected method UNION with anything that can access the internal method.



Default Access modifiers in C#?

An enum has default modifier as public
A class has default modifiers as Internal . It can declare members (methods etc) with following access modifiers:
public
internal
private
protected internal
An interface has default modifier as public
A struct has default modifier as Internal and it can declare its members (methods etc) with following access modifiers:
public
internal
private
A methods, fields, and properties has default access modifier as "Private" if no modifier is specified.



What is method overloading?

Method overloading allows us to write different version of the same method in a class or derived class. Compiler automatically select the most appropriate method based on the parameter supplied.

public class MultiplyNumbers

{

public int Multiply(int a, int b)

{

return a * b;

}

public int Multiply(int a, int b, int c)

{

return a*b*c;

}

}


To call the above method, you can use following code.

MultiplyNumbers mn = new MultiplyNumbers();

int number = mn.Multiply(2, 3) // result = 6

int number1 = mn.Multiply(2, 3, 4) // result = 24

You can't have a overload method with same number parameters but different return type. In order to create overload method, the return type must be the same and parameter type must be different or different in numbers.



What is overriding?

Method overriding is a feature that allows to invoke functions (that have the same signatures) and that belong to different classes in the same hierarchy of inheritance using the base class reference. In C# it is done using keywords virtual andoverrides .

What is Method Overriding? How to override a function in C#?

Use the override modifier to modify a method, a property, an indexer, or an event. An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method.
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.



Can we call a base class method without creating instance?

Yep. But ..
* Its possible If its a static method.
* Its possible by inheriting from that class also.
* Its possible from derived classes using base keyword.



In which cases you use override and new base?

Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.


What is a private constructor? Where will you use it?

When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.
If you declare a Constructor as private then it doesn’t allow to create object for its derived class, i.e you loose inherent facility for that class.
Example:

Class A


{

// some code

Private Void A()

{

//Private Constructor

}

}

Class B:A

{
//code

}

B obj = new B();// will give Compilation Error

Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an object for Class B that constructor will call constructor A but class B have no rights to access the Class A constructor hence we will get compilation error.



Can we declare private class in a Namespace?

No. If you try to create a private class in a Namespace, Compiler will throw a compile time error “Namespace elements cannot be explicitly declared as private, protected, or protected internal”.
Reason: The message says it all. Classes can only be declared as private, protected or protected internal when declared as nested classes, other than that, it doesn't make sense to declare a class with a visibility that makes it unusable, even in the same module. Top level classes cannot be private, they are "internal" by default, and you can just make them public to make them visible from outside your DLL.

In C# :
Parent classes may define and implement “virtual” methods(Which is done using the “virtual” keyword), and derived classes can override them(using the “override” keyword), which means they provide their own definition and implementation.At run-time, when user’s code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code when a method of the base class is called it executes the overriden method.



What can you do to make class available for inheritance but you need to prevent it's method to come in inheritance chain?

Well, Declare a class with public access specifier and mark all it's method to sealed . As anything which is declared with sealed keyword cannot be inherited.



What's the Difference between Interface and Abstract Class

Abstract Class:
Have constructors.
Not necessarily for the class inheriting it to Implement all the Methods.
Doesn't Support Multiple Inheritance.
Where everything is Opposite in the Interfaces.



When to Use Abstract Classes and When Interfaces.

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.



Diversities between an abstract method & virtual method ?

An Abstract method does not provide an implementation and forces overriding to the deriving class (unless the deriving class also an abstract class), where as the virtual method has an implementation and leaves an option to override it in the deriving class. Thus Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.


What is Early binding and late binding?
Calling a non-virtual method, decided at a compile time is known as early binding. Calling a virtual method (Pure Polymorphism), decided at a runtime is known as late binding.


Difference between ASP Session and ASP.NET Session?
Asp.net session supports cookie less session & it can span across multiple servers.



Illustrate Server.Transfer and Response.Redirect?

Server.Transfer, transfers the control of a web page, posting a form data, while Response.Redirect simply redirects a page to another page, it can not post a form data to another page. Server.Transfer is more efficient over the Response.Redirect, because Response.Redirect causes a round trip to server as the page is processed once again on the client and a request is made to server there after.
But the browser url is not changed in case of Server.Transfer i.e. Browser history is not modified in using it.


How's method overriding different from overloading?

When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.



What does the keyword virtual mean in the method definition?
The method can be over-ridden.



Can you declare the override method static while the original method is non-static?
No, you can't, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.



Can you override private virtual methods?
No, you cannot access private methods in inherited classes.

Can you prevent your class from being inherited and becoming a base class for some other classes?

Yes, that's what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot inherit from Sealed class WhateverBaseClassName. It's the same concept as final class in Java.


Can you allow class to be inherited, but prevent the method from being over-ridden?
Yes, just leave the class public and make the method sealed.

Why can't you specify the accessibility modifier for methods inside the interface?
you are not allowed to specify any accessibility, it's public by default.


Static datamembers should be initialized inside the constructor. True or False.
False. Static datamembers should not be initialised inside constructor.

Static methods cannot use non static members. True or False.
True

A constructor can be private. True or False
True. A constructor can be private. We can declare a constructor as private.


What is the work of a constructor?

Constructor creates and initialises the objects in an application.
Name the operators that cannot be overloaded.
sizeof
.
.*
.->
::
?:


What is "this" pointer?

This pointer is a pointer which points to the current object of a class. this is actually a keyword which is used as a pointer which differentiate the current object with global object.

What are the different ways a method can be overloaded?

Different parameter data types, different number of parameters, different order of
parameters.
example: int area(int a, int b)
{
return a*b; --different number of parameters
}
int area(int b)
{
return a*a;
}

--parameter return types
int calc(int a)
{
return a;
}

double calc(double b)
{
return b*5;
}


Can we have Sealed Method in abstarct class ?

Looking at first site the The Keyword Sealed & Abstract are contradictory to each other..In simple terms we can Say Answer is NO...
Look the code below
using System;
abstract class A
{
public abstract void Hello();
public sealed void Hi();
}
when we will complie the code.. we will get the Compile time Error as below
'A.Hi()' cannot be sealed because it is not an override..
But the Crux is We can have Sealed methods in abstract class when the abstract class is Dervided class .. for Eg.
using System;
class A
{
public virtual void Hello()
{
Console.WriteLine(" Say Hello");
}

}
abstract class B : A
{
public sealed override void Hello()
{
Console.WriteLine(" Say Hi");
}
}
class C : B
{
}

class Demo
{
public static void Main()
{
C c1 = new C();
c1.Hello();// Output is Say Hi
}
}
// Thanks



Can we have an Abstract class without having any abstract method ??

Yes we can have Abstract class without having any abstract method ..
See the code below
using System;
abstract class A
{
public void Hello()
{
Console.WriteLine(" Say Hi");
}
}
class B:A
{
}
class Demo
{
public static void Main()
{
B b1 = new B();
b1.Hello();
}
}
// Output is Say HI
the class A is abstract class.. but it does not have any abstract methods..


How does Composition mechanism works ?

This mechanism helps to simplify a complex problem into an easier problem.It generally makes different classes and objects to communicate with each other and thus making the problem solved. It communicates with the problem by making different classes and objects to send a message to each other.



What is the advantage of parametric polymorphism ?

Generally in Parametric polymorphism the code is written without following any specification for the type of data present so this particular code can be used any number of times. Hence code re-usability is achieved.

When We will create architecture and show one application connect with different type of database like SQL or Oracle. Which option is suitable for create architecture?

NOTE: This is objective type question, Please click question title for correct answer.


What is difference in between abstrct classes and interfaces ?

An interface offers an alternative to an abstract class for creating contract among classes and their client. The main difference in between abstract class and interface are given bellow
1. Abstract classes can have concrete methods while interfaces have no methods implemented.
2.Interface do not come in inheriting chain,while abstract classes come in inheritance .


Which of the following is not a part of OOPs?

Select from following answers:
Polymorphism
Type checking
Information hiding
Multitasking

Multitasking is Not integrated in oops .
So,Here Multitasking is not a part of oops






0 comments:

Post a Comment