Quiz Space

Programming Concepts using Java · Quiz 2 · 4 Aug 2024 · May 2024 term

Question 1: Consider the Java code given below. The method boolean co…

Question 1

+6 marksOne 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).

java
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.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 1 of 15 in the IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 4 Aug 2024, in the May 2024 term (IIT M DIPLOMA AN EXAM QDD2 4 Aug 2024). It carries 6 marks.

More questions from this paper

  1. Q2Consider two Java files located in two different packages as shown below. Vehicle.java: Car.java Choose the correct opt…
  2. Q3Consider the Java code given below. Choose the correct option when the program is executed as:\ java -ea Test
  3. Q4Consider the Java code given below. Choose the correct option.
  4. Q5Consider the Java code given below. Choose the correct option(s) to fill in place of CODE BLOCK so that the output is:\…
  5. Q6Consider the Java code given below. What will the output be?
  6. Q7Consider the Java code given below. What will the output be?
  7. Q8Consider the Java code given below. Identify the appropriate option to fill in place of LINE X such that the output is:…
  8. Q9Consider the Java code given below. After type erasure, which of the following correctly represents the ArrayOperations…
  9. Q10Consider the Java code given below. Choose the correct option.
  10. Q11Consider the Java code given below. Choose the correct option to be filled in place of LINE 1 so that the output is:\ […
  11. Q12From among the options, choose the code segment that gives the same output as is given by the Java code inside the CODE…
  12. Q13Consider the Java code given below. What will the output be?
  13. Q14Consider the Java code given below. Identify the appropriate option to fill in place of LINE X such that the output is:…
  14. Q15Consider the Java code given below that prints the most impressive act among a set of given Performer objects. From amo…