Quiz Space

January 2023 term · Programming Concepts using Java · BSCS2005

Java End Term: 30 April 2023, Set QPD1-S1 (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-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
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 ClassOne{
2 public void methodOne(){
3 // ...
4 }
5 public void methodTwo(){
6 // ...
7 methodOne();
8 // ...
9 }
10 }
11 class ClassTwo{
12 public static void methodThree(){
13 // ...
14 ClassOne c = new ClassOne();
15 c.methodTwo();
16 // ...
17 }
18 public static void main(String[] args) {
19 // ...
20 methodThree();
21 }
22 }

During execution of Line 8 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

  • C

Question 2

+4 marksOne correct option

Consider the following code.

Consider the following code.

java
class Equipment implements Cloneable{
String type;
// Constructor
// Accessor method getType()
// Mutator method setType()
public Equipment clone() throws CloneNotSupportedException{
return (Equipment)super.clone();
}
}
class Lab implements Cloneable{
Equipment eqp;
String name;
// Constructor
// Mutator method setName()
public Lab clone() throws CloneNotSupportedException{
Lab lb = (Lab)super.clone();
lb.eqp = lb.eqp.clone();
return lb;
}
public String toString(){
return eqp.getType() + ":" + name;
}
}
public class Test {
public static void main(String[] args) {
Lab lb1 = new Lab(new Equipment("Computer"), "Computer");
try{
Lab lb2 = lb1.clone();
lb2.eqp.setType("Milling Machine");
lb2.setName("Mechanical");
System.out.println(lb1);
System.out.println(lb2);
}
catch(Exception e){
System.out.println(e);
}
}
}

What will the output be?

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

Correct answer

  • C

Question 3

+4 marksOne correct option

Consider the code given below

java
class Animal{
public void legs(){
System.out.println("Number of legs are not known");
}
public void wings(){
System.out.println("Wings may exist");
}
}
class Dog extends Animal{
public void legs(){
System.out.println("Four legs");
}
}
class Ostrich extends Animal{
public void legs(){
System.out.println("Two legs");
}
public void wings(){
System.out.println("Two wings");
}
}
public class Test{
static void show(Animal[] a){
for(int i = 0; i < a.length; i++){
a[i].legs();
a[i].wings();
}
}
public static void main(String[] args){
Animal[] a = {new Dog(), new Ostrich()}; // LINE 1
show(a);
}
}

Choose the correct option.

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

Correct answer

  • B

Question 4

+4 marksOne correct option

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

java
class Rectangle{
private int length;
private int breadth;
//Constructor to initialize instance variables
public double area() {
return (length * breadth);
}
public boolean equals(Object obj) {
// CODE BLOCK
}
}
public class Test {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(3,5);
Rectangle r2 = new Rectangle(5,3);
if(r1.equals(r2))
System.out.println("r1 and r2 are the same");
else
System.out.println("r1 and r2 are not same");
}
}

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

r1 and r2 are the same

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

Correct answer

  • D

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 sum = 0;
}
class Op1 extends Op{
public void run() {
if (sum != 4) {
sum = sum + 5;
}
}
}
class Op2 extends Op{
public void run() {
if (sum != 5) {
sum = sum + 4;
}
}
}
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.sum);
}
}

Choose the correct option.

  1. A

    The program will never generate the output: 0

  2. B

    The program will never generate the output: 4

  3. C

    The program will never generate the output: 5

  4. D

    The program will never generate the output: 9

  5. E

    The output can be 0 or 4 or 5 or 9.

Show answer

Correct answer

  • E

    The output can be 0 or 4 or 5 or 9.

Question 6

+4 marksOne correct option

Consider the code given below.

java
class Circle{
static final double PI = 3.14;
double radius;
public Circle(double r){
radius = r;
}
public Circle(Circle c){
radius = c.radius;
}
public double perimeter(){
return 2 * PI * radius;
}
}
public class ConTest {
public static void main(String[] args) {
Circle c1 = new Circle(6.0);
Circle c2 = new Circle(c1);
Circle c3 = new Circle(c2);
c1.radius = 4.0;
System.out.println(c1.perimeter());
System.out.println(c2.perimeter());
System.out.println(c3.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 batsmen= new LinkedHashMap<String,Integer>();
batsmen.put("Sachin", 100);
batsmen.put("Sachin", 55 + batsmen.getOrDefault("Sachin", 0));
batsmen.put("Sehwag", 100);
batsmen.put("Sehwag", 55);
for(Map.Entry<String, Integer> obj1:batsmen.entrySet())
System.out.println(obj1.getKey()+":"+obj1.getValue());
}
}

Choose the correct option.

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

Correct answer

  • D

Question 8

+4 marksOne correct option

Consider the Java code given below.

java
class RightAngledException extends Exception{
public RightAngledException(String str) {
super(str);
}
}
class Triangle{
double hypotenuse, base, altitude;
//Constructor to initialize instance variables
public boolean isRightAngled() throws RightAngledException{
double hyp = hypotenuse * hypotenuse;
double b = base * base;
double a = altitude * altitude;
if(hyp != (b+a))
throw new RightAngledException("Not a Right Angled Triangle");
else
return true;
}
}
public class ExceptionTest {
public static void main(String[] args) {
var obj1 = new Triangle(13, 12, 10);
var obj2 = new Triangle(5, 4, 3);
try {
System.out.println(obj1.isRightAngled());
System.out.println(obj2.isRightAngled());
} catch (RightAngledException 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 CreditCard implements Serializable{
private String cardNo = "****************";
private transient int pin = 1000;
private String exp = "00/00";
public CreditCard(String cno, int p, String e) {
cardNo = cno;
pin = p;
exp = e;
}
public String toString() {
return cardNo + ", " + pin + ", " + exp;
}
}
public class SerialTest{
public static void main(String[] args) throws Exception{
var fos = new FileOutputStream("credit.txt");
var os = new ObjectOutputStream(fos);
os.writeObject(new CreditCard("4688171329130605", 9999, "03/23"));
var fis = new FileInputStream("credit.txt");
var ois = new ObjectInputStream(fis);
CreditCard card = (CreditCard)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

  • D

Question 10

+4 marksOne correct option

Consider the code given below.

java
import java.util.*;
class Team{
HashMap<String, String> f = new HashMap<String, String>();
public Team() {
f.put("CSK", "Radhakrishnan");
f.put("MI", "Ambani");
}
public String getOwner(String t){
return f.get(t);
}
}
public class OptionalTest {
public static void main(String[] args){
Optional<String> op1 = Optional.ofNullable(new Team().getOwner("CSK"));
Optional<String> op2 = Optional.ofNullable(new Team().getOwner("RCB"));
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

  • A

Question 11

+4 marksOne correct option

Consider the code given below.

java
import java.util.*;
import java.util.stream.*;
class Faculty{
String name;
double salary;
//Constructor to initialize instance variables
//Method toString() to return name of the faculty
}
public class CollectingTest {
public static void main(String[] args){
var fArr = new ArrayList<Faculty>();
fArr.add(new Faculty("Sravya",30000.00));
fArr.add(new Faculty("Thanvi",50000.00));
fArr.add(new Faculty("Sharadha",100000.00));
fArr.add(new Faculty("Pooja",29000.00));
Map<Boolean, List<Faculty>> facMap;
facMap = fArr.stream()
.collect(Collectors.partitioningBy(f->f.salary >= 50000.00));
System.out.println(facMap.get(false));
}
}

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 code given below.
Assume that file "abc.txt" exists and contains the following text.
Hello IITM students
Assume that there is no file named "xyz.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("abc.txt");
Scanner sc = new Scanner(in);
String data = "";
if(sc.hasNext())
data = sc.nextLine();
var out = new FileOutputStream("xyz.txt");
var dout = new DataOutputStream(out);
dout.writeBytes(data);
System.out.println("Data written to file successfully");
out.close();
dout.close();
sc.close();
}
catch (FileNotFoundException e) {
System.out.println("Files does not exist");
}
catch (IOException e) {
System.out.println("Error in writing to a file");
}
}
}

Choose the correct option regarding the code.

  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.
You may make use of the description given below.
The logarithm of a number n to the base x is the number of times x has to be multiplied with itself so get n, and is denoted by log⁡x(n)\log_x(n).
For example, log⁡2(4)=2\log_2(4) = 2, log⁡10(100)=2\log_{10}(100) = 2. Note that log⁡x(1)=1\log_x(1) = 1.

java
class Logarithm{
private int base;
// Constructor to initialize the instance variable base
public int log(int num) {
assert num > 0; //LINE 1
if (num == 1)
return 0;
assert base > 0; //LINE 2
return 1 + log(num/base);
}
}
public class AssertionTest {
public static void main(String[] args) {
Logarithm obj1 = new Logarithm(10);
Logarithm obj2 = new Logarithm(-2);
System.out.println(obj1.log(1000));
System.out.println(obj2.log(-2));
}
}

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

  • B

Question 14

+4 marksOne correct option

Consider the Java program given below.

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

Correct answer

  • C

Question 15

+4 marksOne correct option

Consider the Java program given below.

java
class Counter implements Runnable{
boolean stopRequested = false;
long count = 0;
public void run() {
while (!stopRequested) {
count++;
if (count==1000000) {
stopRequested = true;
}
}
}
public void setStop(boolean stop){
stopRequested = stop;
}
public long getCount(){
return count;
}
}
public class ThreadEx {
public static void main(String[] args) throws InterruptedException {
Counter ctr = new Counter();
Thread backgroundThread = new Thread(ctr);
backgroundThread.start();
Thread.sleep(1);
ctr.setStop(true);
System.out.println(ctr.getCount());
}
}

What will the output be?

  1. A

    0

  2. B

    1000000

  3. C

    Some whole number between 0 and 1000000

  4. D

    999999

Show answer

Correct answer

  • C

    Some whole number between 0 and 1000000

Question 16

+5 marksOne correct option

Consider the code given below.

java
interface Storable{
void store();
}
interface Transferable{
void transfer();
}
class Laptop{
public HardDisk getDisk() {
return new HardDisk();
}
private class HardDisk implements Storable,Transferable {
public void store() {
System.out.println("Stores data");
}
public void transfer() {
System.out.println("Transfers data");
}
}
}
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:

Stores data
Transfers data

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

Correct answer

  • A

Question 17

+5 marksOne or more correct options

Consider the code given below.

java
1 interface Readable{
2 void read();
3 }
4 interface Displayable{
5 void display();
6 }
7 interface Computable extends Displayable, Readable{
8 void compute();
9 }
10 abstract class Calculator implements Computable{
11 public void compute(){
12 System.out.println("Calculator computes");
13 }
14 }
15 class Phone extends Calculator{
16 public void read(){
17 System.out.println("Phone reads");
18 }
19 public void display(){
20 System.out.println("Phone display");
21 }
22 }
23 public class Test{
24 public static void main(String[] args){
25 Readable r = new Phone();
26 r.read();
27 }
28 }

Choose the correct option/s

Select all that apply.

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

Correct answer

  • D

Question 18

+5 marksOne or more correct options

Consider the Java code given below that prints the seating capacity of each vehicle in a list of vehicles. From among the options, identify the appropriate function header for function printSeating that takes as input a list of Vehicle objects and prints the seating capacity of each.

java
import java.util.*;
abstract class Vehicle{
abstract void capacity();
}
class Bike extends Vehicle{
public void capacity() {
System.out.println("Capacity is two");
}
}
class Auto extends Vehicle{
public void capacity() {
System.out.println("Capacity is three");
}
}
public class Test {
// LINE 1: FUNCTION HEADER
{
// invokes method capacity()
// to print the capacity of each vehicle
}
public static void main(String[] args) {
List<Vehicle> vlist = new ArrayList<Vehicle>();
vlist.add(new Bike());
vlist.add(new Auto());
seating(vlist);
}
}

Choose the correct option(s).

Select all that apply.

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

Correct answers

  • B
  • D

Question 19

+5 marksOne or more correct options

Which of the following statements counts the number of integers between 100 and 150 (including 100 and 150) that are not divisible by 2?

Select all that apply.

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

Correct answers

  • A
  • C

Question 20

+5 marksOne or more correct options

Consider the code given below.

java
import java.util.*;
class Account{
int balance;
public synchronized void withdraw(int amount) {
if(balance > amount)
balance = balance - amount;
System.out.println("After withdrawing, balance = "+balance);
}
public synchronized void deposit(int amount) {
balance = balance + amount;
System.out.println("After depositing, balance = "+balance);
}
}
class User extends Thread{
Account acc;
public User(Account obj){
acc = obj;
}
public void run() {
acc.withdraw(500);
acc.deposit(1000);
}
}
public class Test{
public static void main(String args[]) {
Account my_acc = new Account();
User u1 = new User(my_acc);
User u2 = new User(my_acc);
u1.start();
u2.start();
}
}

Choose all the options that would NEVER occur as a result of execution of this code.

Select all that apply.

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

Correct answers

  • C
  • D

Question 21

+5 marksOne or more correct options

Consider the code given below.

java
import java.util.*;
public class Person{
private String name;
private int age;
public Person(String n, int a) {
name = n;
age = a;
}
public int getAge(){
return age;
}
public void print() {
System.out.println(name + " : " + age);
}
}
public class FClass{
public static void main(String[] args) {
var list = new ArrayList<Person>();
list.add(new Person("Robin", 33));
list.add(new Person("Indra", 76));
list.add(new Person("Smita", 35));
list.add(new Person("Rikki", 26));
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:

Indra : 76
Smita : 35
Robin : 33
Rikki : 26

Select all that apply.

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

Correct answers

  • B
  • C

Question 22

+5 marksOne or more correct options

Consider the code given below.

java
import java.util.*;
public class ListExample {
public static void main(String[] args) {
_____________________________ //LINE 1
_____________________________ //LINE 2
list1 = new ArrayList<String>();
list1.add("India");
list1.add("Hyderabad");
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:

India
Hyderabad
India
Hyderabad

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

  • A
  • D

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) {
ArrayList<String> list = new ArrayList<String>();
list.add("Java");
list.add("Programming");
list.add("Python");
list.add("Script");
// 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:

Java Programming Python Script
Java Programming Python Script

Select all that apply.

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

Correct answers

  • B
  • C