Previous | Home | Next |
Answer: Yes it is possible only when main() class should be declared as public and static. here the example
packager4r.co.in; classNewclass { //thisismainclasscalledasNewclass publicstaticvoidmain(Stringargs[]) { System.out.println("thisismainclass"); } } publicclassInvokedclass //thisissubclasscalledasInvokedclass { publicstaticvoidmain(Stringargs[]) { System.out.println("thisisinvokedclasswhichinvokedNewclass"); Newclass.main(args); } } //saveandcompiletheprogrambynameofInvokedclass.java
this is invokedclass which invoked Newclass
this is mainclass
Q.2 What is the difference between java command line arguments and C command line arguments?
Answer: The equals( ) method compares the characters inside a String object while the == operator compares two object references which refer to the same instance.
Example:
//ComparisonoftwoStrings packager4r.co.in; classCompareStrings{ publicstaticvoidmain(Stringargs[]){ Strings1=newString("Hello"); Strings2=newString("Hello"); System.out.println(s1+"equals"+s2+":"+s1.equals(s2)); System.out.println(s1+"=="+s2+":"+(s1==s2)); } } //saveandcompiletheprogrambynameofCompareStrings.java
Hello equals Hello : true
Hello == Hello : false
Answer: Any class that contains one or more abstract methods must be declared abstract by putting the abstract keyword in front of the class and at the beginning of theclass declaration, remember there is no objects of an abstract class. Abstract class method can't be overriding in a another class because an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined.
Through the use of the interface keyword, Java allows to fully abstract the interface from its implementation. Interface is a concept work on sharing the data in superclass ( or Parent class) to subclass( or child class) ,but non of class define protected and abstract. The interface, itself, does not actually define any implementation.
Q.5 What is singleton class & how you can implementation it?.
Answer: Any class which is declare to be static as use a keyword static. The example of a static member is main( ).
main( ) is declared as static because it must be called before any objects exist, while instance variables declared as static are used as global variables. Methods declared as static have several restrictions:
- They can only call other static methods.
- They must only access static data not dynamic data.
- They can't refer to this or super in any way.
- As the program is compile static member ,variable or class is loaded first than dynamic class.
A variable can be declared as final, class can't declare final means a final variable must be initialize a final when it is declared. Final Variables don't occupy memory on per-instance basis. Hence, a final variable is must be constant.
Answer: Final class is only use for prevent a class from being inherited. By declaring a class as final as use final keyword in front of class implicitly declares all of its methods as final too. But, it is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and depend upon its subclasses to provide complete implementations.
//Followingexampleconsiderforfinalclass packager4r.co.in; finalclassA { //followingclassshouldbedeclaretofinalcan'tbeinheritance inta=5; //valuewhichdeclareintointshouldbeconstant Floatb=(float)3.45; } classBextendsA{ publicstaticvoidmain(String[]args){ } }
Q.8 What is difference between Event propagation & Event delegation?
Q.9 What is difference between Unicast & Multicast model?
Answer: A Java Bean is a software component that is designed for the reusable in a variety of different environments and its architecture is totally based on a component which enables programmers to create software.
Components are self-contained, reusable software units that can be visually assembled into composite components, applets, applications, and servlets using visual application builder tools. Also, a bean is use to perform a function like checking the spelling of a document, a complex function such as forecasting the performance of a stock portfolio.
Beans expose properties so they can be customized at design time. Customization is supported in two ways:
- by using property editors.
- by using more sophisticated bean customizers.
Answer: The synchronized keyword can be used for locking in program or utilize for lock/ free the resource for any thread/program. When two or more threads/program want to access a shared resource, they need someway to ensure that the resource will be use by only one thread at a time.
The process by which this is achieved is called synchronization.
//Createmultiplethreadsusingsynchronizationmethod. packager4r.co.in; classMYThreadimplementsRunnable{ Stringname; //nameofthread Threadt; MYThread(Stringthreadname){ name=threadname; t=newThread(this,name); System.out.println("Mythread:"+t); t.start(); //ThreadStartforexecution } synchronizedpublicvoidrun(){ //Initializationofthread. try{ for(inti=0;i<10;i++){ System.out.println(name+":"+i); Thread.sleep(100); //threadsleep } }catch(Exceptione){ System.out.println(name+e); } System.out.println(name+"exiting."); } } classMultiThreadDemo{ publicstaticvoidmain(Stringargs[]){ newMYThread("One"); newMYThread("Two"); try{ Thread.sleep(1000); //Threadsleep }catch(InterruptedExceptione){ System.out.println("MainthreadInterrupted"); } System.out.println("Mythreadexiting."); } }
Q.12 What are the restrictions of an applet & how to make the applet access the local machines resources?
Answer: The java.lang.reflect.* package provides the ability to obtain information about the fields, constructors, methods, and modifiers of a class, also includes a class that access arrays dynamically.
//simple example consider for how reflection works, package r4r.co.in; import java.lang.reflect.*; public class Reflection { public static void main(String args[]) { try { Class c = Class.forName("java.lang.String"); // Here String is loaded class. Method m[] = c.getDeclaredMethods(); // Here get method implemented System.out.println(m[0].toString()); for (int i = 0; i < m.length; i++) { System.out.println(m[i].toString()); } } catch (Exception e) { System.out.println(e); } }
Answer: process of writing the state of an object to a byte stream is called serialization, also, it is used to save the program to persistent storage area, like a file and easily restore using Deserialization process.
Answer: Java does not support return-type-based method overloading. Method can be overload either same name and same signatures( argument/parameter).
Answer: Some time an object( which is lost there object reference) is holding some java resource like file handle ,character font, need to free from the resource before that object is collecting by JVM( Java Virtual machine) or say garbage collection.
Then to finalize the class, simply define the keyword protected finalize( )method( protected prevent access of final method) in front of class, Java at the runtime call this object's resource and JVM easily recycle that class.
Answer: AWT are heavy weight components while Swing are light weight components because AWT is platform dependent while Swing is not dependent any of the platform. Alternate, AWT( heavyweight component ) is one that is associated with its own native screen resource (commonly known as a peer) while Swing( lightweight component ) is one that "borrows" the screen resource of an ancestor ( means it has no native resource of its own).
Answer: Yes, since all AWT components are heavyweight and all Swing components are lightweight (except for the top-level ones: JWindow, JFrame, JDialog, and JApplet), these differences apparent when start mixing Swing components with AWT components.
Answer: Yes. because Swing are platform independent independent
Q.20 What should you do get your browser compatible with swing components?
Answer: Applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a Web document.
Some Applet method which frequently used:
Answer: Int() method is called by applet viewer or browser at the time of load applet application into system while Start() method is called by the applet viewer or browser at the time of when applet is complete loaded into system and start its execution.
Answer: Applet can be navigate with other by appletcontext() method.
Answer: Java applets are typically executed by Web browsers with embedded Java Virtual Machines (VMs) and runtime class libraries. Applets are downloaded by the browser and then executed by the built-in VM on the machine running the browser.
The security of the system depends upon the Java language itself, the runtime class libraries, and the Security Manager of the browser. Applets are of two types:
Trusted Applet: Java virtual machines( JVM ) run applets under a different security regimerefers to a set of conditions than applications. By default, applications are implicitly trusted. The designers of the JVM specification assumed that users start applications at their own initiative and can therefore take responsibility for the application's behavior on their machine. Such code is considered to be trusted
Untrusted Applet: The applet which started automatically by the browser after it downloads and displays a page. Users can't be expected to know what applets a page might contain before they download it, and therefore cannot take responsibility for the applet's behavior on their machine. Applets, therefore, are considered by default to be untrusted.
Answer: An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error must be checked and handled manually. Exceptions can be generated by the Java run-time system (or java virtual machine, JVM ), or they can be manually generated by your code.
Exception is of two type:
- Check Exception or Compiletime Exception. An exception which generate by programming error like user written program or code and must be handle at time of compilation of program.
- Uncheck Exception or Runtime Exception. An exception which can generate at the runtime of program, the compiler doesn’t force the programmers to catch the exception.
Answer: When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself, or pass it on. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Try block is used to monitor for exceptions, Catch block is used to catch this exception and handle it in some rational manner. To manually throw an exception, use the keyword throw. Any code that must be executed before a method returns is put in a finally block.
The following syntax represent exception handling
try { // block of code to monitor for errors } catch (Exception exObject) { // exception handler for ExceptionType1 } finally { // block of code to be executed before try block ends }
Answer: Try block is used to monitor for exceptions, Catch block is used to catch this exception and handle it in some rational manner.
The following syntax represent exception handling
try { // block of code to monitor for errors } catch (Exception exObject) { // exception handler for ExceptionType1 }
Answer: The finally method will be executed after a try or catch execution, and mainly used to free up resources. Any code that must be execute before a method returns is put in a finally block.
Answer: In java four access modifier is used describe below:
Access Modifiers* | Class | Nested class | Subclass | Other packages |
public | Yes | Yes | Yes | Yes |
protected | Yes | Yes | Yes | No |
private | Yes | No | No | No |
package (default) | Yes | Yes | Yes | No |
* The following access modifiers apply to script and instance member like instance functions, script functions, instance variables, and script variables.
Answer: Yes, If more than one of your threads running at the same time and access shared objects, then a synchronized keyword is placed in front of Run method, so synchronized should be modify according to the object that call run method.
Answer: Generally, polymorphism is a concept in which a subclass( or child class) can be easily inherited the member (like method, parameter and signature) of superclass (or parent class) but not vice-versa. Polymorphism may be of two kind-
- Method overloading
- Method overriding
Q.32 What is inheritance ?
Answer: when two methods and operator (within different class) have the same name and same arguments , but different signature, it is known as overloading in Object oriented concepts.
Two types of are:
- Operator overloading is a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments.
- Function overloading or method overloading is allows to creation the several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function. Method overloading is usually associated with statically-typed programming languages which enforce type checking in function calls.
Answer: In overriding, a method in a super class is overridden in the subclass. The method in the subclass will have the same signature as that of the superclass. Since the method in the subclass has the same signature & name as the method of its super class, it is termed as overriding.
// example for method overriding package r4r.co.in; class number { int i, j; number(int a, int b) { i = a; j = b; System.out.println("i and j: " + i + " " + j); } } class numbertest extends number { // Create a subclass by extending class A. int k; numbertest(int a, int b, int c) { super(a, b); k = a + b + c; //airthmetic sum System.out.println("value of k:" + k); } } class Override { public static void main(String args[]) { numbertest b = new numbertest(1, 2, 3); //initilization numbertest } }
Answer: Yes, java supported multi dimension array.
Example:
//Syntax for defining multidimensional array var array = new Array(3); for (var i = 0; i < 3; i++) { array[i] = [' ', ' ', ' ']; }//initialization of an array
array[0][2] = 'x'; array[1][1] = 'x'; array[2][0] = 'x';
Answer: Java doesn't support multiple inheritance is direct form due to arise the problem of ambiguity( Doubtfulness or uncertainty as regards interpretation), but it support it in term of interface.
Answer: Jesperreport.
Answer: A class is a specification (think of it as a blueprint or pattern and a set of instructions) of how to construct something.
Answer: A method declaration is the heading of a method containing the name of the method, its parameters, and its access level.
Answer: OOPs use three basic concepts as the fundamentals for the programming language: classes, objects and methods. Additionally, Inheritance, Abstraction, Polymorphism, Event Handling and Encapsulation are also significant concepts within object-oriented programming languages.
Answer: Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.
Answer: Inheritance means to take something that is already made. It is one of the most important features of Object Oriented Programming. It is the concept that is used for reusability purpose. Inheritance is the mechanism through which we can derive classes from other classes.
The derived class is called as childclass or the subclass or we can say the extended class and the class from which we are deriving the subclass is called the base class or the parent class. To derive a class in java the keyword extends is used.
Answer: A small program with interpret java byte code to machine languages.
Previous | Home | Next |