JavaQuizzes

Quiz Guidelines

 

Descriptive Question

QID: 
52

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

QID: 
53

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

QID: 
54

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

QID: 
55

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!!!");

    }

 

Multiple Choice Question

QID: 
56

Which of these statements concerning the use of modifiers are true?

Select 1 option

A. By default (i.e. no modifier) the member is only accessible to classes in the same package and subclasses of the class. 
B. You cannot specify visibility of local variables. 
C. Local variable always have default accessibility. 
D. Local variables can be declared as private. 
E. Local variables can only be declared as public.

Pages