uiz Space

January 2025 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java Quiz 2: 16 March 2025 (January 2025 term)

The IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 16 Mar 2025, in the January 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
3
MCQ
12

Updated

Official paper: IIT M DIPLOMA AN EXAM QDD2 16 Mar 2025 · No negative marking.

Question 1

+6 marksOne or more correct options

Consider the Java code given below that prints the maximum element of a set. From among the options, identify the appropriate function header for function findMax that takes as input a set of numbers and prints the maximum element of the set.

java
import java.util.*;
class Test {
// FUNCTION HEADER for function findMax
{
// Prints the maximum element of the set
}
public static void main(String[] args) {
Set<Integer> s = new HashSet<>();
s.add(12);
s.add(23);
Set<Double> s1 = new HashSet<>();
s1.add(12.2);
s1.add(23.4);
findMax(s);
findMax(s1);
}
}

Choose the correct option(s)

Select all that apply.

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

Correct answers

  • B
  • C

Question 2

+6 marksOne correct option

Consider the Java code given below.

java
interface Deliverable {
public static void status() {
System.out.println("Delivery status");
}
public default void deliver() {
System.out.println("Order delivered");
}
}
interface Trackable {
public static void status() {
System.out.println("Tracking status");
}
public default void track() {
System.out.println("Tracking order");
}
}
class FoodOrder implements Deliverable, Trackable {
public void deliver() {
System.out.println("Delivered successfully");
}
}
public class Test {
public static void main(String[] args) {
Deliverable o1 = new FoodOrder();
o1.deliver();
o1.track();
Deliverable.status(); //LINE-1
Trackable.status(); //LINE-2
}
}

Choose the correct option.

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

Correct answer

  • C

Question 3

+6 marksOne correct option

The following code maps a set of patients to the number of visits they have made to a hospital and categorizes the patients based on whether they are regular visitors (more than 3 visits) or occasional visitors.

java
import java.util.*;
public class Hospital {
TreeSet<String> rPatients = new TreeSet<String>();
TreeSet<String> oPatients = new TreeSet<String>();
public boolean check(int visits) {
return visits > 3;
}
public void categorizePatients(HashMap<String, Integer> pMap) {
for (Map.Entry<String, Integer> entry : pMap.entrySet()) {
if (check(entry.getValue())) {
rPatients.add(entry.getKey());
} else {
oPatients.add(entry.getKey());
}
}
}
public void displayPatients() {
System.out.println("R Patients: " + rPatients);
System.out.println("O Patients: " + oPatients);
}
public static void main(String[] args) {
HashMap<String, Integer> pMap = new HashMap<String, Integer>();
pMap.put("James", 5);
pMap.put("Ram", 2);
pMap.put("Aisha", 4);
pMap.put("Tanya", 1);
pMap.put("Iva", 3);
Hospital hospital = new Hospital();
hospital.categorizePatients(pMap);
hospital.displayPatients();
}
}

Choose the correct option.

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

Correct answer

  • C

Question 4

+6 marksOne correct option

Consider the code given below.

java
interface Vehicle {
public abstract int getMileage();
}
class Car implements Vehicle, Cloneable {
int mileage;
public Car(int m) {
mileage = m;
}
public int getMileage() {
return mileage;
}
public Car clone() throws CloneNotSupportedException {
return (Car) super.clone();
}
}
public class Test {
public static void main(String[] args) {
Car car1 = new Car(25);
try {
Car car2 = car1.clone();
car1.mileage = 30;
System.out.print(car1.getMileage() + car2.getMileage());
} catch (CloneNotSupportedException e) {
System.out.println("Cloning not supported");
}
}
}

What will the output be?

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

Correct answer

  • A

Question 5

+6 marksOne correct option

Consider the following Java code.

java
import java.util.*;
class Artist {
String name;
int experience;
// Constructor to initialize instance variables
public Artist(String n, int e) {
name = n;
experience = e;
}
public String toString() {
return name;
}
}
public class Test {
public static void modifyArtistList(List<Artist> artistList) {
ListIterator<Artist> it = artistList.listIterator();
while (it.hasNext()) {
Artist a = it.next();
if (a.experience < 5) {
//LINE 1
}
}
}
public static void main(String[] args) {
var artists = new ArrayList<Artist>();
artists.add(new Artist("Tagore", 12));
artists.add(new Artist("Sudha", 3));
artists.add(new Artist("Amitabh", 7));
artists.add(new Artist("Sharukh", 5));
modifyArtistList(artists);
System.out.println(artists);
}
}

Choose the correct option to be filled in place of LINE 1 so that the output is:
[Tagore, Amitabh, Sharukh]

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

Correct answer

  • C

Question 6

+6 marksOne correct option

Consider the following Java code.

java
class SalaryProcessor {
public static void calSalary(int bs, int bonus) throws Exception {
if (bonus < 0) {
throw new Exception("Invalid bonus");
}
System.out.println("Total Salary: " + (bs + bonus));
}
public static void processSalaries() throws Exception {
try {
calSalary(50000, 10000);
calSalary(60000, -5000);
} catch (Exception e) {
System.out.println("Caught in processSalaries");
throw e;
}
}
}
public class Test {
public static void main(String[] args) throws Throwable {
try {
SalaryProcessor.processSalaries();
} catch (Exception e) {
System.out.println("Caught in main");
}
}
}

Choose the correct option.

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

Correct answer

  • C

Question 7

+7 marksOne correct option

Consider the Java code given below.

java
class RegException extends Exception {
public RegException(String message) {
super(message);
}
}
class User {
private String email;
public User(String e) {
email = e;
}
public void register() throws RegException {
if (!email.contains("@")) {
throw new RegException("Registration failed: Invalid email format");
}
System.out.println("Registration successful");
}
}
public class Test {
public static void main(String[] args) {
User u1 = new User("anu.kuriyan@ex.com");
User u2 = new User("narendran.pk");
try {
u1.register();
u2.register();
} catch (RegException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

Choose the correct option.

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

Correct answer

  • A

Question 8

+7 marksOne correct option

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

A.java

java
package pack1;
public class A {
void AMethod() {
System.out.println("Method A");
}
private void BMethod() {
System.out.println("Method B");
}
protected void CMethod() {
System.out.println("Method C");
}
public void DMethod() {
System.out.println("Method D");
}
}

B.java

java
package pack2;
import pack1.A;
public class B extends A {
public void testMethods() {
super.AMethod(); // LINE 1
super.BMethod(); // LINE 2
super.CMethod(); // LINE 3
super.DMethod(); // LINE 4
}
}

Choose the correct option.

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

Correct answer

  • D

Question 9

+7 marksOne correct option

Consider the Java code given below.

java
public class AssertTest{
public static double caltempDiff(double currentTemp, double idealTemp) {
double tempdiff = 0;
assert currentTemp >= -100 && currentTemp <= 150 ; // assert-1
assert idealTemp >= -100 && idealTemp <= 150 ; // assert-2
tempdiff = currentTemp - idealTemp;
assert tempdiff >= 0 ; // assert-3
return tempdiff;
}
public static void main(String[] args) {
double currentTemp = 35.0;
double idealTemp = 37.0;
assert currentTemp >= -100 && currentTemp <= 150 ; // assert-4
double tempDifference =caltempDiff(currentTemp, idealTemp);
}
}

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

  • C

Question 10

+7 marksOne correct option

Consider the Java code given below.

java
interface Washable {
void showWashable();
}
class FrontLoadWasher implements Washable{
public void showWashable() {
System.out.println("FLW is washable");
}
}
class TopLoadWasher implements Washable {
public void showWashable() {
System.out.println("TLW is washable");
}
}
class ApplianceList {
private Object[] appliances = {new FrontLoadWasher(), new TopLoadWasher()};
public void testAppliances() {
for (int i = 0; i < appliances.length; i++) {
//LINE-1
}
}
}
public class Test {
public static void main(String[] args) {
ApplianceList aL = new ApplianceList();
aL.testAppliances();
}
}

Identify the appropriate option to fill in place of LINE 1 such that the output is:
FLW is washable TLW is washable

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

Correct answer

  • A

Question 11

+7 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)), ...
Based on the above information, consider the code given below, and answer the question that follows.

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

What will the output be?

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

Correct answer

  • C

Question 12

+7 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class SetExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.add("Apple");
list.add("Samsung");
list.add("Nokia");
list.add("Samsung");
Set<String> set1 = new LinkedHashSet<String>();
Set<String> set2 = new TreeSet<String>();
for (String str : list) {
set1.add(str);
set2.add(str);
}
for (String str : set1) {
System.out.print(str + " ");
}
System.out.println();
for (String str : set2) {
System.out.print(str + " ");
}
}
}

What will the output be?

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

Correct answer

  • A

Question 13

+7 marksOne or more correct options

Consider the Java code given below.

java
class RollNumber<T extends Number> {
private T part1;
private T part2;
public RollNumber(T p1, T p2) {
part1 = p1;
part2 = p2;
}
public T getPart1() {
return part1;
}
public T getPart2() {
return part2;
}
public RollNumber<Integer> genRollno(ARG) { //LINE 1
RollNumber<Integer> newroll = new RollNumber<>(0, 0);
newroll.part1 = this.part1.intValue() + r.getPart1().intValue();
newroll.part2 = this.part2.intValue() + r.getPart2().intValue();
return newroll;
}
}
public class Test {
public static void main(String[] args) {
RollNumber<Integer> r1 = new RollNumber<>(101, 200);
RollNumber<Integer> r2 = new RollNumber<>(102, 300);
RollNumber<Integer> newroll = r1.genRollno(r2);
System.out.println(newroll.getPart1() + "," + newroll.getPart2());
}
}

Choose all the options which can be used in place of ARG in LINE 1 so that the code outputs the sum of two roll numbers.

Select all that apply.

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

Correct answers

  • A
  • C

Question 14

+7 marksOne or more correct options

The Java code given below should print the names of water filters whose flow rate falls within the range of 5.0 to 15.0 (inclusive).

java
import java.util.*;
import java.util.stream.*;
class WaterFilter {
String name;
double flowRate;
public WaterFilter(String n, double f) {
this.name = n;
this.flowRate = f;
}
}
public class Test {
public static void main(String[] args) {
List<WaterFilter> filters = new ArrayList<>();
filters.add(new WaterFilter("FilterA", 4.5));
filters.add(new WaterFilter("FilterB", 10.0));
filters.add(new WaterFilter("FilterC", 12.0));
filters.add(new WaterFilter("FilterD", 15.0));
filters.add(new WaterFilter("FilterE", 16.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
  2. B
  3. C
  4. D
Show answer

Correct answers

  • B
  • C

Question 15

+8 marksOne correct option

Consider the Java code given below.

java
public class MarksExample {
public <T extends Number> void calculateSum(T[] marks){
// Calculates the sum of marks
}
public <T> void displayMarks(T[] marks){
// Displays the marks
}
public <T extends Number> double calculateAverage(List<T> marks){
// Calculates the average of marks
}
}

How does class MarksExample look after type erasure?

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

Correct answer

  • B