Example of Questions - zhamri/MyClass-STIW3054 GitHub Wiki

SECTION A: STRUCTURED QUESTIONS

  1. Explain the following terms:

    • Real-time system
    • Parallel Computing
    • Thread Group
    • Timed_Waiting State
  2. What is the output of the following program?

class Testing implements Runnable {

    @Override
    public void run() {
        Thread t = Thread.currentThread();
        System.out.println(t.getName() + " is running ...");

    }

    public static void main(String args[]) {
        System.out.println(Thread.currentThread() + " is running ...");
        Thread t1 = new Thread(new Testing(), "t1");
        Thread t2 = new Thread(new Testing());
        t1.start();
        t2.start();
        System.out.println(Thread.currentThread().getName() + " is running ...");
        t1.start();
        System.out.println(t2.getName() + " is stopping ...");
    }
}
  1. Which of the following statements about the Callable call() and Runnable run() methods are correct? Give your reasons.

    A. Both can throw unchecked exceptions.
    B. Callable takes a generic method argument.
    C. Callable can throw a checked exception.
    D. Both can be implemented with lambda expressions.
    E. Runnable returns a generic type.
    F. Callable returns a generic type.
    G. Both methods return void.

  2. Which lines need to be changed to make the code compile? Give your reasons.

     1.	package A171_Exam;
     2.	import Processor.*;
     3.	public class CheckProcessor {  
     4.	    public static void main(String[] args) {
     5.	        int x = Runtime.getRuntime().availableProcessors();
     6.	        int y = Runtime.getRuntime().freeMemory();
     7.	        int z = Runtime.getRuntime().totalMemory();
     8.	    }
     9.	} 
  1. What is the output of the following codes?
import java.util.concurrent.atomic.AtomicInteger;
class Test {
    public static void main(String[] args) throws InterruptedException {
        CountSolution pt = new CountSolution();
        Thread t1 = new Thread(pt, "t1");
        Thread t2 = new Thread(pt, "t2");
        t1.start(); t2.start();
        t1.join(); t2.join();
        System.out.println("MyCount=" + CountSolution.mycount);
        System.out.println("Count=" + pt.getCount());
    }}
class CountSolution implements Runnable {
    static int mycount;
    private AtomicInteger count = new AtomicInteger();
    @Override
    public void run() {
        for (int i = 1; i <= 4; i++) {
            processSomething(i);
            mycount++;
            System.out.println(count.incrementAndGet());
        }}
    public int getCount() {
        return this.count.get();
    }
    private void processSomething(int i) {
        try {
            Thread.sleep(i * 200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }}}
  1. What is the output of the following codes?
import java.util.concurrent.*;
class Exam implements Runnable {
    private int id; CyclicBarrier cyclicBarrier;
    public Exam (int id, CyclicBarrier cyclicBarrier) {
        this.id = id;
        this.cyclicBarrier = cyclicBarrier;
    }
    public void run() {
        System.out.println(id + " starts the exam ...");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread ID:" + id + " finished ...");
        try {
            cyclicBarrier.await();
            System.out.println("After study ...");
        } catch (Exception e) {
            e.printStackTrace();
        }}}
public class MyCyclicBarriers {
    public static void main(String[] args) {
        ExecutorService executorService = 
        Executors.newFixedThreadPool(3);
        CyclicBarrier barrier = new CyclicBarrier(3, 
        new Runnable() {
            @Override
            public void run() {
               System.out.println("We will pass this exam ...");
            }});
        for (int i = 0; i < 3; ++i) {
            executorService.execute(new Exam (i + 1, barrier));
        } executorService.shutdown();}}

SECTION B: CASE STUDY

  1. Write a program using java.util.concurrent.TimeUnit and java.util.Calendar to calculate the number of minutes between 22 August 2017 and 01 January 2010.

  2. You have to develop a program that creates and runs 3 threads. Each thread calculates and prints the multiplication table of a number between one and 3. The example of the output is shown below (MUST create TWO (2) classes).

    Thread-0: 1 * 1 = 1
    Thread-2: 3 * 1 = 3
    Thread-2: 3 * 2 = 6
    Thread-2: 3 * 3 = 9
    Thread-1: 2 * 1 = 2
    Thread-1: 2 * 2 = 4
    Thread-1: 2 * 3 = 6
    Thread-0: 1 * 2 = 2
    Thread-0: 1 * 3 = 3

  3. Complete the following Java codes to ensure that the thread can be started and display unlimited strings. Finally, the thread will be terminated if someone press an ENTER key. (Hints: Must use volatile keyword)

class MyThread extends Thread {


    public void run() {
        
            

            
                
            
                
            
        

    }

    public void shutdown() {



       
    }
}


public class MyVolatile {

    public static void main(String[] args) {
        
        






    }
}

  1. You are required to develop a simple program to simulate Fork and Join using RecursiveAction.