Programming Concepts using Java, End Term
Consider the Java code given below.
class Vehicle{ public void mileage(){ System.out.println("Mileage is not known."); } public void tyres(){ System.out.println("Number of tyres is not known."); }}class Airplane extends Vehicle{ public void mileage(){ System.out.println("Mileage < 1km/L."); }}class Car extends Vehicle{ public void mileage(){ System.out.println("Mileage >= 10km/L."); } public void tyres(){ System.out.println("Number of tyres is 4."); }}public class FClass{ static void compute(Vehicle s){ s.mileage(); s.tyres(); } public static void main(String[] args){ Vehicle c = new Car(); Vehicle a = new Airplane(); compute(c); compute(a); }}What will the output be?
Consider the Java code given below. class Vehicle{ public void mileage(){ System.out.println("Mileage is not known."); } public void tyres(){ System.out.println("Number of tyres is not known."); } } class Airplane extends Vehicle{ public void mileage(){ System.out.println("Mileage < 1km/L."); } } class Car extends Vehicle{ public void mileage(){ System.out.println("Mileage >= 10km/L."); } public void tyres(){ System.out.println("Number of tyres is 4."); } } public class FClass{ static void compute(Vehicle s){ s.mileage(); s.tyres(); } public static void main(String[] args){ Vehicle c = new Car(); Vehicle a = new Airplane(); compute(c); compute(a); } } What will the output be? Consider the Java code given below. Suppose the file `E:\\test.txt` is written successfully by the program. 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("it stores two int"); bos.println("10 is one"); bos.println("20 is another"); 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.next()); System.out.println(sc.nextLine()); System.out.println(sc.nextInt()); System.out.println(sc.nextLine()); return true; }catch(IOException e) { return false; } } public static void main(String[] args) throws IOException{ if(writeToFile("E:\\test.txt") == false) System.out.println("write failed"); if(readFromFile("E:\\test.txt") == false) System.out.println("read failed"); } } What will the output be? The Java code given below produces a toggle button. With the first click of the button, the color of panel `pnlColor` should become red, with the next click it should become blue, and on the next click, it should turn to red again, and so on. Based on the requirement, answer the question that follows the code. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class FClass extends JFrame implements ActionListener{ JButton btnToggle; JPanel pnlColor, pnlBtn; public FClass(){ setSize(200, 200); btnToggle = new JButton("Toggle"); btnToggle.addActionListener(this); btnToggle.setActionCommand("red"); pnlColor = new JPanel(); pnlBtn = new JPanel(); pnlBtn.add(btnToggle); add(pnlBtn, "South"); add(pnlColor, "Center"); 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 given behaviour can be implemented correctly.