Programming Concepts using Java, End Term
Consider the code given below.
class Shared { static int count = 0;}
class Adder extends Thread { public void run() { for(int i=0; i<1000; i++) { Shared.count++; } }}public class Test { public static void main(String[] args) throws InterruptedException { Thread t1 = new Adder(); Thread t2 = new Adder(); t1.start(); t2.start(); t1.join(); t2.join(); System.out.print(Shared.count); }}Choose the correct option.
Consider the code given below. class Shared { static int count = 0; } class Adder extends Thread { public void run() { for(int i=0; i<1000; i++) { Shared.count++; } } } public class Test { public static void main(String[] args) throws InterruptedException { Thread t1 = new Adder(); Thread t2 = new Adder(); t1.start(); t2.start(); t1.join(); t2.join(); System.out.print(Shared.count); } } Choose the correct option. Consider the following Java code that uses chained exceptions. class FileStorageException extends Exception { public FileStorageException(String message) { super(message); } } class FileUploadException extends Exception { public FileUploadException(String message) { super(message); } } class FileService { public void uploadFile(String filename) throws FileUploadException { try { // if(do not have enough disk space to store the file) throw new FileStorageException("Insufficient disk space"); // else upload the file } catch (FileStorageException e) { FileUploadException fue = new FileUploadException("Failed to upload"); fue.initCause(e); throw fue; } } } public class FileApp { public static void main(String[] args) { FileService fileService = new FileService(); try { fileService.uploadFile("report.pdf"); } catch (FileUploadException e) { System.out.println(e.getMessage()); System.out.println("Reason: " + e.getCause().getMessage()); } } } Which of these could be the output of this code? Consider the two Java files given below. Animal.java: package creatures; public class Animal { private String name; private String habitat; private int legs; // Constructor to initialize instance variables public String getName(String name) { return name; } protected String getHabitat(String habitat) { return habitat; } private int getLegs(int legs) { return legs; } public int showLegs(int legs) { return getLegs(legs); } } Test.java: package Animals; public class Test { public static void main(String[] args) { Animal myAnimal = new Animal(); System.out.println("Number of legs: " + myAnimal.showLegs(4)); // LINE 1 System.out.println(myAnimal.getHabitat("Forest")); // LINE 2 System.out.println(myAnimal.getLegs(4)); // LINE 3 } } Choose the correct option.