uiz Space

January 2025 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java End Term: 13 April 2025, Set QDD3 (January 2025 term)

The IIT Madras BS Programming Concepts using Java (Java) End Term paper sat on 13 Apr 2025, in the January 2025 term, set QDD3: 23 questions for 100 marks in 180 minutes. Every question is below with its answer. Take it as a timed mock test to be marked, or read it through first.

Questions
23
Marks
100
Duration
180 min
MCQ
17
MSQ
6

Updated

Official paper: IIT M DIPLOMA AN EXAM QDD3 13 Apr 2025 · No negative marking.

Question 1

+4 marksOne correct option

Consider the code given below.

java
class Shared {
static int count = 2000;
}
class Subtractor extends Thread {
public void run() {
for (int i = 0; i < 1000; i++) {
Shared.count--;
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Subtractor();
Thread t2 = new Subtractor();
t1.start();
t2.start();
t1.join();
t2.join();
System.out.print(Shared.count);
}
}

Choose the correct option.

  1. A

    The code always prints 2000.

  2. B

    The code prints any number in the range 1000 to 2000

  3. C

    The code prints any number in the range 0 to 1000

  4. D

    The code always prints 1000.

Show answer

Correct answer

  • C

    The code prints any number in the range 0 to 1000

Question 2

+4 marksOne correct option

Consider the following Java code that uses chained exceptions.

java
class PaymentException extends Exception {
public PaymentException(String message) {
super(message);
}
}
class OrderProcessingException extends Exception {
public OrderProcessingException(String message) {
super(message);
}
}
class OrderService {
public void placeOrder() throws OrderProcessingException {
try {
// if(payment fails)
throw new PaymentException("Payment not done");
// else process the order
} catch (PaymentException e) {
OrderProcessingException oe = new OrderProcessingException
("Order Failed");
oe.initCause(e);
throw oe;
}
}
}
public class Test {
public static void main(String[] args) {
OrderService orderService = new OrderService();
try {
orderService.placeOrder();
} catch (OrderProcessingException e) {
System.out.println(e.getMessage());
System.out.println("Root cause: " + e.getCause().getMessage());
}
}
}

Which of these could be the output of this code?

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

Correct answer

  • B

Question 3

+4 marksOne correct option

Consider the two Java files given below.

Animal.java:

java
package creatures;
class Animal {
private String name;
private String habitat;
private int legs;
// Constructor to initialize instance variables
public String getName(String name) {
return name;
}
private String getHabitat(String habitat) {
return habitat;
}
protected int getLegs(int legs) {
return legs;
}
public int showLegs(int legs) {
return getLegs(legs);
}
}
class Bird extends Animal {
public void info() {
System.out.println("This is a Bird.");
}
public void displayHabitat() {
System.out.println("Bird's habitat: " + getHabitat("Trees"));
}
}

Test.java:

java
package creatures;
public class Test {
public static void main(String[] args) {
Animal myAnimal = new Animal();
System.out.println("Number of legs: " + myAnimal.showLegs(4));
System.out.println(myAnimal.getHabitat("Forest")); // LINE 1
System.out.println(myAnimal.getLegs(4)); // LINE 2
}
}

Choose the correct option.

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

Correct answer

  • B

Question 4

+4 marksOne correct option

Consider the Java code given below.

java
class TemperatureConverter {
public <T> void convert(T value) { // LINE 1
System.out.println("Converting: " + value);
}
public <U extends Number> void convert(U value) { // LINE 2
System.out.println("C to F: " + (value.doubleValue() * 9/5 + 32));
}
public <T extends Number> void convert(T value) { // LINE 3
System.out.println("C to K: " + (value.doubleValue() + 273.15));
}
}
public class Test {
public static void main(String[] args) {
TemperatureConverter temp = new TemperatureConverter();
temp.convert(25);
temp.convert(100.5);
temp.convert("Cold");
}
}

Choose the correct option.

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

Correct answer

  • C

Question 5

+4 marksOne correct option

Consider the code given below.

java
class Person {
public void work() {
System.out.println("works as a person.");
}
}
class Teacher extends Person {
public void work() {
System.out.println("Works as teacher.");
}
public void grade() {
System.out.println("Grading papers.");
}
}
class MathTeacher extends Teacher {
public void solveEquation() {
System.out.println("Solving complex equations.");
}
}
public class SchoolTest {
public static void main(String[] args) {
Teacher t = new MathTeacher(); // LINE 1
t.work();
t.grade(); // LINE 2
t.solveEquation(); // LINE 3
}
}

Choose the correct option.

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

Correct answer

  • D

Question 6

+4 marksOne correct option

Consider the Java code given below.

java
class Phone {
String model;
public Phone(String m) {
model = m;
}
public String toString() {
return "Model: " + model;
}
}
class Smartphone extends Phone {
int storage;
public Smartphone(String m, int s) {
super(m);
storage = s;
}
public Smartphone(Smartphone p) {
super(p.model);
storage = p.storage;
}
public String toString() {
return super.toString() + ", Storage: " + storage + "GB";
}
}
public class Store {
public static void main(String args[]) {
Phone p1 = new Smartphone("Galaxy S23", 128);
Phone p2 = new Smartphone((Smartphone) p1);
p2.model = "iPhone 15";
System.out.println(p1 + "\n" + p2);
}
}

What will the output be?

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

Correct answer

  • C

Question 7

+4 marksOne correct option

Consider the Java code given below.

java
abstract class SmartDevice {
abstract void operate();
void connect() { // LINE 1
System.out.println("Connecting smart device");
}
}
class SmartLight extends SmartDevice {
void operate() {
System.out.println("Turning on smart light");
}
void connect() {
System.out.println("Connecting smart light to Wi-Fi");
}
}
class SmartThermostat extends SmartDevice {
void operate() {
System.out.println("Setting smart thermostat");
}
void connect() {
System.out.println("Connecting thermostat to network");
}
}
public class SmartHome {
public static void main(String[] args) {
SmartDevice d1 = new SmartLight(); // LINE 2
SmartDevice d2 = new SmartThermostat(); // LINE 3
d1.connect();
d1.operate();
d2.connect();
d2.operate();
}
}

Choose the correct option.

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

Correct answer

  • C

Question 8

+4 marksOne correct option

Consider the Java code given below.

java
interface EventBooking {
void bookEvent(); //LINE 1
abstract void cancelEvent();
}
interface PaymentProcessing {
abstract void processPayment(double amount);
}
class EventManager implements EventBooking, PaymentProcessing { //LINE 2
public void bookEvent() {
System.out.println("Event booked successfully");
}
public void cancelEvent() {
System.out.println("Event cancelled successfully");
}
public void processPayment(double amount) {
System.out.println("Payment of " + amount + " processed");
}
}
public class Test {
public static void main(String[] args) {
EventBooking m = new EventManager();
m.bookEvent();
m.cancelEvent();
m.processPayment(150.0); //LINE 3
}
}

What will the output be?

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

Correct answer

  • D

Question 9

+4 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class Test {
public static void main(String[] args) {
Map<String, Integer> bonus2023 = new TreeMap<String, Integer>();
bonus2023.put("Arun", 5000);
bonus2023.put("Deepa", 4500);
bonus2023.put("Manoj", 6000);
bonus2023.put("Lekha", 5500);
Map<String, Integer> bonus2024 = new TreeMap<String, Integer>();
bonus2024.put("Arun", 5200);
bonus2024.put("Deepa", 4800);
bonus2024.put("Manoj", 6200);
bonus2024.put("Lekha", 5700);
Map<String, Integer> totalBonus = new TreeMap<String, Integer>();
for (Map.Entry<String, Integer> b1 : bonus2023.entrySet())
totalBonus.put(b1.getKey(), b1.getValue());
for (Map.Entry<String, Integer> b2 : bonus2024.entrySet())
totalBonus.merge(b2.getKey(), b2.getValue(), Integer::sum); // LINE 1
System.out.println(totalBonus);
}
}

Choose the correct option.

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

Correct answer

  • B

Question 10

+4 marksOne correct option

The 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.Stream;
public class Test {
public static void main(String[] args) {
Stream.iterate(50, n -> n - 7)
.map(n -> n / 2)
.filter(n -> n % 4 == 0)
.limit(3)
.forEach((x) -> System.out.print(x + " "));
}
}

What will the output be?

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

Correct answer

  • B

Question 11

+4 marksOne correct option

Consider the code given below.

java
class CricketMatch implements Cloneable {
String teamName;
int[] scores;
public CricketMatch(String n, int[] s) {
teamName = n;
scores = s;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
int[] s = {250, 300, 275};
CricketMatch m1 = new CricketMatch("India", s);
CricketMatch m2 = (CricketMatch) m1.clone();
CricketMatch m3 = m1;
m2.scores[1] = 320;
m3.teamName = "Australia";
System.out.println(m1.teamName + " " + m1.scores[1]);
System.out.println(m2.teamName + " " + m2.scores[1]);
System.out.println(m3.teamName + " " + m3.scores[1]);
}
}

What will the output be?

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

Correct answer

  • A

Question 12

+4 marksOne correct option

Method Optional.ofNullable(T value) returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional. Based on this description, consider the code given below, and answer the question that follows.

java
import java.util.*;
class HearingAid {
String brand;
String batteryType;
public HearingAid(String b, String bt) {
this.brand = b;
this.batteryType = bt;
}
}
public class Test {
public static void main(String[] args) {
var aidList = new ArrayList<HearingAid>();
aidList.add(new HearingAid("Phonak", "Rechargeable"));
aidList.add(new HearingAid("Widex", null));
for (HearingAid obj : aidList) {
Optional<String> op1 = Optional.ofNullable(obj.batteryType);
op1.ifPresent(type -> System.out.println(type));
}
}
}

Choose the correct option.

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

Correct answer

  • B

Question 13

+4 marksOne correct option

Consider the code given below.

java
import java.io.*;
class AutomaticGate implements Serializable {
private boolean isOpen;
private transient String location;
private transient int gCode;
// Constructor to initialize instance variable
public String toString() {
return "isOpen=" + isOpen + ", location=" + location + ", gCode=" + gCode;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(gCode + 123);
}
private void readObject(ObjectInputStream in) throws Exception {
in.defaultReadObject();
gCode = in.readInt() - 123;
}
}
public class Test {
public static void main(String[] args) throws Exception {
var fos = new FileOutputStream("GateData.txt");
var oos = new ObjectOutputStream(fos);
AutomaticGate gate1 = new AutomaticGate(true, "North Entrance", 456789);
oos.writeObject(gate1);
var fis = new FileInputStream("GateData.txt");
var ois = new ObjectInputStream(fis);
AutomaticGate obj = (AutomaticGate) ois.readObject();
System.out.println(obj);
}
}

What will the output be?

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

Correct answer

  • C

Question 14

+5 marksOne correct option

Consider the following Java code.

java
class MastersEligibility {
private double gpa;
private int entranceScore;
public MastersEligibility(double gpa, int score) {
assert gpa >= 0 && gpa <= 10 : "GPA must be between 0 and 10";
assert score >= 0 && score <= 100 : "Score must be between 0 and 100";
this.gpa = gpa;
this.entranceScore = score;
}
private boolean isEligible() {
return gpa >= 7.0 && entranceScore >= 50;
}
public void checkEligibility() {
assert isEligible() : "Not eligible for Master's program";
System.out.println("Eligible for Master's program");
}
}
class TestEligibility {
public static void main(String[] args) {
MastersEligibility student = new MastersEligibility(6.5, 55);
student.checkEligibility();
}
}

Choose the correct option when the class is executed as:

bash
java -ea TestEligibility
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • D

Question 15

+5 marksOne correct option

Consider the Java code given below. Assume the file "data.txt" already contains the following text:

All the best for exam

java
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("data.txt");
int i;
while ((i = fis.read()) != -1) {
if (i == 'e') {
System.out.print('#');
} else {
System.out.print((char) i);
}
}
fis.close();
} catch (IOException e) {
System.out.println("File not found!");
}
}
}

Choose the correct option regarding the output of the program:

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

Correct answer

  • A

Question 16

+5 marksOne correct option

Method Collectors.partitioningBy(predicate) returns a Collector which partitions the input elements according to a predicate, and organizes them into a Map<Boolean, List<T>>.

java
import java.util.*;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
var numbers = new ArrayList<Integer>(); // LINE 1
for (int i = 1; i <= 10; i++) {
numbers.add(i);
}
Map<Boolean, List<Integer>> partitioned = numbers.stream()
.collect(Collectors.partitioningBy(num -> num > 5));
System.out.println("true -> " + partitioned.get(true));
System.out.println("false -> " + partitioned.get(false));
}
}

Choose the correct option.

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

Correct answer

  • B

Question 17

+5 marksOne correct option
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 18

+4 marksOne or more correct options

Which of the following statements is/are correct regarding garbage collection?

Select all that apply.

  1. A

    The garbage collector frees unused memory in the heap.

  2. B

    The garbage collector frees unused memory in the stack.

  3. C

    The mark-and-sweep is a common garbage collection technique.

  4. D

    The garbage collection eliminates all memory leaks in every situation.

Show answer

Correct answers

  • A

    The garbage collector frees unused memory in the heap.

  • C

    The mark-and-sweep is a common garbage collection technique.

Question 19

+4 marksOne or more correct options

Consider the Java code given below.

java
import java.util.concurrent.ConcurrentHashMap;
class AddItemThread extends Thread {
private ConcurrentHashMap<String, Integer> cart;
public AddItemThread(ConcurrentHashMap<String, Integer> c) {
cart = c;
}
public void run() {
cart.put("item3", 1);
cart.put("item4", 2);
}
}
class ListItemThread extends Thread {
private ConcurrentHashMap<String, Integer> cart;
public ListItemThread(ConcurrentHashMap<String, Integer> c) {
cart = c;
}
public void run() {
for (String item : cart.keySet()) {
System.out.println(item + ": " + cart.get(item));
}
}
}
public class ShoppingCart {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> cart = new ConcurrentHashMap<>();
cart.put("item1", 1);
cart.put("item2", 2);
AddItemThread addItemThread = new AddItemThread(cart);
ListItemThread listItemThread = new ListItemThread(cart);
addItemThread.start();
listItemThread.start();
try {
addItemThread.join();
listItemThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final Cart: " + cart);
}
}

Which of the following is true about the given code.

Select all that apply.

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

Correct answers

  • A
  • B

Question 20

+4 marksOne or more correct options

Consider the code given below.

java
class ConferenceRoom {
private boolean isRoomAvailable = true;
public synchronized void bookRoom(String employeeName) {
if (isRoomAvailable) {
System.out.println(employeeName + " successfully booked the room.");
isRoomAvailable = false;
} else {
System.out.println(employeeName + " could not book the room.");
}
}
}
class Employee implements Runnable {
private ConferenceRoom conferenceRoom;
private String employeeName;
public Employee(ConferenceRoom cr, String en) {
conferenceRoom = cr;
employeeName = en;
}
public void run() {
conferenceRoom.bookRoom(employeeName);
}
}
public class Test{
public static void main(String[] args) {
ConferenceRoom room = new ConferenceRoom();
Thread emp1 = new Thread(new Employee(room, "Hema"));
Thread emp2 = new Thread(new Employee(room, "Geeta"));
Thread emp3 = new Thread(new Employee(room, "Sita"));
emp1.start();
emp2.start();
emp3.start();
}
}

Which of the following options is/are possible result/s of the above code?

Select all that apply.

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

Correct answers

  • B
  • C

Question 21

+4 marksOne or more correct options

Consider the Java code given below that processes Displayable objects in a shop. From among the options, identify the appropriate function header for function displayAll that takes as input a collection of Displayable objects and calls the display method on each.

java
import java.util.*;
interface Displayable {
void display();
}
class Electronics implements Displayable {
// Constructor
// method display() that prints electronic item details
}
class Clothing implements Displayable {
// Constructor
// method display() that prints clothing item details
}
public class Shop {
// LINE 1: FUNCTION HEADER
{
// invokes display() on each element
}
public static void main(String[] args) {
List<Displayable> items = new ArrayList<>();
items.add(new Electronics("Laptop", 50000));
items.add(new Clothing("T-Shirt", "Medium"));
displayAll(items);
}
}

Choose the correct option(s).

Select all that apply.

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

Correct answers

  • C
  • D

Question 22

+6 marksOne or more correct options

Consider the following Java code.

java
1. class Animal {
2. protected final void makeSound() {
3. System.out.println("Animal makes sound");
4. }
5. }
6. class Bird {
7. public void fly() {
8. System.out.println("Bird flies");
9. }
10. }
11. class Dog extends Animal{
12. public void makeSound(){
13. System.out.println("Dog barks");
14. }
15. }
16. class Parrot extends Animal, Dog{
17. }
18. public class Test {
19. public static void main(String[] args) {
20. Parrot p1 = new Animal();
21. Animal a1 = new Dog();
22. }
23. }

Identify the line/s which has/have errors.

Select all that apply.

  1. A

    Line 2

  2. B

    Line 7

  3. C

    Line 11

  4. D

    Line 12

  5. E

    Line 16

  6. F

    Line 20

  7. G

    Line 21

Show answer

Correct answers

  • D

    Line 12

  • E

    Line 16

  • F

    Line 20

Question 23

+6 marksOne or more correct options

Consider the Java code given below.

java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TokenSystem extends JFrame implements ActionListener {
JButton btnTakeToken, btnCancelToken;
JLabel label;
JPanel p1, p2;
public TokenSystem() {
p1 = new JPanel();
p2 = new JPanel();
btnTakeToken = new JButton("Take Token");
btnCancelToken = new JButton("Cancel Token");
label = new JLabel("Please select an option");
p1.add(btnTakeToken);
p1.add(btnCancelToken);
p2.add(label);
add(p1, "Center");
add(p2, "South");
btnTakeToken.setActionCommand("TakeToken");
btnCancelToken.setActionCommand("CancelToken");
btnTakeToken.addActionListener(this);
btnCancelToken.addActionListener(this);
setSize(350, 150);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// CODE SEGMENT
}
}
public class HospitalQueue {
public static void main(String[] args) {
new TokenSystem();
}
}

Select all that apply.

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

Correct answers

  • A
  • C