JavaAns

download JavaAns

of 12

Transcript of JavaAns

  • 8/6/2019 JavaAns

    1/12

    1. What is object-oriented programming? How is itdifferent from the procedure-oriented programming?

    Ans.

    Object- oriented programming is a method ofimplementation in which programs are organized asco-operative collection of objects, each of whichrepresents an instance of some class and whoseclasses all members of a hierarchy of classes unitedin inheritance relationships.

    With procedural programming you are able tocombine returning sequences of statements into one

    single place. A procedure call is used to invoke theprocedure. After the sequence is processed, flow ofcontrol proceeds right after the position where thecall was made

    3. Distinguish between the following terms:

    1Objects and classes2Data abstraction and data encapsulation

    3Inheritance and polymorphism4Dynamic binding and message passing

    Ans.

    1Objects and classes: Object is a physical entitywhich represents a person, vehicle or a conceptualentity (thing in existence) like bank account,company etc.A set of variables and functions used to describe

    an object is a "class".A class defines the structure and behavior (dataand code) that will be shared by a set of objects.Each object of a given class contains the structureand behavior defined by the class, as if it werestamped out of a mould in the shape of a class. Aclass is a logicalconstruct. An object has physical

  • 8/6/2019 JavaAns

    2/12

    reality. When you create a class, you will specifythe code and data that will constitute that class.Collectively, these elements are called themembers of the class. Specifically, the data

    defined by the class are referred to as membervariables or instance variables. The code thatoperates on that data is referred to as membermethods or just methods, which define the use ofthe member variables.

    1Data abstraction and data encapsulation:Abstraction - the act or process of leaving out ofconsideration one or more qualities of a complexobject so as to attend to others. Solving a problemwith objects requires you to build the objects

    tailored to your solution. We choose to ignore itsinessential details, dealing instead with thegeneralized and idealized model of the object.

    51)Encapsulation - The ability to provide users with awell-defined interface to a set of functions in away, which hides their internal workings. In object-oriented programming, the technique of keepingtogether data structures and the methods(procedures) which act on them. The easiest way

    to think of encapsulation is to reference phones.There are many different types of phones, whichconsumers can purchase today. All of the phonesused today will communicate with each otherthrough a standard interface. For example, aphone made by GE can be used to call a phonemade by Panasonic. Although their internalimplementation may be different, their publicinterface is the same. This is the idea ofencapsulation.

    1Inheritance and polymorphism: Inheritance inobject-oriented programming means that a classof objects can inherit properties from another classof objects. When inheritance occurs, one class isthen referred as the 'parent class' or 'superclass'or 'base class'. In turn, these serve as a pattern for

  • 8/6/2019 JavaAns

    3/12

    a 'derived class' or 'subclass'.Inheritance is an important concept since it allowsreuse of class definition without requiring majorcode changes. Inheritance can mean just reusing

    code, or can mean that you have used a wholeclass of object with all its variables and functions.Polymorphism: It is a key concept in object-oriented programming. Poly means many, morphmeans change (or 'form').Polymorphism is simply a name given to an actionthat is performed by similar objects. Polymorphismallows a common data-gathering message to besent to each class and allows each subclass objectto respond to a message format in an appropriate

    manner to its own properties. Polymorphismencourages something we call 'extendibility'. Inother words, an object or a class can have its usesextended.

    1Dynamic binding and message passing:Dynamic binding in java is the mechanism bywhich compiler cannot determine which methodimplementation to use in advance. Based on theclass of the object, the runtime system selects the

    appropriate method at runtime. Dynamic bindingis also needed when the compiler determines thatthere is more than one possible method that canbe executed by a particular call.

    Java's program units, classes, are loadeddynamically (when needed) by the Java run-timesystem. Loaded classes are then dynamicallylinked with existing classes to form an integratedunit. The lengthy link-and-load step required bythird-generation programming languages iseliminated.Message Passing: In an object based world theonly way for anything to happen is by objectscommunicating with each other and acting on theresults. This communication is called messagepassing and involves one object sending a

  • 8/6/2019 JavaAns

    4/12

    message to another and (possibly) receiving aresult.

    4. Describe inheritance as applied to OOP.Ans.

    Inheritance in object oriented programming meansthat a class of objects can inherit properties fromanother class of objects. When inheritance occurs,one class is then referred to as the 'parent class' or'superclass' or 'base class'. In turn, these serve as apattern for a 'derived class' or 'subclass'.

    Inheritance is an important concept since it allowsreuse of class definition without requiring major codechanges. Inheritance can mean just reusing code, orcan mean that you have used a whole class of objectwith all its variables and functions.

    5. List the eight basic data types used in Java. Giveexamples.

    Ans.The eight basic data types used in java are:

    byte: It is the smallest integer type. This is a signed8-bit type and has a range from -128 to 127. Forexample, the following declaration declares twovariables B and C of type byte.

    byte b,c;

    b =2;

    c = -114;

    short: It is a signed 16-bit type and has a range from-32,768 to 32,767. For example, the followingdeclaration declares variable K of type short.

  • 8/6/2019 JavaAns

    5/12

  • 8/6/2019 JavaAns

    6/12

    Int score;

    Score=90;

    char: this data type is used to store characters. It is

    16-bit type and has a range from 0 to 65,536.

    For example,

    char c1,c2;

    c1 =84;

    c2 ='g';

    boolean: it can have only one of two possiblevalues, true or false.

    For example,

    boolean flag;

    flag= false;

    6. What is type casting? Why is it required inprogramming?

    Ans.

    The process of converting one data type to another iscalled casting.

    Type variable1 = (type) variable2;

    Examples:

    int m = 50;

    byte n = (byte)m;

    long distance = (long)m;

    Type casting is required in programming when youwant to assign the value of one variable to another

  • 8/6/2019 JavaAns

    7/12

    variable of different data type.

    7. In what ways does a switch statement differ froman if statement?

    Ans.

    An if statement can be used to make decisions basedon range of values or conditions, whereas a switchstatement can make decisions based only on a singleinteger value. Also, the value provided to each casestatement must be unique.

    1. Compare in terms of their functions, the followingpairs of statements:

    (a) while and do........while.

    (b) while and for.

    (c) break and continue.

    Ans.

    (a) The difference between the do-while statementand the while statement is that in the whilestatement, the loop body is executed only when thecondition stated in the statement is true. In the do-while loop, the loop body is executed at least once,regardless of the condition evaluating to true orfalse. The while loop is also called the top tested loopwhereas the do-while loop is also called the bottomtested loop.

    (b) In the for loop, three sections, initialization, test

    condition and increment/decrement are placed in thesame line whereas in the while loop, all threesections are placed in three different places in aprogram. In the for loop, more than one variable canbe initialized, tested and incremented at a time.

    (c) The continue statement stops the current

  • 8/6/2019 JavaAns

    8/12

    iteration of a loop and immediately starts the nextiteration of the same loop. When the current iterationof a loop stops, the statements after the continuestatement in the loop are not executed. The break

    statement immediately terminates the loop,bypassing the conditional expression and anyremaining code in the body of the loop. When abreak statement is encountered inside the loop, theloop is terminated and program control resumes thenext statement following the loop.

    8. How do classes help us to organize our programs?

    Ans.

    In essence a class definition is very simple. There arejust two kinds of things that you can include in aclass definition:

    Variables

    Variables are the data types that store data itemsthat typically differentiate one object of the classfrom another. They are also referred to as data

    members of a class. Every class you write in Java isgenerally made up of two components: attributesand behavior. Let's consider an object to define amotorcycle. Attributes are the individual things thatdifferentiate one object from another and determinethe state, appearance, or other qualities of thatobject. The attributes of our motorcycle mightinclude:

    1color: red, green, silver, brown.

    2make: Honda, BMW, Bultaco.3engineOn: true, false.

    Attributes are defined by variables, in fact, you canconsider them because each instance of a class canhave different values for its variables, each variableis called an instance variable.

  • 8/6/2019 JavaAns

    9/12

    Methods

    These define the operations you can perform for theclass--so they determine what you can do to, or with,objects of the class. Methods typically operate on the

    fields--the variables of the class. A class's behaviordetermines what instances of that class do whenasked to by another class or object. Behavior is theonly way that objects can have anything done tothem. Our motorcycle class might well have thefollowing behavior:

    Start the engine

    Stop the engine

    Speed up

    Change gear

    To define an object's behavior you create methods.

    9. What are objects? How are they created from aclass?

    Ans.

    An object in java is a block of memory that contains aspace to store all the instance variables. As with real-world objects, software objects have state andbehavior. In programming terms the state of anobject is determined by its data (variables); thebehavior by its methods. Thus a software object

    combines data and methods into one unit.Creating an object is referred to as instantiating anobject. The creating object to a class is two-stepprocess:

    1.Declare a variable of class type. This variable doesnot define an object instead it is a simply a

  • 8/6/2019 JavaAns

    10/12

    variable that can refer to an object.2.Physical copy of the object is created and assigned

    to that variable.

    10. What is a constructor? What are its special

    properties?

    Ans.

    The central player in object initialization is theconstructor. In Java, constructors are similar tomethods, but they are not methods. Like a method, aconstructor has a set of parameters and a body ofcode. Unlike methods, however, constructors haveno return type. Like methods, you can give access

    specifiers to constructors, but unlike methods,constructors with public, protected, or packageaccess are not inherited by subclasses. (Also, insteadof determining the ability to invoke a method, theaccess level of a constructor determines the ability toinstantiate an object.)

    The special properties of a constructor are:

    Constructor- names are the same as the name of the

    class.

    Constructor does not specify a return type.

    11. What is an array?

    Ans.

    An array is a sequence of logically related dataitems. It is a kind of row made of boxes, with each

    box holding a value. The number associated witheach box is the index of the item. Each box can beaccessed by, first box, second box, third box, and soon, till the nth box. The first box, or the lowest boundof an array is always zero, which means, the firstitem in an array is always at position zero of thatarray. Position in an array is called index. So the third

  • 8/6/2019 JavaAns

    11/12

    item in an array would be at index 2 (0, 1, 2).

    12. Why are arrays easier to use compared to a

    bunch of related variables?

    Ans.

    An array is a sequence of logically related dataitems. It is a kind of row made of boxes, with eachbox holding a value.

    Arrays have following advantages over bunch ofrelated variables:

    1Arrays of any type can be created. They can haveone or more dimensions.

    2Any specific element can be indexed in an array byits index.

    3All like type variables in an array can be referred bya common name.

    13. How does String class differ from the

    StringBuffer class?

    Ans.

    String class has a limit while there is no limit inStringBuffer class. In the StringBuffer class input isflushed while it is not so in the String class.

    String is an array whose all elements are of charactertype. String class is used in the same manner that

    we might use any other predefined class. Forexample we might declare an instance of the Stringclass as follows:

    String name = new String();

    14. Distinguish between Multiprocessing and

  • 8/6/2019 JavaAns

    12/12

    Multithreading.

    Ans.

    Multiprocessing: Refers to a computer system's

    ability to support more than one process (program)at the same time. Multiprocessing operating systemsenable several programs to run concurrentlyMultiprocessing systems are much more complicatedthan single-process systems because the operatingsystem must allocate resources to competingprocesses in a reasonable manner.

    Multithreading : The ability of an operating system toexecute different parts of a program, called threads,simultaneously. The programmer must carefullydesign the program in such a way that all the threadscan run at the same time without interfering witheach other.