Quiz Space

May 2023 term · Programming Concepts using Java · BSCS2005

Java End Term: 3 September 2023, Set QPD1-S2 (May 2023 term)

The IIT Madras BS Programming Concepts using Java (Java) End Term paper sat on 3 Sept 2023, in the May 2023 term, set QPD1-S2: 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 DIPLOMA ET1 EXAM QPD1 S2 03 Sep · No negative marking.

Question 1

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

What will the output be?

  1. A

    0 2 4 6 8 10

  2. B

    0 6 12

  3. C

    2 4 6 8 10 12

  4. D

    0 2 4 6

Show answer

Correct answer

  • B

    0 6 12

Question 2

+4 marksOne correct option

Consider the following java code

java
class Worker implements Cloneable{
String name;
public Worker(String n) {
name = n;
}
public Worker clone() throws CloneNotSupportedException{
return (Worker)super.clone();
}
}
class Work implements Cloneable{
String job;
Worker[] wk;
public Work(String j, Worker[] w) {
job = j;
wk = w;
}
public Work clone() throws CloneNotSupportedException{
Work t = (Work)super.clone();
t.wk = t.wk.clone();
return t;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Worker[] w1 = {new Worker("ABC"), new Worker("XYZ")};
Work obj1 = new Work("Painting", w1);
Work obj2 = obj1.clone();
Worker[] w2 = obj2.wk;
w2[0].name = "MNO";
obj2.job = "Printing";
System.out.println(obj1.job +" : " + obj1.wk[0].name);
System.out.println(obj2.job +" : " + obj2.wk[0].name);
}
}

What will the output be?

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

Correct answer

  • D

Question 3

+4 marksOne correct option

Consider the java code given below

java
class Employee{
private String name;
private double salary;
public Employee() {
this.salary = 40000.0;
}
public Employee(Employee e) {
this.name = e.name;
this.salary = e.salary;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
}
public class Test{
public static void main(String[] args) {
Employee obj1 = new Employee();
Employee obj2 = obj1;
Employee obj3 = new Employee(obj1);
obj1.setSalary(50000.0);
obj1.setName("Anand");
obj2.setName("Karthik");
obj3.setName("Shannu");
System.out.println(obj1.getName() +": " + obj1.getSalary());
System.out.println(obj2.getName() +": " + obj2.getSalary());
System.out.println(obj3.getName() +": " + obj3.getSalary());
}
}

What will the output be?

  1. A

    Anand: 50000.0
    Anand: 50000.0
    Shannu: 40000.0

  2. B

    Karthik: 50000.0
    Karthik: 50000.0
    Shannu: 40000.0

  3. C

    Anand: 50000.0
    Karthik: 50000.0
    Shannu: 40000.0

  4. D

    Shannu: 40000.0
    Shannu: 40000.0
    Shannu: 40000.0

Show answer

Correct answer

  • B

    Karthik: 50000.0
    Karthik: 50000.0
    Shannu: 40000.0

Question 4

+4 marksOne correct option

Consider the Java code given below.
You may make use of the method description given below.
getOrDefault(Object key, V defaultValue): Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

java
import java.util.*;
class Actor{
String name;
String year;
int no_of_hits;
public Actor(String name, String year, int no_of_hits) {
this.name = name;
this.year = year;
this.no_of_hits = no_of_hits;
}
}
public class TestMap{
public static void printActors(ArrayList<Actor> aL) {
var map = new TreeMap<String, Integer>();
for(Actor a:aL) {
map.put(a.name, map.getOrDefault(a.name, 0)+a.no_of_hits);
}
for (Map.Entry<String, Integer> e:map.entrySet()) {
System.out.println(e.getKey()+" = "+e.getValue());
}
}
public static void main(String[] args) {
var aList = new ArrayList<Actor>();
aList.add(new Actor("Sharukh", "2005", 3));
aList.add(new Actor("Hrithik", "2006", 1));
aList.add(new Actor("Hrithik", "2005", 2));
aList.add(new Actor("Sharukh", "2006", 2));
printActors(aList);
}
}

What will the output be?

  1. A

    Hrithik = 3
    Sharukh = 5

  2. B

    Hrithik = 2
    Sharukh = 2

  3. C

    Sharukh = 3
    Hrithik = 1

  4. D

    Sharukh = 5
    Hrithik = 3

Show answer

Correct answer

  • A

    Hrithik = 3
    Sharukh = 5

Question 5

+4 marksOne correct option

Consider the Java code given below

java
import java.util.*;
public class TestSet {
//CODE BLOCK
public boolean property(Integer runs) {
if(runs >= 40)
return false;
return true;
}
public void validate(Map<String, Integer> m) {
for(Map.Entry<String, Integer> entry:m.entrySet()) {
if(property(entry.getValue()))
set1.add(entry.getKey());
else
set2.add(entry.getKey());
}
}
public void display() {
System.out.println(set1);
System.out.println(set2);
}
public static void main(String[] args) {
var batsmen = new LinkedHashMap<String, Integer>();
batsmen.put("Andrew", 28);
batsmen.put("Evon", 97);
batsmen.put("Morkel", 34);
batsmen.put("Chandrapaul", 41);
batsmen.put("Bell", 83);
batsmen.put("Mayank", 39);
TestSet obj = new TestSet();
obj.validate(batsmen);
obj.display();
}
}

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

[Andrew, Mayank, Morkel]
[Bell, Chandrapaul, Evon]

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

Correct answer

  • A

Question 6

+4 marksOne correct option

Consider the following java code

java
class HoursException extends Exception{
public HoursException(String str) {
super(str);
}
}
class CarRental{
final static int rent_per_hour = 300;
public double getRent(int no_of_hours) throws HoursException{
double rent = 0.0;
if(no_of_hours < 0)
throw new HoursException("Please enter valid hours");
rent = no_of_hours * rent_per_hour;
return rent;
}
}
public class TestException {
public static void main(String[] args) {
CarRental r1 = new CarRental();
CarRental r2 = new CarRental();
try {
System.out.println(r1.getRent(34));
System.out.println(r2.getRent(-23));
}
catch(HoursException e) {
System.out.println(e.getMessage());
}
finally {
System.out.println("Execution finished");
}
}
}

What will the output be?

  1. A

    10200.0
    Execution finished

  2. B

    Please enter valid hours
    Execution finished

  3. C

    10200.0
    Please enter valid hours
    Execution finished

  4. D

    Please enter valid hours
    Execution finished
    10200.0

Show answer

Correct answer

  • C

    10200.0
    Please enter valid hours
    Execution finished

Question 7

+4 marksOne correct option

Consider the following java code

java
class Interest{
//p = Principal Amount, r = Rate per Annum, t = Time (years)
private double p, t, r;
public Interest(double p, double t, double r) {
this.p = p;
this.t = t;
this.r = r;
}
public double calculate() {
assert p * t * r > 0; // assert-1
assert p > 0; // assert-2
assert t > 0; // assert-3
assert r > 0; // assert-4
return (p * t * r / 100);
}
}
public class AssertTest {
public static void main(String[] args) {
Interest obj = new Interest(5000.00, -1.00, -16.56);
System.out.println(obj.calculate());
}
}

Identify the first assert statement that throws the AssertionError when the class is executed as:
java -ea AssertTest

  1. A

    assert-1

  2. B

    assert-2

  3. C

    assert-3

  4. D

    assert-4

Show answer

Correct answer

  • C

    assert-3

Question 8

+4 marksOne correct option

Consider the code given below.

java
import java.util.*;
class Founder{
Map<String, String> l = new HashMap<String, String>();
public Founder() {
l.put("Lenovo", "Liu Chuanzhi");
l.put("Samsung", "Lee Byung-chul");
}
public String getFounder(String t){
return l.get(t);
}
}
public class TestOptional {
public static void main(String[] args){
Optional<String> op1 = Optional.ofNullable(new Founder()
.getFounder("Lenovo"));
Optional<String> op2 = Optional.ofNullable(new Founder()
.getFounder("samsung"));
op1.ifPresent(n->System.out.println(n.toUpperCase()));
op2.ifPresent(n->System.out.println(n.toUpperCase()));
}
}

Choose the correct option.

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

Correct answer

  • B

Question 9

+4 marksOne correct option

Consider the Java code given below

java
import java.util.*;
import java.util.stream.*;
public class CollectRes {
public static void main(String[] args) {
var list=new ArrayList<String>();
list.add(null);
list.add("English Willow");
list.add(null);
list.add("Kashmiri Willow");
list.add("Poular willow");
list.add("Poular willow");
list.add("Kashmiri Willow");
list.add("English Willow");
// CODE BLOCK
System.out.println(obj);
}
}

Choose the correct option to be filled in place of CODE BLOCK so that the program generates the output:

[English Willow, Kashmiri Willow, Poular willow]

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

Correct answer

  • C

Question 10

+4 marksOne correct option

Consider the code given below.
Assume that, before execution of the given code, the files "f1.txt" and "f2.txt" have the following text in them.
JavaScript is a scripting language

java
import java.io.*;
public class TestFile {
public static void main(String[] args) {
try {
var out = new FileOutputStream("f1.txt", true);
var dout = new DataOutputStream(out);
dout.writeBytes(", enables you to create dynamically updating content");
dout.close();
var out2 = new FileOutputStream("f2.txt", false);
var dout2 = new DataOutputStream(out2);
dout2.writeBytes(", enables you to create dynamically updating content")
dout2.close();
}
catch(IOException e) {
System.out.println(e);
}
}
}

Choose the correct option regarding the contents of f1.txt and f2.txt after the program finishes execution.

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

Correct answer

  • C

Question 11

+4 marksOne correct option

Consider the code given below.

java
import java.io.*;
class Coupon implements Serializable{
private String c_type="****";
private transient String code="****";
private String exp="****";
public Coupon(String type, String c, String e) {
c_type = type;
code = c;
exp = e;
}
public String toString() {
return c_type + ", " + code + ", " + exp;
}
}
public class STest{
public static void main(String[] args) throws Exception{
var fos = new FileOutputStream("coupon.txt");
var os = new ObjectOutputStream(fos);
os.writeObject(new Coupon("Accessories", "ACCNEW", "12/08/23"));
var fis = new FileInputStream("coupon.txt");
var ois = new ObjectInputStream(fis);
Coupon r = (Coupon)ois.readObject();
System.out.println(r);
}
}

What will the output be?

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

Correct answer

  • C

Question 12

+4 marksOne correct option

Consider the code given below.

java
import java.util.ArrayList;
class Director{
String name;
ArrayList<String> awards;
public Director(String name, ArrayList<String> awards) {
this.name = name;
this.awards = awards;
}
public Director(Director d){
name = d.name;
awards = new ArrayList<String>();
for(String s : d.awards)
awards.add(s);
}
public String toString() {
return name+", "+awards;
}
}
public class TestCon {
public static void main(String[] args) {
ArrayList<String> awd = new ArrayList<String>();
awd.add("Nandi");
awd.add("Filmfare");
Director d1 = new Director("Rajamouli", awd);
Director d2 = new Director(d1);
d2.name = "Trivikram";
d1.awards.add("IIFA");
System.out.println(d1);
System.out.println(d2);
}
}

What will the output be?

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

Correct answer

  • C

Question 13

+4 marksOne correct option

Consider the Java code given below

java
interface Mobile{
abstract void features();
}
class Mobile4G implements Mobile{
public void features() {
System.out.println("Latency ranges from 60 ms to 98 ms");
}
}
class Mobile5G extends Mobile4G{
public void features() {
System.out.println("Latency under 5 milliseconds");
}
}
public class TestGeneric {
// LINE 1 : FUNCTION HEADER {
obj.features();
}
public static void main(String[] args) {
getDetails(new Mobile4G());
getDetails(new Mobile5G());
}
}

Choose the correct option(s) to be filled in place of LINE 1 so that the program generates the output:

Latency ranges from 60 ms to 98 ms
Latency under 5 milliseconds

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

Correct answer

  • C

Question 14

+4 marksOne correct option

Consider the three Java files given below.
TestPack1.java:

java
package com.pack1;
public class TestPack1 {
protected void go() {
System.out.println("go from TestPack1");
}
}

TestPack2.java:

java
package com.pack2;
public class TestPack2 extends com.pack1.TestPack1{ // LINE-1
public void move() {
super.go(); // LINE-2
System.out.println("move from TestPack2");
}
protected void present() {
System.out.println("present from TestPack2");
}
}

Test.java:

java
package com.pack2;
public class Test {
public static void main(String[] args) {
var obj = new com.pack2.TestPack2();
obj.move(); // LINE-3
obj.present(); // LINE-4
}
}

Choose the correct option.

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

Correct answer

  • E

Question 15

+4 marksOne correct option

Consider the 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 16

+4 marksOne correct option

Consider the Java program given below and select the correct option from among the given choices.

java
interface Flyable{
public void fly();
}
interface Moveable extends Flyable{
public void move();
public void run();
}
class Flight implements Moveable{
public void fly(){
System.out.println("Flight is flying");
}
public void move(){
System.out.println("Flight is moving");
}
}
public class Test {
public static void main(String[] args) {
Flight obj= new Flight();
obj.fly();
obj.move();
}
}
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 17

+6 marksOne or more correct options

Consider the Java program given below.

java
import javax.swing.*;
import java.awt.event.*;
public class ButtonEventTest extends JFrame implements ActionListener{
private JButton b1, b2;
private JLabel l1;
JPanel panel1, panel2;
public ButtonEventTest() {
b1=new JButton("Clear");
b2=new JButton("Reload");
panel1=new JPanel();
panel1.add(b1);
panel1.add(b2);
add(panel1,"South");
l1=new JLabel("");
panel2=new JPanel();
panel2.add(l1);
add(panel2,"North");
setVisible(true);
setSize(400,400);
b1.setActionCommand("action1");
b2.setActionCommand("action2");
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//CODE SEGMENT
}
public static void main(String[] args) {
new ButtonEventTest();
}
}

Initial State: window with Clear and Reload buttons, empty label.
On clicking Clear button: label shows "Form cleared".
On clicking Reload button: label shows "Form reloaded".

Choose the correct code segment(s) to be filled inside method actionPerformed() such that on clicking the Clear button, the label text changes to Form cleared and on clicking the Reload button, the label text changes to Form reloaded.

Select all that apply.

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

Correct answers

  • A
  • C

Question 18

+6 marksOne or more correct options

Consider the following code

java
class BookingEnquiry{
int available = 1;
public synchronized void request(int n, String name){
if(available >= n){
available = available - n;
System.out.println(name + " booked " + n + " ticket");
}
else{
System.out.println(name + " cannot book " + n + " ticket");
}
}
}
class TicketBooking implements Runnable{
BookingEnquiry e;
String name;
int n_tickets;
public TicketBooking(BookingEnquiry e1, String n, int t){
e = e1;
name = n;
n_tickets = t;
}
public void run(){
e.request(n_tickets, name);
}
}
public class ThreadTest {
public static void main(String[] args) {
BookingEnquiry obj = new BookingEnquiry();
TicketBooking tb1 = new TicketBooking(obj, "Shannu", 1);
TicketBooking tb2 = new TicketBooking(obj, "Karthik", 1);
Thread t1 = new Thread(tb1);
Thread t2 = new Thread(tb2);
t1.start();
t2.start();
}
}

Which of the following is/are possible output(s)?

Select all that apply.

  1. A

    Shannu booked 1 ticket
    Karthik booked 1 ticket

  2. B

    Karthik booked 1 ticket
    Shannu booked 1 ticket

  3. C

    Shannu booked 1 ticket
    Karthik cannot book 1 ticket

  4. D

    Karthik booked 1 ticket
    Shannu cannot book 1 ticket

  5. E

    Karthik cannot book 1 ticket
    Shannu cannot book 1 ticket

  6. F

    Shannu cannot book 1 ticket
    Karthik cannot book 1 ticket

Show answer

Correct answers

  • C

    Shannu booked 1 ticket
    Karthik cannot book 1 ticket

  • D

    Karthik booked 1 ticket
    Shannu cannot book 1 ticket

Question 19

+6 marksOne or more correct options

Consider the code given below.

java
interface UpGradable{
void upgrade();
}
class PC {
public UpGradable getHardware() {
return new Hardware();
}
public UpGradable getOS() {
return new OS();
}
private class Hardware implements UpGradable{
public void upgrade() {
System.out.println("It is upgradable");
}
}
private class OS implements UpGradable{
public void upgrade() {
System.out.println("It is upgradable");
}
}
}
public class PrivateTest {
public static void main(String[] args) {
// CODE BLOCK
}
}

Choose the correct option(s) to be filled in place of CODE BLOCK so that the output is:

It is upgradable
It is upgradable

Select all that apply.

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

Correct answers

  • A
  • D

Question 20

+5 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class TestQueue{
public static void main(String[] args) {
PriorityQueue<String> queue1 = new PriorityQueue<String>();
queue1.add("Gurgaon");
queue1.add("Calicut");
queue1.add("Udaipur");
queue1.add("Ooty");
queue1.add("Hyderabad");
ArrayDeque<String> queue2 = new ArrayDeque<String>();
while(queue1.size()>0) {
queue2.addFirst(queue1.poll()); //LINE-1
}
System.out.print(queue2);
}
}

Choose the correct option.

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

Correct answer

  • D

Question 21

+5 marksOne correct option

Consider the code given below.

java
import java.util.*;
public class TestHigh {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("RAT");
list.add("SKY");
list.add("FLY");
list.add("EAGLE");
list.add("LEMON");
list.add("VK");
list.add("MSD");
list.stream().takeWhile(s->s.length()==3)
.forEach(s -> System.out.print(s+" "));
System.out.println();
list.stream().dropWhile(s->s.length()==3)
.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 22

+4 marksOne or more correct options

Select all that apply.

  1. A

    Captain can be a subtype of Player.

  2. B

    Player can be a subtype of Captain.

  3. C

    Captain cannot be a subtype of Player.

  4. D

    Player cannot be a subtype of Captain.

Show answer

Correct answers

  • A

    Captain can be a subtype of Player.

  • D

    Player cannot be a subtype of Captain.

Question 23

+4 marksOne or more correct options

Consider the following java code:

java
1.class Actor{
2. public void show(){
3. System.out.println("show Actor name");
4. }
5. public void display(){
6. System.out.println("display Actor details");
7. }
8.}
9.class Hero extends Actor{
10. public void show() {
11. System.out.println("show Hero name");
12. }
13.}
14.class Director extends Actor, Hero{
15. public void show() {
16. System.out.println("show Director name");
17. }
18.}
19.public class Test {
20. public static void main(String[] args){
21. Actor obj= new Actor();
22. obj.show();
23. Actor obj1 = new Hero();
24. Hero obj2 = new Actor();
25. }
26.}

Identify the line/s which has/have error.

Select all that apply.

  1. A

    Line 6

  2. B

    Line 14

  3. C

    Line 15

  4. D

    Line 20

  5. E

    Line 23

  6. F

    Line 24

Show answer

Correct answers

  • B

    Line 14

  • F

    Line 24