Quiz Space

Programming Concepts using Java Quiz 2: 4 August 2024 (May 2024 term)

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

Question 2

+6 marksOne correct option

Consider two Java files located in two different packages as shown below.

Vehicle.java:

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

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.

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

Question 3

+6 marksOne correct option

Consider the Java code given below.

java
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

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

12 more questions in this paper

Sign in with Google — it is free — to see every question with its answer and explanation, practise it in learning mode, or take it as a timed mock test.

More on the Java Quiz 2 4 Aug 2024 paper

The IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 4 Aug 2024, in the May 2024 term: 15 questions for 100 marks in 120 minutes. The first 3 questions are below. Sign in with Google — it is free — to see the whole paper with its answers and explanations, in learning mode or as a timed mock test.

FeatureJava Quiz 2 4 Aug 2024 at a glance
TermMay 2024 term
SubjectProgramming Concepts using Java
Course codeBSCS2005
Questions15
Marks100
Duration120 min
MCQ13
MSQ2
Official paperIIT M DIPLOMA AN EXAM QDD2 4 Aug 2024
Negative markingNo negative marking.
Updated

Same Quiz 2, other subjects

More Java