0% found this document useful (0 votes)
46 views

Assignment Questions What Is The Difference Between A Constructor and A Method?

A constructor is a member function that creates objects and has no return type, while a method is an ordinary member function that may have a return type. An iterator allows traversal of a collection's contents and is created by calling iterator() or listIterator(). Static means associated with a class rather than objects, and a top level class cannot be private or protected - it must be public or have no access modifier.

Uploaded by

Suman Rathore
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Assignment Questions What Is The Difference Between A Constructor and A Method?

A constructor is a member function that creates objects and has no return type, while a method is an ordinary member function that may have a return type. An iterator allows traversal of a collection's contents and is created by calling iterator() or listIterator(). Static means associated with a class rather than objects, and a top level class cannot be private or protected - it must be public or have no access modifier.

Uploaded by

Suman Rathore
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment Questions

1. 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.

4. Can a top level class be private or protected?

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.

6. What are checked and unchecked exceptions?

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.

7. What is the difference between error and an exception?

1. Exception is coverable
2. Error is uncoverable
3. Exception can be handled
4. and Error can not be handled

5. Exceptio is an run time error when an abnormal condition is


6. occured the jvm detect it and make the object of that
7. abnormal condition and throws it to its own class and that
8. object is catched by catched block and catch block handled it

9. Error is the compile time error not runtime and it is not


10. covered it is only fixed by a programer.

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.

22. Is delete a keyword in Java?

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.

25. What is upward and downward casting in Java?


In upward casting small datatype is casted in to big data type such as byte is casted as int
type.but in downward casting the big data type is casted into small data type such as long
is casted into int or byte.

26. What is Anonymous class,Singleton Class and Assertions

Anonymous: instantiating the class without object name.


Singleton: it can instantiated by only one time.

Assertions: to show warning to the user.

27. Can you make a method final, without making the whole class final?
Yes.
e.g.

public class MyClass

private String name Ram ;

public String getName()

{ return name; }

final void display(String str)


{

System.out.println( Hai str);

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.

28. What is the difference between an object and an instance?

Instance means just creating a reference(copy) .

object :means when memory location is associated with the object( is a runtime entity of
the class) by using the new operator

29. Difference between Vector and ArrayList?

1) Synchronization - ArrayList is not thread-safe whereas Vector is thread-safe. In Vector


class each method like add() get(int i) is surrounded with a synchronized block and thus
making Vector class thread-safe.

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.

30. Is Empty .java file a valid source file?

Yes, an empty .java file is a perfectly valid source file.


31. Can main method be declared final?

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.

33. Why constructor is not inherited.


Constructors, static initializers, and instance initializers are not members and therefore
are not inherited.

You might also like