Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Oops interview questions and answers

Oops interview questions and answers?

1.      What is oops?

Old procedural programming languages organized around actions.
Object-oriented programming (OOP) is a programming language model or design philosophy organized around objects rather than "actions" and data rather than logic.

2.      Features of oops?

OOP has the following important features.
1.  Class
2.  Objects
3.  Encapsulation
4.  Abstraction
5.  Inheritance
6.  Polymorphism.
 

3.      What is class and objects?

Class:

Class is simply blueprint or template for type of object, class describes the details of object. In real world example class Vehicle has some common properties like engine, seat and tiers for all objects car, bus, trucks these properties describes the details of object car, bus, truck.

Syntax:

[attributes] [modifiers] class
identifier [:base-list] { class-body }[;]

 

Object:

Object is termed as an instance of a class, and it has its own state, behavior and identity. To create an object or instance of a class, you use the new keyword.

Example with product class:

    class Program
    {
 
        static void Main(string[] args)
        {
            Product MicromaxMobile = new Product("NotExpired");
 
            var isProductavailable = MicromaxMobile.isProductSellable();
        }
    }

4.      What is abstract class?

Abstract Class:

Class uses keyword abstract that enables you to create class members that are incomplete and must be implemented in a derived class.

When a method is declared as abstract in the base class then every derived class of that class must provide its own definition for that method.

 An abstract class can also contain methods with complete implementation, besides abstract methods.

Points to remember:

       1.  You can't create an instance of abstract class; the purpose of an abstract class is to provide a   common definition of a base class that multiple derived classes can share.
      2.   An abstract class cannot be inherited from by structures.
      3.   An abstract class cannot support multiple inheritances.
      4. When a class contains at least one abstract method, then the class must be declared as abstract  class.
      5.   It is mandatory to override abstract method in the derived class.

Example:

      abstract class MathFunctionsSummery
      {
            //A Non abstract method
            public int AddTwoNumbers(int num1, int num2)
            {
                return num1 + num2;
            }
 
            //A Non abstract method
            public int SubtractTwoNumbers(int num1, int num2)
            {
                return num1 - num2;
            }
 
            //An abstract method, to be
            //overridden in derived class
            public abstract int MultiplyTwoNumbers(int num1, int num2);
      }
 
      //A Child Class of Abstract Class
      class MathFunctionsInDetails:MathFunctionsSummery
      {
          //using override keyword,
          //implementing the abstract method
          //MultiplyTwoNumbers
          public override int MultiplyTwoNumbers(int num1, int num2)
          {
              return num1 * num2;
          }
         
            static void Main(string[] args)
            {
               //You can create an
               //instance of the derived class
 
               MathFunctionsInDetails calculate = new MathFunctionsInDetails();
               int added = calculate.AddTwoNumbers(10,20);
               int subtracted = calculate.SubtractTwoNumbers(30, 20);
               int multiplied = calculate.MultiplyTwoNumbers(10,20);
               Console.WriteLine("Added : {0},Subtracted:{1}, Multiplied : {2}", added,subtracted,multiplied);
            }
      }

 

5.      What is interface?

6.      Difference between abstract class and interface?

7.      What is sealed class?

8.      What is method overloading and method overriding?

9.      Difference between method overloading and overriding?

10.  Difference between virtual, override and new keyword?

11.  What is the difference between array and array list in c#?

12.  What is the difference between dispose and finalize methods in c#?

13.  What is the difference between ref and out parameters in c#?

14.  What is difference between late binding and early binding in c#?

15.  Difference between pass by value and pass by reference?

16.  Define constructor?

Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.

17.  What is static constructor?

Static Constructor:

A static constructor is used to initialize static data only once, or used to perform action only once.
It will be executed only once, unlike the normal constructor that is executed whenever the object is created.

Syntax:

 
    class SimpleClass
    {
        // Static constructor
        static SimpleClass()
        {
            //...
        }
    }

Points to remember:

1.  Static Constructor cannot be called directly.
2.   No access modifiers allowed with Static Constructor.
3.   Parameters not allowed in Static Constructor.
4.   A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
5.  The user has no control on when the static constructor is executed in the program.
6.  The static constructor for a class executes before any instance of the class is created.
7.  Static constructor executes only one time.

18.  What is default constructor and parameterized constructor?

Default Constructor:

A special method of the class without any parameter, with same name (Class name) that will be automatically invoked when an instance of the class is created is called as default constructor.

Parameterized constructor:

As name suggests constructor with at least one parameter is called as parameterized constructor.

19.  What is copy constructor?

A parameterized constructor that contains a parameter of same class type is called as copy constructor.

Syntax:

[Access Modifier] ClassName([Class instance Parameter])
{
 
}

Example:

  public class student
    {
        public string StudentName {get;set;}
        
        public string TeacherName;
        
        public student(string teacher)
        {
            TeacherName = teacher;
        }
        
        {
            TeacherName = teacher;
        }
        //copy constructor
        public student(student Student)
        {
            TeacherName = Student.TeacherName;
        }
        
        {
            TeacherName = Student.TeacherName;
        }
        public void DisplayStudentDetails()
        {
            
        {
            Console.WriteLine("Student Name: {0}", StudentName);
            
            Console.WriteLine("Teacher Namne: {0}", TeacherName);
        }
    }
        }
    }
 
class Program
    {
        static void Main(string[] args)
        {
            
        {
            var ITStudent = new student("Shivaji");
            ITStudent.StudentName = 
            ITStudent.StudentName = "Nagnath";
            
            var ElectronicsStudent = new student(ITStudent);
            ElectronicsStudent.StudentName = 
            ElectronicsStudent.StudentName = "Girish";
            ITStudent.DisplayStudentDetails();
            ElectronicsStudent.DisplayStudentDetails();
 
        }
    }
    }
            ITStudent.DisplayStudentDetails();
            ElectronicsStudent.DisplayStudentDetails();
 
        }
    }
    }

 

20.  What is private constructor?

Private constructor: Private constructor is a special instance constructor with "private" access modifier in a class.

It is generally used in classes that contain static members only.

Syntax:

[Private Access Modifier] ClassName()
{
 ..
}

Example:

class Program
    {
 
        static void Main(string[] args)
        {
            
        {
            studentPrivate.InstituteName = "PLGP";
            
            var ITStudent = new studentPrivate("Shivaji");
            ITStudent.StudentName = 
            ITStudent.StudentName = "Nagnath";
            
            var ElectronicsStudent = new studentPrivate("Shivaji");
            ElectronicsStudent.StudentName = 
            ElectronicsStudent.StudentName = "Girish";
           
            ITStudent.DisplayStudentDetails();
            ElectronicsStudent.DisplayStudentDetails();
 
        }
   }
 
           
            ITStudent.DisplayStudentDetails();
            ElectronicsStudent.DisplayStudentDetails();
 
        }
   }
 
public class studentPrivate
    {
        public static string InstituteName;
        
        public string StudentName { getset; }
        
        public string TeacherName;
 

        public studentPrivate(string teacher)
        {
            TeacherName = teacher;
        }
        
        {
            TeacherName = teacher;
        }
        //private Constructor
        private studentPrivate()
        {
            InstituteName=
        {
            InstituteName="JNEC";
        }
 
        
        }
 
        public void DisplayStudentDetails()
        {
            
        {
            Console.WriteLine("Institute Namne: {0}", InstituteName);
            
            Console.WriteLine("Student Name: {0}", StudentName);
            
            Console.WriteLine("Teacher Namne: {0}", TeacherName);
        }
    }
 
        }
    }
 
 

21.  What is destructor?

 

22.  Explain interface properties?

Interface Properties:

You can declare properties on interface. The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.


23.  What are types of access modifiers in C#?

Access modifiers in C#:

Encapsulation is one of the biggest advantages of OOP, it is a process of binding the data members and member functions into a single unit. Encapsulating data and methods ensures independency, and limited access.
You can use access modifiers to set the allowed access to not only classes, but also to all members of those classes.

C# five access Specifiers:

1.      Public

2.      Private

3.      Protected

4.      Internal

5.      Protected Internal

 

24.  Difference between public and private access modifiers?

Public:
Accessible outside the class through object reference.
Private:
Accessible inside the class only through member functions.

25.  What is internal access modifier?

Visible inside the assembly only, accessible through objects.

26.  What is protected access modifier?

Just like private but Accessible in derived classes also through member functions.

27.  What is protected internal access modifier?

Visible inside the assembly through objects and in derived classes outside the assembly through member functions.

28.  What is the use of base keyword?

In C# base keyword is used to call the base class constructor and base class members (field, property, method etc.) that has been overridden or hidden in the current class.

Example with base method:

    class Collage
    {
        protected virtual void StudentInfo()
        {
            Console.WriteLine("I'm Collage");
        }
    }
 
    class Department : Collage
    {
        protected override void StudentInfo()
        {
            Console.WriteLine("I'm Department");
        }
 
        public void Bar()
        {
            StudentInfo();
            base.StudentInfo();
        }
}


 Base Class Constructor:

    public class Father
    {
        int num;
 
        public Father()
        {
            Console.WriteLine("Indian origin peoplr");
        }
 
        public Father(String Address)
        {
 
            Console.WriteLine("Contact Information:" + Address);
        }
 
        public int GetNum()
        {
            return num;
        }
    }
 
    public class Son : Father
    {
        // This constructor will call Father.Father()
        public Son()
            : base()
        {
        }
 
        // This constructor will call Father.Father(String Address)
        public Son(string  address)
            : base(address)
        {
            Console.WriteLine("My Birth location is :Latur,Mahrashtra");
        }
 
        static void Main()
        {
            Son Son = new Son();
            Son Son1 = new Son(1);
        }
}

    

Points to remember:

    Base keyword is not allowed in static method and static class.


29.  Explain property, indexer and fields?

  Property is a class member that looks like fields or variables if you try to access using objects, but they use accessor methods to get and set their data.

get accessor:
get acessor is like method,It must return a value of the property type.
When you reference the property, except as the target of an assignment, the get accessor is invoked to read the value of the property
set accessor:
 Set accessor is like method whose return type is void. It uses an implicit parameter called value, whose type is the type of the property.
When you assign a value to the property, the set accessor is invoked by using an argument that provides the new value.

Read only property Example:

    class Product
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
        }
    }

More Details: http://codechef4u.com/post/2015/07/03/properties

30.  Can you declare a private class in a namespace?

31.  A structure in C# can implement one or more interfaces. Is it true or false?

32.  What is the difference between procedural and object-oriented programming?

33.  What are queues and stacks?

34.  What are structures?

35.  Difference between a class and a structure?

36.  What are collections and generics?

37.  Can you specify the accessibility modifier for methods inside the interface?

38.  Is it possible for a class to inherit the constructor of its base class?

39.  Why is the virtual keyword used in code?

40.  What is enumeration?

41.  Does .NET support multiple inheritances?

42.  How can you prevent a class from overriding in C#?

43.  Can we use return type for constructor?

No, You cant. 

44.  Is it possible that one class have more than one constructor?

Yes, in single class using multiple constructors is possible

Check following example:

public class Student
    {
        public string StudentName { get; set; }
        public string TeacherName { get; set; }
        public string CollageName { get; set; }
 
        //Default Constructor
        public Student()
        {
            CollageName = "PLGPL";
        }
 
        //parameterized constructor
        public Student(string teacher)
        {
            TeacherName = teacher;
        }
 
        //copy constructor
        public Student(Student Student)
        {
            TeacherName = Student.TeacherName;
        }
 
        public void DisplayStudentDetails()
        {
 
            Console.WriteLine("Student Name: {0}", StudentName);
            Console.WriteLine("Teacher Namne: {0}", TeacherName);
        }
 
    }
 

45.  Class without constructor is possible in C#?

If you don't declare any constructors for a non-static class, the compiler provides a public (or protected for abstract classes) default constructor for you.

    public class StudentNoConstructor
    {
        public string StudentName { get; set; }
        public string TeacherName { get; set; }
        public string CollageName { get; set; }
        public void DisplayStudentDetails()
        {
 
            Console.WriteLine("Student Name: {0}", StudentName);
            Console.WriteLine("Teacher Namne: {0}", TeacherName);
        }
 
    }

For this example Compiler generates empty default constructor:

       public StudentNoConstructor ()

        {

           

       }

46.  We have two classes ,ParentClass or BaseClass and ChildClass,Child class inherits Parent class,

If we create instance of child class then which class constructor called first?

 Parent class.

47.  Can we declare abstract method in non abstract class?

   no if you crate method using abstract keyword , class should be abstract. 

48.  Can you define methods in interface?

Interface Properties:

You can declare properties on interface. The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

Syntax:

[attributes] [new] type identifier {interface-accessors}

49.  What is static class?

A static class is a class that cannot be instantiated and allows only static members. A static class is created by using keyword 'Static' .

Example:

static class CulterSpecificCartTotal 

{
       

public static string CountryCode;
     

public static double cartTotal;
   

public static string GetCartTotal()
       

{
           
var cartTotalVal = cartTotal.ToString("0.00", CultureInfo.CreateSpecificCulture(CountryCode));
           
return cartTotalVal;
       

}
 }

 

Features:

1.  Allows only static members in class.
2.  Static classes are sealed, therefore cannot be inherited.
3.  Static class cannot be instantiated
4.  Static classes cannot contain Instance Constructors
5.  All members can be accessible with its own name (Class name)
6.  In C# .net static class is loaded by the CLR when the program that references the class is loaded.
7.  They can have static constructor.
 

50.  Can we inherit class with private constructor?

A class with private constructor cannot be inherited.

51.  Can we crate instance of class with private constructor?

1. You cannot create an object of the class which has private constructor only.
2. You can create instance of class if class contain public constructor along with private constructor.

52.  Can we use default constructor as private constructor?

You can use only parameterized constructor with private constructor.

53.  What are read only properties and auto implemented properties?

Read-only Properties:

You can also create read-only properties if you avoid setting accessor method. For example, to
make the Name property a read-only property, you use this code:
    class Product
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
        }
    }

Auto implemented properties:

In C#3.0 and above Microsoft introduced feature called auto implemented properties.

For auto implemented properties no custom accessor required. They also enable client code to create objects. When you declare a property, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

Example:

public class Product
    {
        string ProductUseCategory;
 

        private string name;
 
        public Product(string cetgory)
        {
            ProductUseCategory =cetgory;
        }
       
        
        {
            ProductUseCategory =cetgory;
        }
       
        //auto implemented properties
        public int ProductID { get; set; }
 
        //simple property declartion
        public string ProductName
        {
            
        {
            get
            {
                return name;
            }
            
            }
            set
            {
                name = value;
            }
        }
            }
        }
     
        public double? UnitPrice { get; set; }

        public bool isProductSellable()
        {
            
        {
            if (ProductUseCategory== "Expired")
            {
                
            {
                return false;
            }
            
            }
            else
            {
                return true;
            }
        }
       
    }
            }
        }
       
    }

54.  Does interface property have a body?

1.  The accessor of an interface property does not have a body.
2.  Use of accessors is to indicate whether the property is read-write, read-only, or write-only.

55.  Can we create instance for static class?

You do not need to make any instance of this class; all class members can be accessible with its own name.

56.  Can we declare field as static const?

A static field cannot be declared as static const, a const field is essentially static in its behavior.

57.  Can we use parameters with static constructor?

         Parameters not allowed in Static Constructor

58.  Can we use access modifiers with static constructor?

          No access modifiers allowed with Static Constructor.

Invalid entry,please enter valid data.

Loading