uiz Space

September 2025 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java Quiz 2: 23 November 2025 (September 2025 term)

The IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 23 Nov 2025, in the September 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
MSQ
4
MCQ
11

Updated

Official paper: IIT M DIPLOMA AN EXAM QDD2 23 Nov 2025 NEW · No negative marking.

Question 1

+7 marksOne or more correct options

Consider the Java code given below that should print the names of students whose grades are between 70.0 and 90.0 (both inclusive).

java
import java.util.*;
class Student {
String name;
double grade;
public Student(String n, double g) {
name = n;
grade = g;
}
}
public class TestGrades {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Anu", 85.5));
students.add(new Student("Bindu", 92.0));
students.add(new Student("Hari", 78.0));
students.add(new Student("David", 70.0));
students.add(new Student("Hasan", 65.0));
//CODE BLOCK
}
}

Choose the correct option(s) to fill in place of CODE BLOCK to obtain the right answer.

Select all that apply.

  1. A

    students.stream()
    .map(s -> s.grade >= 70.0 && s.grade <= 90.0)
    .forEach(s -> System.out.println(s.name));

  2. B

    students.stream()
    .filter(s -> s.grade >= 70.0 && s.grade <= 90.0)
    .forEach(s -> System.out.println(s.name));

  3. C

    students.stream()
    .filter(s -> s.grade >= 70.0)
    .filter(s -> s.grade <= 90.0)
    .forEach(s -> System.out.println(s.name));

  4. D

    students.stream()
    .filter(s -> s.grade >= 70.0)
    .map(s -> s.grade <= 90.0)
    .forEach(s -> System.out.println(s.name));

Show answer

Correct answers

  • B

    students.stream()
    .filter(s -> s.grade >= 70.0 && s.grade <= 90.0)
    .forEach(s -> System.out.println(s.name));

  • C

    students.stream()
    .filter(s -> s.grade >= 70.0)
    .filter(s -> s.grade <= 90.0)
    .forEach(s -> System.out.println(s.name));

Question 2

+5 marksOne correct option

Consider the code given below.
interface Vehicle {
public abstract double getFuelEfficiency();
}
class Car implements Vehicle, Cloneable {
protected double fuelCapacity;
protected double mileage;
public Car(double fuel, double m) {
fuelCapacity = fuel;
mileage = m;
}
public double getFuelEfficiency() {
return fuelCapacity * mileage;
}
public Car clone() throws CloneNotSupportedException {
return (Car) super.clone();
}
}
class ElectricCar extends Car {
public ElectricCar(double batteryCapacity, double mileage) {
super(batteryCapacity, mileage);
}
public ElectricCar clone() throws CloneNotSupportedException {
return (ElectricCar) super.clone();
}
}
public class VehicleTest {
public static void main(String[] args) {
try {
ElectricCar e1 = new ElectricCar(50, 2);
ElectricCar e2 = e1.clone();
e1.fuelCapacity = 100;
System.out.print(e1.getFuelEfficiency() + e2.getFuelEfficiency());
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported");
}
}
}
What will the output be?

  1. A

    200.0

  2. B

    300.0

  3. C

    400.0

  4. D

    Cloning not supported

Show answer

Correct answer

  • B

    300.0

Question 3

+7 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class PalindromeChecker {
public static boolean isPalindrome(String str) {
Stack<Character> s = new Stack<>();
Deque<Character> d = new ArrayDeque<>();
for (char ch : str.toCharArray()) {
s.push(ch);
d.add(ch);
}
// CODE BLOCK
return true;
}
public static void main(String[] args) {
String word1 = "level";
String word2 = "hello";
System.out.println("Is word1 a palindrome? " + isPalindrome(word1));
System.out.println("Is word2 a palindrome? " + isPalindrome(word2));
}
}

Choose the correct option to fill in place of CODE BLOCK so that the output is:

text
Is word1 a palindrome? true
Is word2 a palindrome? false

Please note the following methods from type Stack and Deque:

pop(): Removes the object at the top of this stack and returns it.

poll(): Retrieves and removes the head of this deque, or returns null if empty.

peek(): Retrieves, but does not remove, the head of this deque, or returns null if empty.

  1. A

    while (!s.isEmpty()) {
    if (s.pop().equals(d.peek())) {
    return false;
    }
    }

  2. B

    while (!s.isEmpty()) {
    if (s.pop().equals(d.poll())) {
    return false;
    }
    }

  3. C

    while (!s.isEmpty()) {
    if (!s.pop().equals(d.poll())) {
    return false;
    }
    }

  4. D

    while (!s.isEmpty()) {
    if (!s.pop().equals(d.peek())) {
    return false;
    }
    }

Show answer

Correct answer

  • C

    while (!s.isEmpty()) {
    if (!s.pop().equals(d.poll())) {
    return false;
    }
    }

Question 4

+7 marksOne correct option

Consider the following Java code.

java
import java.util.*;
import java.util.stream.*;
public class StreamTest {
public static void main(String[] args) {
List<String> words = Arrays.asList("apple", "banana", "pear",
"kiwi", "plum");
words.stream()
.filter(w -> w.length() > 4)
.map(w -> w.toUpperCase())
.skip(1)
.forEach(System.out::println);
}
}

What will the output be?

  1. A

    APPLE
    BANANA

  2. B

    BANANA

  3. C

    APPLE

  4. D

    KIWI
    PLUM

Show answer

Correct answer

  • B

    BANANA

Question 5

+7 marksOne correct option

Consider the Java code given below.

java
public class MathUtils {
public <T> T getFirst(T[] arr) {
// Returns the first element of the array
}
public <T extends Number> double sum(T[] arr) {
// Returns the sum of all elements in the array
}
}

How does class MathUtils look after type erasure?

  1. A

    public class MathUtils {
    public Object getFirst(Object[] arr) {
    // Returns the first element of the array
    }
    public double sum(Object[] arr) {
    // Returns the sum of all elements in the array
    }
    }

  2. B

    public class MathUtils {
    public Object getFirst(Object[] arr) {
    // Returns the first element of the array
    }
    public double sum(Number[] arr) {
    // Returns the sum of all elements in the array
    }
    }

  3. C

    public class MathUtils {
    public T getFirst(T[] arr) {
    // Returns the first element of the array
    }
    public double sum(T[] arr) {
    // Returns the sum of all elements in the array
    }
    }

  4. D

    public class MathUtils {
    public Object getFirst(Object[] arr) {
    // Returns the first element of the array
    }
    public double sum(Double[] arr) {
    // Returns the sum of all elements in the array
    }
    }

Show answer

Correct answer

  • B

    public class MathUtils {
    public Object getFirst(Object[] arr) {
    // Returns the first element of the array
    }
    public double sum(Number[] arr) {
    // Returns the sum of all elements in the array
    }
    }

Question 6

+7 marksOne correct option

Consider the Java code given below.
interface Discount{
public void apply(int price);
}
class ShoppingCart{
public Discount getDiscount(String type){
Discount d = null;
if(type == "festival")
d = new FestivalDiscount();
return d;
}
private class FestivalDiscount implements Discount{
public void apply(int price){
System.out.println("Final Price: " + (price - 100));
}
}
}
public class CartTest {
public static void main(String[] args) {
// CODE SEGMENT //
disc.apply(500);
}
}
Choose the correct option to fill in at CODE SEGMENT such that the output is:
Final Price: 400

  1. A

    FestivalDiscount disc = new ShoppingCart().getDiscount("festival");

  2. B

    Discount disc = new ShoppingCart().getDiscount("festival");

  3. C

    ShoppingCart sc = new ShoppingCart();
    FestivalDiscount disc = sc.getDiscount("festival");

  4. D

    No line/s of code at CODE SEGMENT can generate the given output because FestivalDiscount is a private inner class.

Show answer

Correct answer

  • B

    Discount disc = new ShoppingCart().getDiscount("festival");

Question 7

+7 marksOne correct option

Consider two Java files located in two different packages as shown below.
Account.java:
package com.bank;
public class Account {
int accountNumber = 1001;
private double balance = 5000.0;
protected void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
public void displayBalance() {
System.out.println("Balance: " + balance);
}
}
SavingsAccount.java:
package com.customer;
import com.bank.Account;
public class SavingsAccount extends Account {
public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount();
System.out.println(sa.accountNumber); // LINE 1
System.out.println(sa.balance); // LINE 2
sa.deposit(1000); // LINE 3
sa.displayBalance(); // LINE 4
}
}
Choose the correct option.

  1. A

    LINE 1 and LINE 2 generate compilation errors.

  2. B

    LINE 2 generates a compilation error, while others compile fine.

  3. C

    LINE 1, LINE 2, and LINE 3 generate compilation errors.

  4. D

    Only LINE 3 generates a compilation error.

Show answer

Correct answer

  • A

    LINE 1 and LINE 2 generate compilation errors.

Question 8

+7 marksOne or more correct options

Consider the Java code given below.
interface Shape {
void draw();
}
class ShapeFactory {
private String type;
public void setType(String t) {
type = t;
}
public String getType() {
return type;
}
public Shape createShape() {
switch (getType()) {
case "Circle":
return new Circle();
case "Square":
return new Square();
case "Triangle":
return new Triangle();
default:
return null;
}
}
private class Circle implements Shape {
public void draw() {
System.out.println("Drawing Circle");
}
}
private class Square implements Shape {
public void draw() {
System.out.println("Drawing Square");
}
}
private class Triangle implements Shape {
public void draw() {
System.out.println("Drawing Triangle");
}
}
}
public class ShapeDemo {
public static void main(String[] args) {
ShapeFactory sf = new ShapeFactory();
sf.setType("Square");
// --------- LINE 1 ---------
}
}
Identify the appropriate option(s) to fill in place of LINE 1 such that the output is:
Drawing Square

Select all that apply.

  1. A

    Shape s = sf.createShape();
    s.draw();

  2. B

    sf.draw();

  3. C

    sf.createShape().draw();

  4. D

    Shape s = sf;
    s.draw();

Show answer

Correct answers

  • A

    Shape s = sf.createShape();
    s.draw();

  • C

    sf.createShape().draw();

Question 9

+6 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 ProjectScores {
public static void main(String[] args) {
Map<String, Integer> projectA = new TreeMap<>();
projectA.put("Alpha", 88);
projectA.put("Beta", 76);
projectA.put("Gamma", 92);
Map<String, Integer> projectB = new TreeMap<>();
projectB.put("Beta", 80);
projectB.put("Gamma", 85);
projectB.put("Delta", 70);
Map<String, Integer> combined = new TreeMap<>();
for (Map.Entry<String, Integer> e : projectA.entrySet())
combined.put(e.getKey(), e.getValue()); //LINE 1
for (Map.Entry<String, Integer> e : projectB.entrySet())
combined.merge(e.getKey(), e.getValue(),
(oldVal, newVal) -> Math.min(oldVal, newVal));
System.out.println(combined);
}
}

Choose the correct option.

  1. A

    LINE 1 generates a compilation error due to duplicate keys.

  2. B

    The program generates the output:
    {Alpha=88, Beta=76, Delta=70, Gamma=85}

  3. C

    The program generates the output:
    {Alpha=88, Beta=80, Delta=70, Gamma=92}

  4. D

    The program generates the output:
    {Alpha=88, Beta=156, Delta=70, Gamma=177}

Show answer

Correct answer

  • B

    The program generates the output:
    {Alpha=88, Beta=76, Delta=70, Gamma=85}

Question 10

+7 marksOne or more correct options

Consider the Java code given below that prints the average rating of a set of Rateable objects. From among the options, identify the appropriate function header for the function printAverageRating that takes as input an array of Rateable objects and prints the average rating.
import java.util.*;
interface Rateable {
public abstract double getRating();
}
class Product implements Rateable {
private double rating;
// Constructor
// method getRating() that returns rating of Product
}
public class Test {
// LINE 1: FUNCTION HEADER
{
// invokes method getRating()
// to compute and print the average rating
}
public static void main(String[] args) {
Rateable[] products = {new Product(4.5), new Product(3.8),
new Product(5.0)};
printAverageRating(products);
}
}
Choose the correct option(s).

Select all that apply.

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

Correct answers

  • B
  • D

Question 11

+6 marksOne or more correct options

Consider the code given below.

java
import java.util.stream.*;
import java.util.*;
class Student {
private String name;
private int marks;
public Student(String n, int m){
name = n;
marks = m;
}
public int getMarks(){
return marks;
}
public String toString(){
return name + " : " + marks;
}
}
public class FClass {
public static void main(String[] args){
var sList = new ArrayList<Student>();
sList.add(new Student("Amit", 85));
sList.add(new Student("Neha", 42));
sList.add(new Student("Raj", 90));
sList.add(new Student("Pooja", 35));
var outputList = ____________________; // LINE 1
outputList.forEach(s -> System.out.println(s));
}
}

Identify the appropriate option(s) to fill in the blank at LINE 1 such that the output of the program is:

text
Amit : 85
Raj : 90

Select all that apply.

  1. A

    sList.stream().filter(s -> s.getMarks() >= 50)

  2. B

    sList.stream().filter(s -> s >= 50)

  3. C

    sList.stream().filter((Student s) -> s.getMarks() >= 50)

  4. D

    sList.stream().takeWhile(s -> s.getMarks() >= 50)

Show answer

Correct answers

  • A

    sList.stream().filter(s -> s.getMarks() >= 50)

  • C

    sList.stream().filter((Student s) -> s.getMarks() >= 50)

Question 12

+6 marksOne correct option

Consider the Java code given below.
class Student {
private int marks;
public Student(int marks){
assert marks >= 0 && marks <= 100 : "bad marks"; // assert-1
this.marks = marks;
}
}
class LoanApplication {
private double loanAmount;
public LoanApplication(double loanAmount){
assert loanAmount > 0 : "bad loan"; // assert-2
this.loanAmount = loanAmount;
}
}
class Product {
private int quantity;
public Product(int quantity){
assert quantity >= 0 : "bad qty"; // assert-3
this.quantity = quantity;
}
}
public class FClass {
public static void main(String[] args){
LoanApplication l = new LoanApplication(-50000);
Product p = new Product(10);
Student s = new Student(105);
}
}
Identify the assert statement that throws the AssertionError when the class is executed as: java -ea:Student -da:LoanApplication FClass

  1. A

    assert-1

  2. B

    assert-2

  3. C

    assert-3

  4. D

    None of these

Show answer

Correct answer

  • A

    assert-1

Question 13

+7 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class MapIteratorTest {
public static void main(String[] args) {
var map1 = new HashMap<Integer, String>();
map1.put(3, "Banana");
map1.put(1, "Apple");
map1.put(4, "Date");
map1.put(2, "Cherry");
var map2 = new TreeMap<Integer, String>(map1);
for (var entry : map1.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
for (var entry : map2.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}

Choose the correct option.

  1. A

    map1 will print entries in the order in which they were inserted.
    map2 will print entries in sorted order of keys.

  2. B

    map1 will print entries in unspecified order.
    map2 will print entries in sorted order of keys.

  3. C

    map1 will print entries in sorted order of keys.
    map2 will print entries in insertion order.

  4. D

    Both map1 and map2 will print entries in unspecified order.

Show answer

Correct answer

  • B

    map1 will print entries in unspecified order.
    map2 will print entries in sorted order of keys.

Question 14

+7 marksOne correct option

Consider the Java code given below.
interface Chargeable {
void showChargeStatus();
}
class Laptop implements Chargeable {
public void showChargeStatus() {
System.out.println("Laptop is charging");
}
}
class Mobile implements Chargeable {
public void showChargeStatus() {
System.out.println("Mobile is charging");
}
}
class DeviceList {
private Object[] devices = {new Laptop(), new Mobile()};
public void testDevices() {
for (int i = 0; i < devices.length; i++) {
//LINE-1
}
}
}
public class TestDevices {
public static void main(String[] args) {
DeviceList dL = new DeviceList();
dL.testDevices();
}
}
Identify the appropriate option to fill in place of LINE-1 such that the output is:
Laptop is charging
Mobile is charging

  1. A

    ((Chargeable) devices[i]).showChargeStatus();

  2. B

    devices[i].showChargeStatus();

  3. C

    ((Laptop) devices[i]).showChargeStatus();

  4. D

    ((Mobile) devices[i]).showChargeStatus();

Show answer

Correct answer

  • A

    ((Chargeable) devices[i]).showChargeStatus();

Question 15

+7 marksOne correct option

Consider the Java code given below.
class NegativeBalanceException extends Exception {
public NegativeBalanceException() {
super("Transaction failed: negative balance");
}
}
class BankAccount {
private double balance;
public BankAccount(double b) {
balance = b;
}
public void withdraw(double amount) throws NegativeBalanceException {
if(balance - amount < 0)
throw new NegativeBalanceException();
balance -= amount;
System.out.println("Withdrawal successful: " + balance);
}
}
public class TestBank {
public static void main(String[] args) {
try {
BankAccount acc1 = new BankAccount(500);
BankAccount acc2 = new BankAccount(200);
acc1.withdraw(600);
acc2.withdraw(150);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Choose the correct option.

  1. A

    This program generates output:
    Transaction failed: negative balance

  2. B

    This program generates output:
    Transaction failed: negative balance
    Withdrawal successful: 50.0

  3. C

    This program generates output:
    Withdrawal successful: -100.0
    Withdrawal successful: 50.0

  4. D

    The program crashes due to the uncaught exception: NegativeBalanceException

Show answer

Correct answer

  • A

    This program generates output:
    Transaction failed: negative balance