Sei sulla pagina 1di 12

OBJECT ORIENTED PROGRAMMING Thanks to Kiruthika.P Kongu engineering college B.E. CSE(2007-2011) PUBLISHED BY Naveen Raj.

B CSE(2007-2011)

1. What is a programming language? Programming languages are notations for describing computations to people and to machines. 2. What is Object Oriented Programming? Object-oriented programming (OOP) is a programming paradigm that uses "objects" data structures consisting of datafields and methods together with their interactions to design applications and computer programs. 3. What is Object Based Programming Languages? Object-based Programming languages support encapsulation and object identity without supporting important features of OOP languages such as polymorphism, inheritance and message based communication. 4. What is Object Oriented Programming Languages? Object-oriented languages incorporate all the features of object-based programming languages along with inheritance and polymorphism. 5. Where does Structured Programming and Object Oriented Programming differ? Structured Programming views the two core elements of any program-data and functions as separate entities whereas OOPs view them as single entity. 6. What are the features of OOPs ? Features of OOPs include Encapsulation, Data Abstraction, Inheritance, Polymorphism, Message Passing, Extensibility, Persistence, Delegation, Genericity and Multiple Inheritance. 7. What is an Object? An object Is an unique, identifiable, self-contained entity that possesses operations and contains attributes Possesses all the know-how and information it needs to perform the services for which it was designed Is a "black box" which receives and sends messages 8.What is a Class ? A Class is a software template that defines the methods and variables to be

included in a particular kind of Object. Is a blue print used to create objects. As it is a blue print, at run time it will not occupy any memory. Examples : Animal, Human being, Automobiles 9.What is Encapsulation? Encapsulation is "a technique that separates the external aspects of an object form the internal implementation. A "capsule" or module, packages together functions and the related data, and protects its data from other modules or outside functions. 10.What is the main objective of object oriented programming? The main objective of Object oriented programming is to hide the data item Accessibility of data item with a class is only possible through member methods. 11.What is the difference between encapsulation and abstraction? Abstraction tells us what external face we should present to the world where as Encapsulation ensures that the implementation of the interface doesnt leak out to the outside world. 12.What are access specifiers? Used for accessibility of data member and member methods from within and outside the class boundary Example: Private Public Protected Note: Some OOP language uses more access specifiers also. (For e.g. Java supports Friendly) 13. What is data hiding? Process of hiding the members from outside the class Implemented by the concept of Private access specifiers Can be accessed only by the member methods of that class where it is defined Data hiding is an important feature of OO programming which allows the data member to be kept safe Also called as information hiding. 14.What is inheritance? Ability to compose new abstraction from existing one Implements the concept of Re-usability Classes are arranged in a tree like structure called a hierarchy Base class: the class providing the implementation Derived class:

the class inheriting the implementation 15.What are the advantages of inheritance? Software reusability Design and code sharing Software components Rapid prototyping 16.What is an abstract class? A class that contains one or more abstract methods (methods which are not complete in terms of implementation) Other classes can extend them and make them concrete by implementing the abstract methods 17.What is polymorphism? Polymorphism Is the ability of different objects to respond in their own unique way to the same message Implemented by overloading and overriding 18.what are the ways for implementing polymorphism? Different Object Oriented Languages use different concepts of implementing polymorphism Function Overloading Operator Overloading Function Overriding C++ supports all the above features where as java does not support Operator Overloading. 19.what is function/method overloading? * Ability to define two or more methods with the same names and different signatures * Signature means number of arguments, type of arguments and sequence of arguments Ex: 1. void disp(int ix); 2. void disp(int ix,int iy); 20.What is operator overloading? Operator overloading in the language gives a new meaning for the operands of a specific class Operator overloading provides a way to define and use operators such as +, -, *, /, and [] for user defined types such as classes. 21.Why overload operators ? It's common to define a meaning for an existing operator for objects of a new class. We want to use the operators for user defined data types (user defined class) also. C++ supports overloading of operators where as java does not allow this.

22.What is overriding? Process of defining the methods with the same name and signature in derived class which already exists in the Base/Parent class Process of re-defining the methods in the derived class Methods must have argument lists with the identical type and order as the superclass 23.What is binding? Process of connecting a method call to a method body. Static Binding is performed before the program runs Compile time Dynamic Binding is performed at the time of execution Run time 24. What is static binding? Every time a function call is executed, the system will jump to the location where the function code is stored, and will execute the code. This is called static binding or early binding. Static binding is the default behavior for all C++ functions and member functions. Overloaded functions are implemented using static binding. 25.What is dynamic binding? The method called depends upon the type of the object and not the type of the expression. The type cannot always be resolved at compile time Method overriding implements the concept of dynamic binding. 26.What are the additional constructs used by oop language to implement dynamic binding? OOP language use additional constructs while implementing dynamic binding C++ uses a construct called Virtual Function Java uses a construct called Dynamic method dispatch 27. What is virtual constructors/destructors?

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the baseclass destructor function (matching the pointer type) is called on the object. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. 28.What do you mean by pure virtual functions? A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero. class Shape { public: virtual void draw() = 0; }; 29. What is namespace?

Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces. The form to use namespaces is: namespace identifier { namespace-body } 30. What is RTTI? Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type of an object and to convert the type of a pointer or reference (that is, dynamic typing). The need came from practical experience with C++. RTTI replaces many homegrown versions with a solid, consistent approach. 31. What is a template? Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones: template template <class indetifier> function_declaration; <typename indetifier> function_declaration;

The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way. 32. What do you mean by inline function? The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables. 33. What is virtual class and friend class? Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has. 34. Difference between realloc() and free()? The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer

specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer 35.What do you mean by binding of data and functions? Encapsulation. 36. What is the difference between an object and a class? Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects. A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change. The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed. 37. What is a scope resolution operator? A scope resolution operator (::), can be used to define the member functions of a class outside the class. 38. What are virtual functions? A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class. 39.What is friend function? As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition. 40.What is the difference between class and structure? Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private. 41.What is public, protected, private? Public, protected and private are three access specifiers in C++. Public data members and member functions are accessible outside the class. Protected data members and member functions are only available to derived classes. Private data members and member functions cant be accessed outside the class. However there is an exception can be using friend classes.

42.How can you achieve Multiple Inheritance in Java? Java's interface mechanism can be used to implement multiple inheritance, with one important difference from c++ way of doing MI: the inherited interfaces must be abstract. This obviates the need to choose between different implementations, as with interfaces there are no implementations. 43. What is a transient variable? A transient variable is a variable that may not be serialized. If you don't want some field not to be serialized, you can mark that field transient or static. 44.. What is the difference between Serializalble and Externalizable interface? When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject()two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process. 45. What are synchronized methods and synchronized statements? Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement. 46. What is synchronization and why is it important? With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often causes dirty data and leads to significant errors. 47. What is the purpose of finalization? The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected. 48. What classes of exceptions may be caught by a catch clause? A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types. 49. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy? The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented. 50. What happens when a thread cannot acquire a lock on an object? If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object's lock, it enters the waiting state until the lock becomes available.

51. What restrictions are placed on method overriding? Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method. 52. What restrictions are placed on method overloading? Two methods may not have the same name and argument list but different return types. 53.How does multithreading take place on a computer with a single CPU? The operating system's task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially. 54.How is it possible for two String objects with identical values not to be equal under the == operator? The == operator compares two objects to determine if they are the same object in memory. It is possible for two String objects to have the same value, but located indifferent areas of memory. 55. How are this() and super() used with constructors? this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor. 56.What class allows you to read objects directly from a stream? The ObjectInputStream class supports the reading of objects from input streams. 57.What is Serialization and deserialization? Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects. 58.Can you write Java code for declaration of multiple inheritance in Java ? Class C extends A implements B { } 59. What are the disadvantages of using threads? DeadLock 60.What do you mean by static methods? By using the static method there is no need creating an object of that class to use that method. We can directly call that method on that class. For example, say class A has static function f(), then we can call f() function as A.f(). There is no need of creating an object of class A.

61.What does the "abstract" keyword mean in front of a method? A class? Abstract keyword declares either a method or a class. If a method has a abstract keyword in front of it, it is called abstract method.Abstract method has no body. It has only arguments and return type. Abstract methods act as placeholder methods that are implemented in the subclasses. Abstract classes can't be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract. 62.Does Java have destructors? No garbage collector does the job working in the background 63.Are constructors inherited? Can a subclass call the parent's class constructor? When? You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language. 64.What does the "final" keyword mean in front of a variable? A method? A class? FINAL for a variable : value is constant FINAL for a method : cannot be overridden FINAL for a class : cannot be derived 65.What is the main difference between Java platform and other platforms? The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms. The Java platform has two components: 1. The Java Virtual Machine (Java VM) 2. The Java Application Programming Interface (Java API) 66.What is the Java Virtual Machine? The Java Virtual Machine is a software that can be ported onto various hardwarebased platforms. 67.What is the Java API? The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets. 68.What is the package? The package is a Java namespace or part of Java libraries. The Java API is grouped into libraries of related classes and interfaces; these libraries are known as packages.

69.What is native code? The native code is code that after you compile it, the compiled code runs on a specific hardware platform. 70.Is Java code slower than native code? Not really. As a platform-independent environment, the Java platform can be a bit slower than native code. However, smart compilers, well-tuned interpreters, and justin-time bytecode compilers can bring performance close to that of native code without threatening portability. 71.How to make a class or a bean serializable? By implementing either the java.io.Serializable interface, or the java.io.Externalizable interface. As long as one class in a class's inheritance hierarchy implements Serializable or Externalizable, that class is serializable. 72.What is a transient variable? A transient variable is a variable that may not be serialized. If you don't want some field not to be serialized, you can mark that field transient or static. 73.What are three ways in which a thread can enter the waiting state? A thread can enter the waiting state by invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method. 74.If a method is declared as protected, where may the method be accessed? A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared. 75.What is the difference between yielding and sleeping? When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state. 76.Is sizeof a keyword? The sizeof operator is not a keyword. 77.What are wrapped classes? Wrapped classes are classes that allow primitive types to be accessed as objects. 78.Does garbage collection guarantee that a program will not run out of memory?

No, it doesn't. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection 79.Which class is the superclass for every class. Object 80.What is the difference between throw and throws keywords? The throw keyword denotes a statement that causes an exception to be initiated. It takes the Exception object to be thrown as argument. The exception will be caught by an immediately encompassing try-catch construction or propagated further up the calling hierarchy. The throws keyword is a modifier of a method that designates that exceptions may come out of the mehtod, either by virtue of the method throwing the exception itself or because it fails to catch such exceptions that a method it calls may throw. 81.Name primitive Java types. The primitive types are byte, char, short, int, long, float, double, and boolean. 82.What is the difference between static and non-static variables? A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance. 83.What is the difference between the paint() and repaint() methods? The paint() method supports painting via a Graphics object. The repaint() method is used to cause paint() to be invoked by the AWT painting thread. 84.What is casting? There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference. 85.Is Java a super set of JavaScript? No. They are completely different. Some syntax may be similar. 86.Why Java does not support pointers? Because pointers are unsafe. Java uses reference types to hide pointers and programmers feel easier to deal with reference types without pointers. This is why Java and C# shine.

87.How many times may an objects finalize() method be invoked by the garbage collector? An objects finalize() method may only be invoked once by the garbage collector. 88.What is the purpose of the finally clause of a try-catch-finally statement? The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught. 89.What is the argument type of a programs main() method A programs main() method takes an argument of the String[] type. 90.Which Java operator is right associative? The = operator is right associative. 91.Can we define private and protected modifiers for variables in interfaces? No 92.What is mutable object and immutable object? If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, ) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, )