Quiz Space

January 2023 term · Programming Concepts using Java · BSCS2005

Java End Term: 30 April 2023, Set QPD1-S2 (January 2023 term)

The IIT Madras BS Programming Concepts using Java (Java) End Term paper sat on 30 Apr 2023, in the January 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
16
MSQ
7

Updated

Official paper: IIT M DIPLOMA ET1 EXAM QPD1 S2 30 Apr 2023 · No negative marking.

Question 1

+4 marksOne correct option

Consider the Java code given below.

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

During execution of Line 14 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
Show answer

Correct answer

  • D

Question 2

+4 marksOne correct option

Consider the following code.

java
class ContactInfo implements Cloneable{
String email;
// Constructor
// Accessor method getEmail()
// Mutator method setEmail()
public ContactInfo clone() throws CloneNotSupportedException{
return (ContactInfo)super.clone();
}
}
class Student implements Cloneable{
String name;
ContactInfo ci;
// Constructor
// Mutator method setName()
public Student clone() throws CloneNotSupportedException{
Student s = (Student)super.clone();
s.ci = s.ci.clone();
return s;
}
public String toString(){
return name + ":" + ci.getEmail();
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student("Rahul", new ContactInfo("mail"));
try{
Student s2 = s1.clone();
s2.ci.setEmail("new mail");
s2.setName("Sreeja");
System.out.println(s1);
System.out.println(s2);
}
catch(Exception e){
System.out.println(e);
}
}
}

What will the output be?

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

Correct answer

  • A

Question 3

+4 marksOne correct option

Consider the code given below

java
class Shape{
public void area(){
System.out.println("Area exists");
}
public void sides(){
System.out.println("Sides may exist");
}
}
class Circle extends Shape{
public void area(){
System.out.println("Circle area");
}
}
class Rectangle extends Shape{
public void area(){
System.out.println("Rectangle area");
}
public void sides(){
System.out.println("Four sides");
}
}
public class Test{
static void display(Shape[] s){
for(int i = 0; i < s.length; i++){
s[i].area();
s[i].sides();
}
}
public static void main(String[] args){
Shape[] s = {new Circle(), new Rectangle()}; // LINE 1
display(s);
}
}

Choose the correct option.

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

Correct answer

  • C

Question 4

+4 marksOne correct option

Consider the code given below that checks whether two laptops are the same. Method equals is overridden to compare two Laptop objects as follows: If two laptops have the same serial number, then they are the same. Based on the given information, answer the question that follows.

java
class Laptop{
private String sno;
//Constructor to initialize instance variable
public boolean equals(Object obj) {
// CODE BLOCK
}
}
public class Test {
public static void main(String[] args) {
Laptop l1 = new Laptop("A001");
Laptop l2 = new Laptop("A001");
if(l1.equals(l2))
System.out.println("l1 and l2 are the same");
else
System.out.println("l1 and l2 are not same");
}
}

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

l1 and l2 are the same

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

Correct answer

  • B

Question 5

+4 marksOne correct option

Consider the Java code given below, and answer the question that follows:

java
abstract class Op implements Runnable{
static int mul = 1;
}
class Op1 extends Op{
public void run() {
if (mul != 3) {
mul = mul * 2;
}
}
}
class Op2 extends Op{
public void run() {
if (mul != 2) {
mul = mul * 3;
}
}
}
public class RaceCondition {
public static void main(String[] args) {
Op o1 = new Op1();
Op o2 = new Op2();
Thread t1 = new Thread(o1);
Thread t2 = new Thread(o2);
t1.start();
t2.start();
System.out.println(Op.mul);
}
}

Choose the correct option.

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

Correct answer

  • E

Question 6

+4 marksOne correct option

Consider the code given below.

java
class Rectangle{
double length, width;
public Rectangle(double l, double w) {
length = l;
width = w;
}
public Rectangle(Rectangle r){
length = r.length;
width = r.width;
}
public double perimeter(){
return 2 * (length + width);
}
}
public class ConTest {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(6.0, 3.20);
Rectangle r2 = new Rectangle(r1);
Rectangle r3 = new Rectangle(r2);
r1.width = 3.0;
System.out.println(r1.perimeter());
System.out.println(r2.perimeter());
System.out.println(r3.perimeter());
}
}

What will the output be?

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

Correct answer

  • C

Question 7

+4 marksOne correct option

Consider the code given below.

java
import java.util.*;
public class MapTest {
public static void main(String[] args) {
var bowler= new TreeMap<String,Integer>();
bowler.put("Umran", 5);
bowler.put("Starc", 5);
bowler.put("Starc", 3);
bowler.put("Umran", 3 + bowler.getOrDefault("Umran", 0));
for(Map.Entry<String, Integer> obj1:bowler.entrySet())
System.out.println(obj1.getKey()+":"+obj1.getValue());
}
}

What will the output be?

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

Correct answer

  • B

Question 8

+4 marksOne correct option

Consider the Java code given below.

java
class InvalidIncomeException extends Exception{
public InvalidIncomeException(String str) {
super(str);
}
}
class TaxPayer{
String name;
double income;
public TaxPayer(String n, double i) {
name = n;
income = i;
}
public double getTax() throws InvalidIncomeException{
if(income < 0)
throw new InvalidIncomeException("Invalid income");
else if(income > 700000)
return 0.1*income;
else
return 0;
}
}
public class ExceptionTest {
public static void main(String[] args) {
var obj1 = new TaxPayer("ABC", -700000);
var obj2 = new TaxPayer("XYZ", 500000);
try {
System.out.println(obj1.getTax());
System.out.println(obj2.getTax());
} catch (InvalidIncomeException e) {
System.out.println(e.getMessage());
}
}
}

What will the output be?

  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.io.*;
class MobilePay implements Serializable{
private String userNo = "9999999999";
private transient int pin = 0000;
private String upiID = "9999999999@ybl";
public MobilePay(String uno, int p, String id) {
userNo = uno;
pin = p;
upiID = id;
}
public String toString() {
return userNo + ", " + pin + ", " + upiID;
}
}
public class SerialTest{
public static void main(String[] args) throws Exception{
var fos = new FileOutputStream("upi.txt");
var os = new ObjectOutputStream(fos);
os.writeObject(new MobilePay("9977665544", 5432, "9977665544@ybl"));
var fis = new FileInputStream("upi.txt");
var ois = new ObjectInputStream(fis);
MobilePay mPay = (MobilePay)ois.readObject();
System.out.println(mPay);
}
}

What will the output be?

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

Correct answer

  • A

Question 10

+4 marksOne correct option

Consider the code given below.

java
import java.util.*;
class Television{
TreeMap<String, String> f = new TreeMap<String, String>();
public Television() {
f.put("LG", "Korea");
f.put("Toshiba", "Japan");
}
public String getManufacturer(String t){
return f.get(t);
}
}
public class OptionalTest {
public static void main(String[] args){
Optional<String> op1 = Optional.ofNullable(new Television()
.getManufacturer("Toshiba"));
Optional<String> op2 = Optional.ofNullable(new Television()
.getManufacturer("lg"));
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

  • D

Question 11

+4 marksOne correct option

Consider the code given below.

java
import java.util.*;
import java.util.stream.*;
class Student{
String id;
double cgpa;
//Constructor to initialize instance variables
//Method toString() to return id of the student
}
public class CollectingTest {
public static void main(String[] args){
var sArr = new ArrayList<Student>();
sArr.add(new Student("ID6789", 9.75));
sArr.add(new Student("ID5345", 6.75));
sArr.add(new Student("ID4378", 8.00));
sArr.add(new Student("ID7790", 8.45));
Map<Boolean, List<Student>> stuMap;
stuMap = sArr.stream()
.collect(Collectors.partitioningBy(s->s.cgpa >= 8.00));
System.out.println(stuMap.get(true));
System.out.println(stuMap.get(false));
}
}

What will the output be?

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

Correct answer

  • C

Question 12

+4 marksOne correct option

Consider the code given below.
Assume that file "data.txt" exists and contains the following text.
I love my country
Assume that there is no file named "newdata.txt".

java
import java.io.*;
import java.util.Scanner;
public class FileTest {
public static void main(String[] args) throws Exception{
try {
FileInputStream in = new FileInputStream("data.txt");
Scanner sc = new Scanner(in);
String data = "";
if(sc.hasNext())
data = sc.nextLine();
var out = new FileOutputStream("newdata.txt", false);
var dout = new DataOutputStream(out);
dout.writeBytes(data);
System.out.println("Data written to newdata.txt successfully");
out.close();
dout.close();
sc.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found.....");
}
catch (IOException e) {
System.out.println("Sorry there is an IO Error");
}
}
}

Choose the correct option regarding the code.

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

Correct answer

  • B

Question 13

+4 marksOne correct option

Consider the Java code given below.

java
class Triangle{
private double base, height;
// Constructor Triangle(double b, double h) to initialize base, height
public double getArea() {
assert base > 0; //LINE 1
assert height > 0; //LINE 2
double area = 0.5 * base * height;
assert area > 0; //LINE 3
return area;
}
}
public class AssertionTest {
public static void main(String[] args) {
Triangle obj = new Triangle(3.0, -4.0);
System.out.println(obj.getArea());
}
}

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

  • D

Question 14

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

Correct answer

  • B

Question 15

+4 marksOne correct option

Consider the code given below.

java
class Example extends Thread{
Thread t;
Example(Thread t){
this.t = t;
}
public void run(){
try {
t.join();
} catch (InterruptedException ex) {
}
for(int i = 1; i < 3; i++){
System.out.print(i + " ");
}
}
}
public class FClass{
public static void main(String[] args) throws InterruptedException{
Thread t1 = Thread.currentThread();
Example t2 = new Example(t1);
t2.start();
for(int i = 3; i <= 5; i++){
System.out.print(i + " ");
}
}
}

What will be the possible output?

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

Correct answer

  • B

Question 16

+5 marksOne correct option

Consider the code given below.

java
interface Chargable{
void charge();
}
interface Supplyable{
void supply();
}
class Bike{
public Battery getBattery() {
return new Battery();
}
private class Battery implements Chargable,Supplyable {
public void charge() {
System.out.println("Charges automatically while you ride");
}
public void supply() {
System.out.println("Supplies current the bike requires");
}
}
}
public class PrivateTest {
public static void main(String[] args) {
//CODE BLOCK
}
}

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

Charges automatically while you ride
Supplies current the bike requires

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

Correct answer

  • D

Question 17

+5 marksOne or more correct options

Consider the code given below.

java
1 interface Travelable{
2 void travel();
3 }
4 interface Flyable{
5 void fly();
6 }
7 interface Seatable extends Travelable, Flyable{
8 void seat();
9 }
10 abstract class Vehicle implements Seatable {
11 public void seat(){
12 System.out.println("Vehicle seat");
13 }
14 }
15 class Airplane extends Vehicle{
16 public void travel(){
17 System.out.println("Plane travel");
18 }
19 public void fly(){
20 System.out.println("Plane fly");
21 }
22 }
23 public class Test{
24 public static void main(String[] args){
25 Travelable t = new Airplane();
26 t.travel();
27 }
28 }

Choose the correct option/s

Select all that apply.

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

Correct answer

  • B

Question 18

+5 marksOne or more correct options

Consider the Java code given below that prints the recharge time of each charger in a list of chargers. From among the options, identify the appropriate function header for function display that takes as input a list of Charger objects and prints the recharge time of each.

java
import java.util.*;
abstract class Charger{
abstract void rechargeTime();
}
class USBCharger extends Charger{
public void rechargeTime() {
System.out.println("USB recharge time");
}
}
class LightiningCharger extends Charger{
public void rechargeTime() {
System.out.println("Lightining charger recharge time");
}
}
public class Test {
// LINE 1: FUNCTION HEADER
{
// invokes method rechargeTime()
// to print the recharge time of each charger
}
public static void main(String[] args) {
List<Charger> clist = new ArrayList<Charger>();
clist.add(new USBCharger());
clist.add(new LightiningCharger());
display(clist);
}
}

Choose the correct option(s).

Select all that apply.

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

Correct answers

  • C
  • D

Question 19

+5 marksOne or more correct options

Select all that apply.

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

Correct answers

  • A
  • D

Question 20

+5 marksOne or more correct options

Consider the code given below.

java
class Stock{
int available = 40;
public synchronized void request(String uname, int n){
if(available >= n){
available = available - n;
System.out.println(uname + " ordered " + n + " item(s)");
}
else
System.out.println(uname + " cannot order " + n + " item(s)");
}
}
class Order implements Runnable{
Stock st;
String username;
int nItems;
public Order(Stock s, String u, int n){
st = s;
username = u;
nItems = n;
}
public void run(){
st.request(username, nItems);
}
}
public class Test {
public static void main(String[] args) {
Stock stk = new Stock();
Order od1 = new Order(stk, "Ram", 15);
Order od2 = new Order(stk, "Sita", 30);
Thread t1 = new Thread(od1);
Thread t2 = new Thread(od2);
t1.start();
t2.start();
}
}

From among the options given below, which are possible outputs of the given code?

Select all that apply.

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

Correct answers

  • A
  • D

Question 21

+5 marksOne or more correct options

Consider the code given below.

java
import java.util.*;
class Student{
private String name;
private int total;
public Student(String n, int t) {
name = n;
total = t;
}
public int getTotal(){
return total;
}
public void print() {
System.out.println(name + " : " + total);
}
}
public class FClass{
public static void main(String[] args) {
var list = new ArrayList<Student>();
list.add(new Student("Robin", 133));
list.add(new Student("Indra", 176));
list.add(new Student("Smita", 135));
list.add(new Student("Rikki", 126));
Collections.sort(list, _____________________); // LINE 1
for(var l: list)
l.print();
}
}

Identify the appropriate option(s) to fill in the blank at LINE 1, such that the output is:

Rikki : 126
Robin : 133
Smita : 135
Indra : 176

Select all that apply.

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

Correct answers

  • A
  • C

Question 22

+5 marksOne or more correct options

Consider the code given below.

java
public class ListExample {
public static void main(String[] args) {
------------------------------- //LINE 1
------------------------------- //LINE 2
list1 = new ArrayList<String>();
list1.add("Apples");
list1.add("Grapes");
list2 = new LinkedList<String>(list1);
for(String s : list1)
System.out.println(s);
for(String s : list2)
System.out.println(s);
}
}

If the code given above produces the output:

Apples
Grapes
Apples
Grapes

What should be the correct choice for LINE 1 and LINE 2?

Select all that apply.

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

Correct answers

  • B
  • C

Question 23

+5 marksOne or more correct options

Consider the Java code given below.

java
import java.util.*;
public class SetTest {
public static void main(String[] args) {
var list = new LinkedList<String>();
list.add("Modern");
list.add("Application");
list.add("Development");
list.add("Development");
//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:

Application Development Modern
Modern Application Development

Select all that apply.

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

Correct answer

  • B