Programming Concepts using Java, End Term
Consider the Java code given below.
import java.io.*;import java.util.*;
public class FClass{ static boolean writeToFile(String filename) { try { FileOutputStream fos = new FileOutputStream(filename); PrintStream bos = new PrintStream(fos); bos.println("2 numbers are"); bos.println("3.14 and"); bos.println("10"); return true; }catch(IOException e) { return false; } } static boolean readFromFile(String filename) { try { FileInputStream fis = new FileInputStream(filename); Scanner sc = new Scanner(fis); System.out.println(sc.nextInt()); System.out.println(sc.nextLine()); System.out.println(sc.nextDouble()); System.out.println(sc.nextLine()); return true; }catch(IOException e) { return false; } } public static void main(String[] args) throws IOException{ if(writeToFile("E:\\myfile.txt") == false) System.out.println("write failed"); if(readFromFile("E:\\myfile.txt") == false) System.out.println("read failed"); }}Suppose the file E:\\myfile.txt is written successfully by the program. What will the output be?
Consider the Java code given below. import java.io.*; import java.util.*; public class FClass{ static boolean writeToFile(String filename) { try { FileOutputStream fos = new FileOutputStream(filename); PrintStream bos = new PrintStream(fos); bos.println("2 numbers are"); bos.println("3.14 and"); bos.println("10"); return true; }catch(IOException e) { return false; } } static boolean readFromFile(String filename) { try { FileInputStream fis = new FileInputStream(filename); Scanner sc = new Scanner(fis); System.out.println(sc.nextInt()); System.out.println(sc.nextLine()); System.out.println(sc.nextDouble()); System.out.println(sc.nextLine()); return true; }catch(IOException e) { return false; } } public static void main(String[] args) throws IOException{ if(writeToFile("E:\\myfile.txt") == false) System.out.println("write failed"); if(readFromFile("E:\\myfile.txt") == false) System.out.println("read failed"); } } Suppose the file `E:\\myfile.txt` is written successfully by the program. What will the output be? The Java code given below produces a checkbox. The initial color of panel `pnlColor` is blue. If the checkbox is selected, the color of panel `pnlColor` should become red. Again, if the checkbox is deselected, the color of panel `pnlColor` should become blue. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class FClass extends JFrame implements ActionListener{ JCheckBox cbColor; JPanel pnlColor, pnlBtn; public FClass(){ setSize(200, 200); cbColor = new JCheckBox("Red Panel"); cbColor.addActionListener(this); pnlColor = new JPanel(); pnlBtn = new JPanel(); pnlBtn.add(cbColor); add(pnlBtn, "South"); add(pnlColor, "Center"); pnlColor.setBackground(Color.blue); setVisible(true); } public void actionPerformed(ActionEvent e) { /*--------------------*/ /*****CODE SEGMENT*****/ /*--------------------*/ } public static void main(String[] args){ new FClass(); } } Choose the correct code segment inside method `actionPerformed()` such that the execution of the code results in the expected behaviour. Consider the Java code given below. class Employee{ String name; int salary; public Employee(String n, int s){ name = n; salary = s; } public Employee(Employee e){ this.name = e.name; this.salary = e.salary; } public double bonus(){ return salary * 0.1; } } public class Test { public static void main(String[] args) { Employee e1 = new Employee("raju",12000); var e2 = new Employee(e1); e2.salary = 10000; System.out.println(e1.bonus()); System.out.println(e2.bonus()); } } Choose the correct option.