Quiz Space

May 2023 term · Programming Concepts using Java · BSCS2005

Java End Term: 3 September 2023, Set QPD1-S1 (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-S1: 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 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(8, n -> n-1)
.map(n -> n + 2)
.filter(n -> n % 3 == 0)
.limit(4)
.forEach((x) -> System.out.print(x + " "));
}
}

What will the output be?

  1. A

    12 9 6 3

  2. B

    9 6 3 0

  3. C

    10 7 4 1

  4. D

    7 4 1 0

Show answer

Correct answer

  • B

    9 6 3 0

Question 2

+4 marksOne correct option

Consider the following java code

java
class Player implements Cloneable{
String name;
public Player(String n) {
name = n;
}
public Player clone() throws CloneNotSupportedException{
return (Player)super.clone();
}
}
class Team implements Cloneable{
String teamname;
Player[] ply;
public Team(String tn, Player[] p) {
teamname = tn;
ply = p;
}
public Team clone() throws CloneNotSupportedException{
Team t = (Team)super.clone();
t.ply = t.ply.clone();
return t;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Player[] p1 = {new Player("Nikita"), new Player("Ram")};
Team t1 = new Team("Orange", p1);
Team t2 = t1.clone();
Player[] p2 = t2.ply;
p2[0].name = "Srikanth";
t2.teamname = "Mango";
System.out.println(t1.teamname +" : " + t1.ply[0].name);
System.out.println(t2.teamname +" : " + t2.ply[0].name);
}
}

What will the output be?

  1. A

    Orange : Srikanth
    Orange : Srikanth

  2. B

    Mango : Srikanth
    Orange : Srikanth

  3. C

    Mango : Nikita
    Orange : Srikanth

  4. D

    Orange : Srikanth
    Mango : Srikanth

Show answer

Correct answer

  • D

    Orange : Srikanth
    Mango : Srikanth

Question 3

+4 marksOne correct option

Consider the java code given below

java
class Instructor{
private String name;
private double salary;
public Instructor() {
this.salary = 40.0;
}
public Instructor(Instructor t) {
this.name = t.name;
this.salary = t.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) {
Instructor obj1 = new Instructor();
Instructor obj2 = obj1;
Instructor obj3 = new Instructor(obj1);
obj1.setSalary(50.0);
obj1.setName("Suresh");
obj2.setName("Ram");
obj3.setName("Ravi");
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

    Suresh: 50.0
    Ram: 50.0
    Ravi: 40.0

  2. B

    Ram: 50.0
    Ram: 50.0
    Ravi: 40.0

  3. C

    Ram: 50.0
    Ravi: 40.0
    Ravi: 40.0

  4. D

    Ravi: 50.0
    Ram: 50.0
    Ravi: 40.0

Show answer

Correct answer

  • B

    Ram: 50.0
    Ram: 50.0
    Ravi: 40.0

Question 4

+4 marksOne correct option
  1. A

    Statement 1 and Statement 2 are correct

  2. B

    Statement 1 and Statement 3 are correct

  3. C

    Statement 2 and Statement 3 are correct

  4. D

    All the Statements is correct

Show answer

Correct answer

  • C

    Statement 2 and Statement 3 are correct

Question 5

+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 Player{
String name;
String year;
int runs;
// Constructor to initialize name, year and runs
}
public class MapTest{
public static void printPlayers(ArrayList<Player> pL) {
var map = new TreeMap<String, Integer>();
for(Player p:pL) {
map.put(p.name, map.getOrDefault(p.name, 0)+p.runs);
}
for (Map.Entry<String, Integer> e:map.entrySet()) {
System.out.println(e.getKey()+" = "+e.getValue());
}
}
public static void main(String[] args) {
ArrayList<Player> pList = new ArrayList<Player>();
pList.add(new Player("Dhoni", "2015", 756));
pList.add(new Player("Kohli", "2017", 1050));
pList.add(new Player("Dhoni", "2017", 345));
pList.add(new Player("Kohli", "2016", 675));
printPlayers(pList);
}
}

What will the output be?

  1. A

    Dhoni = 756
    Kohli = 1050

  2. B

    Dhoni = 345
    Kohli = 675

  3. C

    Dhoni = 1101
    Kohli = 1725

  4. D

    Kohli = 1725
    Dhoni = 1101

Show answer

Correct answer

  • C

    Dhoni = 1101
    Kohli = 1725

Question 6

+4 marksOne correct option

Consider the Java code given below

java
import java.util.*;
public class SetTest {
//CODE BLOCK
public boolean property(Integer marks) {
if(marks>=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 student=new LinkedHashMap<String, Integer>();
student.put("AI", 26);
student.put("EDC", 90);
student.put("MEFA", 38);
student.put("COA", 48);
student.put("BEFA", 80);
student.put("M1", 36);
SetTest obj=new SetTest();
obj.validate(student);
obj.display();
}
}

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

[AI, M1, MEFA]
[BEFA, COA, EDC]

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

Correct answer

  • C

Question 7

+4 marksOne correct option

Consider the following java code

java
class HoursException extends Exception{
public HoursException(String str) {
super(str);
}
}
class BusRental{
final static int rent_per_hour = 500;
public double getTotalRent(int no_of_hours) throws HoursException{
double total_rent = 0.0;
if(no_of_hours < 0)
throw new HoursException("Invalid hours");
total_rent = no_of_hours * rent_per_hour;
return total_rent;
}
}
public class ExceptionTest {
public static void main(String[] args) {
BusRental r1 = new BusRental();
BusRental r2 = new BusRental();
try {
System.out.println(r1.getTotalRent(-2));
System.out.println(r1.getTotalRent(12));
}
catch(HoursException e) {
System.out.println(e.getMessage());
}
finally {
System.out.println("Execution finished");
}
}
}

What will the output be?

  1. A

    Invalid hours
    Execution finished

  2. B

    Invalid hours
    Execution finished
    6000.0

  3. C

    Invalid hours
    6000.0

  4. D

    Invalid hours
    6000.0
    Execution finished

Show answer

Correct answer

  • A

    Invalid hours
    Execution finished

Question 8

+4 marksOne correct option

Consider the following java code

java
class Cube{
//l = length, b = breadth, h = height
private double l, b, h;
public Cube(double l, double b, double h) {
this.l = l;
this.b = b;
this.h = h;
}
public double getVolume() {
assert l * b * h > 0; // assert-1
assert l > 0; // assert-2
assert b > 0; // assert-3
assert h > 0; // assert-4
return (l * b * h);
}
}
public class AssertionTest {
public static void main(String[] args) {
Cube obj = new Cube(1.0, -2.5, -2.5);
System.out.println(obj.getVolume());
}
}

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

  1. A

    assert-1

  2. B

    assert-2

  3. C

    assert-3

  4. D

    assert-4

Show answer

Correct answer

  • C

    assert-3

Question 9

+4 marksOne correct option

Consider the code given below.

java
import java.util.*;
class Cricketer{
Map<String, String> players = new HashMap<String, String>();
public Cricketer() {
players.put("Rohit", "India");
players.put("Ali", "England");
}
public String getCountry(String t){
return players.get(t);
}
}
public class OptionalTest {
public static void main(String[] args){
Optional<String> op1 = Optional.ofNullable(new Cricketer()
.getCountry("Rohit"));
Optional<String> op2 = Optional.ofNullable(new Cricketer()
.getCountry("ali"));
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 10

+4 marksOne correct option

Consider the Java code given below

java
import java.util.*;
import java.util.stream.*;
public class CollectResTest {
public static void main(String[] args) {
var list=new ArrayList<String>();
list.add(null);
list.add("ODI");
list.add(null);
list.add("RANJI");
list.add("TEST");
list.add("TEST");
list.add("RANJI");
list.add("ODI");
//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:

[ODI, RANJI, TEST]

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

Correct answer

  • C

Question 11

+4 marksOne correct option

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.
Java is a programming language

java
import java.io.*;
public class FileTest {
public static void main(String[] args) {
try {
var out = new FileOutputStream("file1.txt", false);
var dout = new DataOutputStream(out);
dout.writeBytes(", released by Sun Microsystems in 1995");
dout.close();
var out2 = new FileOutputStream("file2.txt", true);
var dout2 = new DataOutputStream(out2);
dout2.writeBytes(", released by Sun Microsystems in 1995");
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

  • A

Question 12

+4 marksOne correct option

Consider the code given below.

java
import java.io.*;
class Voucher implements Serializable{
private String v_type="****";
private transient String code="****";
private String exp="****";
public Voucher(String type, String c, String e) {
v_type = type;
code = c;
exp = e;
}
public String toString() {
return v_type + ", " + code + ", " + exp;
}
}
public class SCTest{
public static void main(String[] args) throws Exception{
var fos = new FileOutputStream("voucher.txt");
var os = new ObjectOutputStream(fos);
os.writeObject(new Voucher("Footwear", "NEWVOU", "14/09/23"));
var fis = new FileInputStream("voucher.txt");
var ois = new ObjectInputStream(fis);
Voucher v = (Voucher)ois.readObject();
System.out.println(v);
}
}

What will the output be?

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

Correct answer

  • C

Question 13

+4 marksOne correct option

Consider the code given below.

java
import java.util.*;
class Faculty{
String id;
ArrayList<String> achievements;
public Faculty(String id, ArrayList<String> achievements) {
this.id = id;
this.achievements = achievements;
}
public Faculty(Faculty f){
id = f.id;
achievements = new ArrayList<String>();
for(String s : f.achievements)
achievements.add(s);
}
public String toString() {
return id+", "+achievements;
}
}
public class ConTest {
public static void main(String[] args) {
ArrayList<String> ach = new ArrayList<String>();
ach.add("Wipro Certified");
ach.add("Oracle Certified");
Faculty f1 = new Faculty("ID101", ach);
Faculty f2 = new Faculty(f1);
f2.id = "ID102";
f2.achievements.add("Infosys Certified");
System.out.println(f1);
System.out.println(f2);
}
}

What will the output be?

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

Correct answer

  • D

Question 14

+4 marksOne correct option

Consider the Java code given below

java
interface Vehicle{
abstract void features();
}
class BS4Vehicle implements Vehicle {
public void features() {
System.out.println("BS4 vehicle is less polluted than BS3 Vehicle");
}
}
class BS6Vehicle extends BS4Vehicle{
public void features() {
System.out.println("BS6 vehicle is less polluted than BS4 Vehicle");
}
}
public class GenericTest {
// LINE 1 : FUNCTION HEADER {
obj.features();
}
public static void main(String[] args) {
getDetails(new BS4Vehicle());
getDetails(new BS6Vehicle());
}
}

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

BS4 vehicle is less polluted than BS3 Vehicle
BS6 vehicle is less polluted than BS4 Vehicle

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

Correct answer

  • C

Question 15

+4 marksOne correct option

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

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

Pack2Test.java:

java
package com.pack2;
public class Pack2Test extends com.pack1.Pack1Test{ // LINE-1
public void clear() {
super.show(); // LINE-2
System.out.println("clear from Pack2Test");
}
protected void display() {
System.out.println("display from Pack2Test");
}
}

Test.java:

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

Choose the correct option.

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

Correct answer

  • E

Question 16

+4 marksOne correct option

ConcurrentHashMap is a hash table implementation that supports full concurrency of retrievals and high expected concurrency for updates. All operations on this collection are thread-safe. Consider the code given below that uses a ConcurrentHashMap, and answer the question that follows.

java
class Example extends Thread{
Map siMap;
//Constructor is defined here
public void run(){
siMap.put("D",4);
}
}
public class FClass{
public static void main (String[] args) {
Map<String, Integer> siMap = new ConcurrentHashMap<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();
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 NOT true about the given code?

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

Correct answer

  • A

Question 17

+4 marksOne correct option

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

java
interface Software{
public void status();
}
interface Hardware extends Software{
public void chip();
public void display();
}
class Mobile implements Hardware{
public void chip(){
System.out.println("Motherboard is working");
}
public void status(){
System.out.println("Software is working");
}
}
public class Test {
public static void main(String[] args) {
Mobile obj= new Mobile();
obj.chip();
obj.status();
}
}
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 18

+6 marksOne or more correct options

Consider the Java program given below.

java
import javax.swing.*;
import java.awt.event.*;
public class ButtonEvents extends JFrame implements ActionListener{
private JButton btn1, btn2;
private JLabel lb;
JPanel panel1, panel2;
public ButtonEvents() {
btn1=new JButton("Submit");
btn2=new JButton("Cancel");
panel1=new JPanel();
panel1.add(btn1);
panel1.add(btn2);
add(panel1,"South");
lb=new JLabel("");
panel2=new JPanel();
panel2.add(lb);
add(panel2,"North");
setVisible(true);
setSize(400,400);
btn1.setActionCommand("sac1");
btn2.setActionCommand("sac2");
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//CODE SEGMENT
}
public static void main(String[] args) {
new ButtonEvents();
}
}

Initial state: window with Submit and Cancel buttons, empty label.
On clicking Submit button: label shows "Submit clicked".
On clicking Cancel button: label shows "Cancel clicked".

Choose the correct code segment(s) to be filled inside method actionPerformed() such that on clicking the Submit button, the label text changes to Submit clicked and on clicking the Cancel button, the label text changes to Cancel clicked.

Select all that apply.

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

Correct answers

  • A
  • C

Question 19

+6 marksOne or more correct options

Consider the following code

java
class SeatEnquiry{
int available = 1;
public synchronized void request(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{
SeatEnquiry s;
String name;
int n_seats;
public SeatBooking(SeatEnquiry s1, String n, int st){
s = s1;
name = n;
n_seats = st;
}
public void run(){
s.request(n_seats, name);
}
}
public class ThreadTest {
public static void main(String[] args) {
SeatEnquiry obj = new SeatEnquiry();
SeatBooking tb1 = new SeatBooking(obj, "Anvesh", 1);
SeatBooking tb2 = new SeatBooking(obj, "Sandeep", 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
  2. B
  3. C
  4. D
  5. E
  6. F
Show answer

Correct answers

  • C
  • D

Question 20

+6 marksOne or more correct options

Consider the code given below.

java
interface Movable{
void speed();
}
class ES implements Movable{
public void speed() {
System.out.println("You can go upto 30 Km/ph");
}
}
class Sonic{
public ES getsonic() {
return new HighSpeed();
}
private class HighSpeed extends ES implements Movable{
public void speed() {
System.out.println("You can go upto 60 Km/ph");
}
}
}
public class Test {
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:

You can go upto 60 Km/ph

Select all that apply.

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

Correct answers

  • A
  • D

Question 21

+5 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class QueueTest{
public static void main(String[] args) {
PriorityQueue<String> queue1 = new PriorityQueue<String>();
queue1.add("Ganguly");
queue1.add("Cameron");
queue1.add("Unadkat");
queue1.add("Oliver");
queue1.add("Hayden");
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 22

+5 marksOne correct option

Consider the code given below.

java
import java.util.*;
public class HighTest {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("APPLE");
list.add("EAGLE");
list.add("IND");
list.add("AUS");
list.add("BAN");
list.add("USMAN");
list.add("UKRANTH");
list.stream().takeWhile(s->s.length() == 5)
.forEach(s -> System.out.print(s+" "));
System.out.println();
list.stream().dropWhile(s->s.length() == 5)
.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 23

+4 marksOne or more correct options

Consider the following java code:

java
1. class Player{
2. public void show(){
3. System.out.println("show player name");
4. }
5. public void display(){
6. System.out.println("display player details");
7. }
8. }
9. class Batsman extends Player{
10. public void show() {
11. System.out.println("show Batsman name");
12. }
13. }
14. class Captain extends Player, Batsman{
15. public void show() {
16. System.out.println("show Captain name");
17. }
18. }
19. public class Test {
20. public static void main(String[] args){
21. Player obj= new Player();
22. obj.show();
23. Player obj1 = new Batsman();
24. Batsman obj2 = new Player();
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 22

  5. E

    Line 23

  6. F

    Line 24

Show answer

Correct answers

  • B

    Line 14

  • F

    Line 24