Sie sind auf Seite 1von 5

1) What are basic tenets of Object Oriented Programming Ans: The four basic tenets of object oriented programming

are: a) Abstraction: This is the mechanism by which the implementation details are hidden from the client that is using the object. b) Encapsulation: Is the mechanism by which the data and the programs that operate on the data are encapsulated into a single object to avoid misuse or mishandling of the data. The data should be accessed through the methods that operate on data. c) Polymorphism: Polymorphism is a mechanism by which the same method can exhibit different behavior. a. Static Polymorphism: Also called method overloading or compile time polymorphism is a mechanism by which the method to be invoked is known at compile time. Based on the type, number and order of the inputs, a method is chosen which can be determined at compile time itself. b. Dynamic Polymorphism: Also called as method overriding or runtime polymorphism is a mechanism by which the subclass can override the behavior of an inherited method with a same signature. The method invoked can only be determined at runtime when we know whether the method is being invoked on the superclass or the subclass instance. d) Inheritance: Inheritance is a mechanism by which a subclass can inherit state and behavior (data and methods) from a superclass. 2) What are the different access modifiers Ans: Access modifiers set/removes constraints on how a class, field, a method, or an object can be access by other entities. The basic access modifiers are a. Private: This is only applicable to a field or a method. The field or method is only accessible to the other methods in the same class b. Protected: The field, or method is only accessible within the package and additionally to any other classes that directly subclass from this class. c. Public: The class, field, or method is accessible to any other class, or method.

d. Default (Not specifying any of the above three): The class, field or method is only accessible within the package in which the class is defined. Additionally following access modifiers are present: a. Static: The field or method is only accessible to other class level methods b. Abstract: The class or method is only an abstraction and there is not implementation. An abstract method has an empty body/content. A class with at least one abstract method is called an abstract. c. Volatile: If a field is declared volatile, a Thread must reconcile its working copy of the field with the master copy every time it accesses the variable d. Final: The class or method cannot be overridden e. Transient: A field declared as transient is not persisted 3) What is the difference between an interface and an abstract class? Ans: a. An interface defines only the method signatures that should be implemented by any class implementing the interface so that the clients of the interface are totally decoupled from the implementation details of the methods. This allows multiple inheritance in Java. This fosters greater decoupling and abstraction of implementation. This also fosters easy code maintenance. b. An abstract class has one or more abstract methods. The classes extending the abstract class should implement all the abstract methods if they themselves dont want to be abstract. This allows for better code reusability. Java doesnt support multiple inheritance from normal classes or abstract classes. Based on the specific need, we should either use an interface or an abstract class weighing whether we want to give importance to code reusability, or extensibility or code maintenance. Normally we use interfaces when we know that the eco system of classes is very unlikely to change, serving as a contract between the interacting entities. 4) What is a constructor and a destructor? A constructor is a block of code which is run once an instance of the corresponding class is created. This block of code is usually used to initialize

the state of the object. A destructor is a block of code which is executed when an object is being taken out of the heap memory normally as a consequence of garbage collection of the object space in memory. 5) What is an object reference Ans: An object reference is a handle to an object in memory. Through the different members (data and methods) of the object can be accessed/invoked. When an object is created using a new operator, the operation returns a reference to the newly created object. It is possible to have two references of objects pointing to the same object. A reference can be assigned to another reference through an = operator in which case, both the references point the same object. E.g., MyObject myObj1 = new MyObject(); // Statement 1 MyObject myObj2= myObj1; // Now myObj2 also refers the same object created in statement 1 6) What is the difference between an == operator and equals() method Ans: The == operator checks if two references are pointing to the same object or not and returns true or false accordingly. In the above case, myObj1 == myObj2 would return true. equals method checks if both the objects are equal or not. For example: String tempStr1 = new String(test); // Object 1 String tempStr2 = test; //Object 2 In this case tempStr1.equals(tempStr2) would return true, even though the underlying objects are discrete. 7) What is garbage collection? Ans: When a system is depleted of the memory, it would be wise to remove any unused objects from the heap memory to free up the space in memory. An object is considered unused if there are no reference to that object that are alive. The virtual machine would then clean up this object from the memory. This process is termed garbage collection. Garbage collection cannot be run forcefully. In java, System.gc() would send a request to virtual machine to invoke the garbage collection. However, the VM may, at its discretion, may or may not invoke garbage collection. 8) What are immutable objects?

Ans: Immutable objects are objects whose values cannot be changed. Example: In Java, a string is an immutable object. When the following statement is executed a new object is created: String str = new String(test); // Statement 1 Now the value of the string itself cannot be modified. For example: str.toUpperCase(); // Statement 2 method will return a upper case string TEST but will not modify the string that is created in statement 1. After statement 2 is executed, if we print str, we still get test. 9) What is multiple inheritance? Multiple inheritance is a mechanism by which a single class can subclass from more than one superclass. It is not supported in Java. Is it supported in .NET? 10) What is a thread?

A thread is a single unit of work. For example, a program that read the user name from console and that prints the welcome message to the user is a single thread. The threads are programs that run a specified peace of code. 11) What is the different object oriented languages and object based languages? Object based languages too use an ecosystem of objects but unlike OOP, it doesnt support inheritance. Examples of OOP languages: Java, .NET, smalltalk. Examples of OBL: Javascript 12) What is object persistence?

Object persistence is a mechanism by which the object state can be persisted to a stream of output. It may later be read from an input stream to recreate the object with the same state. For example, we can write an object into a FileSystem and then read the object again back from the File System and recreate the same object that was persisted. 13) What is a deadlock?

A deadlock is a situation at least two processes (threads) are waiting for each other to release resources and neither one completes forever. 14) What is pass by reference and pass by value

Pass by reference is a semantic where an objects reference is passed to another reference so that both the references point to the same object. Pass by reference is used by objects. Example: { Person p = new Person; PrintName(p); // Object being passed by reference. Actual argument. } PrintName(Person p) { // Object passed by reference. Formal Argument System.out.println(p.getName()); } In pass by value, the value is passed . This applies to primitive types: { int radius = 3.5; printRadius(radius); // passed by value. Actual argument. }print printRadius(int radius){ // passed by value. Formal Argument System.out.println(radius); } 15) ssdfdsfd

Das könnte Ihnen auch gefallen