Quiz Space

January 2022 term · Programming Concepts using Java · BSCS2005

Java End Term: 3 April 2022, Set FN1 (January 2022 term)

The IIT Madras BS Programming Concepts using Java (Java) End Term paper sat on 3 Apr 2022, in the January 2022 term, set FN1: 22 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
22
Marks
100
Duration
180 min
MCQ
14
MSQ
8

Updated

Official paper: IIT M FOUNDATION DIPLOMA ENDTERM FN1 3 Apr 2022 · No negative marking.

Question 1

+3 marksOne correct option

Consider the Java code given below.

java
class Vehicle{
public void mileage(){
System.out.println("Mileage is not known.");
}
public void tyres(){
System.out.println("Number of tyres is not known.");
}
}
class Airplane extends Vehicle{
public void mileage(){
System.out.println("Mileage < 1km/L.");
}
}
class Car extends Vehicle{
public void mileage(){
System.out.println("Mileage >= 10km/L.");
}
public void tyres(){
System.out.println("Number of tyres is 4.");
}
}
public class FClass{
static void compute(Vehicle s){
s.mileage();
s.tyres();
}
public static void main(String[] args){
Vehicle c = new Car();
Vehicle a = new Airplane();
compute(c);
compute(a);
}
}

What will the output be?

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

Correct answer

  • A

Question 2

+3 marksOne correct option

Consider the Java code given below.

Suppose the file E:\\test.txt is written successfully by the program.

java
import java.io.*;
import java.util.*;
public class FClass{
static boolean writeToFile(String filename) {
try {
FileOutputStream fos = new FileOutputStream(filename);
PrintStream bos = new PrintStream(fos);
bos.println("it stores two int");
bos.println("10 is one");
bos.println("20 is another");
return true;
}catch(IOException e) {
return false;
}
}
static boolean readFromFile(String filename) {
try {
FileInputStream fis = new FileInputStream(filename);
Scanner sc = new Scanner(fis);
System.out.println(sc.next());
System.out.println(sc.nextLine());
System.out.println(sc.nextInt());
System.out.println(sc.nextLine());
return true;
}catch(IOException e) {
return false;
}
}
public static void main(String[] args) throws IOException{
if(writeToFile("E:\\test.txt") == false)
System.out.println("write failed");
if(readFromFile("E:\\test.txt") == false)
System.out.println("read failed");
}
}

What will the output be?

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

Correct answer

  • C

Question 3

+3 marksOne correct option

The Java code given below produces a toggle button. With the first click of the button, the color of panel pnlColor should become red, with the next click it should become blue, and on the next click, it should turn to red again, and so on. Based on the requirement, answer the question that follows the code.

java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FClass extends JFrame implements ActionListener{
JButton btnToggle;
JPanel pnlColor, pnlBtn;
public FClass(){
setSize(200, 200);
btnToggle = new JButton("Toggle");
btnToggle.addActionListener(this);
btnToggle.setActionCommand("red");
pnlColor = new JPanel();
pnlBtn = new JPanel();
pnlBtn.add(btnToggle);
add(pnlBtn, "South");
add(pnlColor, "Center");
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
/*--------------------*/
/*****CODE SEGMENT*****/
/*--------------------*/
}
public static void main(String[] args){
new FClass();
}
}

Choose the correct code segment inside method actionPerformed() such that the given behaviour can be implemented correctly.

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

Correct answer

  • B

Question 4

+3 marksOne correct option

Consider the Java code given below.

java
class Box{
int length, breadth;
public Box(int l, int b){
length = l;
breadth = b;
}
public Box(Box b){
this.length = b.length;
this.breadth = b.breadth;
}
public int area(){
return length * breadth;
}
}
public class Test {
public static void main(String[] args) {
Box b1 = new Box(3,4);
var b2 = new Box(b1);
b2.length = 5;
System.out.println(b1.area());
System.out.println(b2.area());
}
}

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.*;
class Example{
ArrayList<String> outputlist = new ArrayList<String>();
public void iterateList(ArrayList<String> inputlist) {
Iterator<String> it = inputlist.iterator();
while(it.hasNext()) {
String element = it.next();
if(!outputlist.contains(element))
outputlist.add(element);
else
it.remove();
}
System.out.println(outputlist);
System.out.println(inputlist);
}
}
public class IteratorTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("India");
list.add("IIT");
list.add("JAVA");
list.add("Madras");
list.add("India");
list.add("IIT");
list.add("JAVA");
list.add("Madras");
Example obj = new Example();
obj.iterateList(list);
}
}

What will the output be?

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

Correct answer

  • D

Question 6

+4 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
var results = new ArrayList<Double>();
results.add(19.8);
results.add(20.2);
results.add(18.9);
results.add(30.5);
Stream<Double> s = results.stream();
Optional<Double> value = s.filter(n -> n < 20).max(Double::compareTo);
value.ifPresentOrElse(v -> System.out.println(v),
() -> System.out.println("No value found"));
}
}

Choose the correct option

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

Correct answer

  • D

Question 7

+4 marksOne correct option

Consider the code given below.

java
import java.io.*;
class Ebanking implements Serializable{
String userId;
transient Integer password;
Ebanking(String uid,Integer p){
userId = uid;
password = p;
}
private void writeObject(ObjectOutputStream oos) throws Exception{
oos.defaultWriteObject();
Integer encrypt = (password*100)+10;
oos.writeObject(encrypt);
}
private void readObject(ObjectInputStream ois) throws Exception{
ois.defaultReadObject();
Integer decrypt = (Integer)ois.readObject();
password = (decrypt/100)-10;
}
}
public class Test{
static void login(Ebanking eb){
if(eb.password == null){
System.out.println("Password field is empty.");
}else if(eb.password == 1234){
System.out.println("ID :"+eb.userId+"\nPassword: "+eb.password);
}else{
System.out.println(eb.password);
}
}
public static void main(String[] args) throws Exception{
Ebanking user1 = new Ebanking("user1@net",1234);
FileOutputStream fos = new FileOutputStream("File.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(user1);
FileInputStream fis = new FileInputStream("File.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Ebanking eb = (Ebanking)ois.readObject();
Test.login(eb);
}
}

What will the output be?

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

Correct answer

  • B

Question 8

+4 marksOne or more correct options

Consider the following two .java files.

java
//Dashboard.java
package clientpck;
public class Dashboard{
private String usrname;
public Dashboard(String un){ usrname = un; }
private String operate1(){ return "Operate1 " + usrname; }
String operate2(){ return "Operate2 " + usrname; }
protected String operate3(){ return "Operate3 " + usrname; }
public String operate4(){ return "Operate4 " + usrname; }
}
//MyClass.java
import clientpck.Dashboard;
class ClientDashboard extends Dashboard{
public ClientDashboard(String un){ super(un); }
public String operate1(){ return super.operate1(); } //LINE-1
public String operate2(){ return super.operate2(); } //LINE-2
public String operate3(){ return super.operate3(); } //LINE-3
public String operate4(){ return super.operate4(); } //LINE-4
}
public class MyClass{
public static void main(String[] args){
ClientDashboard cd = new ClientDashboard("TestUser");
//some code
}
}

Among the following lines, which is/are the ones that generate(s) compiler error?

Select all that apply.

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

Correct answers

  • A
  • B

Question 9

+4 marksOne or more correct options

A strong password is a password having a combination of upper case and lower case alphabets, numbers and special characters. Consider the Java code given below that generates 10 passwords randomly. If the generated password is a strong password, then it adds it to a list. Else, it converts it to a strong password (symbolically, here it is converted to upper case), and then adds it to the list.

java
class Producer{
public String producePwd() {
//Generate and return a random password
}
}
public class PwdGen {
static boolean isStrong(String passwd) {
//return true if and only if passwd is a strong password
}
public static void main(String[] args) {
Producer p = new Producer();
List<String> pwdList = new ArrayList<String>();
List<String> strPwd = new ArrayList<String>();
for (int i=0;i<10;i++) {
String s = new String(p.producePwd());
pwdList.add(s);
}
//CODE SEGMENT BEGINS
pwdList.stream()
.forEach(
pwd -> { if (PwdGen.isStrong(pwd)) {
strPwd.add(pwd);
}
else {
strPwd.add(pwd.toUpperCase());
}
}
);
Collections.sort(strPwd);
//CODE SEGMENT ENDS
System.out.println("Strong password list: "+strPwd.toString());
}
}

Choose the code segments each of which can replace the code inside CODE SEGMENT such that the program generates the same output.

Method Signature:
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b): Creates a concatenated stream whose elements are all the elements of Stream a followed by all the elements of Stream b.

Select all that apply.

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

Correct answer

  • A

Question 10

+4 marksOne or more correct options

Consider the following code below.

java
import java.util.*;
import java.util.concurrent.*;
class Test extends Thread{
static ConcurrentHashMap<Integer,Integer> mp = new ConcurrentHashMap();
public void run(){
Integer[] prime_no = {3, 5, 7, 11, 13, 17, 19, 23};
for(int i=0;i<prime_no.length;i++){
mp.put((i+2),prime_no[i]);
}
}
public static void main(String[] args) throws InterruptedException{
mp.put(1,2);
Test t=new Test();
t.start();
for (Object o : mp.entrySet()){
System.out.println(o);
Thread.sleep(100);
}
}
}

Which of the following is/are NOT true about the given code?

Select all that apply.

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

Correct answers

  • A
  • C

Question 11

+5 marksOne correct option

Consider the following Java code and choose the correct option.

java
import java.util.*;
import java.util.stream.*;
class Employee{
String name;
int service;
Employee(String name, int service){
this.name = name;
this.service = service;
}
public String toString() {
return name;
}
}
public class PartitionStream{
public static void main(String[] args){
var empArr=new ArrayList<Employee>();
empArr.add(new Employee("Tapti",3));
empArr.add(new Employee("Nila",5));
empArr.add(new Employee("Narmada",10));
empArr.add(new Employee("Satluj",11));
Map<Boolean, List<Employee>> empMap = empArr.stream()
.collect(Collectors.partitioningBy(i->i.service<10));
System.out.println(empMap.get(false));
}
}
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • D

Question 12

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

Which among the following is NOT a possible output?

  1. A

    0

  2. B

    2

  3. C

    3

  4. D

    5

  5. E

    All of these are possible outputs

Show answer

Correct answer

  • E

    All of these are possible outputs

Question 13

+5 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class Cricket {
public static void main(String[] args) {
var wc= new LinkedHashMap<String,String>();
var ac= new LinkedHashMap<String,String>();
var india= new LinkedHashMap<String,String>();
india.put("IND WC", "Twice");
ac.put("IND AC", india.getOrDefault("IND AC", "N/A"));
wc.put("IND WC", india.getOrDefault("IND WC", "N/A"));
var bd= new LinkedHashMap<String,String>();
bd.put("BD AC", "Once");
wc.put("BD WC", bd.getOrDefault("BD WC", "N/A"));
ac.put("BD AC", bd.getOrDefault("BD AC", "N/A"));
for(Map.Entry<String, String> obj1:wc.entrySet())
System.out.println(obj1.getKey()+" "+obj1.getValue());
for(Map.Entry<String, String> obj2:ac.entrySet())
System.out.println(obj2.getKey()+" "+obj2.getValue());
}
}

Choose the correct option.

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

Correct answer

  • B

Question 14

+5 marksOne correct option

Consider the Java code given below.

java
class Normal{
public void show() {
System.out.println("This is A show()");
}
}
class Outer{
private Inner obj;
public Inner getObj() {
return obj;
}
public void createInnerObj() {
this.obj = new Inner();
}
private class Inner extends Normal{
public void show() {
System.out.println("This is C show()");
}
}
}
public class PrivateClassTest {
public static void main(String[] args) {
Outer outerObj=new Outer();
outerObj.createInnerObj();
//LINE-1
innerObj.show();
}
}

Choose the correct option for LINE-1 such that the program generates the following output.

text
This is C show()
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • A

Question 15

+5 marksOne correct option

Consider the Java code given below.

java
interface Inter1{
public void travelOnWater();
}
interface Inter2{
public void travelOnLand();
}
interface Inter3 extends Inter1, Inter2{
public void travelOnIce();
}
class Travel implements Inter3{
public void travelOnWater() {
System.out.println("It travels over water.");
}
public void travelOnLand() {
System.out.println("It travels over land.");
}
public void travelOnIce() {
System.out.println("It travels over ice.");
}
}
class Hovercraft {
public void travel(________) { //LINE-1
obj.travelOnWater();
obj.travelOnLand();
obj.travelOnIce();
}
}
public class Test {
public static void main(String[] args) {
Hovercraft hc = new Hovercraft();
hc.travel(new Travel());
}
}

Choose the correct option to fill in the blank at LINE-1 such that the program generates the following output.

text
It travels over water.
It travels over land.
It travels over ice.
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 16

+5 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class Test {
ArrayList<String> obj1 = new ArrayList<String>();
TreeSet<String> obj2 = new TreeSet<String>();
public boolean property(String element) {
if(element.contains(" "))
return false;
return true;
}
public void move(ArrayList<String> list) {
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String e = it.next();
if(property(e)) {
if(!obj1.contains(e))
obj1.add(e);
}
else {
obj2.add(e);
}
}
System.out.println(obj1);
System.out.println(obj2);
}
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
al.add("Taj Mahal");
al.add("Khajuraho");
al.add("Mysore Palace");
al.add("Charminar");
al.add("Taj Mahal");
al.add("Charminar");
al.add("Mysore Palace");
al.add("Khajuraho");
Test test = new Test();
test.move(al);
}
}

What will the output be?

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

Correct answer

  • C

Question 17

+5 marksOne correct option

Consider the code given below.

java
class Skill implements Cloneable{
private String str;
public Skill(String s) {
str = s;
}
public void updateSkill(String s) {
str = s;
}
public Skill clone() throws CloneNotSupportedException {
return (Skill)super.clone();
}
public String toString() {
return str;
}
}
class Employee implements Cloneable{
private String name;
private Skill sk;
public Employee(String n, String s) {
name = n;
sk = new Skill(s);
}
public void updateEmployee(String n, String s) {
name = n;
sk.updateSkill(s);
}
public Employee clone() throws CloneNotSupportedException {
return (Employee)super.clone();
}
public String toString() {
return name + " : " + sk;
}
}
public class FClass{
public static void main(String[] args) throws CloneNotSupportedException {
Employee e1 = new Employee("Raj", "Python");
Employee e2 = e1.clone();
e2.updateEmployee("Rohini", "Java");
System.out.println(e1 + ", " + e2);
}
}

What will the output be?

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

Correct answer

  • B

Question 18

+5 marksOne or more correct options

Consider the following code which has 4 user-defined threads, two threads would work as depositors to a bank account (object) and the other two work as withdrawers from the same account.

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 Threadutil extends Thread{
Account acc;
public Threadutil(Account obj, String t_name){
super(t_name);
acc = obj;
}
public void run() {
if(Thread.currentThread().getName().contains("withdrawer"))
acc.withdraw(500);
else
acc.deposit(1000);
}
}
public class TClass{
public static void main(String args[]) {
Account my_acc = new Account();
Threadutil t1 = new Threadutil(my_acc,"depositor1");
Threadutil t2 = new Threadutil(my_acc,"withdrawer1");
Threadutil t3 = new Threadutil(my_acc,"depositor2");
Threadutil t4 = new Threadutil(my_acc,"withdrawer2");
t1.start();
t2.start();
t3.start();
t4.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

  • A
  • B

Question 19

+6 marksOne or more correct options

Consider the Java code given below.

java
class CncrOutput extends Thread{
private String msg;
public CncrOutput(String m) {
msg = m;
}
public void run() {
System.out.print(msg + " ");
}
}
public class FClass{
public static void main(String[] args) throws InterruptedException{
Thread th1 = new CncrOutput("A");
Thread th2 = new CncrOutput("B");
th1.start();
System.out.print("C" + " ");
th1.join();
th2.start();
System.out.print("D" + " ");
th2.join();
System.out.print("E" + " ");
}
}

Among the following, which are possible outputs of the program?

Select all that apply.

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

Correct answers

  • B
  • D

Question 20

+6 marksOne or more correct options

Consider the code given below.

java
import java.util.*;
interface Computable{
boolean boundIt(int value);
}
public class FClass{
static ArrayList<Integer> applyBound(
ArrayList<Integer> dataLst, Computable com){
ArrayList<Integer> resLst = new ArrayList<Integer>();
for(Integer data : dataLst) {
if(com.boundIt(data))
resLst.add(data);
}
return resLst;
}
public static void main(String[] args) {
Integer[] values = {5, 2, 1, 7, 8, 3, 4, 6, 9};
ArrayList<Integer> iLst = new ArrayList<Integer>(Arrays.asList(values));
ArrayList<Integer> oLst = applyBound(iLst, ______________); //LINE-1
for(int i : oLst)
System.out.print(i + " ");
}
}

Since the second parameter of applyBound requires to be of functional interface Computable type, it may be substituted by a lambda expression. Identify the correct option(s) to fill in the blank at LINE-1 such that the output is 5 7 8 6.

Select all that apply.

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

Correct answers

  • B
  • C

Question 21

+6 marksOne or more correct options

Consider the Java code given below.

java
1 interface Food{
2 int getCalories();
3 }
4 interface Vegetarian extends Food{
5 boolean isLeafy();
6 }
7 interface NonVegetarian extends Food{
8 boolean isSeaFood();
9 }
10 abstract class Soup implements Vegetarian, NonVegetarian{
11 int calories = 1000;
12 public int getCalories() {
13 return calories;
14 }
15 }
16 class ManchowSoup extends Soup{
17 public boolean isLeafy() {
18 return true;
19 }
20 public boolean isSeaFood() {
21 return false;
22 }
23 }
24 public class SubTypeInherit {
25 public static void main(String[] args) {
26 Food s = new ManchowSoup();
27 System.out.println(s.getCalories());
28 }
29 }

What will the result(s) of compiling/executing this code be?

Select all that apply.

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

Correct answer

  • F

Question 22

+6 marksOne or more correct options

Consider the following incomplete code that should segregate positive numbers from a mixed collection of numbers.

java
import java.util.*;
public class Test{
// LINE - 1: Signature of pickPositive method {
for(S val : src) {
if(val.doubleValue() > 0) {
target.add(val.doubleValue());
}
}
}
public static void main(String args[]) {
ArrayList<Integer> num1 = new ArrayList<>();
ArrayList<Double> num2 = new ArrayList<>();
ArrayList<Number> positive_num = new ArrayList<>();
num1.add(67);
num1.add(-8);
num2.add(-42.56);
num2.add(29.33);
pickPositive(num1,positive_num);
pickPositive(num2,positive_num);
System.out.println(positive_num);
}
}

Choose all the correct option(s) corresponding to LINE-1 that would make the program work as expected.

Select all that apply.

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

Correct answer

  • A