Copyright © 3S Technologies
Best viewed in Internet Explorer ver. 9.0 - 1024 X 768 resolution.
Java Objectives - Part 3
The reason for documenting this topic is to provide important technical questions and answers on Java language. These questions can appear in an aptitude test, organized by different recruiting IT organizations.
Some questions and answers on packages and interface:
1) What are packages ? What is use of packages ?
Ans: The package statement defines a name space in which classes are stored.If you omit the package, the classes are put into the default package.
Signature... package pkg;
Use: * It specifies to which package the classes defined in a file belongs to. * Package is both naming and a visibility control mechanism.
2) What is difference between importing "java.applet.Applet" and "java.applet.*;" ?
Ans: "java.applet.Applet" will import only the class Applet from the package java.applet
Where as "java.applet.*" will import all the classes from java.applet package.
3) What do you understand by package access specifier?
public: Anything declared as public can be accessed from anywhere
private: Anything declared in the private can’t be seen outside of its class.
default: It is visible to subclasses as well as to other classes in the same package.
4) What is interface? What is use of interface?
Ans: It is similar to class which may contain method’s signature only but not bodies.
Methods declared in interface are abstract methods. We can implement many interfaces on a class which support the multiple inheritance.
5) Is it is necessary to implement all methods in an interface?
Ans: Yes. All the methods have to be implemented.
6) Which is the default access modifier for an interface method?
7) Can we define a variable in an interface ? and what type it should be ?
Ans: Yes we can define a variable in an interface. They are implicitly final and static.
8) What is difference between interface and an abstract class?
Ans: All the methods declared inside an Interface are abstract. Where as abstract class must have at least one abstract method and others may be concrete or abstract.
In Interface we need not use the keyword abstract for the methods.
9) By default, all program import the java.lang package. True/False ?
10) Java compiler stores the .class files in the path specified in CLASSPATH environmental variable. True/False ?
11) User-defined package can also be imported just like the standard packages.
12) When a program does not want to handle exception, the ______class is used.
13) The main subclass of the Exception class is _______ class.
14) Only subclasses of ______class may be caught or thrown.
15) Any user-defined exception class is a subclass of the _____ class.
16) The catch clause of the user-defined exception class should ______ its
Base class catch clause.
17) A _______ is used to separate the hierarchy of the class while declaring an
18) All standard classes of Java are included within a package called _____.
19) All the classes in a package can be simultaneously imported using ____.
20) Can you define a variable inside an Interface. If no, why? If yes, how?
Ans.: YES. final and static
21) How many concrete classes can you have inside an interface?
22) Can you extend an interface?
23) Is it necessary to implement all the methods of an interface while implementing the interface?
24) If you do not implement all the methods of an interface while implementing , what specifier should you use for the class ?
25) How do you achieve multiple inheritance in Java?
Ans: Using interfaces.
26) How to declare an interface example?
Ans: access class classname implements interface.
27) Can you achieve multiple interface through interface?
28) Can variables be declared in an interface ? If so, what are the modifiers?
Ans: Yes. final and static are the modifiers can be declared in an interface.
29) What are the possible access modifiers when implementing interface methods?
30) Can anonymous classes be implemented an interface?
31) Interfaces can’t be extended.
32) Name interfaces without a method?
Ans: Serializable, Cloneble & Remote.
33) Is it possible to use few methods of an interface in a class ? If so, how?
Ans: Yes. Declare the class as abstract.
Some questions and answers on Exception Handling:
1) What is the difference between ‘throw’ and ‘throws’ ?And it’s application?
Ans: Exceptions that are thrown by java runtime systems can be handled by Try and catch blocks. With throw exception we can handle the exceptions thrown by the program itself. If a method is capable of causing an exception that it does not
handle, it must specify this behavior so the callers of the method can guard
against that exception.
2) What is the difference between ‘Exception’ and ‘Error’ in java?
Ans: Exception and Error are the subclasses of the Throwable class. Exception class is used for exceptional conditions that user program should catch. With exception class we can subclass to create our own custom exception.
Error defines exceptions that are not excepted to be caught by you program. Example is Stack Overflow.
3) What is ‘Resource leak’?
Ans: Freeing up other resources that might have been allocated at the beginning of a method.
4)What is the ‘finally’ block?
Ans: Finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also execute.
5) Can we have catch block with out try block? If so when?
Ans: No. Try/Catch or Try/finally form a unit.
6) What will happen to the Exception object after exception handling?
Ans: It will go for Garbage Collector. And frees the memory.
7) How many Exceptions we can define in ‘throws’ clause?
Ans: We can define multiple exceptions in throws clause.
type method-name (parameter-list) throws exception-list
8) The finally block is executed when an exception is thrown, even if no catch matches it. True/False ?
9) The subclass exception should precede the base class exception when used within the catch clause. True/False?
10) Exceptions can be caught or rethrown to a calling method. True/False?
11) The statements following the throw keyword in a program are not executed. True/False ?
12) The toString() method in the user-defined exception class is overridden. True/False ?
Some questions and answers on Multithreading:
1) What are the two types of multitasking?
2) What are the two ways to create the thread?
Ans: 1.by implementing Runnable
2.by extending Thread
3) What is the signature of the constructor of a thread class?
Ans: Thread(Runnable threadob,String threadName)
4) What are all the methods available in the Runnable Interface?
5) What is the data type for the method isAlive() and this method is
available in which class?
Ans: boolean, Thread
6) What are all the methods available in the Thread class?
7) What are all the methods used for Inter Thread communication and what is the class in which these methods are defined?
Ans:1. wait(),notify() & notifyall()
2. Object class
8) What is the mechanism defined by java for the Resources to be used by only one Thread at a time?
9) What is the procedure to own the monitor by many threads?
Ans: not possible
10) What is the unit for 1000 in the below statement?
Ans: long milliseconds
11) What is the data type for the parameter of the sleep() method?
12) What are all the values for the following level?
13) What is the method available for setting the priority?
14) What is the default thread at the time of starting the program?
Ans: main thread
15) The word synchronized can be used with only a method.True/ False ?
16) Which priority Thread can prompt the lower primary Thread?
Ans: Higher Priority
17) How many threads at a time can access a monitor?
18) What are all the four states associated in the thread?
Ans: 1. new 2. runnable 3. blocked 4. dead
19) The suspend()method is used to terminate a thread. True /False ?
20) The run() method should necessary exist in classes created as subclass of thread. True /False ?
21) When two threads are waiting on each other and can't proceed the programe is said to be in a deadlock. True/False ?
22) Which method waits for the thread to die ?
Ans: join() method
23) Which of the following is true?
1) wait(),notify(),notifyall() are defined as final & can be called only from within a synchronized method
2) Among wait(),notify(),notifyall() the wait() method only throws IOException
3) wait(),notify(),notifyall() & sleep() are methods of object class
d) 1 & 2
e) 1,2 & 3
24) Garbage collector thread belongs to which priority?
25) What is meant by time slicing or time sharing?
Ans: Time slicing is the method of allocating CPU time to individual threads in a priority schedule.
26) What is meant by daemon thread? In java runtime, what is it's role?
Ans: Daemon thread is a low priority thread which runs intermittently in the background doing the garbage collection operation for the java runtime system.
Online Education for all