Quiz Space

September 2023 term · Programming Concepts using Java · BSCS2005

Java End Term: 24 December 2023, Set FDD1 (September 2023 term)

The IIT Madras BS Programming Concepts using Java (Java) End Term paper sat on 24 Dec 2023, in the September 2023 term, set FDD1: 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
19
MSQ
4

Updated

Official paper: IIT M DIPLOMA FN EXAM FDD1 24 Dec 2023 · No negative marking.

Question 1

+5 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
import java.util.stream.*;
class Player {
private String name;
int point;
//Constructor to initialize instance variables
public String toString() {
return name;
}
}
public class Test {
public static void main(String[] args) {
var pArr = new ArrayList<Player>();
pArr.add(new Player("Sharan", 150));
pArr.add(new Player("Virat", 500));
pArr.add(new Player("Rahul", 100));
pArr.add(new Player("Sanju", 250));
Map<Boolean, List<Player>> playMap;
playMap = pArr.stream()
.collect(Collectors.partitioningBy(p -> p.point >= 250));
System.out.println(playMap.get(false));
}
}

Choose the correct option.

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

Correct answer

  • C

Question 2

+5 marksOne correct option

Consider the code given below.

java
class Doctor implements Cloneable{
private String name;
private String[] qualification ;
public Doctor(String n, String[] q) {
name = n;
qualification = q;
}
//method setName(String name) to initialize name
//method setQualification(int indx,String q) to initialize
//qualification at a particular index
//method getName() to return name
//method getQualification(int indx) to return qualification
//at a particular index
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
public class Testclone {
public static void main(String[] args) throws CloneNotSupportedException{
String[] q = {"MBBS", "MD", "M.Ch"};
Doctor d1 = new Doctor("Mathew", q);
Doctor d2 = (Doctor)d1.clone();
Doctor d3 = d1;
d2.setQualification(1, "MS");
d3.setName("John");
System.out.println(d1.getName() + " " + d1.getQualification(1));
System.out.println(d2.getName() + " " + d2.getQualification(1));
System.out.println(d3.getName() + " " + d3.getQualification(1));
}
}

Choose the correct option.

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

Correct answer

  • C

Question 3

+5 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(10, n -> n - 1)
.map(n -> n * 3)
.filter(n -> n % 5 == 0)
.limit(4)
.forEach(x -> System.out.print(x + " "));
}
}

What will the output be?

  1. A

    10 5 0 -5

  2. B

    30 27 24 21

  3. C

    30 15 0 -15

  4. D

    0 -15 -30 -45

Show answer

Correct answer

  • C

    30 15 0 -15

Question 4

+6 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
import java.util.concurrent.*;
class Example extends Thread{
Map siMap;
Example(Map m){
this.siMap = m;
}
public void run(){
siMap.put("D",4);
}
}
public class Test{
public static void main (String[] args) throws InterruptedException{
Map<String, Integer> siMap = new LinkedHashMap<String, Integer>();
String[] str = {"A", "B", "C"};
Integer[] arr = {1, 2, 3};
for(int i = 0; i < str.length; i++){
siMap.put(str[i],arr[i]);
}
Example t = new Example(siMap);
t.start();
t.join();
Set s = siMap.entrySet();
Iterator itr = s.iterator();
while(itr.hasNext()){
Map.Entry m = (Map.Entry)itr.next();
System.out.println(m.getKey() + " => " + m.getValue());
}
}
}

Which of the following is true about the given code?

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

Correct answer

  • B

Question 5

+6 marksOne correct option

Consider the Java program given below.

  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 InsufficientFundsException extends Exception {
public InsufficientFundsException(String str) {
super(str);
}
}
class Account {
private double balance;
// Constructor to initialize the balance
public void withdraw(double amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Insufficient funds to withdraw");
} else {
balance -= amount;
}
}
}
public class Test {
public static void main(String[] args) {
var a1 = new Account(1000.0);
var a2 = new Account(500.0);
try {
a1.withdraw(1200.0);
a2.withdraw(700.0);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
}
}

Choose the correct option.

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

Correct answer

  • A

Question 7

+4 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class FinalMarks{
public static void main(String[] args) {
Map<String, Integer> sem1 = new TreeMap<String, Integer>();
sem1.put("Geography", 80);
sem1.put("Mathematics", 100);
sem1.put("Science", 95);
sem1.put("Chemistry", 85);
Map<String, Integer> sem2 = new TreeMap<String, Integer>();
sem2.put("Politics", 70);
sem2.put("Mathematics", 90);
sem2.put("Science", 100);
sem2.put("Chemistry", 85);
Map<String, Integer> aggregateMarks = new TreeMap<String, Integer>();
for(Map.Entry<String, Integer> e : sem1.entrySet())
aggregateMarks.put(e.getKey(), e.getValue());
for(Map.Entry<String, Integer> e : sem2.entrySet())
aggregateMarks.merge(e.getKey(), e.getValue(), (x, y) -> y + x);//LINE 1
System.out.println(aggregateMarks);
}
}

Choose the correct option.

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

Correct answer

  • B

Question 8

+4 marksOne correct option

Method Optional.ofNullable(T value) returns an Optional that describes the specific 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 Monument{
public String getState(String name) {
String answer;
switch(name) {
case "Taj Mahal": answer = "India";
break;
case "Machu Picchu": answer = "Peru";
break;
default: answer = null;
break;
}
return answer;
}
}
public class OptionalTest {
public static void main(String[] args){
Optional<String> op = Optional.ofNullable(new Monument()
.getState("Statue of Liberty"));
op.ifPresent(n -> System.out.println(n.toUpperCase()));
}
}

Choose the correct option.

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

Correct answer

  • A

Question 9

+4 marksOne correct option

Consider the Java code given below.

java
class Faculty{
private String name;
public Faculty(String n){
this.name = n;
}
public Faculty(Faculty f){
this.name = f.name;
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
}
public class Test{
public static void main(String[] args){
Faculty f1 = new Faculty("Sundaran");
Faculty f2 = f1;
Faculty f3 = new Faculty(f1);
f1.setName("Krishnan");
System.out.println("f2.name : " + f2.getName());
System.out.println("f3.name : " + f3.getName());
}
}

What will the output be?

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

Correct answer

  • A

Question 10

+4 marksOne correct option

Consider the code given below.

java
class CustomSequence extends Thread {
private int start;
public CustomSequence(int s) {
start = s;
}
public void run() {
for (int i = start; i <= start + 9; i++) {
System.out.print(i + " ");
try {
sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test{
public static void main(String[] args) throws InterruptedException {
Thread th1 = new CustomSequence(1);
Thread th2 = new CustomSequence(11);
Thread th3 = new CustomSequence(21);
th1.start();
th1.join();
th2.start();
th2.join();
th3.start();
}
}

Choose the correct option.

  1. A

    It may print 1 to 10, 11 to 20, and 21 to 30 in an interleaved manner.

  2. B

    It prints 21 to 30 first, followed by 1 to 10, and 11 to 20 in an interleaved manner.

  3. C

    It may print 1 to 10 and 11 to 20, in an interleaved manner, followed by 21 to 30.

  4. D

    It prints 1 to 10 first, followed by 11 to 20, and followed by 21 to 30.

Show answer

Correct answer

  • D

    It prints 1 to 10 first, followed by 11 to 20, and followed by 21 to 30.

Question 11

+4 marksOne correct option

Consider the Java code given below.

java
interface Displayable {
public default void display() {
System.out.println("Prints documents");
}
}
class Printer implements Displayable { // LINE 1
}
class Scanner implements Displayable {
public void display() {
System.out.println("Scans documents and prints");
}
}
public class Test {
public static void main(String[] args) {
Displayable d1 = new Printer();
d1.display(); // LINE 2
Displayable d2 = new Scanner();
d2.display();
}
}

Choose the correct option.

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

Correct answer

  • B

Question 12

+4 marksOne correct option

Consider the code given below.

java
class Sports {
public void rules() {
System.out.println("Follow the rules");
}
public void play() {
System.out.println("Playing");
}
}
class Indoor extends Sports {
public void play() {
System.out.println("Playing indoor");
}
public void score() {
System.out.println("Scored a point");
}
}
class Basketball extends Indoor {
public void score() {
System.out.println("Goal in basketball");
}
}
public class Test {
public static void main(String[] args) {
Sports sport = new Basketball();
sport.rules();
sport.play();
sport.score(); //LINE 1
}
}

Choose the correct option.

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

Correct answer

  • C

Question 13

+4 marksOne correct option

Consider the code given below.

java
class Product {
private int productId;
private double price;
public Product(int id, double p) {
productId = id;
price = p;
}
public final double discount() {
return 0.1 * price;
}
}
class PremiumProduct extends Product {
public PremiumProduct(int id, double p) {
super(id, p);
}
public final double discount() { //LINE 1
return 0.2 * price; //LINE 2
}
}
public class Test {
public static void main(String[] args) {
Product p1 = new PremiumProduct(101, 500.0); //LINE 3
PremiumProduct pp1 = new Product(202, 700.0); //LINE 4
p1.discount();
pp1.discount();
}
}

Which of the following statements is FALSE?

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

Correct answer

  • C

Question 14

+4 marksOne correct option

Consider the following block of code.

java
// ...
Integer i = sc.nextInt();
if(i < 0)
throw new RuntimeException("Input is a negative integer");
// ...

Which among the following code blocks can replace the given code in order to generate a customized assertion error as shown below?

text
Exception in thread "main" java.lang.AssertionError: Input is a negative integer
at PositiveAssert.main(PositiveAssert.java:<line number>)
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • D

Question 15

+4 marksOne correct option

Consider the code given below.

java
interface Communication {
void connect();
}
class Device {
public WiredCommunication getCommunication1() {
return new WiredCommunication();
}
public WirelessCommunication getCommunication2() {
return new WirelessCommunication();
}
private class WiredCommunication implements Communication {
public void connect() {
System.out.println("Wired connection established");
}
}
private class WirelessCommunication extends WiredCommunication {
public void connect() {
System.out.println("Wireless connection established");
}
}
}
public class Test {
public static void main(String[] args) {
Device d = new Device();
//CODE BLOCK
obj1.connect();
obj2.connect();
}
}

Choose the correct option(s) to fill in place of CODE BLOCK so that the output is:
Wired connection established
Wireless connection established

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

Correct answer

  • D

Question 16

+4 marksOne correct option

FileOutputStream(String name,boolean append) method creates a file output stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
Consider the code given below. Assume that, before execution of the given code, the files "file1.txt" and "file2.txt" have the following text in them.

text
What is your name?
java
public class Test {
public static void main(String[] args) {
try {
var out = new FileOutputStream("file1.txt", true);
var dout = new DataOutputStream(out);
dout.writeBytes(", Where are u from?");
dout.close();
var out2 = new FileOutputStream("file2.txt", false);
var dout2 = new DataOutputStream(out2);
dout2.writeBytes(", Where are u from?");
dout2.close();
}
catch(IOException e) {
System.out.println(e);
}
}
}

Choose the correct option regarding the contents of file1.txt and file2.txt after the program finishes execution.

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

Correct answer

  • C

Question 17

+4 marksOne correct option

Consider the Java code given below.

java
1 class Example {
2 public void methodOne() {
3 // ...
4 methodTwo();
5 // ...
6 }
7 public void methodTwo() {
8 // ...
9 }
10 public static void methodThree() {
11 // ...
12 Example example = new Example();
13 example.methodOne();
14 // ...
15 }
16 public static void main(String[] args) {
17 // ...
18 methodThree();
19 }
20 }

During the execution of Line 5 in the given code, which method's activation record is at the top of the stack of activation records?

  1. A

    main

  2. B

    methodOne

  3. C

    methodTwo

  4. D

    methodThree

Show answer

Correct answer

  • B

    methodOne

Question 18

+4 marksOne correct option

Consider the Java code given below.

java
import java.io.*;
class IDCard implements Serializable {
private String cardNo = "******";
private transient int accessCode = 1000;
private String issueDate = "00/00";
public IDCard(String cno, int a, String i) {
cardNo = cno;
accessCode = a;
issueDate = i;
}
public String toString() {
return cardNo + ", " + accessCode + ", " + issueDate;
}
}
public class Test {
public static void main(String[] args) throws Exception {
var fos = new FileOutputStream("idcard.txt");
var os = new ObjectOutputStream(fos);
os.writeObject(new IDCard("ID123456", 9999, "11/23"));
var fis = new FileInputStream("idcard.txt");
var ois = new ObjectInputStream(fis);
IDCard card = (IDCard) ois.readObject();
System.out.println(card);
}
}

What will the output be?

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

Correct answer

  • B

Question 19

+4 marksOne correct option

Consider the Java code given below.

java
class Exponential {
private int base;
public Exponential(int base) {
this.base = base;
}
public int power(int exponent) {
assert exponent >= 0; // LINE 1
if (exponent == 0)
return 1;
assert base > 0; // LINE 2
return base * power(exponent - 1);
}
}
class AssertionTest {
public static void main(String[] args) {
Exponential obj1 = new Exponential(2);
Exponential obj2 = new Exponential(-3);
int result1 = obj1.power(4);
assert result1 > 0; //LINE 3
System.out.println(result1);
int result2 = obj2.power(3);
assert result2 > 0; //LINE 4
System.out.println(result2);
}
}

Choose the correct option when the program is executed as:
java -ea AssertionTest

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

Correct answer

  • C

Question 20

+4 marksOne or more correct options

Consider the Java code given below.

java
class Hotel {
int available = 1;
public synchronized void bookRoom(int n, String name) {
if (available >= n) {
available = available - n;
System.out.println(name + " booked " + n + " room");
} else {
System.out.println(name + " cannot book " + n + " room");
}
}
}
class RoomBooking implements Runnable {
private Hotel h;
private String name;
private int n_rooms;
public RoomBooking(Hotel h, String n, int r) {
// constructor
}
public void run() {
h.bookRoom(n_rooms, name);
}
}
public class Test {
public static void main(String[] args) {
Hotel obj = new Hotel();
RoomBooking rb1 = new RoomBooking(obj, "Karthik", 1);
RoomBooking rb2 = new RoomBooking(obj, "Mrinal", 1);
Thread t1 = new Thread(rb1);
Thread t2 = new Thread(rb2);
t1.start();
t2.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 21

+4 marksOne or more correct options

Consider the Java code given below.

java
import java.util.*;
public class Test {
public static void main(String[] args) {
var list = new ArrayList<String>();
list.add("Apple");
list.add("Mango");
list.add("Orange");
list.add("Pomegranate");
// CODE BLOCK
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+" ");
}
}

Choose the correct option(s) to be filled in place of CODE BLOCK so that the program always generates the following output:
Apple Mango Orange Pomegranate
Apple Mango Orange Pomegranate

Select all that apply.

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

Correct answers

  • B
  • C

Question 22

+4 marksOne or more correct options

Consider the Java code given below.

java
abstract class Spectacles {
public abstract void wear();
}
class ReadingGlasses extends Spectacles {
public void wear() {
System.out.println("Wearing Reading Glasses");
}
}
class SunGlasses extends Spectacles {
public void wear() {
System.out.println("Wearing Sunglasses");
}
}
class SpecsList {
private Object[] sArr = {new ReadingGlasses(), new SunGlasses()};
public void trySpecs() {
for (int i = 0; i < sArr.length; i++) {
//LINE-1
}
}
}
public class Test {
public static void main(String[] args) {
SpecsList c = new SpecsList();
c.trySpecs();
}
}

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

Wearing Reading Glasses
Wearing Sunglasses

Select all that apply.

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

Correct answer

  • D

Question 23

+5 marksOne or more correct options

Consider the Java code given below that prints the animal with highest weight. From among the options, identify the appropriate function header for the function printHeaviestAnimal that takes as input an array of Animal objects and prints the heaviest animal among them.

java
import java.util.*;
abstract class Animal {
public abstract double getWeight();
}
class Dog extends Animal {
// getWeight() method that returns weight of the dog
}
class Cat extends Animal {
// getWeight() method that returns weight of the cat
}
public class WeightComparison {
// LINE 1: FUNCTION HEADER
{
//invokes method getWeight()
//to print the value of highest weight
}
public static void main(String[] args) {
Animal[] a = { new Dog(), new Cat() };
printHeaviestAnimal(a);
}
}

Choose the correct option(s).

Select all that apply.

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

Correct answers

  • A
  • C