JavaQuizzes

Descriptive Question

Predict the result:

public class NullOverloadCheck {

	static void myMethod(Float o)
	{
		System.out.println("Float");
	}
	
	static void myMethod(Integer i){
		
		System.out.println("Integer");
	}
	
	public static void main(String[] args)
	{
		myMethod(null);
	}
	
}

 

Descriptive Question

Predict the output:

public class ThreadSynchronization {

  public static void main(String[] args) throws InterruptedException {
    Object lockObject = new Object();

    Thread t1 = new Thread(new MyRunnableEven(lockObject));
    Thread t2 = new Thread(new MyRunnableOdd(lockObject));
    t1.start();
    t2.start();
  }
}

class MyRunnableEven implements Runnable {

  private Object lockObject;

  MyRunnableEven(Object lockObject) {
    this.lockObject = lockObject;
  }

  @Override
  public void run() {

    for (int i = 0; i <= 10; i = i + 2) {
      synchronized (lockObject) {
        try {
          System.out.println("Even " + i);
          lockObject.notify();
          lockObject.wait();

        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

class MyRunnableOdd implements Runnable {

  private Object lockObject;

  MyRunnableOdd(Object lockObject) {
    this.lockObject = lockObject;
  }

  @Override
  public void run() {

    for (int i = 1; i < 10; i = i + 2) {

      synchronized (lockObject) {
        try {
          System.out.println("Odd " + i);
          lockObject.notify();
          lockObject.wait();

        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

Hints: There is no compilation error.

Descriptive Question

Predict the output:

public class ThreadSynchronization {

  public static void main(String[] args) throws InterruptedException {
    Object lockObject = new Object();

    Thread t1 = new Thread(new MyRunnableEven(lockObject));
    Thread t2 = new Thread(new MyRunnableOdd(lockObject));
    t1.start();
    t2.start();
  }
}

class MyRunnableEven implements Runnable {

  private Object lockObject;

  MyRunnableEven(Object lockObject) {
    this.lockObject = lockObject;
  }

  @Override
  public void run() {

    for (int i = 0; i <= 10; i = i + 2) {

      synchronized (lockObject) {
        try {
          System.out.println("Even " + i);
          lockObject.notify();
          lockObject.wait();

        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

class MyRunnableOdd implements Runnable {

  private Object lockObject;

  MyRunnableOdd(Object lockObject) {
    this.lockObject = lockObject;
  }

  @Override
  public void run() {

    for (int i = 1; i < 10; i = i + 2) {
      
      synchronized (lockObject) {
        try {
          lockObject.wait();
          System.out.println("Odd " + i);
          lockObject.notify();
          

        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

Descriptive Question

Based on assumptions and facts, find the output.

Assumption: filename.txt does not exist.

Fact: FileNotFoundException is a child of IOException.

Program:
 

    try {

      File file = new File("filename.txt");

      Scanner sc = new Scanner(file);

      throw new IOException();

    } catch (IOException e) {

      System.out.println("IOException called!!!");

    } catch (FileNotFoundException e) {

      System.out.println("FileNotFoundException called!!!");

    }

 

Pages

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!