Question: What is the ultimate ancestor of all java classes
Answer: Object class is the ancestor of all the java classes
Question: What are important methods of Object class
Answer: wait(), notify(), notifyAll(), equals(), toString().
Question: What is the difference between “= =” and “equals()”
Answer: “= =” does shallow comparison, It retuns true if the two object points to the same address in the memory, i.e if the same the same reference
“equals()” does deep comparison, it checks if the values of the data in the object are same
Question: What would you use to compare two String variables - the operator == or the method equals()?
Answer: I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object
Question: Give example of a final class
Answer: Math class is final class and hence cannot be extended
Question: What is the difference between String and StringBuffer
Answer: String is an immutable class, i.e you cannot change the values of that class
Example:
String str = “java”; // address in memory say 12345
And now if you assign a new value to the variable str then
str = “core java”; then the value of the variable at address 12345 will not change but a new memory is allocated for this variable say 54321
So in the memory address 12345 will have value “java”
And the memory address 54321 will have value “core java” and the variable str will now be pointing to address 54321 in memory
StringBuffer can be modified dynamically
Example:
StringBuffer strt =”java” // address in memory is say 12345
And now if you assign a new value to the variable str then
Str = “core java”; then value in the address of memory will get replaced, a new memory address is not allocated in this case.
Question: What will be the result if you compare StringBuffer with String if both have same values
Answer: It will return false as you cannot compare String with StringBuffer
Question: What is Collection API
Answer: The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.
Question: What are different types of collections
Answer: A collection has no special order and does not reject duplicates
A list is ordered and does not reject duplicates
A set has no special order but rejects duplicates
A map supports searching on a key field, values of which must be unique
Question: Tell me something about Arrays
Answer: Arrays are fast to access, but are inefficient if the number of elements grow and if you have to insert or delete an element
Question: Difference between ArrayList and Vector
Answer: Vector methods are synchronized while ArrayList methods are not
Question: Iterator a Class or Interface? What is its use?
Answer: Iterator is an interface which is used to step through the elements of a Collection
Question: Difference between Hashtable and HashMap
Answer: Hashtable does not store null value, while HashMap does
Hashtable is synchronized, while HashMap is not
Question: What are wrapped classes
Answer: Wrapped classes are classes that allow primitive types to be accessed as objects.
Question: What are the four general cases for Conversion and Casting
Answer:
Conversion of primitives
Casting of primitives
Conversion of object references
Casting of object references
Question: When can conversion happen
Answer:
It can happen during
Assignment
Method call
Arithmetic promotion
Question: What are the rules for primitive assignment and method call conversion
Answer:
A boolean can not be converted to any other type
A non Boolean can be converted to another non boolean type, if the conversion is widening conversion
A non Boolean cannot be converted to another non boolean type, if the conversion is narrowing conversion
See figure below for simplicity
Question: What are the rules for primitive arithmetic promotion conversion
Answer:
For Unary operators :
If operant is byte, short or a char it is converted to an int
If it is any other type it is not converted
For binary operands :
If one of the operands is double, the other operand is converted to double
Else If one of the operands is float, the other operand is converted to float
Else If one of the operands is long, the other operand is converted to long
Else both the operands are converted to int
Question: What are the rules for casting primitive types
Answer:
You can cast any non Boolean type to any other non boolean type
You cannot cast a boolean to any other type; you cannot cast any other type to a boolean
Question: What are the rules for object reference assignment and method call conversion
Answer: An interface type can only be converted to an interface type or to object. If the new type is an interface, it must be a superinterface of the old type. A class type can be converted to a class type or to an interface type. If converting to a class type the new type should be superclass of the old type. If converting to an interface type new type the old class must implement the interface. An array maybe converted to class object, to the interface cloneable, or to an array. Only an array of object references types may be converted to an array, and the old element type must be convertible to the new element.
Question: What are the rules for Object reference casting
Answer: Casting from Old types to Newtypes
Compile time rules
- When both Oldtypes and Newtypes are classes, one should be subclass of the other
- When both Oldtype ad Newtype are arrays, both arrays must contain reference types (not primitive), and it must be legal to cast an element of Oldtype to an element of Newtype
- You can always cast between an interface and a non-final object
Runtime rules
- If Newtype is a class. The class of the expression being converted must be Newtype or must inherit from Newtype
- If NewType is an interface, the class of the expression being converted must implement Newtype
Question: What is the difference between while and do while loop
Answer: Do while loop walways executes the body of the loop at least once, since the test is performed at the end of the body
Question: When do you use continue and when do you use break statements
Answer: When continue statement is applied it prematurely completes the iteration of a loop.
When break statement is applied it causes the entire loop to be abandoned.
Question: What is the base class from which all exceptions are subclasses
Answer: All exceptions are subclasses of a class called java.lang.Throwable
Question: How do you intercept and thereby control exceptions
Answer: We can do this by using try/catch/finally blocks
You place the normal processing code in try block
You put the code to deal with exceptions that might arise in try block in catch block
Code that must be executed no matter what happens must be place in finally block
Question: When do we say an exception is handled
Answer: When an exception is thrown in a try block and is caught by a matching catch block, the exception is considered to have been handled
Question: When do we say an exception is not handled
Answer: There is no catch block that names either the class of exception that has been thrown or a class of exception that is a parent class of the one that has been thrown, then the exception is considered to be unhandled, in such condition the execution leaves the method directly as if no try has been executed
Question: In what sequence does the finally block gets executed
Answer: If you put finally after a try block without a matching catch block then it will be executed after the try block
If it is placed after the catch block and there is no exception then also it will be executed after the try block
If there is an exception and it is handled by the catch block then it will be executed after the catch block
Question: What can prevent the execution of the code in finally block
Answer:
- The death of thread
- Use of system.exit()
- Turning off the power to CPU
- An exception arising in the finally block itself
What are the rules for catching multiple exceptions
A more specific catch block must precede a more general one in the source, else it gives compilation error
Only one catch block, that is first applicable one, will be executed
Question: What does throws statement declaration in a method indicate
Answer: This indicates that the method throws some exception and the caller method should take care of handling it
Question: What are checked exception
Answer: Checked exceptions are exceptions that arise in a correct program, typically due to user mistakes like entering wrong data or I/O problems
Question: What are runtime exceptions
Answer: Runtime exceptions are due to programming bugs like out of bond arrays or null pointer exceptions.
Question: What is difference between Exception and errors
Answer: Errors are usually compile time and exceptions can be runtime or checked
Question: How will you handle the checked exceptions
Answer: You can provide a try/catch block to handle it. OR Make sure method declaration includes a throws clause that informs the calling method an exception might be thrown from this particular method. When you extend a class and override a method, can this new method throw exceptions other than those that were declared by the original method. No it cannot throw, except for the subclasses of those exceptions.
Question: Is it legal for the extending class which overrides a method which throws an exception, not o throw in the overridden class
Answer: Yes it is perfectly legal
Question: Explain the user defined Exceptions?
Answer: User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.
Example:
class myCustomException extends Exception {
// The class simply has to exist to be an exception
}
Question: What's the difference between constructors and other methods
Answer: Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.
Question: What is the difference between Overloading and Overriding
Answer: Overloading : Reusing the same method name with different arguments and perhaps a different return type is called as overloading
Overriding : Using the same method name with identical arguments and return type is know as overriding
Question: What do you understand by late binding or virtual method Invocation. (Example of runtime polymorphism)
Answer: When a compiler for a non object oriented language comes across a method invocation, it determines exactly what target code should be called and build machine language to represent that call. In an object oriented language, this is not possible since the proper code to invoke is determined based upon the class if the object being used to make the call, not the type of the variable. Instead code is generated that will allow the decision to be made at run time. This delayed decision making is called as late binding
Question: Can overriding methods have different return types
Answer: No they cannot have different return types
Question: If the method to be overridden has access type protected, can subclass have the access type as private
Answer: No, it must have access type as protected or public, since an overriding method must not be less accessible than the method it overrides
Question: Can constructors be overloaded
Answer: Yes constructors can be overloaded
Question: What happens when a constructor of the subclass is called
Answer: A constructor delays running its body until the parent parts of the class have been initialized. This commonly happens because of an implicit call to super() added by the compiler. You can provide your own call to super(arguments..) to control the way the parent parts are initialized. If you do this, it must be the first statement of the constructor.
Question: If you use super() or this() in a constructor where should it appear in the constructor
Answer: It should always be the first statement in the constructor
Question: What is an inner class
Answer: An inner class is same as any other class, but is declared inside some other class
Question: How will you reference the inner class
Answer: To reference it you will have to use OuterClass$InnerClass
Question: Can objects that are instances of inner class access the members of the outer class
Answer: Yes they can access the members of the outer class
Question: What modifiers may be used with an inner class that is a member of an outer class?
Answer: A (non-local) inner class may be declared as public, protected, private, static, final, or abstract
Question: Can inner classes be static
Answer: Yes inner classes can be static, but they cannot access the non static data of the outer classes, though they can access the static data
Question: Can an inner class be defined inside a method
Answer: Yes it can be defined inside a method and it can access data of the enclosing methods or a formal parameter if it is final
Question: What is an anonymous class
Answer: Some classes defined inside a method do not need a name, such classes are called anonymous classes
Question: What are the rules of anonymous class
Answer: The class is instantiated and declared in the same place The declaration and instantiation takes the form new Xxxx () {// body}
Where Xxxx is an interface name. An anonymous class cannot have a constructor. Since you do not specify a name for the class, you cannot use that name to specify a constructor
Question: Where does java thread support reside
Answer: It resides in three places
The java.lang.Thread class (Most of the support resides here)
The java.lang.Object class
The java language and virtual machine
Question: What is the difference between Thread and a Process
Answer: Threads run inside process and they share data.
One process can have multiple threads, if the process is killed all the threads inside it are killed, they dont share data
Question: What happens when you call the start() method of the thread
Answer: This registers the thread with a piece of system code called thread scheduler
The schedulers determines which thread is actually running
Question: Does calling start () method of the thread causes it to run
Answer: No it merely makes it eligible to run. The thread still has to wait for the CPU time along with the other threads, then at some time in future, the scheduler will permit the thread to run
Question: When the thread gets to execute, what does it execute
Answer: The thread executes a method call run(). It can execute run() method of either of the two choices given below :
The thread can execute it own run() method.
The thread can execute the run() method of some other objects
For the first case you need to subclass the Thread class and give your subclass a run() method
For the second method you need to have a class implement the interface runnable. Define your run method. Pass this object as an argument to the Thread constructor
Question: How many methods are declared in the interface runnable
Answer: The runnable method declares only one method :
public void run();
Question: Which way would you prefer to implement threading , by extending Thread class or implementing Runnable interface
Answer: The preferred way will be to use Interface Runnable, because by subclassing the Thread class you have single inheritance i.e you wont be able to extend any other class
Question: What happens when the run() method returns
Answer: When the run() method returns, the thread has finished its task and is considered dead. You can't restart a dead thread. You can call the methods of dead thread
Question: What are the different states of the thread
Answer: They are as follows:
Running: The state that all thread aspire to be
Various waiting states : Waiting, Sleeping, Suspended and Bloacked
Ready : Waiting only for the CPU
Dead : All done
Question: What is Thread priority
Answer: Every thread has a priority, the higher priorit thread gets preference over the lower priority thread by the thread scheduler
Question: What is the range of priority integer
Answer: It is from 1 to 10. 10 beings the highest priority and 1 being the lowest
Question: What is the default priority of the thread
Answer: The default priority is 5
Question: What happens when you call Thread.yield()
Answer: It caused the currently executing thread to move to the ready state if the scheduler is willing to run any other thread in place of the yielding thread. Yield is a static method of class Thread
Question: What is the advantage of yielding
Answer: It allows a time consuming thread to permit other threads to execute
Question: What happens when you call Thread.sleep()
Answer: It passes time without doing anything and without using the CPU. A call to sleep method requests the currently executing thread to cease executing for a specified amount of time.
Question: Does the thread method start executing as soon as the sleep time is over
Answer: No, after the specified time is over the thread enters into ready state and will only execute when the scheduler allows it to do so.
Question: What do you mean by thread blocking
Answer: If a method needs to wait an indeterminable amount of time until some I/O occurrence takes place, then a thread executing that method should graciously step out of the Running state. All java I/O methods behave this way. A thread that has graciously stepped out in this way is said to be blocked.
Question: What threading related methods are there in object class
Answer: wait(), notify() and notifyAll() are all part of Object class and they have to be called from synchronized code only
Question: What is preemptive scheduling
Answer: In preemptive scheduling there are only two ways for the thread to leave the running state without ecplicitly calling wait() or suspended() It can cease t be ready to execute ()by calling a blocking I/O method). It can get moved out by CPU by a higher priorit thread that becomes ready to execute
Question: What is non-preemptive or Time sliced or round robin scheduling
Answer: With time slicing the thread is allowd to execute for a limited amount of time. It is then moved to ready state, where it must contend with all the other ready threads.
Question: What are the two ways of synchronizing the code
Answer: Synchronizing an entire method by putting the synchronized modifier in the methods declaration. To execute the method, a thread must acquire the lock of the object that owns the method.
Synchronize a subset of a method by surrounding the desired lines of code with curly brackets and inserting the synchronized expression before the opening curly. This allows you to synchronize the block on the lock of any object at all, not necessarily the object that owns the code
Question: What happens when the wait() method is called
Answer: The calling thread gives up CPU
The calling thread gives up the lock
The calling thread goes into the monitor's waiting pool
Question: What happens when the notify() method is called
Answer: One thread gets moved out of monitors waiting pool and into the ready state
The thread that was notified ust reacquire the monitors locl before it can proceed
Question: Using notify () method how you can specify which thread should be notified
Answer: You cannot specify which thread is to be notified, hence it is always better to call notifyAll() method
Question: What are access modifiers
Answer: These public, protected and private, these can be applied to class, variables, constructors and methods. But if you don't specify an access modifier then it is considered as Friendly
Question: Can protected or friendly features be accessed from different packages
Answer: No when features are friendly or protected they can be accessed from all the classes in that package but not from classes in another package
Question: How can you access protected features from another package
Answer: You can access protected features from other classes by subclassing the that class in another package, but this cannot be done for friendly features
Question: What are the rules for overriding
Answer:
Private method can be overridden by private, friendly, protected or public methods
Friendly method can be overridden by friendly, protected or public methods
Protected method can be overridden by protected or public methods
Public method can be overridden by public method
Question: Explain modifier final
Answer: Final can be applied to classes, methods and variables and the features cannot be changed. Final class cannot be subclassed, methods cannot be overridden
Question: Can you change the reference of the final object
Answer: No the reference cannot be change, but the data in that object can be changed
Question: Can abstract modifier be applied to a variable
Answer: No it is applied only to class and methods
Question: Can abstract class be instantiated
Answer: No abstract class cannot be instantiated i.e you cannot create a new object of this class
Question: When does the compiler insist that the class must be abstract
Answer:
If one or more methods of the class are abstract.
If class inherits one or more abstract methods from the parent abstract class and no implementation is provided for that method
If class implements an interface and provides no implementation for those methods
Question: How is abstract class different from final class
Answer: Abstract class must be subclassed and final class cannot be subclassed
Question: Where can static modifiers be used
Answer: They can be applied to variables, methods and even a block of code, static methods and variables are not associated with any instance of class
Question: When are the static variables loaded into the memory
Answer: During the class load time
Question: When are the non static variables loaded into the memory
Answer: They are loaded just before the constructor is called
Question: How can you reference static variables
Answer: Via reference to any instance of the class
Computer comp = new Computer ();
comp.harddisk where hardisk is a static variable
comp.compute() where compute is a method
Via the class name
Computer.harddisk
Computer.compute()
Question: Can static method use non static features of there class
Answer: No they are not allowed to use non static features of the class, they can only call static methods and can use static data
Question: What is static initializer code
Answer: A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g.
public class Demo{
static int =10;
static{
System.out.println(“Hello world');
}
}
And this code is executed exactly once at the time of class load
Where is native modifier used It can refer only to methods and it indicates that the body of the method is to be found else where and it is usually written in non java language
Question: What are transient variables
Answer: A transient variable is not stored as part of objects persistent state and they cannot be final or static
Question: What is synchronized modifier used for
Answer: It is used to control access of critical code in multithreaded programs
Question: What are volatile variables
Answer: It indicates that these variables can be modified asynchronously
Question: What are null or Marker interfaces in Java
Answer: The null interfaces are marker interfaces, they do not have function declarations in them, they are empty interfaces, this is to convey the compiler that they have to be treated differently
Question: Does java Support multiple inheritance
Answer: Java does not support multiple inheritance directly like C++, because then it is prone to ambiguity, example if a class extends 2 other classes and these 2 parent classes have same method names then there is ambiguity. Hence in Java Multiple inheritance is supported using Interfaces
Question: What are virtual function
Answer: In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class functions overridden by the derived class, a problem then arises when a derived object has been cast as the base class type. When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous.
The distinction between virtual and not virtual is provided to solve this issue. If the function in question is designated "virtual" then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.
Question: Does java support virtual functions
Answer: No java does not support virtual functions direclty like in C++, but it supports using Abstract class and interfaces
Question: Describe what happens when an object is created in Java
Answer: Several things happen in a particular order to ensure the object is constructed properly:
1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the
object and its superclasses. Implemenation-specific data includes pointers to class and method data.
2. The instance variables of the objects are initialized to their default values.
3. The constructor for the most derived class is invoked. The first thing a constructor does is call the
consctructor for its superclasses. This process continues until the constrcutor for java.lang.Object is called,
as java.lang.Object is the base class for all objects in java.
4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks
are executed. Then the body of the constructor is executed. Thus, the constructor for the base class
completes first and constructor for the most derived class completes last.
Question: What is the purpose of System Class
Answer: The purpose of the system class is to provide the access to the System reources
Question: What is instanceOf operator used for
Answer: It is used to if an object can be cast into a specific type without throwing Class cast exception
Question: Why we should not have instance variable in an interface
Answer: Since all data fields and methods in an Interface are public by default, then we implement that interface in our class then we have public members in our class and this class will expose these data members and this is violation of encapsulation as now the data is not secure
Question: What is JVM
Answer: When we install a java package. It contains 2 things
* The Java Runtime Environment (JRE)
* The Java Development Kit (JDK)
The JRE provides runtime support for Java applications. The JDK provides the Java compiler and other development tools. The JDK includes the JRE.
Both the JRE and the JDK include a Java Virtual Machine (JVM). This is the application that executes a Java program. A Java program requires a JVM to run on a particular platform
Question: Can abstract class be final
Answer: No, abstract class cannot be final
Question: When a new object of derived Class is created, whose constructor will be called first, childs or parents
Answer: Even when the new object of child class is created, first the Base class constructor gets executed and then the child classes constructor
Question: What is a singleton class
Answer: A singleton is an object that cannot be instantiated. The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM) - by prevent direct instantiation we can ensure that developers don't create a second copy.
Question: Can an abstract class have final method
Answer: Yes
Question: Can a final class have an abstract method
Answer: No, the compiler will give an error
Question: What is the difference between Authentication and Authorization
Answer: Authentication is a process for verifying that an individual is who they say they are. Authorization is an additional level of security, and it means that a particular user (usually authenticated), may have access to a particular resource say record, file, directory or script.
Question: Why do we need public static void main(String args[]) method in Java
Answer: We need
- public: The method can be accessed outside the class / package
- static: You need not have an instance of the class to access the method
- void: Your application need not return a value, as the JVM launcher would return the value when it exits
- main(): This is the entry point for the application
If the main() was not static, you would require an instance of the class in order to execute the method.
If this is the case, what would create the instance of the class? What if your class did not have a public constructor?
Question: What is the difference between an Interface and an Abstract class
Answer: In abstract class you can define as well as declare methods, the methods which are declared are to be marked as abstract.
In interface all we just declare methods and the definition is provided by the class which is implementing it
Question: Explain serialization
Answer: Serialization means storing a state of a java object by coverting it to byte stream
Question: What are the rules of serialization
Answer: Rules:
1. Static fileds are not serialized because they are not part of any one particular object
2. Fileds from the base class are handled only if hose are serializable
3. Transient fileds are not serialized
Question: What is difference between error and exception
Answer: Error occurs at runtime and cannot be recovered, Outofmemory is one such example. Exceptions on the other hand are due conditions which the application encounters such as FileNotFound exception or IO exceptions
Question: What do you mean by object oreiented programming
Answer: In object oreinted programming the emphasis is more on data than on the procedure and the program is divided into objects.
The data fields are hidden and they cant be accessed by external functions.
The design approach is bottom up.
The functions operate on data that is tied together in data structure
Question: What are 4 pillars of object oreinted programming
Answer:
1. Abstraction
It means hiding the details and only exposing the essentioal parts
2. Polymorphism
Polymorphism means having many forms. In java you can see polymorphism when you have multiple methods with the same name
3. Inheritance
Inheritance means the child class inherits the non private properties of the parent class
4. Encapsulation
It means data hiding. In java with encapsulate the data by making it private and even we want some other class to work on that data then the setter and getter methods are provided
Question: Difference between procedural and object oreinted language
Answer: In procedural programming the instructions are executed one after another and the data is exposed to the whole program
In OOPs programming the unit of program is an object which is nothing but combination of data and code and the data is not exposed outside the object
Question: What is the difference between constructor and method
Answer: Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.
Question: What is the difference between parameters and arguments
Answer: While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.
Question: What is reflection in java
Answer: Reflection allows Java code to discover information about the fields, methods and constructors of loaded classes and to dynamically invoke them
Question: What is a cloneable interface and how many methods does it contain
Answer: It is not having any method because it is a TAGGED or MARKER interface
Question: What's the difference between a queue and a stack
Answer: Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule
Question: Can you make an instance of abstract class
Answer: No you cannot create an instance of abstract class
Question: What are parsers
Answer: Parsers are used for processing XML documents. There are 2 types of parsers DOM parser and SAX Parser
Question: Difference between SAX and DOM parser
Answer: DOM parsers are Object based and SAX parsers are event based
DOM parsers creates Tree in the memory whereas SAX parser does not and hence it is faster than DOM
DOM parser are useful when we have to modify the XML, with SAX parser you cannot modify the xml, it is read only
Question: What is the difference between Java Bean and Java Class
Answer: Basically a Bean is a java class but it has getter and setter method and it does not have any logic in it, it is used for holding data.
On the other hand the Java class can have what a java bean has and also has some logic inside it
Question: To what value is a variable of the boolean type automatically initialized?
The default value of the boolean type is false.
Question: Can try statements be nested?
Try statements may be tested.
Question: What is the difference between the prefix and postfix forms of the ++ operator?
The prefix form performs the increment operation and returns the value ofthe increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value.
Question: What is the purpose of a statement block?
A statement block is used to organize a sequence of statements as a single statement group.
Question: What is a Java package and how is it used?
A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.
Question: What modifiers may be used with a top-level class?
A top-level class may be public, abstract, or final.
Question: What are the Object and Class classes used for?
The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.
Question: How does a try statement determine which catch clause should be used to handle an exception?
When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.
Question: Can an unreachable object become reachable again?
Answer: An unreachable object may become reachable again. This can happen when the object's finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects.
Question: When is an object subject to garbage collection?
Answer: An object is subject to garbage collection when it becomes unreachable to the program in which it is used.
Question: What method must be implemented by all threads?
Answer: All tasks must implement the run() method, whether they are a subclass ofThread or implement the Runnable interface.
Question: What methods are used to get and set the text label displayed by a Button object?
Answer: getLabel() and setLabel()
Question: Which Component subclass is used for drawing and painting?
Answer: Canvas
Question: What are synchronized methods and synchronized statements?
Answer: Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.
Question: What are the two basic ways in which classes that can be run as threads may be defined?
Answer: A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface.
Question: What are the problems faced by Java programmers who don't use layout managers?
Answer: Without layout managers, Java programmers are faced with determining how their GUI will be displayed across multiple windowing systems and finding a common sizingand positioning that will work within the constraints imposed by each windowing system.
Question: What is the difference between an if statement and a switch statement?
Answer: The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.
Question: What happens when you add a double value to a String?
Answer: The result is a String object.
Question: What is the List interface?
Answer: The List interface provides support for ordered collections of objects.
No comments:
Post a Comment