Quiz Space

May 2024 term · Programming Concepts using Java · BSCS2005

Java End Term: 1 September 2024, Set QDF1 (May 2024 term)

The IIT Madras BS Programming Concepts using Java (Java) End Term paper sat on 1 Sept 2024, in the May 2024 term, set QDF1: 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
18
MSQ
5

Updated

Official paper: IIT M FOUNDATION DIPLOMA AN EXAM QDF3 01 Sep 2024 · No negative marking.

Question 1

+4 marksOne correct option

Consider the Java code given below.

java
class Monitor {
public void screenSize() {
System.out.println("Normal screen size");
}
public void resolution() {
System.out.println("Normal resolution");
}
}
class LCD extends Monitor {
public void screenSize() {
System.out.println("Screen size is large");
}
}
class LED extends Monitor {
public void screenSize() {
System.out.println("Screen size is medium");
}
public void resolution() {
System.out.println("HD resolution");
}
}
public class Test {
static void show(Monitor[] monitors) {
for (int i = 0; i < monitors.length; i++) {
monitors[i].screenSize();
monitors[i].resolution();
}
}
public static void main(String[] args) {
Monitor[] monitors = {new LCD(), new LED()}; // LINE 1
show(monitors);
}
}

Choose the correct option.

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

Correct answer

  • C

Question 2

+4 marksOne correct option

Consider the code given below that checks whether two candidates are from the same college. Method equals is overridden to compare two Candidate objects as follows. If two candidates are from the same college then they are said to be equal. Based on the given information, answer the question that follows.

java
class Candidate {
private String name;
private String college;
// Constructor to initialize instance variables
public String toString() {
return name;
}
public boolean equals(Object obj) {
// CODE BLOCK
}
}
public class Test {
public static void main(String[] args) {
Candidate c1 = new Candidate("Shreya", "IITMadras");
Candidate c2 = new Candidate("Hari", "IITDelhi");
Candidate c3 = new Candidate("Aisha", "IITMadras");
if (c1.equals(c3)) {
System.out.println(c1 + " and " + c3 + " belong to the same college");
}
if (c2.equals(c3)) {
System.out.println(c2 + " and " + c3 + " belong to the same college");
}
}
}

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

Shreya and Hari belong to the same college

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

Correct answer

  • C

Question 3

+4 marksOne correct option

Consider the Java code given below.

java
class Intern {
private String name;
public Intern(String n) {
name = n;
}
public Intern(Intern i) {
this.name = i.name;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
public class Test {
public static void main(String[] args) {
Intern i1 = new Intern("Jaya");
Intern i2 = new Intern(i1);
Intern i3 = i1;
i1.setName("Subash");
System.out.println(i1.getName());
System.out.println(i2.getName());
System.out.println(i3.getName());
}
}

What will the output be?

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

Correct answer

  • B

Question 4

+4 marksOne correct option

Consider the code given below.

java
class Device {
public void powerOn() {
System.out.println("Device is on");
}
}
class Mobile extends Device {
public void display() {
System.out.println("Mobile display");
}
class Smartphone extends Mobile {
public void display() {
System.out.println("Smartphone display");
}
public void connect() {
System.out.println("Connected to Internet");
}
}
public class TestDevice {
public static void main(String[] args) {
Device d = new Mobile();
Mobile m = new Smartphone(); // LINE 1
d.powerOn();
((Mobile)d).display(); // LINE 2
m.connect(); // LINE 3
}
}

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 Java code given below.

java
import java.util.*;
public class Test{
public static void main(String[] args) {
ArrayDeque<String> queue1 = new ArrayDeque<String>();
queue1.add("Violet");
queue1.addFirst("Yellow");
queue1.add("Pink");
queue1.addFirst("Blue");
queue1.add("Blue");
System.out.println(queue1);
TreeSet<String> set = new TreeSet<String>(queue1);
System.out.println(set);
}
}

What will the output be?

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

Correct answer

  • A

Question 6

+4 marksOne correct option

Consider the Java code given below.

java
class Chef {
String name;
public Chef(String n) {
name = n;
}
}
class Dish implements Cloneable {
String dishName;
Chef[] chefs;
public Dish(String name, Chef[] chefs) {
dishName = name;
this.chefs = chefs;
}
public Dish clone() throws CloneNotSupportedException {
Dish d = (Dish) super.clone();
d.chefs = this.chefs.clone();
return d;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Chef[] chefs1 = { new Chef("Ravi"), new Chef("Raju") };
Dish d1 = new Dish("Biriyani", chefs1);
Dish d2 = d1.clone();
Chef[] chefs2 = d2.chefs;
chefs2[0].name = "Veena";
d2.dishName = "FriedRice";
System.out.println(d1.dishName + " : " + d1.chefs[0].name);
System.out.println(d2.dishName + " : " + d2.chefs[0].name);
}
}

What will the output be?

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

Correct answer

  • D

Question 7

+4 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Date");
list.add("Durian");
list.add("Banana");
list.add("Cherry");
list.add("Dragonfruit");
list.stream().takeWhile(s -> s.startsWith("D"))
.forEach(s -> System.out.print(s + " "));
System.out.println();
list.stream().dropWhile(s -> s.startsWith("D"))
.forEach(s -> System.out.print(s + " "));
}
}

What will the output be?

  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
1 class ClassOne{
2 public void methodOne(){
3 // ...
4 methodTwo();
5 // ...
6 }
7 public void methodTwo(){
8 // ...
9 }
10 }
11 class ClassTwo{
12 public static void methodThree(){
13 // ...
14 ClassOne c = new ClassOne();
15 c.methodOne();
16 // ...
17 }
18 public static void methodFour(){
19 // ...
20 methodThree();
21 // ...
22 }
23 public static void main(String[] args) {
24 // ...
25 methodFour();
26 }
27 }

During the execution of Line 16 in the above code, the activation record of which method is at the top of the stack of activation records?

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

Correct answer

  • D

Question 9

+4 marksOne correct option

Consider the Java code given below.

java
interface TransportService {
void bookRide();
}
class TransportApp {
public TaxiService getTaxiService() {
return new TaxiService();
}
public BusService getBusService() {
return new BusService();
}
private class TaxiService implements TransportService {
public void bookRide() {
System.out.println("Booking a taxi");
}
}
private class BusService implements TransportService {
public void bookRide() {
System.out.println("Booking a bus");
}
}
}
public class Test {
public static void main(String[] args) {
TransportApp t = new TransportApp();
//CODE BLOCK
obj1.bookRide();
obj2.bookRide();
}
}

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

Booking a taxi
Booking a bus

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

Correct answer

  • B

Question 10

+4 marksOne correct option

Consider the Java code given below.

java
interface OnlineCourse {
default void showDetails() {
System.out.println("Course duration is 3 months");
}
default void enroll() {
System.out.println("Enrolled");
}
}
class CloudCourse implements OnlineCourse { //LINE 1
public void enroll() {
System.out.println("Enrolled in cloud course");
}
}
class MLCourse implements OnlineCourse { //LINE 2
public void showDetails() {
System.out.println("ML Course");
}
}
public class Test {
public static void main(String[] args) {
OnlineCourse courses[] = new OnlineCourse[2];
courses[0] = new CloudCourse();
courses[1] = new MLCourse();
for (OnlineCourse course : courses) {
course.showDetails();
course.enroll();
}
}
}

Choose the correct option.

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

Correct answer

  • D

Question 11

+4 marksOne correct option

Consider the Java code given below.

java
abstract class Bag {
abstract void open();
void carry() { // LINE 1
System.out.println("Carrying bag");
}
}
class Backpack extends Bag {
void open() {
System.out.println("Opening backpack");
}
void carry() {
System.out.println("Wearing backpack");
}
}
class ToteBag extends Bag {
void open() {
System.out.println("Opening tote bag");
}
void carry() {
System.out.println("Holding tote bag");
}
}
public class Test {
public static void main(String[] args) {
Bag bag1 = new Backpack(); // LINE 2
Bag bag2 = new ToteBag(); // LINE 3
bag1.carry();
bag1.open();
bag2.carry();
bag2.open();
}
}

Choose the correct option.

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

Correct answer

  • C

Question 12

+4 marksOne correct option

Consider the Java code given below.

java
interface Iterator{
public boolean has_next();
public Object get_next();
}
abstract class Printable{
public abstract void print();
}
class ProductList{
private final int limit = 3;
private Product[] list = { new Product("Laptop", "P1001"),
new Product("Smartphone", "P1002"),
new Product("Smartwatch", "P1003")
};
private class Product extends Printable{
private String name, productId;
//Constructor to initialize instance variables
public void print() {
System.out.println(productId + ", " + name);
}
}
private class ProdIter implements Iterator{
private int indx;
public ProdIter() {
//constructor
}
public boolean has_next() {
//if next element available in list return true;
//else false
}
public Object get_next() {
//return next element from list
}
}
public Iterator getIterator() {
return new ProdIter();
}
}
java
public class IterTest {
public static void main(String[] args) {
ProdList pList = new ProdList();
Iterator iter = pList.getIterator();
while(iter.has_next()) {
--------------------------------------; //LINE 1
}
}
}

Identify the appropriate statement to fill in the blank at LINE 1, such that the output is:
P1001, Laptop
P1002, Smartphone
P1003, Smartwatch

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

Correct answer

  • A

Question 13

+4 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
class ZeroValueException extends Exception {
public String toString() {
return "Zero encountered during update";
}
}
public class Test {
public static void update(int[] array, int index) throws ZeroValueException
{
if (array[index] == 0) {
throw new ZeroValueException();
}
array[index] = array[index] * 5;
}
public static void main(String[] args) {
int[] arr = {1, -1, 0, 2, -2};
try {
for (int i = 0; i < arr.length; i++) {
update(arr, i);
}
} catch (ZeroValueException e) {
System.out.println(e);
}
for (int n : arr) {
System.out.print(n + " ");
}
}
}

What will the output be?

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

Correct answer

  • D

Question 14

+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 Movie {
HashMap<String, String> actors = new HashMap<>();
public Movie() {
actors.put("Action", "Akshay");
actors.put("Comedy", "Kapil");
}
public String getActor(String genre) {
return actors.get(genre);
}
}
public class Test {
public static void main(String[] args) {
Optional<String> a1 = Optional.ofNullable(new Movie().getActor("Action"));
Optional<String> a2 = Optional.ofNullable(new Movie().getActor("Thriller"));
a1.ifPresent(n ->System.out.println(n.toUpperCase()));
a2.ifPresent(n -> System.out.println(n.toUpperCase()));
}
}

Choose the correct option.

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

Correct answer

  • A

Question 15

+4 marksOne correct option

Consider the Java code given below.

java
import java.io.*;
class HealthCard implements Serializable {
private String cardNumber = "******";
private transient String insuranceProvider = "Unknown";
private String issueDate = "00/00";
public HealthCard(String cN, String iP, String iD) {
cardNumber = cN;
insuranceProvider = iP;
issueDate = iD;
}
public String toString() {
return cardNumber + ", " + insuranceProvider + ", " + issueDate;
}
}
public class Test {
public static void main(String[] args) throws Exception {
var fos = new FileOutputStream("healthcard.txt");
var os = new ObjectOutputStream(fos);
os.writeObject(new HealthCard("H123456", "HInsurance", "03/24"));
os.close();
var fis = new FileInputStream("healthcard.txt");
var ois = new ObjectInputStream(fis);
HealthCard card = (HealthCard) ois.readObject();
ois.close();
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 16

+4 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
import java.util.stream.*;
class Car {
private String model;
private double mileage;
//Constructor to initialize instance variables
public double getMileage() {
return mileage;
}
public String toString() {
return model;
}
}
public class Test {
public static void main(String[] args) {
var carArr = new ArrayList<Car>();
carArr.add(new Car("Toyota", 20.5));
carArr.add(new Car("Ford", 25.3));
carArr.add(new Car("Honda", 18.9));
carArr.add(new Car("Chevrolet", 22.0));
Map<Boolean, List<Car>> mileageMap;
mileageMap = carArr.stream()
.collect(Collectors.partitioningBy(c -> c.getMileage() >= 22.0));
System.out.println(mileageMap.get(false));
}
}

Choose the correct option.

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

Correct answer

  • C

Question 17

+4 marksOne correct option

Consider the code given below. Assume that the file food.txt contains the following lines of text in it.

A balanced diet is key to good health.
Food provides essential nutrients for the body.
Food preparation is an art form.

java
import java.io.*;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
try {
var in=new FileInputStream("food.txt");
var scanner=new Scanner(in); //LINE 1
System.out.println("Data from file:");
System.out.println(scanner.nextLine());
System.out.println(scanner.next());
System.out.println(scanner.nextLine());
}
catch (FileNotFoundException e) {
System.out.println("File does not exist.");
}
catch (IOException e) {
System.out.println("Error in writing a file.");
}
}
}

Choose the correct option.

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

Correct answer

  • C

Question 18

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

Correct answer

  • C

Question 19

+5 marksOne or more correct options

Consider the Java code given below that prints the highest priced stock among a set of given Stock objects. From among the options, identify the appropriate function header for the function printHighestPricedStock that takes as input an array of Stock objects and prints the highest priced stock.

java
import java.util.*;
interface Stock {
public abstract double getPrice();
}
class AStock implements Stock {
private double price;
// Constructor
// method getPrice() that returns price
}
class BStock implements Stock {
private double price;
// Constructor
// method getPrice() that returns price
}
public class Test {
// LINE 1: FUNCTION HEADER
{
// invokes method getPrice()
// to print the value of highest priced stock
}
public static void main(String[] args) {
Stock[] stocks = {
new AStock(150.50),
new BStock(200.75),
new AStock(160.25)
};
printHighestPricedStock(stocks);
}
}

Choose the correct option(s).

Select all that apply.

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

Correct answers

  • B
  • D

Question 20

+5 marksOne or more correct options

Consider the Java code given below.

java
class Stadium {
int available = 1;
public synchronized void bookSeat(int n, String name) {
if (available >= n) {
available = available - n;
System.out.println(name + " booked " + n + " seat");
} else {
System.out.println(name + " cannot book " + n + " seat");
}
}
}
class SeatBooking implements Runnable {
private Stadium s;
private String name;
private int n_seats;
public SeatBooking(Stadium s, String n, int ns) {
this.s = s;
this.name = n;
this.n_seats = ns;
}
public void run() {
s.bookSeat(n_seats, name);
}
}
public class ThreadTest {
public static void main(String[] args) {
Stadium obj = new Stadium();
SeatBooking sb1 = new SeatBooking(obj, "Virat", 1);
SeatBooking sb2 = new SeatBooking(obj, "Saniya", 1);
Thread t1 = new Thread(sb1);
Thread t2 = new Thread(sb2);
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

+5 marksOne or more correct options

Choose the correct option.

java
import java.util.*;
import java.util.concurrent.*;
class Example extends Thread {
Map cuMap;
Example(Map m) {
this.cuMap = m;
}
public void run() {
cuMap.put("4","Four");
}
}
public class Test {
public static void main (String[] args) {
Map<Integer, String> cuMap = new ConcurrentHashMap();
Integer[] iarr = {1, 2, 3};
String[] arr = {"One", "Two", "Three"};
for(int i = 0; i < iarr.length; i++) {
cuMap.put(iarr[i],arr[i]);
}
Example t = new Example(cuMap);
t.start();
Set s = cuMap.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.

Select all that apply.

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

Correct answers

  • B
  • C

Question 22

+6 marksOne or more correct options

Select all that apply.

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

Correct answers

  • A
  • C

Question 23

+6 marksOne or more correct options

Consider the Java code given below.

java
class Pattern implements Runnable {
boolean stopRequested = false;
String[] pattern = {"One", "Two", "Three", "Four", "Five"};
int index = 0;
public void run() {
while (!stopRequested) {
System.out.print(pattern[index] + " ");
index = (index + 1) % pattern.length;
}
}
public void setStop(boolean stop) {
stopRequested = stop;
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
Pattern p = new Pattern();
Thread t1 = new Thread(p);
t1.start();
p.setStop(true);
}
}

Choose the correct option(s).

Select all that apply.

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

Correct answers

  • C
  • D