OOPS Interview Questions and Answers (133) - Page 4

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;
}
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.
Define OOPS. What are its benefits?

OOPS

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 Frorm 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.
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..

Thanks
Can we have Multiple Main Methods in one .cs file

Yes we can Have multiple Main methods in one .cs file.
The crux is we can have Multiple classes in one .cs file; and we can define one Main method in each class.

& while doing compliation we can spcify the compiler to choose the Main methods from the specific class .

for ef see the code below

using System;


class Test
{
public static void Main()
{
Console.WriteLine("Test");
}
}

class Demo
{
public static void Main()
{
Console.WriteLine("Demo");
}
}
We have got two class which we can save in single .cs file say Hello.cs

while doing compliation we can say

csc Hello.cs /main:Demo        --> In order to choose Main from the Demo class

and
csc Hello.cs /main:Test --> In order to choose Main from the Test class

Happy coding..
Which of these terms defines the hiding of an object's details from the other program?

NOTE: This is objective type question, Please click question title for correct answer.
If the Function has same parameter but different return type (int and float), Is it a overloading?

NOTE: This is objective type question, Please click question title for correct answer.
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.
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 .
Overloading is Static Polymorphism and Overriding is Dynamic Polymorphism ? True or False ?

NOTE: This is objective type question, Please click question title for correct answer.
Which of the following is not a part of OOPs?

NOTE: This is objective type question, Please click question title for correct answer.
What is the default access modifier of a class?

The default access modifier for a class is internal if it's defined within the same namespace. It is private if it's defined within another class.

It can declare members (methods etc) with following access modifiers:
public
internal
private
protected internal
Can a constructors that is declared within a base class, inherited by subclasses ? Yes or No

NOTE: This is objective type question, Please click question title for correct answer.
What is Abstract Class and interfaces?

When we talk about abstract classes we are defining characteristics of an object type , specifying what an object is.
But in the case of an interface we define a capability and we bond to provide that capability , we are talking about establishing a contract about what the object can do.
How to Create object For Inner Class? or how to access inner class methods?

class A
{
public string DisplayName()
{

return "This is Class A";
}


public class B
{
public string DisplayName()
{

return "This is Class B";
}
}
}


-------------------------------------------------------

var obja=new A.B();
string txt = obja.DisplayName();
Response.Write(txt);
Difference between Encapsulation and Data hiding?

Data Encapsulation is nothing but grouping up of related members (variables and methods) into a single unit called class

Data Hiding is nothing but restricting outside access of a class members using access modifiers such as private , protected and internal etc.,
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories