Wednesday, February 4, 2009

Fundamental of JAVA 2

<-Pre HomeNext->
What is the return type of the main method?
Main method doesn't return anything hence declared void.

Why is the main method declared static?   *IMP*
main method is the entry point for a Java application and main method is called by the JVM even before the instantiation of the class hence it is declared as static. static methods can be called even before the creation of objects

What is the argument of main method?
Accepts an array of String objects as argument.

How to pass an argument to main method?
You should pass the argument as a command line argument. Command line arguments are seprated by a space. The following is an example:

java Test Hi World

In the above command line Test is the class, Hi is the first argument and World is the second argument.

What will happen if no argument is passed to the main method?
If you dont access the argument, the main method will execute without any problem. If try to access the argument, NullPointerException will be thrown.

Can a main method be overloaded?
Yes. You can have any number of main methods with different method signature and implementation in the class.

Can a main method be declared final?
Yes. Any inheriting class will not be able to have it's own default main method.

Does the order of public and static declaration matter in main method?    *IMP*
No it doesn't matter.

Can a source file contain more than one Class declaration?
Yes. A single source file can contain any number of Class declarations but only one of the class can be declared as public.

If a source file has 2 class declaration in it, on compilation how many class files will be created?
2 class files will be generated by the compiler.

Can the source file name and the class name in the file be different?
If the class in the source is not of public access, then the name can be anything but should confirm to identifier rules.

Can the first line of the source code be a comment?
Yes. Comments can appear anywhere in the code. It is just a "skip this line" instruction to the conpiler.

Which keyword is used for inheriting from another class?
extends keyword is used.

Can a subclass be referenced for a super class object?
No. If Vehicle is super class and Bus is the subclass then the following reference would be wrong – Bus c = new Vehicle();

Can a parent class be referenced for a subclass object?     *IMP*
Yes. The below is an example  :
Vehicle v = new Bus();

Can an interface be referenced for any object?
Yes. We can create a object reference for an interface. The object should have provided the implementation for the object.
Runnable r = new Test();
Test class should have implemented the runnable interface and overridded the run method.

^Back to top

What are constructors?
Constructors are used to initialise an object. Constructors are invoked when the new operator on the class are called.  Constructors doesn’t return anything – not even void.

What is a default constructor?
Default constructor is a no argument constructor which initialises the instance variables to their default values. This will be provided by the compiler at the time of compilation. Default constructor will be provided only when you don’t have any constructor defined.

What are the decalaration of a constructor?
Constructor name should be the same as class name.

Does constructors throw exceptions?
Yes. Like methods constructors can also throw exceptions.

Can constructors be overloaded like other method of the class?
Yes. Constructors can be overloaded.

Is it compulsory to define a constructor for a class?
No. If you don’t define a constructor, the compiler will provide a default constructor.

Explain about “this” operator?
“this” is used to refer the currently executing object and it’s state. “this” is also used for chaining constructors and methods. 

Explain about “super” operator?
“super” is used to refer to the parent object of the currently running object. “super” is also to invoke super class methods and classes.

Waht is Polymorphism?
Polymorphism - Poly(many)-morphism (forms)
Polymorphism is a term that describes a situation where one name may refer to different methods. In java there are two type of polymorphism: overloading type and overriding type.

What is Overriding? *IMP*
subclass inherits a method from a superclass and subclass redefine that method keeping name, return type and argument same as that of superclass method is called overrding.

Which package is imported by default?
java.lang package is imported by default even without a package declaration.

What will happen if there is no package for Java source file?
The classes will packaged into a no name default package. In practice, we always put classes into a meaningful package.

What do you mean by * during importing(for example import java.util.*;? *IMP*
When a * is used in a import statement, it indicates that all the class in the package java.util. can be used in the current source. you can use concurrent class by importing java.util.*;(i.e. java.util.concurrent ) but not java.util.concurrent.atomic.For using this you need to import java.util.concurrent.* .

Is it possible to declared class as private or protected. *IMP*
A class can't be declared as private or protected.

What is the access scope of a protected method?

A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.

What is the impact of marking a constructor as private?
Oject cann't be created using that constructor.Give compile time error saying XYZ() has private access in XYZ.

What is meant by default access? *IMP*
default access means the class,method,construtor or the variable can be accessed only within the package.

^Back to top



Is default a keyword?
Yes. default is a keyword but is associated with switch statement not with access specifiers.

Then how to give default access to a class?
If you don't providing any access specifier, then it means the class is of default access.

Is it possible to give two access specifiers to a method at the same time?
No its not possible. It will result in compile time error, stating illegle combination of modifiers .


Previous Next



*****Please feel free to give feedback at shrawan.iitg@gmail.com and comments below *****

No comments:

Post a Comment