16. The method below is designed to print the smaller of two values received as
ID: 3812530 • Letter: 1
Question
16. The method below is designed to print the smaller of two values received as arguments Select the correct expression to complete the method. public void show Smaller (Comparable value1, Comparable value2) if System. out.println (value1 is smaller 8e System. out.println (value2 is smaller a) value1 value2 b value1 .compareTo (value2) o cO value1 .comparero (value2) o d value1.compareTo (value2) o 17. Which of the following is the correct syntax for creating a File object? a File in File File ("input.txt b File in File new File ("input.txt c File inFile File ope ("input.txt n d Fil in File new Fil ope ("input.txt n 18. Insert the missing code in the following code fragment. This code is intended to open a file and handle the situation where the file cannot be found. try String filename Scanner in new Scanner (new File (filename exception printStackTrace a catch (IOException exception) b catch (new exception (IOException) c) catch (IllegalArgumentException exception) d catch (IOException)Explanation / Answer
16) Answer (d)
public void showSmaller(Comparable value1, Comparable value2){
if(value1.compareTo(value2)<0){
System.out.println(value1 +"is Smaller");
}else{
System.out.println(value2 +"is Smaller");
}
}
It also depends upon the object what we take as Comparable, like Comparable<Object>
Eg: Comparable<Integer>, Comparable<String>, Comparable<Employee> etc.
compareTo compares one Object with other object in three value modes. CompareTo returns negative, positive or Zero values. if object1<object2 return negative, object1>object2 return positive or returns Zero.
17) Answer
b is the right answer. That is, File inFile = new File(File Name). Other declarations are invalid.
File constructor takes a String parameter of type File Name and read the file in different modes.
Eg:
File inFile = new File("input.txt");
18) Answer
a is right answer. Because, whenever file is read from the local machine or remote, there need to handle a Exception called "FileNotFoundException", or we can also handle super class of FileNotFoundException i.e IOException, as exception in catch block or throw it to method.
Example
try{
String filename="D:\input.txt";
Scanner in = new Scanner(new File(filename));
}
catch(IOException exception){
exception.printStackTrace();
}
19) Answer
b is right answer, because, Exception class is super class of all Exceptions, i.e FileNotFoundException, IOException and all checked and unchecked exceptions. So, what ever exception that is called inside try block can be handled by catch block, since catch block is calling Exception class which is super class of all Exceptions.
Example:
try{
String filename="D:\input.txt";
Scanner in = new Scanner(new File(filename));
}
catch(Exception exception){
exception.printStackTrace();
}
20) Answer
c is right answer.
Because, Scanner take parameter as InputStream or File , like Scanner(InputStream) or Scanner(File). So, we need to pass File as a parameter to the Scanner Constructor. Since we are reading input, we need to read from the file through Scanner object
Example
try{
String inputFileName="D:\input.txt";
String outputFileName="D:\output.txt";
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
}
catch(IOException exception){
exception.printStackTrace();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.