K K Wagh Polytechnic, Nashik – 3
Question Bank
: Java Programming (9113)
Class
: TYIF (IF/V/C)
Chapter No. 04
Multithreaded Programming and Exception Handling
Review Questions from previous MSBTE question papers: 1. 2. 3. 4. 5. 6. 7. 8.
9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
What is an exception? What will happen if the exception is not handled by the program? List all the in-built exception classes in Java. Explain the use of throw statement with appropriate example. Explain the method printStackTrace( ) with suitable example. How throws clause can be used to avoid occurrence of exception in program? Differentiate between checked and unchecked exceptions. Describe the application of finally clause with proper example. Give the meaning of following message when the exception is generated: Exception in thread "main" java.lang.ArithmeticException: / by zero at Uncaught.main(Uncaught.java:8) Illustrate the use of nested try-catch with example. Explain try-catch-finally exception handling in detail. Explain the meaning of multithreading. List methods of Thread class along with their application. What is the default priority for all the threads? How to obtain and change this priority? Describe the life cycle of the thread. Illustrate the process of synchronization with respect to Java program. Explain the importance of synchronization in multithreading. Single class can be used to initiate multiple threads. Justify. What are the advantages of creating a thread by implementing Runnable and by extending Thread class? According to you, which is better?
Programming exercises: 1. 2. 3.
Create a try block that is likely to generate three different types of exceptions and then incorporate necessary catch blocks to catch and handle them properly. Write a program to handle StringIndexOutOfBoundsException. Create a user-defined exception “EvenNumberException” if the number inputted in an even.
By Mr. Kute T. B. for (TYIF) 2008-2009
-1-
K K Wagh Polytechnic, Nashik – 3
4. 5. 6. 7. 8.
9. 10.
11. 12.
13.
14. 15. 16. 17.
Accept a string from user and throw the exception if that string does not start with a capital letter. Input the password and login name from command line and throw an exception if it does not match with our previously stored values. Accept two numbers from user perform any arithmetic operation on them and throw an exception when result is negative. Accept a number from user and throw an exception if that number is prime then throw “PrimeNumberException”. Create two threads, first will print even numbers and second will print odd numbers from 1 to 50 as, odd = 1 even = 2 odd = 3 ..... Display seconds using one thread and minutes using another thread. Define a thread class and create two thread objects t1 and t2 and perform following operations: a. Set priorities and name of thread object b. Behavior of the thread object is to display the name of the thread for 2 times and each time goes into sleep for 1 second. c. Main thread executes the t1 and t2 thread respectively and waits for t1 and t2 to finish the job. d. Print message “exit” after exit of each thread. Display the string “I love Java programming” one letter at a time with delay of 200 milliseconds. Generate following output with synchronization: Thread1: Welcome Thread2: to Thread1: Java Thread2: Programming Thread3: Language Declare a class’ Account’ having data members accno and balance. Declare a class ‘Transaction’ having method deposit( ) which will deposit an amount to an account. Synchronize method deposit( ) when used by multiple threads. Using Runnable interface: using one thread print all prime numbers from 1 to 50 and non-primes numbers using another thread from 1 to 50. Create a sample application based program to simulate synchronized object. Accept two strings from user and throw a “NoCaseMatchException” when case is not matching and throw “NotEqualsException” when strings are not equal. Create an exception “VectorIndexOutOfBoundsException” when a vector value crosses the boundary of 10 objects.
By Mr. Kute T. B. for (TYIF) 2008-2009
-2-
K K Wagh Polytechnic, Nashik – 3
18. 19. 20.
Print following string on the screen using two threads. Each subsequent character must be printed by two different threads. “That is a Core Java Programming” Create ten different threads, doing different jobs having different priorities and simulate their working. Modify the “Producer-Consumer problem” to add “Retailer” in between them so as to work them in sequence: “Producer-Retailer-Consumer”.
Objective Questions: 1.
Assuming a method contains code which may raise an Exception (but not a RuntimeException), what is the correct way for a method to indicate that it expects the caller to handle that exception: a) throw Exception b) throws Exception c) new Exception d) Don't need to specify anything
2.
What is the result of executing the following code, using the parameters 4 and 0 to a and b? public void divide(int a, int b) { try { int c = a / b; } catch (Exception e) { System.out.print("Exception "); } finally { System.out.println("Finally"); } a) Prints out: Exception Finally b) Prints out: Finally c) Prints out: Exception d) No output
3.
What will happen when we attempt to compile and run the following code? class Background implements Runnable{ int i=0; public int run(){ while(true){ i++; System.out.println("i="+i); } return 1; } }
By Mr. Kute T. B. for (TYIF) 2008-2009
-3-
K K Wagh Polytechnic, Nashik – 3
a) It will compile and the run method will print out the increasing value of i. b) It will compile and calling start will print out the increasing value of i. c) The code will cause an error at compile time. d) Compilation will cause an error because while cannot take a parameter of true. 4.
What is the name of the interface that can be used to define a class that can execute within its own thread? a) Runnable b) Run c) Threadable d) Thread e) Executable
5.
What is the name of the method used to schedule a thread for execution? a) init(); b) start(); c) run(); d) resume(); e) sleep();
6.
Which methods may cause a thread to stop executing? a) sleep(); b) stop(); c) yield(); d) wait(); e) notify();
7.
What is the result of compiling and executing the following Java class: public class ThreadTest extends Thread { public void run() { System.out.println("In run"); suspend(); resume(); System.out.println("Leaving run"); } public static void main(String args []) { (new ThreadTest()).start(); } } a) Compilation will fail in the method main. b) Compilation will fail in the method run. c) The string "In run" will be printed to standard out.
By Mr. Kute T. B. for (TYIF) 2008-2009
-4-
K K Wagh Polytechnic, Nashik – 3
d) Both strings will be printed to standard out. e) Nothing will happen. 8.
What is the effect of issuing a wait( ) method on an object? a) If a notify( ) method has already been sent to that object then it has no effect. b) The object issuing the call to wait( ) will halt until another object sends a notify() or notifyAll( ) method. c) An exception will be generated. d) The object issuing the call to wait( ) will be automatically synchronized with any other objects.
9.
A sleep( ) method might throw: a) IOException b) SecurityException c) ThreadStateException d) InterruptedException
10.
Which of the following is not a final method? a) currentThread() b) wait() c) notify() d) isAlive()
By Mr. Kute T. B. for (TYIF) 2008-2009
-5-