Programming Concepts using Java, Quiz 2
Consider the Java code given below.
The method boolean containsKey (Object key) in the class Map returns true if and only if the map contains an entry for a key k such that Objects.equals(key, k).
import java.util.*;interface Vehicle { void start();}class Car implements Vehicle { public void start() { System.out.println("Starting a Car"); }}class Motorcycle implements Vehicle { public void start() { System.out.println("Starting a Motorcycle"); }}class Garage<T extends Vehicle> { private Map<String, T> vehicles; public Garage() { vehicles = new LinkedHashMap<String, T>();//LINE A }
public void add(String name, T vehicle) { vehicles.put(name, vehicle); } public void startVehicle(String name) { if (vehicles.containsKey(name)) { T v = vehicles.get(name); //LINE B v.start(); } else { System.out.println("Vehicle not found"); } }}public class TestGarage { public static void main(String[] args) { Garage<Vehicle> g = new Garage<Vehicle>(); Vehicle v1 = new Car(); Vehicle v2 = new Motorcycle(); g.add("car", v1); g.add("motorcycle", v2); g.startVehicle("car"); g.startVehicle("bicycle"); }}Choose the correct option.
Consider the Java code given below. The method boolean containsKey (Object key) in the class Map returns true if and only if the map contains an entry for a key k such that Objects.equals(key, k). import java.util.*; interface Vehicle { void start(); } class Car implements Vehicle { public void start() { System.out.println("Starting a Car"); } } class Motorcycle implements Vehicle { public void start() { System.out.println("Starting a Motorcycle"); } } class Garage<T extends Vehicle> { private Map<String, T> vehicles; public Garage() { vehicles = new LinkedHashMap<String, T>();//LINE A } public void add(String name, T vehicle) { vehicles.put(name, vehicle); } public void startVehicle(String name) { if (vehicles.containsKey(name)) { T v = vehicles.get(name); //LINE B v.start(); } else { System.out.println("Vehicle not found"); } } } public class TestGarage { public static void main(String[] args) { Garage<Vehicle> g = new Garage<Vehicle>(); Vehicle v1 = new Car(); Vehicle v2 = new Motorcycle(); g.add("car", v1); g.add("motorcycle", v2); g.startVehicle("car"); g.startVehicle("bicycle"); } } Choose the correct option. Consider two Java files located in two different packages as shown below. Vehicle.java: package com.transport; public class Vehicle { void startEngine() { System.out.println("Engine started"); } private void stopEngine() { System.out.println("Engine stopped"); } protected void accelerate() { System.out.println("Accelerating"); } public void brake() { System.out.println("Braking"); } } Car.java package com.automobile; import com.transport.Vehicle; public class Car extends Vehicle { public static void main(String[] args) { Car car = new Car(); car.startEngine(); // LINE A car.stopEngine(); // LINE B car.accelerate(); // LINE C car.brake(); // LINE D } } Choose the correct option. Consider the Java code given below. class NewUser{ private String name, password; public NewUser(String s, String p) { this.name = s; this.password = p; } public String getUserName() { assert name != null : "Invalid name"; //LINE 1 assert password.length() == 10 : "Should be 10 characters"; //LINE 2 return name + "@acct.com"; } } public class Test { public static void main(String[] args) { NewUser u1 = new NewUser("", "password"); NewUser u2 = new NewUser("Sudarshan", "1234567890"); String username1 = u1.getUserName(); //LINE 3 assert username1 != null : "Should not be null"; // LINE 4 String username2 = u2.getUserName(); //LINE 5 assert username2 != null : "Should not be null"; // LINE 6 System.out.println(username1); System.out.println(username2); } } Choose the correct option when the program is executed as:\ java -ea Test