uiz Space

May 2025 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java Quiz 2: 3 August 2025 (May 2025 term)

The IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 3 Aug 2025, in the May 2025 term: 15 questions for 100 marks in 120 minutes. Every question is below with its answer. Take it as a timed mock test to be marked, or read it through first.

Questions
15
Marks
100
Duration
120 min
MCQ
11
MSQ
4

Updated

Official paper: IIT M DEGREE AN EXAM QDB2 03 Aug 2025 · No negative marking.

Question 1

+6 marksOne correct option

Consider the Java code given below.

java
class Printer {
public <T extends Number> void printDetails(T value) { // LINE 1
System.out.println("Number: " + (value.doubleValue() * 3));
}
public <U extends Number> void printDetails(U value) { // LINE 2
System.out.println("Double Number: " + (value.doubleValue() * 6));
}
public <T> void printDetails(T value) { // LINE 3
System.out.println("Value: " + value);
}
}
public class Test {
public static void main(String[] args) {
Printer p = new Printer();
p.printDetails(7);
p.printDetails(3.5);
p.printDetails("Java");
}
}

Choose the correct option.

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

Correct answer

  • C

Question 2

+6 marksOne correct option

Consider the following Java code.

java
import java.util.*;
public class SetTest {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(20);
list.add(30);
list.add(10);
list.add(40);
HashSet<Integer> set1 = new HashSet<Integer>(list);
TreeSet<Integer> set2 = new TreeSet<Integer>(set1);
set2.addAll(set1);
System.out.println(set2);
}
}

Choose the correct option.

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

Correct answer

  • B

Question 3

+6 marksOne correct option

Consider the following Java code.

java
class User {
private String username;
private String password;
public User(String u, String p) {
assert u != null; // assert-1
assert p != null; // assert-2
username = u;
password = p;
}
public boolean login() {
assert !username.isEmpty(); // assert-3
assert password.length() >= 6; // assert-4
return username.equals("admin") && password.equals("admin123");
}
}
public class AssertTest {
public static void main(String[] args) {
User user = new User("admin", "");
System.out.println("Login status: " + user.login());
}
}

Identify the first assert statement that throws the AssertionError when the class is executed as: java -ea AssertTest

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

Correct answer

  • D

Question 4

+6 marksOne correct option

You are developing a logistics system. The following files are part of a Java application.

File: TransportUnit.java

java
package logistics.base;
public class TransportUnit {
public String getId() {
return "GenericUnit-001";
}
protected double calculateFuelCost(double distance, double rate){
return distance * rate;
}
private String getInternalCode() {
return "INT-LOG-999";
}
}

File: DeliveryTruck.java

java
package logistics.vehicle;
import logistics.base.TransportUnit;
public class DeliveryTruck extends TransportUnit {
public String getId() {
return "Truck-Delivery-204";
}
protected double calculateFuelCost(double distance, double rate) {
return (distance * rate) + 20.0; // Includes loading cost
}
public String getInternalCode() {
return "TRK-INT-204";
}
}

File: FleetManager.java

java
package logistics.app;
import logistics.base.TransportUnit;
import logistics.vehicle.DeliveryTruck;
public class FleetManager {
public static void main(String[] args) {
TransportUnit unit = new DeliveryTruck();
System.out.println(unit.getId()); // LINE 1
System.out.println(unit.calculateFuelCost(100, 1.5)); // LINE 2
System.out.println(unit.getInternalCode()); // LINE 3
}
}

Choose the correct option.

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

Correct answer

  • C

Question 5

+6 marksOne correct option

Method Stream.iterate(e, f) returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element e, producing a Stream consisting of e, f(e), f(f(e)), etc.

Based on the above information, consider the code given below, and answer the question that follows.

java
import java.util.stream.*;
public class StreamTest {
public static void main(String[] args) {
Stream.iterate(5, n -> n + 3)
.filter(n -> n % 4 == 0)
.map(n -> n / 2)
.limit(4)
.forEach((x) -> System.out.print(x + " "));
}
}

What will the output be?

  1. A

    2 4 6 8

  2. B

    4 6 8 10

  3. C

    4 10 16 22

  4. D

    8 16 24 32

Show answer

Correct answer

  • C

    4 10 16 22

Question 6

+6 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class Sample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 20, 25, 15, 5, 30, 35);
numbers.stream()
.takeWhile(n -> n % 2 == 0)
.forEach(n -> System.out.print(n + " "));
System.out.println();
numbers.stream()
.dropWhile(n -> n % 2 == 0)
.forEach(n -> System.out.print(n + " "));
}
}

What will the output be?

  1. A

    10 20 25
    15 5 30 35

  2. B

    10 20
    25 15 5 30 35

  3. C

    10 20 25
    15 5 30

  4. D

    10 20 25 15 5 30 35

Show answer

Correct answer

  • B

    10 20
    25 15 5 30 35

Question 7

+6 marksOne correct option

Consider the code given below.

java
class Dancer {
public void perform() {
System.out.println("Dancer performs");
}
public void perform(String style) {
System.out.println("Dancer performs " + style);
}
}
class StreetDancer extends Dancer {
public void perform(String move) {
System.out.println("StreetDancer performs " + move);
}
}
public class TestDance {
public static void main(String[] args) {
Dancer d = new StreetDancer(); // LINE 1
d.perform();
d.perform("hip-hop"); // LINE 2
}
}

Choose the correct option.

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

Correct answer

  • D

Question 8

+7 marksOne correct option

The merge(K key, V value, remappingFunction) function is defined as: If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.

Consider the Java code given below.

java
import java.util.*;
public class DepartmentScores {
public static void main(String[] args) {
Map<String, Integer> dept1 = new TreeMap<>();
dept1.put("HR", 75);
dept1.put("Finance", 80);
dept1.put("Tech", 95);
Map<String, Integer> dept2 = new TreeMap<>();
dept2.put("Finance", 85);
dept2.put("Tech", 90);
dept2.put("HR", 78);
dept2.put("Admin", 70);
Map<String, Integer> combined = new TreeMap<>();
for (Map.Entry<String, Integer> e : dept1.entrySet())
combined.put(e.getKey(), e.getValue()); //LINE 1
for (Map.Entry<String, Integer> e : dept2.entrySet())
combined.merge(e.getKey(), e.getValue(),
(oldVal, newVal) -> Math.max(oldVal, newVal));
System.out.println(combined);
}
}

Choose the correct option.

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

Correct answer

  • B

Question 9

+7 marksOne correct option

Consider the Java code given below.

java
class LogFailureException extends Exception {
public LogFailureException(String message) {
super(message);
}
}
class Logger {
public void logEvent(String content) throws LogFailureException {
//check whether content = null or content = ""
if (content == null || content.isEmpty()) {
throw new LogFailureException("Log content is empty");
}
System.out.println("LOG: " + content);
}
}
class AuthService {
private Logger logger = new Logger();
public void authenticate(String username, String password)
throws LogFailureException {
if (username.equals("admin") && password.equals("admin123")) {
logger.logEvent("User " + username + " authenticated.");
} else {
logger.logEvent("");
}
}
}
public class TestUser {
public static void main(String[] args) {
AuthService auth = new AuthService();
try {
auth.authenticate("guest", "12345");
auth.authenticate("admin", "admin123");
} catch (LogFailureException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}

Choose the correct option.

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

Correct answer

  • B

Question 10

+7 marksOne correct option

Consider the code given below.

java
interface Shape {
public abstract double getArea();
}
class Rectangle implements Shape, Cloneable {
protected double width;
protected double height;
public Rectangle(double w, double h)
width = w;
height = h;
}
public double getArea() {
return width * height;
}
public Rectangle clone() throws CloneNotSupportedException {
return (Rectangle) super.clone();
}
}
class Square extends Rectangle {
public Square(double side) {
super(side, side);
}
public Square clone() throws CloneNotSupportedException {
return (Square) super.clone();
}
}
public class ShapeTest {
public static void main(String[] args) {
try {
Square s1 = new Square(5);
Square s2 = s1.clone();
s1.width = 10;
System.out.print(s1.getArea() + s2.getArea());
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported");
}
}
}

What will the output be?

  1. A

    125.0

  2. B

    75.0

  3. C

    50.0

  4. D

    Cloning not supported

Show answer

Correct answer

  • B

    75.0

Question 11

+8 marksOne correct option

Consider the Java code given below.

java
public class GenericExample {
public <T extends Comparable<T>> boolean compareValues(T a, T b) {
// check whether a equal b
}
public <T> void printValues(T[] values) {
// display values
}
public <T extends Number> double sumValues(List<T> values) {
// add the values
}
}

How does class GenericExample look after type erasure?

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

Correct answer

  • D

Question 12

+7 marksOne or more correct options

The following Java code maps books to the number of times they have been borrowed from a library and classifies them as popular books (borrowed more than 2 times) or less-read books.

java
import java.util.*;
public class Library {
TreeSet<String> popularBooks = new TreeSet<>();
TreeSet<String> lessReadBooks = new TreeSet<>();
public boolean isPopular(int count) {
return count > 2;
}
public void classifyBooks(HashMap<String, Integer> bookMap) {
for (Map.Entry<String, Integer> entry : bookMap.entrySet()) {
// LINE 1: if statement
{
popularBooks.add(entry.getKey());
} else {
lessReadBooks.add(entry.getKey());
}
}
}
public void displayBooks() {
System.out.println("Popular Books: " + popularBooks);
System.out.println("Less-read Books: " + lessReadBooks);
}
public static void main(String[] args) {
HashMap<String, Integer> bookMap = new HashMap<>();
bookMap.put("W", 4);
bookMap.put("X", 1);
bookMap.put("Y", 3);
bookMap.put("Z", 2);
Library lib = new Library();
lib.classifyBooks(bookMap);
lib.displayBooks();
}
}

Choose the correct option to be filled in place of LINE 1 so that the output is:

text
Popular Books: [W, Y]
Less-read Books: [X, Z]

Select all that apply.

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

Correct answers

  • A
  • B

Question 13

+7 marksOne or more correct options

Consider the Java code given below.

java
import java.util.*;
class Test {
public static String findElement(Deque<String> dq, String element){
while(!dq.isEmpty()) {
// LINE 1
{
return "Element found";
}
}
return "Element not found";
}
public static void main(String[] args) {
Deque<String> dq = new ArrayDeque<String>();
dq.push("apple");
dq.push("banana");
dq.push("cherry");
dq.push("date");
System.out.println(findElement(dq, "cherry"));
}
}

Identify the appropriate option(s) to fill in place of LINE 1 such that the output is

Element found

Select all that apply.

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

Correct answers

  • C
  • D

Question 14

+7 marksOne or more correct options

From among the options, choose the code segment(s) that produce(s) the same output as the Java code given in the CODE BLOCK.

java
import java.util.stream.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
//CODE BLOCK begins here
Stream.of("a", "bb", "ccc", "dddd")
.map(s -> s.length())
.forEach(n -> System.out.println(n));
//CODE BLOCK ends here
}
}

Select all that apply.

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

Correct answers

  • A
  • B
  • C

Question 15

+8 marksOne or more correct options

Consider the Java code given below, which prints the names of employees. From among the options, identify the appropriate function header(s) for the function printEmployeeNames that takes as input a list of employees and prints their names.

java
import java.util.*;
class Employee {
private String name;
public Employee(String n) {
name = n;
}
public String getName() {
return name;
}
}
class Manager extends Employee {
public Manager(String n) {
super(n);
}
}
class Developer extends Employee {
public Developer(String n) {
super(n);
}
}
public class Company {
// FUNCTION HEADER for function printEmployeeNames
{
for (int i = 0; i < empList.size(); i++) {
System.out.print(empList.get(i).getName() + ", ");
}
}
public static void main(String[] args) {
List<Manager> managers = new ArrayList<>();
managers.add(new Manager("Rohan"));
managers.add(new Manager("Arjun"));
List<Developer> developers = new ArrayList<>();
developers.add(new Developer("Karthik"));
developers.add(new Developer("Diya"));
printEmployeeNames(managers);
printEmployeeNames(developers);
}
}

Choose the correct option(s).

Select all that apply.

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

Correct answers

  • A
  • B