Oops Interview Question and Answer

Share Embed Donate


Short Description

Download Oops Interview Question and Answer...

Description

OOPS INTERVIEW QUESTION AND ANSWER

Questions : 1 What is Object Oriented Programming ? Answers : 1 It is a problem solving technique to develop software systems. It is a technique to think real world in terms of objects. Object maps the software model to real world concept. These objects have responsibilities and provide services to application or other objects. Questions : 2 What is a Class ? Answers : 2 A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It is a comprehensive data type which represents a blue print of objects. It’s a template of object. Questions : 3 What is an Object ? Answers : 3 It is a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Objects are members of a class. Attributes and behavior of an object are defined by the class definition. Questions : 4 What is the relation between Classes and Objects? Answers : 4 They look very much same but are not same. Class is a definition, while object is instance of the class created. Class is a blue print while objects are actual objects existing in real world. Example we have class CAR which has attributes and methods like Speed, Brakes, Type of Car etc.Class CAR is just a prototype, now we can create real time objects which can be used to provide functionality. Example we can create a Maruti car object with 100 km speed and urgent brakes. Questions : 5 What are different properties provided by Object-oriented systems ? Answers : 5 Following are characteristics of Object Oriented System’s:Abstraction It allows complex real world to be represented in simplified manner. Example color is abstracted to RGB.By just making the combination of these three colors we can achieve any color in world. It’s a model of real world or concept. Encapsulation The process of hiding all the internal details of an object from the outside world. Communication Using messages when application wants to achieve certain task it can only be done using combination of objects. A single object can not do the entire task. Example if we want to make order processing form. We will use Customer object, Order object, Product object and Payment object to achieve this functionality. In short these objects should communicate with each other. This is achieved when objects send messages to each other. Object lifetime All objects have life time. Objects are created, initialized, necessary functionalities are done and later the object is destroyed. Every object have there own state and identity, which differ from instance to instance. Questions : 6 What is an Abstract class ? Answers : 6 Abstract class defines an abstract concept which can not be instantiated and comparing o interface it can have some implementation while interfaces can not. Below are some points for abstract class:=>We can not create object of abstract class it can only be inherited in a below class. => Normally abstract classes have base implementation and then child classes derive from the abstract class to make the class concrete.

Questions : 7 What are Abstract methods? Answers : 7 Abstract class can contain abstract methods. Abstract methods do not have implementation. Abstract methods should be implemented in the subclasses which inherit them. So if an abstract class has an abstract method class inheriting the abstract class should implement the method or else java compiler will through an error. In this way, an abstract class can define a complete programming interface thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface. Abstract methods are defined using "abstract" keyword. Below is a sample code snippet. abstract class pcdsGraphics { abstract void draw(); } Any class inheriting from "pcdsGraphics" class should implement the "draw" method or else the java compiler will throw an error. so if we do not implement a abstract method the program will not compile. Questions : 8 What is the difference between Abstract classes and Interfaces ? Answers : 8 Difference between Abstract class and Interface is as follows:Abstract class can only be inherited while interfaces can not be it has to be implemented. Interface cannot implement any methods, whereas an abstract class can have implementation. Class can implement many interfaces but can have only one super class. Interface is not part of the class hierarchy while Abstract class comes in through inheritance. Unrelated classes can implement the same interface. Questions : 9 What is difference between Static and Non-Static fields of a class ? Answers : 9 Non-Static values are also called as instance variables. Each object of the class has its own copy of Non-Static instance variables. So when a new object is created of the same class it will have completely its own copy of instance variables. While Static values have only one copy of instance variables and will be shared among all the objects of the class. Questions : 10 What are inner classes and what is the practical implementation of inner classes? Answers : 10 Inner classes are nested inside other class. They have access to outer class fields and methods even if the fields of outer class are defined as private. public class Pcds { class pcdsEmp { // inner class defines the required structure String first; String last; } // array of name objects clsName personArray[] = {new clsName(), new clsName(), new clsName()}; } Normally inner classes are used for data structures like one shown above or some kind of helper classes. Questions : 11 What is a constructor in class? Answers : 11 Constructor has the same name as the class in which it resides and looks from syntax point of view it looks similiar to a method. Constructor is automatically called immediately after the object is created, before the new operator completes. Constructors have no return type, not even void. This is because the implicit return type of a class' constructor is the class type itself. It is the constructor's

job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately. Questions : 12 Can constructors be parameterized? Answers : 12 Yes we can have parameterized constructor which can also be termed as constructor overloading. Below is a code snippet which shows two constructors for pcdsMaths class one with parameter and one with out. class pcdsMaths { double PI; // This is the constructor for the maths constant class. pcdsMaths() {PI = 3.14;} pcdsMaths(int pi) { PI = pi; }} Questions : 13 What is the use if instanceof keyword? and How do refer to a current instance of object? Answers : 13 "instanceof" keyword is used to check what is the type of object. we can refer the current instance of object using "this" keyword. For instance if we have class which has color property we can refer the current object instance inside any of the method using "this.color". Questions : 14 what is Bootstrap, Extension and System Class loader? or Can you explain primordial class loader? Answers : 14 There three types of class loaders:BootStrap Class loader also called as primordial class loader. Extension Class loader. System Class loader. Let’s now try to get the fundamentals of these class loaders. Bootstrap Class loader Bootstrap class loader loads those classes those which are essential for JVM to function properly. Bootstrap class loader is responsible for loading all core java classes for instance java.lang.*, java.io.* etc. Bootstrap class loader finds these necessary classes from “jdk/ jre/lib/rt.jar”. Bootstrap class loader can not be instantiated from JAVA code and is implemented natively inside JVM. Extension Class loader The extension class loader also termed as the standard extensions class loader is a child of the bootstrap class loader. Its primary responsibility is to load classes from the extension directories, normally located the “jre/lib/ext” directory. This provides the ability to simply drop in new extensions, such as various security extensions, without requiring modification to the user's class path. System Class loader The system class loader also termed application class loader is the class loader responsible for loading code from the path specified by the CLASSPATH environment variable. It is also used to load an application’s entry point class that is the "static void main ()" method in a class. Questions : 15 what’s the main difference between ArrayList / HashMap and Vector / Hashtable? Answers : 15 Vector / HashTable are synchronized which means they are thread safe. Cost of thread safe is performance degradation. So if you are sure that you are not dealing with huge number of threads then you should use ArrayList / HashMap.But yes you can still synchronize List and Map’s using Collections provided methods :-

List OurList = Collections.synchronizedList (OurList); Map OurMap = Collections.synchronizedMap (OurMap); Questions : 16 What are access modifiers? Answers : 16 Access modifiers decide whether a method or a data variable can be accessed by another method in another class or subclass. four types of access modifiers: Public: - Can be accessed by any other class anywhere. Protected: - Can be accessed by classes inside the package or by subclasses ( that means classes who inherit from this class). Private - Can be accessed only within the class. Even methods in subclasses in the same package do not have access. Default - (Its private access by default) accessible to classes in the same package but not by classes in other packages, even if these are subclasses. Questions : 17 Define exceptions ? Answers : 17 An exception is an abnormal condition that arises in a code sequence at run time. Basically there are four important keywords which form the main pillars of exception handling: try, catch, throw and finally. Code which you want to monitor for exception is contained in the try block. If any exception occurs in the try block its sent to the catch block which can handle this error in a more rational manner. To throw an exception manually you need to call use the throw keyword. If you want to put any clean up code use the finally block. The finally block is executed irrespective if there is an error or not. Questions : 18 What is serialization? How do we implement serialization actually? Answers : 18 Serialization is a process by which an object instance is converted in to stream of bytes. There are many useful stuff you can do when the object instance is converted in to stream of bytes for instance you can save the object in hard disk or send it across the network. In order to implement serialization we need to use two classes from java.io package ObjectOutputStream and ObjectInputStream. ObjectOutputStream has a method called writeObject, while ObjectInputStream has a method called readObject. Using writeobject we can write and readObject can be used to read the object from the stream. Below are two code snippet which used the FileInputStream and FileOutputstream to read and write from harddisk.

OOPs Concepts Interview Questions and Answers What is difference between overloading and overriding?

Having same name methods with different parameters is called overloading, while having same name and.... Category: OOPs Concepts

Date: 7/3/2010 1:53:22 PM

What do you mean by Object Slicing?

When a derived class object is assigned to a base class, only the base class's part of content in th.... Category: OOPs Concepts

Date: 10/8/2009 2:55:39 AM

Give some examples of programming languages which support object oriented characteristics?

Some of the programming languages which support object oriented characteristics are ABAP, C++, Perl,.... Category: OOPs Concepts

Date: 10/7/2009 6:41:12 AM

What is object oriented programming language?

Object oriented programming language allows concepts such as abstraction, modularity, encapsulation,.... Category: OOPs Concepts

Date: 10/7/2009 6:35:57 AM

What is message passing in object oriented programming?

Message passing is a method by which an object sends data to another object or requests other object.... Category: OOPs Concepts

Date: 10/7/2009 6:30:57 AM

What are the advantages of OOPs?

The major advantages of OOPs are: 1. Simplicity: Software objects model real world objects, so th.... Category: OOPs Concepts

Date: 9/12/2009 7:51:12 PM

How polymorphism can be implemented?

Some examples to implement polymorphism: 1) Method Overriding 2) Method Overloading 3) Operator Over.... Category: OOPs Concepts

Date: 9/7/2009 1:12:37 AM

What are the main concepts of OOPs?

Following are the main concepts of OOPs? 1) Abstraction 2) Encapsulation 3) Inheritance 4) Polymorph.... Category: OOPs Concepts

Date: 9/4/2009 6:30:22 AM

What is Operator Overloading?

The operator overloading is a specific case of polymorphisms in which some or all of operators like .... Category: OOPs Concepts

Date: 8/29/2009 9:17:06 PM

What is Method Overriding?

Method overriding allows a subclass to override a specific implementation of a method that is alread.... Category: OOPs Concepts

Date: 8/29/2009 9:10:46 PM

What is Method Overloading?

Method overloading is the ability to define several methods all with the same name. Category: OOPs Concepts

Date: 8/29/2009 9:07:19 PM

What is Aggregation?

Aggregation is a special form of association. Aggregation is the composition of an object out of a s.... Category: OOPs Concepts

Date: 8/29/2009 9:03:49 PM

What is Association?

Association defines a relationship between different objects of different classes which allows one o.... Category: OOPs Concepts

Date: 8/29/2009 8:55:57 PM

What is the Difference between Class and Object?

A Class is actually a blueprint or a template to create an Object. Whereas an Object is a an actual .... Category: OOPs Concepts

Date: 8/29/2009 8:41:21 PM

What is Polymorphism?

Polymorphism is an ability to appear in many forms. It refers to the ability of the system to call c.... Category: OOPs Concepts

What is Inheritance?

In OOPs terminology, inheritance is a way to form new classes using classes that have already been d.... Category: OOPs Concepts

Date: 8/29/2009 6:52:23 PM

What is Abstraction?

Abstraction is the process of generalization by reducing the information content of an entity, typic.... Category: OOPs Concepts

Date: 8/29/2009 6:45:58 PM

Date: 8/29/2009 7:03:57 PM

What is Encapsulation?

In OOPs terminology, Encapsulation refers to encapsulating data with the methods that operates on th.... Category: OOPs Concepts

Date: 8/29/2009 6:25:10 PM

What is an Object?

Instances of classes are called as objects. The class of Employee defines all possible employees by.... Category: OOPs Concepts

Date: 8/29/2009 8:07:26 AM

What is a Class?

Classes defines a template or blueprint for an entity. It defines the characteristics (attributes o.... Category: OOPs Concepts

Date: 8/29/2009 8:02:21 AM

What is OOPs?

Object Oriented Programming (OOPs) is a programming methodology in which each entity is an object. .... Category: OOPs Concepts

Date: 8/29/2009 7:55:59 AM

What is Polymorphisms? Posted by: Raja 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

What is Abstract method? Posted by: Raja Abstract method doesn't provide the implementation & forces the derived class to override the method.

What is Virtual method? Posted by: Raja Virtual Method has implementation & provide the derived class with the option to override it.

Can Struct be inherited? Posted by: Raja No, Struct can't be inherited as this is implicitly sealed.

What is Object? Posted by: Poster Object is anything that is identifiable as a single material item.

What is Class? Posted by: Poster A Class is the generic definition of what an object is a template. The keyword class in C# indicates that we are going to define a new class (type of object)

What is Static field? Posted by: Poster To indicate that a field should only be stored once no matter how many instance of the class we create.

What is Static Method? Posted by: Poster 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 Inheritance? Posted by: Poster It provides a convenient way to reuse existing fully tested code in different context thereby saving lot of coding. Inheritance of classes in C# is always implementation Inheritance.

What is Virtual keyword? Posted by: Poster 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 New modifiers? Posted by: Poster The new modifiers hides a member of the base class. C# supports only hide by signature.

What is Abstract Class? Posted by: Poster Abstract class is a class that can not be instantiated, it exists extensively for inheritance and it must be inherited. There are scenarios in which it is useful to define classes that is not intended to instantiate; because such classes normally are used as base-classes in inheritance hierarchies, we call such classes abstract classes. Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the properties or methods and derived classes that inherit this implements it's properties or methods. Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract. For detailed example, read this article http://www.dotnetfunda.com/articles/article467abstract-class--explained.aspx

What is Sealed modifiers? Posted by: Poster 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.

What is an Interface? Posted by: Poster 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? Posted by: Charugoel 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.

1) What is meant by Object Oriented Programming? OOP is a method of programming in which programs are organised as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy. 2) What is a Class? Class is a template for a set of objects that share a common structure and a common behaviour. 3) 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. 4) 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. 5) What are the core OOP’s concepts? Abstraction, Encapsulation,Inheritance and Polymorphism are the core OOP’s concepts. 6) 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. 7) 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. 8) 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. 9) What is meant by Polymorphism? Polymorphism literally means taking more than one form. Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to something that was declared in a parent class. 10) 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.

11) 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. 12) 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. 13) What is a subclass? Subclass is a class that inherits from one or more classes 14) What is a superclass? superclass is a class from which another class inherits. 15) What is a constructor? Constructor is an operation that creates an object and/or initialises its state. 16) 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. 17) What is meant by Binding? Binding denotes association of a name with a class. 18) 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. 19) 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. 20) Define Modularity? Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules. 21) What is meant by Persistence? Persistence is the property of an object by which its existence transcends space and time. 22) What is colloboration? Colloboration is a process whereby several objects cooperate to provide some higher level behaviour. 23) 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. 24) How is polymorphism acheived in java? Inheritance, Overloading and Overriding are used to acheive Polymorphism in java.

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF