uiz Space

January 2025 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java End Term: 13 April 2025, Set QDD1 (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 QDD1: 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
16
MSQ
7

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 = 0;
}
class Adder 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 Adder();
Thread t2 = new Adder();
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

  • B

    The code prints any number in the range 1000 to 2000

Question 2

+4 marksOne correct option

Consider the following Java code that uses chained exceptions.

java
class FileStorageException extends Exception {
public FileStorageException(String message) {
super(message);
}
}
class FileUploadException extends Exception {
public FileUploadException(String message) {
super(message);
}
}
class FileService {
public void uploadFile(String filename) throws FileUploadException {
try {
// if(do not have enough disk space to store the file)
throw new FileStorageException("Insufficient disk space");
// else upload the file
} catch (FileStorageException e) {
FileUploadException fue = new FileUploadException("Failed to upload");
fue.initCause(e);
throw fue;
}
}
}
public class FileApp {
public static void main(String[] args) {
FileService fileService = new FileService();
try {
fileService.uploadFile("report.pdf");
} catch (FileUploadException e) {
System.out.println(e.getMessage());
System.out.println("Reason: " + 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

  • A

Question 3

+4 marksOne correct option

Consider the two Java files given below.

Animal.java:

java
package creatures;
public class Animal {
private String name;
private String habitat;
private int legs;
// Constructor to initialize instance variables
public String getName(String name) {
return name;
}
protected String getHabitat(String habitat) {
return habitat;
}
private int getLegs(int legs) {
return legs;
}
public int showLegs(int legs) {
return getLegs(legs);
}
}

Test.java:

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

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 following Java code.

java
class CreditCard {
private double limit, balance, interestRate;
public CreditCard(double l, double b, double i) {
assert i > 0 : "Interest rate must be positive";
limit = l;
balance = b;
interestRate = i;
}
private boolean isValidTransaction(double amount) {
return (balance + amount <= limit);
}
public double processPayment(double amount) {
assert amount > 0 : "Payment must be positive";
assert isValidTransaction(amount) : "Transaction exceeds credit limit";
balance += amount;
return balance;
}
}
class CreditCardTest {
public static void main(String[] args) {
CreditCard card = new CreditCard(5000.00, 4000.00, 18.99);
System.out.println(card.processPayment(2000.00));
}
}

Choose the correct option when the class is executed as:

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

Correct answer

  • C

Question 5

+4 marksOne correct option

Consider the Java code given below.

java
class Calculator {
public <T extends Number> void calculate(T value) { // LINE 1
System.out.println("Calculating: " + (value.doubleValue() * 2));
}
public <U extends Number> void calculate(U value) { // LINE 2
System.out.println("Double Calculation: " + (value.doubleValue() * 4));
}
public <T> void calculate(T value) { // LINE 3
System.out.println("Printing: " + value);
}
}
public class Test {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.calculate(5);
calc.calculate(2.1);
calc.calculate("Hello");
}
}

Choose the correct option.

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

Correct answer

  • C

Question 6

+4 marksOne correct option

Consider the Java code given below.

java
class Executive {
String name;
public Executive(String n) {
this.name = n;
}
public String toString() {
return "name = " + name;
}
}
class Director extends Executive {
int project_count;
public Director(String n, int p) {
super(n);
project_count = p;
}
public Director(Director d) {
super(d.name);
project_count = d.project_count;
}
public String toString() {
return super.toString() + ", project_count = " + project_count;
}
}
public class Corporation {
public static void main(String args[]) {
Executive d1 = new Director("Anjali", 5);
Executive d2 = new Director((Director) d1);
d2.name = "Rajesh";
System.out.println(d1 + "\n" + d2);
}
}

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 Vehicle {
abstract void start();
void fuel() { // LINE 1
System.out.println("Fueling vehicle");
}
}
class Car extends Vehicle {
void start() {
System.out.println("Starting car");
}
void fuel() {
System.out.println("Filling petrol in car");
}
}
class Bike extends Vehicle {
void start() {
System.out.println("Starting bike");
}
void fuel() {
System.out.println("Filling petrol in bike");
}
}
public class Test {
public static void main(String[] args) {
Vehicle v1 = new Car(); // LINE 2
Vehicle v2 = new Bike(); // LINE 3
v1.fuel();
v1.start();
v2.fuel();
v2.start();
}
}

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 LightControl {
void turnOn(); //LINE 1
abstract void turnOff();
}
interface TemperatureControl {
abstract void setTemperature(int temp);
}
class SmartHome implements LightControl, TemperatureControl { //LINE 2
public void turnOn() {
System.out.println("Lights turned ON");
}
public void turnOff() {
System.out.println("Lights turned OFF");
}
public void setTemperature(int temp) {
System.out.println("Temperature set to " + temp + "°C");
}
}
public class Test {
public static void main(String[] args) {
LightControl home = new SmartHome();
home.turnOn();
home.turnOff();
home.setTemperature(22); //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> exam1 = new TreeMap<String, Integer>();
exam1.put("Amit", 85);
exam1.put("Neha", 78);
exam1.put("Rahul", 90);
exam1.put("Priya", 88);
Map<String, Integer> exam2 = new TreeMap<String, Integer>();
exam2.put("Amit", 80);
exam2.put("Neha", 85);
exam2.put("Rahul", 92);
exam2.put("Priya", 86);
Map<String, Integer> totalMarks = new TreeMap<String, Integer>();
for (Map.Entry<String, Integer> e1 : exam1.entrySet())
totalMarks.put(e1.getKey(), e1.getValue());
for (Map.Entry<String, Integer> e2 : exam2.entrySet())
totalMarks.merge(e2.getKey(), e2.getValue(), Integer::sum); // LINE 1
System.out.println(totalMarks);
}
}

Choose the correct option.

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

Correct answer

  • B

Question 10

+4 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.Stream;
public class Test {
public static void main(String[] args) {
Stream.iterate(2, n -> n + 3)
.map(n -> n * 2)
.filter(n -> n % 5 == 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 Rider implements Cloneable {
String name;
int[] distances;
public Rider(String n, int[] d) {
name = n;
distances = d;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test{
public static void main(String[] args) throws CloneNotSupportedException {
int[] d = {50, 60, 70};
Rider r1 = new Rider("Raj", d);
Rider r2 = (Rider) r1.clone();
Rider r3 = r1;
r2.distances[1] = 90;
r3.name = "Amit";
System.out.println(r1.name + " " + r1.distances[1]);
System.out.println(r2.name + " " + r2.distances[1]);
System.out.println(r3.name + " " + r3.distances[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 GrindingMachine {
String brand;
String powerSource;
public GrindingMachine(String b, String pS) {
this.brand = b;
this.powerSource = pS;
}
}
public class Test {
public static void main(String[] args) {
var machineList = new ArrayList<GrindingMachine>();
machineList.add(new GrindingMachine("Bosch", "Electric"));
machineList.add(new GrindingMachine("Hitachi", null));
for (GrindingMachine obj : machineList) {
Optional<String> op1 = Optional.ofNullable(obj.powerSource);
op1.ifPresent(source -> System.out.println(source));
}
}
}

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 Laptop implements Serializable {
private double price;
private transient String model;
private transient int serialNum;
// Constructor to initialize instance variables
public String toString() {
return "price=" + price + ", model=" + model + ", serialNum=" + serialNum;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(serialNum + 100);
}
private void readObject(ObjectInputStream in) throws Exception {
in.defaultReadObject();
serialNum = in.readInt() - 100;
}
}
public class Test {
public static void main(String[] args) throws Exception {
var fos = new FileOutputStream("Laptop.txt");
var oos = new ObjectOutputStream(fos);
Laptop l1 = new Laptop(899.99, "Dell", 987654);
oos.writeObject(l1);
var fis = new FileInputStream("Laptop.txt");
var ois = new ObjectInputStream(fis);
Laptop obj = (Laptop) 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 Java code given below.

Assume the file "data.txt" already contains the following text:

Welcome to Java Programming

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 == 'a') {
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 15

+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 = 10; i <= 30; i += 5) {
numbers.add(i);
}
Map<Boolean, List<Integer>> partitioned = numbers.stream()
.collect(Collectors.partitioningBy(num -> num % 2 == 0));
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

  • A

Question 16

+6 marksOne correct option

Consider the Java code given below.

java
import javax.swing.*;
import java.awt.*;
public class FeedbackForm extends JFrame {
JPanel pnlName, pnlFeedback, pnlSubmit;
JLabel lblName, lblFeedback;
JTextField txtName;
JTextArea txtFeedback;
JButton btnSubmit;
public FeedbackForm() {
lblName = new JLabel("Name:");
lblFeedback = new JLabel("Feedback:");
txtName = new JTextField(15);
txtFeedback = new JTextArea(5, 20);
btnSubmit = new JButton("Submit");
pnlName = new JPanel();
//add lblName and txtName to pnlName
pnlFeedback = new JPanel();
//add lblFeedback and txtFeedback to pnlFeedback
pnlSubmit = new JPanel();
//add btnSubmit to pnlSubmit
//CODE BLOCK
setVisible(true);
setSize(400, 300);
}
public static void main(String[] args) {
new FeedbackForm();
}
}
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 17

+4 marksOne or more correct options

Which of the following statements is/are correct?

Select all that apply.

  1. A

    The heap stores dynamically allocated data.

  2. B

    Activation records created for functions are allocated on the heap.

  3. C

    The heap storage needs to be explicitly requested by the programmer.

  4. D

    Storage allocated on the heap cannot be deallocated in any way.

Show answer

Correct answers

  • A

    The heap stores dynamically allocated data.

  • C

    The heap storage needs to be explicitly requested by the programmer.

Question 18

+4 marksOne or more correct options

Consider the Java code given below.

java
import java.util.concurrent.ConcurrentHashMap;
class UpdaterThread extends Thread {
private ConcurrentHashMap<String, Integer> map;
public UpdaterThread(ConcurrentHashMap<String, Integer> m) {
map = m;
}
public void run() {
map.put("C", 3);
map.put("D", 4);
}
}
class IteratorThread extends Thread {
private ConcurrentHashMap<String, Integer> map;
public IteratorThread(ConcurrentHashMap<String, Integer> m) {
map = m;
}
public void run() {
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
}
}
public class Test {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.put("A", 1);
map.put("B", 2);
UpdaterThread updaterThread = new UpdaterThread(map);
IteratorThread iteratorThread = new IteratorThread(map);
updaterThread.start();
iteratorThread.start();
try {
updaterThread.join();
iteratorThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final Map: " + map);
}
}

Which of the following is/are true about the given code.

Select all that apply.

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

Correct answers

  • A
  • C

Question 19

+4 marksOne or more correct options

Consider the code given below.

java
class Library {
private boolean isBookAvailable = true;
public synchronized void borrowBook(String readerName) {
if (isBookAvailable) {
System.out.println(readerName + " borrowed the book.");
isBookAvailable = false;
} else {
System.out.println(readerName + " could not borrow the book.");
}
}
}
class Reader implements Runnable {
private Library library;
private String readerName;
public Reader(Library lib, String rn) {
this.library = lib;
this.readerName = rn;
}
public void run() {
library.borrowBook(readerName);
}
}
public class Test {
public static void main(String[] args) {
Library library = new Library();
Thread reader1 = new Thread(new Reader(library, "Rahul"));
Thread reader2 = new Thread(new Reader(library, "Ram"));
Thread reader3 = new Thread(new Reader(library, "Raghu"));
reader1.start();
reader2.start();
reader3.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

  • A
  • C

Question 20

+4 marksOne or more correct options

Consider the Java code given below that processes Drawable objects. From among the options, identify the appropriate function header for function drawAll that takes as input a collection of Drawable objects and calls the draw method on each.

java
import java.util.*;
interface Drawable {
void draw();
}
class Rectangle implements Drawable {
// Constructor
// method draw() that prints rectangle details
}
class Triangle implements Drawable {
// Constructor
// method draw() that prints triangle details
}
public class Canvas {
// LINE 1: FUNCTION HEADER
{
// invokes draw() on each element
}
public static void main(String[] args) {
Set<Drawable> shapes = new HashSet<>();
shapes.add(new Rectangle(4, 5));
shapes.add(new Triangle(3, 4, 5));
drawAll(shapes);
}
}

Choose the correct option(s).

Select all that apply.

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

Correct answers

  • C
  • D

Question 21

+4 marksOne or more correct options

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) {
Person p = new MathTeacher(); // LINE 1
p.work();
p.grade(); // LINE 2
p.solveEquation(); // LINE 3
}
}

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 Parrot extends Animal, Bird {
12. public void makeSound() {
13. System.out.println("Parrot squawks");
14. }
15. }
16. public class TestProgram {
17. public static void main(String[] args) {
18. Parrot p1 = new Animal();
19. }
20. }

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 18

Show answer

Correct answers

  • C

    Line 11

  • D

    Line 12

  • E

    Line 18

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 OrderDemo extends JFrame implements ActionListener {
JButton btnOrder, btnCancel;
JLabel label;
JPanel p1, p2;
public OrderDemo() {
p1 = new JPanel();
p2 = new JPanel();
btnOrder = new JButton("Order");
btnCancel = new JButton("Cancel Order");
label = new JLabel("Select an option");
p1.add(btnOrder);
p1.add(btnCancel);
p2.add(label);
add(p1, "Center");
add(p2, "South");
btnOrder.setActionCommand("Order");
btnCancel.setActionCommand("Cancel");
btnOrder.addActionListener(this);
btnCancel.addActionListener(this);
setSize(350, 150);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// CODE SEGMENT
}
}
public class OrderTest {
public static void main(String[] args) {
new OrderDemo();
}
}

Select all that apply.

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

Correct answers

  • A
  • C