Athipathy Java Answers

  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Athipathy Java Answers as PDF for free.

More details

  • Words: 2,449
  • Pages: 14
1). Automatic Conversion

In Java type conversions are performed automatically when the type  of the expression on the right hand side of an assignment operation  can be safely promoted to the type of the variable on the left hand  side of the assignment. Thus we can safely assign: byte ­> short ­>  int ­> long ­> float ­> double. The ­> symbol used here should be  interpreted as "to a". For example:  // 64 bit long integer long myLongInteger; // 32 bit standard integer int myInteger; myLongInteger = myInteger; The extra storage associated with the long integer, in the above  example, will simply be padded with extra zeros.

When Java Code is compiled a byte code is generated which is independent of the system. This byte code is fed to the JVM (Java Virtual Machine) which is resided in the system. Since every system has its own JVM, it doesn't matter where you compile the source code. The byte code generated by the compiler can be interpreted by any JVM of any machine. Hence it is called Platform independent Language. 2).

Java's bytescodes are desgined to be read and interpreted in exactly same manner on any computer hardware or operating system that supports Java Runtime Environment.

In Java deallocation is handled automatically. The technique that accomplishes this is called “GARBAGE COLLECTION”. When no references to an object exist, that object is assumed to be no longer needed and the memory occupied by the object can be reclaimed. There is no 3).

explicit need to destroy objects as in C++. Garbage collection only occurs sporadically during the execution of your program. Different Java run-time implementations will take varying approaches to garbage collection. 4).

Refer page number 47 in the book

5). CONSTRUCTOR: A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes. Constructors do not have a return type and not even void because the implicit return type of a class constructor is the class type itself. The constructor’s job is to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately. METHODS: General form of a method is: Type name (parameter list) { //body of method } Here, type specifies the type of data returned by the method. This can be any valid type including class types that you create. If the method does not return a value, its return type must be void. The name of the method is specified by name. This can be any legal identifier. The parameter list is a sequence of type and identifier pairs separated by commas. Methods that have a return type other than void return a value to the calling routine using the following form of the return statement: return value; Here, value is the value returned. 6). In terms of implementation we can recognize universal polymorphism when the same code is executed for any admissible type, whereas in case of ad-hoc polymorphism different code is executed for each type. There are two major

kinds of universal polymorphism: parametric and inclusion polymorphism, and two major kinds of ad-hoc polymorphism: coercion and overloading. Parametric polymorphism allows a simple piece of code to be typed “generically”, using variables in place of actual types. These type variables are instantiated with concrete types. Parametric polymorphism guarantees uniform behavior on the range of types. In inclusion polymorphism an object can be viewed as belonging to many different classes that need not to be disjoint; that is, there may be inclusion of classes. Inclusion polymorphism models subtyping and subclassing (inheritance). 7).

Static and instance methods:

When a method declaration includes a static modifier, that method is said to be a static method. When no static modifier is present, the method is said to be an instance method. A static method does not operate on a specific instance, and it is a compile-time error to refer to this in a static method. An instance method operates on a given instance of a class. 8).

Abstract class:

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. 9).

Refer page number 45 in the book

10). The capability of using the final modifier on formal parameters was introduced in the Java 1.1 update. Basically, it's just like using final on a class field. That is to say that

using it tells readers and the compiler that that parameter will not be modified in the method. Note that the use of final on a formal parameter is not part of the method's signature. I.e., the modifier can be added or removed from subclass implementations of that method. Note also that using final on a reference variable does not mean that you can't change the value that the reference points to -- only that you won't change the reference itself. So, use the final modifier on formal parameters to methods when you want to make it clear that the supplied value is not modified by that method. 11). We declare, main method in java as: public static void main(String args[]) public : means it can be accessible to all other classes. static : main is the entry point of a class. In java everything thing is written in a class.Now when you run java filename on command prompt, loader will load the class and jvm will search the main method to enter into the class. so making the main() as static, will make jvm access it directly through classname.main() Now once jvm get the main(), object instantiated there are accessed. void : main method should return anything. main : its the method name, the JVM will seek for after the class is loaded on runing command like e.g java classname String args[] : String array named args used to access the string variables passed on command line. e.g java classname firstname lastname

Static Block and Static Initializer in Java

The static variables or static blocks in a class will be initialized before the class gets instantiated. Also, we all know that the static variables are not tied up with the instances. So even if we have the reference as null, the variable s value had already been initialized to "hello". So calling it that way will print the values Inside Method and Hello as the variable call will be made with the class name as they are tied up with the class. If you make the variable non - static, you will get a NPE.

12). Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class. A class that is derived from an abstract class may still implement interfaces. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code. For INTERFACES refer page number 192 in the book

14). Access Specifiers An access specifier controls the access of class members and variables by other objects. The various types of access specifiers in Java are:

public private protected friendly or package

The public Access Specifier Class members with public specifier can be accessed anywhere in the same class, package in which the class is created, or a package other than the one in which the class is declared. You can use a public class, data member, or a method from any object in a Java program. The public keyword is used to declare a member as public. The following statement shows how to declare a data member of a class as public: public ; The class Account defines the show () method and various data members, such as name and account number. All the classes in the program can access the various details of a customer, such as name and account number. Therefore, these data members and the method are declared public. The show () method is used to display the account number and customer name of a customer. You can use the following code snippet to define a class, Account that contains public data variables and method: public class Account { public int account_no; // Data members are accessible outside the class public 'String name; public string name;

public void show() //Method declaration { System. out. printIn("Name =" + name); //Statement of the method System.out.printIn("Account number of this customer is= " + account no); } }

The private Access Specifier The private access specifier provides most restricted level access. A data member of class declared as private is accessible at the class level only in which it is defined. You can use the private access specifier to declare members that should be available to the class within which they are declared. The private keyword is used to declare a member as private. In Java, you implement the concept of encapsulation by using the private keyword. The following syntax shows how to declare a data member of a class as private: private ; // Private data member of float type private methodName(); // Private method The Account class defines the show() method and the various data members, such as balance and age. These members are to be accessed only by the objects of the same class. Therefore, these methods art declared private. You can use the following code snippet that shows the Account class with private data variables, such as age and balance:

class Account { private int account_no; private String name; private int age; private float balance; public void show() { System.out.println ("Age of Customer ="+ age); System.out.println("Balance of this customer is=" + balance) ; } }

The protected Access Specifier

The variables and methods that are declared protected are accessible only to the subclasses of the class in which they are declared. The protected keyword is used to declare a member as protected. The following statement shows how to declare a member as protected: protected ;

In an airline reservation application, you can create the Ticket class that consists of various data members, such as flightNumber, date, time, and destination. You can derive the ConfirmedTicket subclass from the Ticket class that consists of an additional data member, seatNumber. You can declare the data members of the Ticket class as protected, which can be accessed by the ConfirmedTicfcet subclass. You can use the following code snippet to define the Ticket class that has protected data variables: public class Ticket { protected int flightNumber; protected String date; protected String time; protected String destination; protected void showData() { //Code Body } } In the preceding code snippet, various data members and methods are declared protected.

The friendly or package Access Specifier

If you do not specify any access specifier, the scope of data members and methods is friendly. Java provides a large number of classes, which are organized into groups in a package. A class, variable, or method that has friendly access is accessible only to the classes of a package. The data members, such as pageNumbers and price, and the showData() method of the Books class are not given access specifiers. The following code snippet shows the Books class that has friendly access specifier: Class Books { Int pageNumbers; float price; void showdata() { //code body } } In Java, friendly is not a keyword. It is a term that is used for the access level when no access specifier has been specified. You cannot declare a class, variable, or method with the friendly specifier.

15). Java This and Super

this The current class instance. Can be used with variables (line 6) or methods (line 10).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

class Numbers { private int aNumber = 42; public int returnANumber() { return this.aNumber; } public int returnANumber(int intIn) { return (intIn * this.returnANumber()); } public static void main(String[] args) { Numbers numberTest = new Numbers(); System.out.println("The Number is " + numberTest.returnANumber() ); //output is: The Number is 42 System.out.println("The Number is " + numberTest.returnANumber(2) ); //output is: The Number is 84

18 19 20 21 22

} }

super Used to specify methods in the parent class.

class Cat { public String name; public Cat() {name = "no nameIn";} public Cat(String nameIn) {name = nameIn;} public String getName() { return(name + " the Cat"); } }

class Himalayan extends Cat { public Himalayan() {} public Himalayan(String nameIn) { name = nameIn; } public String getName() { return (name + " the Himalayan");

} public String getNameAsCat() { return super.getName(); } public static void main(String[] args) {

Himalayan cappuccino = new Himalayan("Cappuccino"); System.out.println("The Himalayan name is " + cappuccino.getName() ); //output is: The Himalayan name is // Cappuccino the Himalayan System.out.println("The Cat name is " + cappuccino.getNameAsCat() ); //output is: The Cat name is // Cappuccino the Cat } }

16). Generating Prime Number Here is the example of prime number in Java. It display all the prime numbers between 1 and the limit entered by user. Get the source code of prime number example here : import java.io.*; class Primenumberexample { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter Limit to Print Prime Number :"); int k = Integer.parseInt(bf.readLine()); System.out.println("Prime Numbers Are : "); for (int i=1; i < k; i++ )

{ for (int j=2; j < i; j++) { int a = i%j; if (a==0){ break; } } if(i == j) { System.out.print(" "+i); } } } } 17). Definition: An exception is an event, which occurs during the execution of a program, which disrupts the normal flow of the program's instructions. 21). Refer page number 192 in the book 26). class Prime_number { public static void main(String[] args) { int num = 11; int i; for (i=2; i < num ;i++ ) { int n = num%i;

if (n==0) { System.out.println("not Prime!"); break; } } if(i == num){ System.out.println("Prime number!"); } } }

Related Documents