Вы находитесь на странице: 1из 8

JAVA ASSIGNMENT-by Anurag Jayant 1-> what is the difference between Employee e = new Employee(); System.out.

println(e); and Employee e = new Employee(); System.out.println(e.toString()); Ans 1). The java toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object. Program: public class Employee { public static void main(String ...arg) { Employee e=new Employee(); System.out.println(e); System.out.println(e.toString()); } } Output: Employee@19821f Employee@19821f

2->which package have wrapper class? Ans 2). Wrapper Classes are used broadly with Collection classes in the java.util package and with the classes in the java.lang.reflect reflection package.

3-> when we use equals method and == ? Ans 3). They both differ very much in their significance. equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the '==' operator is expected to check the actual object instances are same or not.

4-> what is the use of instanceof operator? Ans 4). The keyword instanceOf in java programming language is a boolean operator that is used to test whether an object is of an specified type or not and returns the value accordingly. f (node instanceof TreeNode){ / write your code here } Where, condition (node instanceOf TreeNode) returns true if the class node is an instance or is an instance of a subclass of TreeNode.

5->what is early(Static) and late(Dynamic) binding? Ans 5). 1) Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime. 2) private, final and static methods and variables uses static binding and bonded by compiler while virtual methods are bonded during runtime based upon runtime object. 3) Static binding uses Type(Class in Java) information for binding while Dynamic binding uses Object to resolve binding. 3) Overloaded methods are bonded using static binding while overridden methods are bonded during compile time. here is an example which will help you to understand both static and dynamic binding in Java.

6).What is the scope of public,protected,default and private specifiers? Ans 6). public keyword specifies that the public class, the public fields and the public methods can be accessed from anywhere.

private: This keyword provides the accessibility only within a class i.e. private fields and methods can be accessed only within the same class.

protected: This modifier makes a member of the class available to all classes in the same package and all sub classes of the class.

default : Its not a keyword. When we don't write any access modifier then default is considered. It allows the class, fields and methods accessible within the package only.

7->how can we achieve multiple inheritance in java? Ans 7). The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface.

8->what is casting? Ans 8). Type Casting refers to changing an entity of one datatype into another. This is important for the type conversion in developing any application. If you will store a int value into a byte variable directly, this will be illegal operation.

9->what is the use of this keyword? Ans 9). The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps us to avoid name conflicts. 10->what is boxing and unboxing? Ans 10). In Boxing ,it converts primitive values to wrapper types: if p is a value of a primtiveType, boxing converts p into corresponding WrapperType, such that ref.primitiveTypeValue() == p. In Unboxing , it converts objects of wrapper types to primitive types: if ref is a reference of a WrapperType, unboxing converts the reference ref into ref.primitiveTypeValue().

11->what is object class? Ans 11).The Object class sits at the top of the class hierarchy tree in the Java development environment. Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must

have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class.

12->what is true *constructor overloading is happened in java *constructor is inherited in java Ans 12). constructor overloading is happened in java->True

13->what is signatures? Ans 13). A signature is a list that specifies a class constructor, an instance method, or a static method, thereby distinguishing it from other constructors, instance methods, or static methods. Two forms of signatures are accepted: simple and full. A simple signature is a single element list containing the name of the method or constructor. In most cases a simple signature is all that is needed as the Java method resolver is able to disambiguate overloaded Java methods based on the types of Java object arguments. There are some cases where the Java method resolver is unable to determine which Java method you intended to invoke so you will need to use the full signature for the method or constructor. The full signature is used to distinguish between two or more methods or constructors that have the same number of arguments. The full signature of a method is a Tcl list containing the method name followed by the name of the Java object type for each parameter of the method.

14->what is overriden and overriding methods? Ans 14). In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.

15->difference between glabal and local variables? Ans 15). Global variables once declared they can be used any where in the program i.e. even in many functions. If possible u can use the global variables in the different user defined header files as like in packages in java. On the other hand global variables values can be changed programmatically

local variables are local to a functional and cant be used beyond that function.

16->difference between static and and final variables? Ans 16).Final: A java variable can be declared using the keyword final. Then the final variable can be assigned only once. A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it. Java classes declared as final cannot be extended. Restricting inheritance! Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not. final parameters values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding. Java local classes can only reference local variables and parameters that are declared as final. A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.

Static: It is a variable which belongs to the class and not to object(instance) Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables A single copy to be shared by all instances of the class A static variable can be accessed directly by the class name and doesnt need any object Syntax : <class-name>.<variable-name>

17->default value of refferance type variable? Ans 17). Default value of any reference variable is null.

18->how many types of data type available in java? Ans 18).

There are two data types available in Java:

1. Primitive Data Types 2. Reference/Object Data Types

int It is a 32-bit signed two's complement integer data type. It ranges from 2,147,483,648 to 2,147,483,647. This data type is used for integer values. However for wider range of values use long.
byte The byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127 (inclusive). We can save memory in large arrays using byte. We can also use byte instead of int to increase the limit of the code. short The short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to 32,767. short is used to save memory in large arrays. long The long data type is a 64-bit signed two's complement integer. It ranges from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger range of values. float The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float (instead of double) to save memory in large arrays. We do not use this data type for the exact values such as currency. For that we have to use java.math.BigDecimal class. double This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). This data type is generally the default choice for decimal values. boolean The boolean data type is 1-bit and has only two values: true and false. We use this data type for conditional statements. true and false are not the same as True and False. They are defined constants of the language. char The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535. They are not same as ints, shorts etc.

19->what is object class? Ans 19). The Object class sits at the top of the class hierarchy tree in the Java development environment. Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class.

20->what is the use of entity class and return type of setter and getter method? Ans 20). public class Employee1{ private String name; private int Age; private String Id; private double salary; public void setName(String name){ this.name=name;} public void setAge(int a){ this.Age=a;} public void setId(String id) { this.Id=id;} public void setSalary(double d){ this.salary=d;} String getName(){ return name;} int getAge(){ return Age;} double getSalary(){ return salary;} String getId(){ return Id;}} class Demo extends Employee1{ public static void main(String ...arg){ Employee1 e=new Employee1(); e.setName("anuj"); System.out.println(e.getName());

e.setId("abc"); System.out.println(e.getId()); e.setAge(20); System.out.println(e.getAge()); e.setSalary(2000); System.out.println(e.getSalary());} }