Tuesday, January 12, 2010

Java Fundmental-VII

1. Do I need to use synchronized on setValue(int)?
Ans : It depends whether the method affects method local variables, class static or instance variables. If only method local variables are changed, the value is said to be confined by the method and is not prone to threading issues.
2. Do I need to use synchronized on setValue(int)?
Ans : It depends whether the method affects method local variables, class static or instance variables. If only method local variables are changed, the value is said to be confined by the method and is not prone to threading issues.
3. What is the SwingUtilities.invokeLater(Runnable) method for?
Ans : The static utility method invokeLater(Runnable) is intended to execute a new runnable thread from a Swing application without disturbing the normal sequence of event dispatching from the Graphical User Interface (GUI). The method places the runnable object in the queue of Abstract Windowing Toolkit (AWT) events that are due to be processed and returns immediately. The runnable object’s run() method is only called when it reaches the front of the queue. The deferred effect of the invokeLater(Runnable) method ensures that any necessary updates to the user interface can occur immediately, and the runnable work will begin as soon as those high priority events are dealt with. The invoke later method might be used to start work in response to a button click that also requires a significant change to the user interface, perhaps to restrict other activities, while the runnable thread executes.
4. What is the volatile modifier for?
Ans : The volatile modifier is used to identify variables whose values should not be optimized by the Java Virtual Machine, by caching the value for example. The volatile modifier is typically used for variables that may be accessed or modified by numerous independent threads and signifies that the value may change without synchronization.
5. Which class is the wait() method defined in?
Ans : The wait() method is defined in the Object class, which is the ultimate superclass of all others. So the Thread class and any Runnable implementation inherit this method from Object. The wait() method is normally called on an object in a multi-threaded program to allow other threads to run. The method should should only be called by a thread that has ownership of the object’s monitor, which usually means it is in a synchronized method or statement block.
6. Which class is the wait() method defined in?
Ans : I get incompatible return type for my thread’s getState( ) method! - It sounds like your application was built for a Java software development kit before Java 1.5. The Java API Thread class method getState() was introduced in version 1.5. Your thread method has the same name but different return type. The compiler assumes your application code is attempting to override the API method with a different return type, which is not allowed, hence the compilation error.
7. What is a working thread?
Ans : A working thread, more commonly known as a worker thread is the key part of a design pattern that allocates one thread to execute one task. When the task is complete, the thread may return to a thread pool for later use. In this scheme a thread may execute arbitrary tasks, which are passed in the form of a Runnable method argument, typically execute(Runnable). The runnable tasks are usually stored in a queue until a thread host is available to run them. The worker thread design pattern is usually used to handle many concurrent tasks where it is not important which finishes first and no single task needs to be coordinated with another. The task queue controls how many threads run concurrently to improve the overall performance of the system. However, a worker thread framework requires relatively complex programming to set up, so should not be used where simpler threading techniques can achieve similar results.
8. What is a green thread?
Ans : A green thread refers to a mode of operation for the Java Virtual Machine (JVM) in which all code is executed in a single operating system thread. If the Java program has any concurrent threads, the JVM manages multi-threading internally rather than using other operating system threads. There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations. Current JVM implementations make more efficient use of native operating system threads.
9. What are native operating system threads?
Ans : Native operating system threads are those provided by the computer operating system that plays host to a Java application, be it Windows, Mac or GNU/Linux. Operating system threads enable computers to run many programs simultaneously on the same central processing unit (CPU) without clashing over the use of system resources or spending lots of time running one program at the expense of another. Operating system thread management is usually optimised to specific microprocessor architecture and features so that it operates much faster than Java green thread processing.

Monday, January 11, 2010

Java Fundmental-II

1. The Java language is:
a. Interpreted at run time.
b. Compiled to obtain executable target files.
c. Designed for recursive string processing.
d. Designed for matrix algorithm processing.

2. The Java language is designed to:
a. Run on a simulated Java machine.
b. Run directly on Intel microprocessors.
c. Designed for RISC computing architectures.
d. Designed for CISC computing architectures.

3. The Java language will run:
a. On any computer where the Java simulated machine is installed.
b. On any computer attached to the Internet.
c. On any computer with a Web browser installed.
d. On any computer which will also run C++.

4. The Java language provides the programmer with:
a. No ability to access or manipulate memory.
b. Protected access to machine memory.
c. Dynamically created data structures linked with pointers.
d. Access to the memory of a local machine through a network.

5. The Java language is:
a. An object oriented programming language.
b. A structured programming language.
c. A procedural programming language.
d. A stack oriented programming language.

6. The Java language was designed:
a. To create event driven programs with graphical interfaces.
b. To unify all different approaches to object orientation.
c. To provide a learning tool for beginning programmers.
d. To force programmers to implement all needed program elements themselves.

7. The Java language has most in common with the following language:
a. C#.                      b. C.                  c. C++.                 d. Basic.
8. The basis for object oriented programming can be found in which of the following?
a. All of the items listed.
b. It is an outgrowth of the availability of powerful hardware.
c. It is useful in the development of graphical user interfaces.
d. It solved some of the problems associated with structured programming.

9. That unit of code in Java that contains the specifications for objects is:
a. A class.          b. A method.         c. An instance variable.      d. A constructor.

10. Which of the following is true?
a. An object receives copies of all instance variables, but shares method code.
b. An object receives copies of all instance variables and all methods.
c. An object receives copies of all methods, but shares instance variables.
d. An object shares both instance variables and methods with other objects.

11. Encapsulation refers to the idea that:
a. Instance variables can only be accessed by means of methods.
b. Methods can only be accessed by means of instance variables.
c. All other methods must be defined within the main() method.
d. Instance variables can only be declared within methods.

12. In order to use objects of a given class the programmer needs to:
a. Know what the methods do, but not how they do it.
b. Know all of the instance variables of the class.
c. Know how the method algorithms were implemented.
d. Know the types of all of the instance variables of the class.

13. Java programs have to be saved with the following extension:
a. .java               b. .txt           c. .doc             d. .xls

14. In Java programs, the name of the class has to:
a. Be the same as the name of the file it is saved in.
b. Be different from the name of the file it is saved in.
c. Be all capital letters.
d. Be all small letters.

Search Aptipedia