Which class is extended by all other classes?
The Object class is extended by all other classes
What is the purpose of the System class?
The purpose of the System class is to provide access to system resources
Which package is always imported by default?
The java.lang package is always imported by default.
If a class is declared without any access modifiers, where may the class be accessed?
A class that is declared without any access modifiers is said to have default package access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.
How are this and super used?
this is used to refer to the current object instance. super is used to refer to the variables and methods of the superclass of the current object instance.
Can a class extend many classes?
No. A class can extend only one class
Can an abstract class be final?
An abstract class may not be declared as final.
Can there be an abstract class with no abstract methods in it?
yes.
Can an Interface be final?
yes.
yes.
Can an Interface have an inner class?
Yes public interface abc { static int i=0; void dd(); class a1 { a1() { int j; System.out.println("in interfia"); }; public static void main(String a1[]) { System.out.println("in interfia"); } } }
What modifiers are allowed for methods in an Interface?
Only public and abstract modifiers are allowed for methods in interfaces.
Does interface provide implementation?
No. Interfaces are limited to public methods and constants with no implementation
Can a class implement several interfaces?
yes
What interface must an object implement before it can be written to a stream as an object? An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.
How many methods in the Serializable interface?
There is no method in the Serializable interface. The Serializable interface acts as a marker, telling the object serialization tools that your class is serializable.
How many methods in the Externalizable interface?
There are two methods in the Externalizable interface. You have to implement these two methods in order to make your class externalizable.
These two methods are
1. readExternal()
2. writeExternal().
What is the difference between Serializalble and Externalizable interface?
When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.
What is a transient variable in Java?
A transient variable is a variable that may not be serialized. If you don't want some field to be serialized, you can mark that field transient or static
what is Constructor overloading:-
Writing two (or) more constructors with the same name but with difference in parameters is called constructor overloading.
Constructor returns a value but, what ?
No.The default return type of a constructor is the object of the same class. You cannot make a constructor explicitly return any other data type.
what is the purpose of constructor?
The purpose of the constructor is to instantiate the class
Can constructor perform other tasks instead of initialization?
Yes, like object creation, starting a thread, calling method etc. You can perform any operation in the constructor as you perform in the method
why main method is static?
Ans) because object is not required to call static method. if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation
Can we override static method?
No, static method cannot be overridden. It can be proved by runtime polymorphism
Why we cannot override static method?
because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.
Can we override java main method?
No, because main is a static method.It can be overloaded
What is the difference between method Overloading and Method Overriding?
There are three basic differences between the method overloading and method overriding. They are as follows:
Method Overloading
|
Method Overriding
|
1) Method overloading is used to increase the readability of the program.
|
Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
|
2) method overloading is performed within a class.
|
Method overriding occurs in two classes that have IS-A relationship.
|
3) In case of method overloading parameter must be different.
Is final method inherited? Ans) Yes, final method is inherited but you cannot override it. For Example:
class Bike
{
final void run()
{
System.out.println("running...");
}
}
class Honda extends Bike
{
public static void main(String args[])
{
new Honda().run();
}
}
Output:running..
|
In case of method overriding parame
|
What are the different ways to create an object in Java?
There are many ways to create an object in java. They are:
· By new keyword
· By newInstance() method
· By clone() method
- By factory method etc.
Can we overload main() method?
Yes, bymethod overloading. You can have any number of main methods in a class by
method overloading. Let's see the simple example:
class Simple
{
public static void main(int a)
{
System.out.println(a);
}
public static void main(String args[])
{
System.out.println("main() method invoked");
main(10);
}
}
{
public static void main(int a)
{
System.out.println(a);
}
public static void main(String args[])
{
System.out.println("main() method invoked");
main(10);
}
}
Can we start a thread twice?
No. After staring a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown.
What are the high-level thread states?
The high-level thread states are ready, running, waiting, and dead
What method must be implemented by all threads?
All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.
What are three ways in which a thread can enter the waiting state?
A thread can enter the waiting state by invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.
How can a dead thread be restarted?
A dead thread cannot be restarted
What if we call run() method directly instead start() method?
- Each thread starts in a separate call stack.
- Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.
What are the different identifier states of a Thread?
The different identifiers of a Thread are:
R - Running or runnable thread
S - Suspended thread
CW - Thread waiting on a condition variable
MW - Thread waiting on a monitor lock
MS - Thread suspended waiting on a monitor lock
What is an object's lock and which objects have locks?
An object's lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object's lock. All objects and classes have locks. A class's lock is acquired on the class's Class object
what are the different methods used in thread?
start() - method is used to start the execution of the thread
wait(), sleep(), aLiveThread.join() -used to leave the running state
start()-registers a thread in thread scheduler
wait()-stops directly the execution of thread
notify(),notifyall(), wait(long msecs)- methods of object class
notify()-wakes up a single thread that is waiting on objects monitor
When a thread executes an object's wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object's notify() or notifyAll() methods.
What happens when you invoke a thread's interrupt method while it is sleeping or waiting?
When a task's interrupt() method is executed, the task enters the ready state. The next time the task enters the running state, an InterruptedException is thrown.
What is the difference between notify() and notifyAll()?
notify() is used to unblock one waiting thread; notifyAll() is used to unblock all of them.
What state does a thread enter when it terminates its processing?
When a thread terminates its processing, it enters the dead state
What's new with the stop(), suspend() and resume() methods in JDK 1.2?
The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.
What happens when a thread cannot acquire a lock on an object?
If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object's lock, it enters the waiting state until the lock becomes available
How does multithreading take place on a computer with a single CPU?
The operating system's task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.
What is thread?
A thread is an independent path of execution in a system.
What is multi-threading?
Multi-threading means various threads that run in a system.
What's new with the stop(), suspend() and resume() methods in JDK 1.2?
The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.
Can each Java object keep track of all the threads that want to exclusively access to it?
Yes. Use Thread.currentThread() method to track the accessing thread
What invokes a thread's run() method?
After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed
What are the high-level thread states?
The high-level thread states are ready, running, waiting, and dead.
what is the purpose of the wait(), notify(), and notifyAll() methods?
The wait(),notify(), and notifyAll() methods are used to provide an efficient way for threads to communicate each other
What happens when a thread cannot acquire a lock on an object?
If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object's lock, it enters the waiting state until the lock becomes available.
What is the difference between yielding and sleeping?
When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state
What is the difference between Process and Thread?
A process can contain multiple threads. In most multithreading operating systems, a process gets its own memory address space; a thread doesn't. Threads typically share the heap belonging to their parent process.
How to define an Abstract class?
A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
How to define an Interface in Java ?
In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.
Example of Interface:
public interface sampleInterface
{
public void functionOne();
public void functionOne();
public long CONSTANT_ONE = 1000;
}
}
What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass
What is the difference between a static and a non-static inner class?
A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.
What are some alternatives to inheritance?
Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn't force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).
No comments:
Post a Comment