Академический Документы
Профессиональный Документы
Культура Документы
Programming exercises:
4. Accept a string from user and throw the exception if that string does
not start with a capital letter.
5. Input the password and login name from command line and throw an
exception if it does not match with our previously stored values.
6. Accept two numbers from user perform any arithmetic operation on
them and throw an exception when result is negative.
7. Accept a number from user and throw an exception if that number is
prime then throw “PrimeNumberException”.
8. 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
.....
9. Display seconds using one thread and minutes using another thread.
10. 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.
11. Display the string “I love Java programming” one letter at a time with
delay of 200 milliseconds.
12. Generate following output with synchronization:
Thread1: Welcome
Thread2: to
Thread1: Java
Thread2: Programming
Thread3: Language
13. 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.
14. 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.
15. Create a sample application based program to simulate synchronized
object.
16. Accept two strings from user and throw a “NoCaseMatchException”
when case is not matching and throw “NotEqualsException” when
strings are not equal.
17. Create an exception “VectorIndexOutOfBoundsException” when a
vector value crosses the boundary of 10 objects.
18. 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”
19. Create ten different threads, doing different jobs having different
priorities and simulate their working.
20. Modify the “Producer-Consumer problem” to add “Retailer” in between
them so as to work them in sequence: “Producer-Retailer-Consumer”.
Objective Questions:
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;
}
}
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
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.