Assignment Questions What Is The Difference Between A Constructor and A Method?
Assignment Questions What Is The Difference Between A Constructor and A Method?
A constructor is a member function of a class that is used to create objects of that class. It
has the same name as the class itself, has no return type, and is invoked using the new
operator.
A method is an ordinary member function of a class. It has its own name, a return type
(which may be void), and is invoked using the dot operator.
2. What is an Iterator?
Some of the collection classes provide traversal of their contents via a java.util.Iterator
interface. This interface allows you to walk through a collection of objects, operating on
each object in turn. Remember when using Iterators that they contain a snapshot of the
collection at the time the Iterator was obtained; generally it is not advisable to modify the
collection itself while traversing an Iterator.
The List and Set collections provide iterators, which are objects that allow going over all
the elements of a collection in sequence. The java.util.Iterator<E> interface provides for
one-way traversal andjava.util.ListIterator<E> provides two-way traversal. Iterator<E> is
a replacement for the olderEnumeration class which was used before collections were
added to Java.
Iterators are created by calling the iterator() or listIterator() method of a List, Set,
or other data collection with iterators.
3. What is static in java?
Static means one per class, not one for each object no matter how many instance of a
class might exist. This means that you can use them without creating an instance of a
class.Static methods are implicitly final, because overriding is done based on the type of
the object, and static methods are attached to a class, not an object. A static method in a
superclass can be shadowed by another static method in a subclass, as long as the original
method was not declared final. However, you can't override a static method with a
nonstatic method. In other words, you can't change a static method into an instance
method in a subclass.
No. A top level class can not be private or protected. It can have either "public" or no
modifier. If it does not have a modifier it is supposed to have a default access.If a top
level class is declared as private the compiler will complain that the "modifier private is
not allowed here". This means that a top level class can not be private. Same is the case
with protected.
5. What are wrapper classes?
Wrapper classes are classes that are used to make primitive variables into objects, and to
make wrapped objects into primitives. int, boolean, double are all primitive data types
and their respective wrapper classes are Integer, Boolean, and Double. Wrapper classes
are useful in storing primitive data types in higher level data structures such as
Stack<Object>, List<Object>, Queue<Object>, since primitives cannot be directly placed
in these data structures they must be boxed in the wrapper classes. But, with the new
Java 5.0, there is no need no worry about wrapper classes and boxing and unboxing ,
since there is auto-boxing and unboxing, therefore, one can directly "add" primitives to a
data structure and let the JVM do the rest.
A checked exception is some subclass of Exception (or Exception itself), excluding class
RuntimeException and its subclasses.Making an exception checked forces client programmers to
deal with the possibility that the exception will be thrown. eg, IOException thrown by
java.io.FileInputStream's read() method·Unchecked exceptions are RuntimeException and any of
its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception,
however, the compiler doesn't force client programmers either to catch theexception or declare it
in a throws clause. In fact, client programmers may not even know that the exception could be
thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method· Checked
exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often
cannot be.
1. Exception is coverable
2. Error is uncoverable
3. Exception can be handled
4. and Error can not be handled
11. Note
12. When Error occur then our programe stops and donot run
13. but If exception occur then nothing happining only try block
14. will be suspended and after that if any code is written that
15. will be executed properly means our system or programe work
16. properly.
17. If my class already extends from some other class what should I do if I want an
instance of my class to be thrown as an exception object?
Java does not allow multiple inheritance and does not provide any exception interface as
well.
18. If I write System.exit (0); at the end of the try block, will the finally block still
execute?
No in this case the finally block will not execute because when you say System.exit (0);
the control immediately goes out of the program, and thus finally never executes.
19. If I write return at the end of the try block, will the finally block still execute?
Yes even if you write return as the last statement in the try block and no exception
occurs, the finally block will execute. The finally block will execute and then the control
return.
20. Does garbage collection guarantee that a program will not run out of
memory?
Garbage collection does not guarantee that a program will not run out of memory. It
ispossible for programs to use up memory resources faster than they are garbage
collected.It is also possible for programs to create objects that are not subject to garbage
collection
21. Can a .java file contain more than one java classes?
Yes, it can. However, there can only be one public class per .java file, as public classes must
have the same name as the source file.
The purpose of including multiple classes in one source file is to bundle related support
functionality (internal data structures, support classes, etc) together with the main public class.
No, delete is not a keyword in Java. Java does not make use of explicit destructors the
way C++ does.
23. What will be the output of the following statement?
System.out.println ("1" + 3);
Ans. 13
24. What will be the default values of all the elements of an array defined as an instance
variable?
If the array is an array of primitive types, then all the elements of the array will be initialized to
the default value corresponding to that primitive type. e.g. All the elements of an array of int will
be initialized to 0, while that of boolean type will be initialized to false. Whereas if the array is
an array of references (of any type), all the elements will be initialized to null.
27. Can you make a method final, without making the whole class final?
Yes.
e.g.
{ return name; }
In the above class only the display() method is 'final'. we can override the method
'getName()' in the derived class but we can't override display(String) method.
object :means when memory location is associated with the object( is a runtime entity of
the class) by using the new operator
2) Data growth - Internally both the ArrayList and Vector hold onto their contents using
an Array. When an element is inserted into an ArrayList or a Vector the object will need
to expand its internal array if it runs out of room. A Vector defaults to doubling the size
of its array while the ArrayList increases its array size by 50 percent.
Yes, the main method can be declared final, in addition to being public static.
32. Why use interfaces to develop Java applications?
It is advisable to design relatively large applications using interfaces because it makes the
whole system easier to modify, extend and integrate new features. To start with, you may
only have one implementation of a given interface, but if you find you need slightly
different behaviour in special circumstances, you only need write a class that conforms to
one of the existing interfaces and it will drop in place without major modifications.
Interfaces also allow you to adapt a class from a different hierarchy to work in an existing
application. The class only needs to declare it implements the interface, provide the
necessary methods and it can be integrated directly as if it were created for the job.