State use of finalize( ) method with its syntax.

1 Answer

Answer :

Use of finalize( ): Sometimes an object will need to perform some action when it is destroyed. Eg. If an object holding some non java resources such as file handle or window character font, then before the object is garbage collected these resources should be freed. To handle such situations java provide a mechanism called finalization. In finalization, specific actions that are to be done when an object is garbage collected can be defined. To add finalizer to a class define the finalize() method. The java run-time calls this method whenever it is about to recycle an object.

Syntax: protected void finalize() { }  

Related questions

Description : Define type casting. Explain its types with syntax and example.

Last Answer : 1. The process of converting one data type to another is called casting or type casting. 2. If the two types are compatible, then java will perform the conversion automatically. 3. It ... requires to Narrowing conversion to inform the compiler that you are aware of the possible loss of precision.

Description : Describe the use of any methods of vector class with their syntax. 

Last Answer : boolean add(Object obj)-Appends the specified element to the end of this Vector. Boolean add(int index,Object obj)-Inserts the specified element at the specified position in this Vector. ... vector. void removeAllElements()-Removes all components from this vector and sets its size to zero.

Description : Write syntax and example of : 1) drawRect() 2)drawOval()

Last Answer : 1)drawRect() : drawRect () method display an outlined rectangle. Syntax: void drawRect(int top,int left, int width,int height) The upper-left corner of the Rectangle is at top and left. The ... and height the following program draws several ellipses and circle. Example: g.drawOval(10,10,50,50);

Description : Write the syntax of try-catch-finally blocks.

Last Answer : try{ //Statements to be monitored for any exception } catch(ThrowableInstance1 obj) { //Statements to execute if this type of exception occurs } catch(ThrowableInstance2 obj2) { //Statements }finally{ //Statements which should be executed even if any exception happens }

Description : Describe final variable and final method

Last Answer : Final method: making a method final ensures that the functionality defined in this method will never be altered in any way, ie a final method cannot be overridden. Syntax: final void findAverage() ... on individual objects of the class. Example of declaring final variable: final int size = 100;

Description : Explain the concept of Dynamic method dispatch with suitable example.

Last Answer : Method overriding is one of the ways in which Java supports Runtime Polymorphism. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden ... B(); // object of type B C c = new C(); // object of type C 

Description : Differentiate between method overloading and method overriding.

Last Answer : Sr. No. Method overloading Method overriding 1 Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two ... 4 overloading is a compiletime concept. Overriding is a run-time concept

Description : Explain dynamic method dispatch in Java with suitable example.

Last Answer : Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called through a superclass reference, Java determines which version ( ... ; // calling C's version of m1() ref.m1(); } }

Description : Define a class student with int id and string name as data members and a method void SetData ( ). Accept and display the data for five students.

Last Answer : import java.io.*; class student { int id; String name; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); void SetData() { try { System.out.println("enter id and name for student"); ... (String are[]) {  student[] arr;  arr = new student[5];  int i;  for(i=0;i

Description : List any four methods of string class and state the use of each.

Last Answer : The java.lang.String class provides a lot of methods to work on string. By the help of these methods, We can perform operations on string such as trimming, concatenating, converting, comparing, replacing ... " System.out.println(s2); Output: Kava is a programming language. Kava is a platform.

Description : Define exception. State built-in exceptions. 

Last Answer : An exception is a problem that arises during the execution of a program. Java exception handling is used to handle error conditions in a program systematically by taking the necessary action ... browser's security setting. StackOverflowException: Caused when the system runs out of stack space.

Description : Define array. List its types. 

Last Answer : An array is a homogeneous data type where it can hold only objects of one data type. Types of Array:  1)One-Dimensional 2)Two-Dimensional 

Description : Define Constructor. List its types. 

Last Answer : Constructor: A constructor is a special member which initializes an object immediately upon creation. It has the same name as class name in which it resides and it is syntactically similar to ... . Types of constructors: 1. Default constructor 2. Parameterized constructor 3. Copy constructor 

Description : Define stream class. List its types.

Last Answer : Definition of stream class: An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other ... , Writer. Types of stream classes: i. Byte stream classes ii. Character stream classes.

Description : Describe the use of following methods: (i) Drawoval ( ) (ii) getFont ( ) (iii) drawRect ( ) (iv) getFamily ( )

Last Answer : (i) Drawoval ( ): Drawing Ellipses and circles: To draw an Ellipses or circles used drawOval() method can be used. Syntax: void drawOval(int top, int left, int width, int height) The ellipse is drawn ... the family of the font. String family = f.getFamily(); Where f is an object of Font class

Description : On successful compilation a file with the class extension is created. a) True b) False

Last Answer : a) True

Description : The Java interpreter is used for the execution of the source code. a) True b) False

Last Answer : a) True

Description : Write a program to perform following task (i) Create a text file and store data in it. (ii) Count number of lines and words in that file. 

Last Answer : import java.util.*; import java.io.*; class Model6B { public static void main(String[] args) throws Exception { int lineCount=0, wordCount=0; String line = ""; BufferedReader br1 = new BufferedReader(new ... : " + lineCount);  System.out.println("Number of words is : " + wordCount); } }

Description : Explain how to pass parameter to an applet ? Write an applet to accept username in the form of parameter and print “Hello ”.

Last Answer : Passing Parameters to Applet User defined parameters can be supplied to an applet using <PARAM ..> tags. PARAM tag names a parameter the Java applet needs to run, and provides a value for that ... } public void paint(Graphics g) { g.drawString(str,10,100); } }

Description : Write a program to create two threads one thread will print even no. between 1 to 50 and other will print odd number between 1 to 50.

Last Answer : import java.lang.*; class Even extends Thread { public void run() { try { for(int i=2;i<=50;i=i+2) { System.out.println("\t Even thread :"+i); sleep(500); } } ... public static void main(String args[]) { new Even().start(); new Odd().start(); } }

Description : Define package. How to create user defined package? Explain with example.

Last Answer : Java provides a mechanism for partitioning the class namespace into more manageable parts. This mechanism is the package. The package is both naming and visibility controlled mechanism. Package can be created by including package as the first ... []) { Box b=new Box(); b.display(); } } 

Description : Write a program to create a vector with five elements as (5, 15, 25, 35, 45). Insert new element at 2nd position. Remove 1st and 4th element from vector.

Last Answer : import java.util.*; class VectorDemo { public static void main(String[] args) { Vector v = new Vector(); v.addElement(new Integer(5)); v.addElement(new Integer(15)); v.addElement(new ... .addElement(new Integer(45)); System.out.println("Original array elements are "); for(int i=0;i

Description : Differentiate between array and vector.

Last Answer : Array Vector 1) An array is a structure that holds multiple values of the same type. 1)The Vector is similar to array holds multiple objects and like an array; it contains components that ... 6) Array is the static memory allocation. 6) Vector is the dynamic memory allocation.

Description : Explain any two logical operator in java with example.

Last Answer : Logical Operators: Logical operators are used when we want to form compound conditions by combining two or more relations. Java has three logical operators as shown in table: Program demonstrating logical Operators public class ... & b = false a || b = true !(a && b) = true

Description : Explain life cycle of thread.

Last Answer : Thread Life Cycle Thread has five different states throughout its life. 1. Newborn State 2. Runnable State 3. Running State 4. Blocked State 5. Dead State Thread should be in ... it reaches to end of the method. The stop method may be used when the premature death is required. 

Description : Differentiate between class and interfaces

Last Answer : Class Interface 1)doesn't Supports multiple inheritance 1) Supports multiple inheritance 2) extend keyword is used to inherit 2) implements keyword is used ... declaration }  7)syntax Inteface Innterfacename { Final Variable declaration, abstract Method declaration }

Description : Explain life cycle of Applet.

Last Answer : When an applet begins, the AWT calls the following methods, in this sequence: 1. init( ) 2. start( ) 3. paint( ) When an applet is terminated, the following sequence of ... ( ) method is called when the environment determines that your applet needs to be removed completely from memory. 

Description : Explain the following classes. i)Byte stream class ii)Character Stream Class

Last Answer : i)Byte stream class: 1) InputStream and OutputStream are designed for byte streams 2) Use the byte stream classes when working with bytes or other binary objects. 3) Input Stream ... for handling characters in file are FileReader (for reading characters) and FileWriter (for writing characters).

Description : Define a class circle having data members pi and radius. Initialize and display values of data members also calculate area of circle and display it.

Last Answer : class abc {  float pi,radius; abc(float p, float r) { pi=p; radius=r; } void area() { float ar=pi*radius*radius; System.out.println("Area="+ar); } void display() { System.out.println("Pi="+pi ... void main(String args[]) { abc a=new abc(3.14f,5.0f); a.display(); a.area(); } }

Description : Differentiate between String and String Buffer.

Last Answer : String String Buffer c String is a major class String Buffer is a peer class of String Length is fixed (immutable) Length is flexible (mutable)  Contents of object cannot be modified ... new Ex:- String s= abc ; Ex:- StringBuffer s=new StringBuffer ( abc );

Description : List access specifiers in Java.

Last Answer : 1)public 2)private 3)friendly 4)protected 5)Private Protected

Description : List any four Java API packages.

Last Answer : 1.java.lang 2.java.util 3.java.io 4.java.awt 5.java.net 6.ava.applet

Description : Define error. List types of error.

Last Answer : Errors are mistakes that can make a program go wrong. Errors may be logical or may be typing mistakes. An error may produce an incorrect output or may terminate the execution of the program ... crash. Errors are broadly classified into two categories:  1. Compile time errors  2. Runtime errors

Description : List the methods of File Input Stream Class.

Last Answer : • void close() • int read() • int read(byte[] b) • read(byte[] b, int off, int len) • int available()

Description : Define Class and Object.

Last Answer : Class: A class is a user defined data type which groups data members and its associated functions together.  Object: It is a basic unit of Object Oriented Programming and represents the real life ... . A typical Java program creates many objects, which as you know, interact by invoking methods. 

Description : Describe the applet life cycle in detail.

Last Answer : Below is the description of each applet life cycle method: init(): The init() method is the first method to execute when the applet is executed. Variable declaration and initialization operations are ... ) The method execution sequence when an applet is closed is: stop() destroy()

Description : Write a program to input name and salary of employee and throw user defined exception if entered salary is negative. 

Last Answer : import java.io.*; class NegativeSalaryException extends Exception { public NegativeSalaryException (String str) { super(str); } } public class S1 { public static void main(String[] args) ...  catch (NegativeSalaryException a) {  System.out.println(a);  } } }

Description : Explain the command line arguments with suitable example.

Last Answer : Java Command Line Argument: The java command-line argument is an argument i.e. passed at the time of running the java program. The arguments passed from the console can be received in the java program ... [0]); } } compile by > javac CommandLineExample.java run by > java CommandLineExample sonoo

Description : Write a program to create two threads. One thread will display the numbers from 1 to 50 (ascending order) and other thread will display numbers from 50 to 1 (descending order).

Last Answer : class Ascending extends Thread {  public void run() {  for(int i=1; i<=15;i++) {  System.out.println("Ascending Thread : " + i);  } } } class Descending extends Thread { ... a=new Ascending(); a.start(); Descending d=new Descending(); d.start(); } }

Description : Write a program to copy content of one file to another file.

Last Answer : class fileCopy {  public static void main(String args[]) throws IOException { FileInputStream in= new FileInputStream("input.txt"); FileOutputStream out= new FileOutputStream("output.txt"); int c=0; ... .close();  if(out!=null)  out.close();  }  } }

Description : Differentiate between Java Applet and Java Application

Last Answer : Sr. No. Java Applet Java Application 1 Applets run in web pages  Applications run on standalone systems. 2 Applets are not full featured application programs. Applications ... file system and resources. 7 Applets are event driven Applications are control driven.

Description : Explain the four access specifiers in Java.

Last Answer : There are 4 types of java access modifiers: 1. private 2. default 3. Protected 4. public 1) private access modifier: The private access modifier is accessible only within class. 2) ... : The public access specifier is accessible everywhere. It has the widest scope among all other modifiers.

Description : Describe instance Of and dot (.) operators in Java with suitable example.

Last Answer : Instance of operator: The java instance of operator is used to test whether the object is an instance of the specified type (class or subclass or interface).  The instance of in java is also known as type ... by this' keyword c.getdata(); where getdata() is a method invoked on object c'. 

Description : Write a program to count number of words from a text file using stream classes.

Last Answer : import java.io.*; public class FileWordCount { public static void main(String are[]) throws IOException { File f1 = new File("input.txt"); int wc=0; FileReader fr = new FileReader (f1); int c=0; try {  while(c ... of words :"+(wc+1)); } finally {  if(fr!=null)  fr.close();  }  } }

Description : Distinguish between Input stream class and output stream class.

Last Answer : Java I/O (Input and Output) is used to process the input and produce the output. Java uses the concept of a stream to make I/O operation fast. The java.io package contains all ... , Byte Array Output Stream , Filter output Stream, Piped Output Stream, Object Output Stream, DataOutputStream

Description : Explain the two ways of creating threads in Java.

Last Answer : Thread is a independent path of execution within a program. There are two ways to create a thread: 1. By extending the Thread class. Thread class provide constructors and methods to create and perform operations on a ... = new MyThread();  Thread t = new Thread(m);  t.start();  } }

Description : Explain the types of constructors in Java with suitable example.

Last Answer : Constructors are used to initialize an object as soon as it is created. Every time an object is created using the new' keyword, a constructor is invoked. If no constructor is defined in a class, java compiler creates ... of First Second Rectangle : "+ (r1.length*r1.breadth)); } }

Description : Explain the concept of platform independence and portability with respect to Java language.

Last Answer : Java is a platform independent language. This is possible because when a java program is compiled, an intermediate code called the byte code is obtained rather than the machine code. Byte code is a highly ... of the JVM will defer from platform to platform, all interpret the same byte code.

Description : List the types of inheritances in Java.

Last Answer : Types of inheritances in Java: i. Single level inheritance ii. Multilevel inheritance iii. Hierarchical inheritance iv. Multiple inheritance v. Hybrid inheritance

Description : List any eight features of Java.

Last Answer : Features of Java: 1. Data Abstraction and Encapsulation 2. Inheritance 3. Polymorphism 4. Platform independence 5. Portability 6. Robust 7. Supports multithreading 8. Supports distributed applications 9. Secure 10. Architectural neutral 11. Dynamic