uiz Space

September 2024 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java Quiz 2: 1 December 2024 (September 2024 term)

The IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 1 Dec 2024, in the September 2024 term: 15 questions for 100 marks in 120 minutes. Every question is below with its answer. Take it as a timed mock test to be marked, or read it through first.

Questions
15
Marks
100
Duration
120 min
MCQ
13
MSQ
2

Updated

Official paper: IIT M DIPLOMA AN EXAM QDD2 01 Dec 2024 · No negative marking.

Question 1

+6 marksOne correct option

Consider the Java code given below that prints the price of laptops. From among the options, identify the appropriate function header for the function printPrice that takes as input a list of laptops and prints their prices.

java
import java.util.*;
class Laptop {
private double price;
public Laptop(double p) {
price = p;
}
public double getPrice() {
return price;
}
}
class Dell extends Laptop {
public Dell(double p) {
super(p);
}
}
class HP extends Laptop {
public HP(double p) {
super(p);
}
}
public class Test {
// FUNCTION HEADER for function printPrice
{
for (int i = 0; i < lst.size(); i++) {
System.out.println(lst.get(i).getPrice());
}
}
public static void main(String[] args) {
List<Dell> d = new ArrayList<Dell>();
d.add(new Dell(1000.00));
d.add(new Dell(1200.00));
List<HP> h = new ArrayList<HP>();
h.add(new HP(900.00));
h.add(new HP(1100.00));
printPrice(d);
printPrice(h);
}
}

Choose the correct option.

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

Correct answer

  • C

Question 2

+6 marksOne correct option

Consider the Java code given below.

java
interface Purchasable {
public default void purchase() {
System.out.println("Product purchased");
}
}
interface Reviewable {
public default void review() {
System.out.println("Product reviewed");
}
}
class Headphone implements Purchasable, Reviewable {
public void purchase() {
System.out.println("Headphone purchased successfully");
}
}
public class Test {
public static void main(String[] args) {
Purchasable p1 = new Headphone();
p1.purchase();
p1.review();
}
}

Choose the correct option.

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

Correct answer

  • C

Question 3

+6 marksOne correct option

The following code maps a set of athletes names to the number of medals they have won and categorizes the athletes based on whether they are medalists or not.

java
import java.util.*;
public class Olympics {
TreeSet<String> t1 = new TreeSet<String>();
TreeSet<String> t2 = new TreeSet<String>();
public boolean hasMedal(int medals) {
if(medals > 0){
return true;
}
return false;
}
public void filterAthletes(TreeMap<String, Integer> medals) {
for (Map.Entry<String, Integer> entry : medals.entrySet()) {
if (hasMedal(entry.getValue())) {
t1.add(entry.getKey());
} else {
t2.add(entry.getKey());
}
}
}
public void display() {
System.out.println("Medalists: " + t1);
System.out.println("Non-Medalists: " + t2);
}
public static void main(String[] args) {
TreeMap<String, Integer> medals = new TreeMap<String, Integer>();
medals.put("Bolt", 2);
medals.put("Sindhu", 0);
medals.put("Manu", 1);
medals.put("Rahul", 0);
medals.put("Gulan", 3);
Olympics o = new Olympics();
o.filterAthletes(medals);
o.display();
}
}

Choose the correct option.

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

Correct answer

  • B

Question 4

+6 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
class Stock {
String symbol;
String company;
int sharesTraded;
public Stock(String sym, String comp, int shares) {
symbol = sym;
company = comp;
sharesTraded = shares;
}
}
public class Test {
public static void printStocks(ArrayList<Stock> stockList) {
var map = new LinkedHashMap<String, Integer>();
for (Stock s : stockList) {
map.put(s.symbol, map.getOrDefault(s.symbol, 0) + s.sharesTraded);
}
for (Map.Entry<String, Integer> e : map.entrySet()) {
System.out.println(e.getKey() + " = " + e.getValue());
}
}
public static void main(String[] args) {
ArrayList<Stock> stockList = new ArrayList<Stock>();
stockList.add(new Stock("AAPL", "Apple", 1500));
stockList.add(new Stock("MSFT", "Microsoft", 2000));
stockList.add(new Stock("GOOGL", "Alphabet", 1200));
stockList.add(new Stock("AAPL", "Apple", 800));
printStocks(stockList);
}
}

What will the output be?

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

Correct answer

  • B

Question 5

+6 marksOne or more correct options

Consider the Java code given below that prints the highest purchased amount among a set of given Buyer objects. From among the options, identify the appropriate function header for the function printHighestPurchase that takes as input an array of Buyer objects and prints the highest purchase amount.

java
import java.util.*;
interface Buyer {
public abstract double getPurchaseAmount();
}
class GoldCustomer implements Buyer {
private double purchaseAmount;
// Constructor
// method getPurchaseAmount() that returns purchase amount
}
class SilverCustomer implements Buyer {
private double purchaseAmount;
// Constructor
// method getPurchaseAmount() that returns purchase amount
}
public class Test {
// LINE 1: FUNCTION HEADER for printHighestPurchase()
{
// invokes method getPurchase()
// to print the value of highest purchase amount
}
public static void main(String[] args) {
Buyer[] cust = {
new GoldCustomer(90000),
new SilverCustomer(60000),
new GoldCustomer(45000)
};
printHighestAmount(cust);
}
}

Choose the correct option(s) for LINE 1.

Select all that apply.

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

Correct answers

  • B
  • D

Question 6

+7 marksOne correct option

Consider the Java code given below.

java
class AccessDeniedException extends Exception {
public AccessDeniedException(String message) {
super(message);
}
}
class User {
private String username;
private int accessStatus;
public User(String u, int a) {
this.username = u;
this.accessStatus = a;
}
public void checkPermission() throws AccessDeniedException {
if (accessStatus == 0) {
throw new AccessDeniedException("Access denied");
}
else {
System.out.println("Access enabled");
}
}
}
public class Test {
public static void main(String[] args) {
User obj1 = new User("Jimmy", 1);
User obj2 = new User("Amritha", 0);
try {
obj1.checkPermission();
obj2.checkPermission();
} catch (AccessDeniedException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

Choose the correct option.

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

Correct answer

  • A

Question 7

+7 marksOne correct option

Consider two Java files located in the same package as shown below.

A.java:

java
package com.pack1;
public class A {
void methodOne() {
System.out.println("Display methodOne");
}
private void methodTwo() {
System.out.println("Display methodTwo");
}
protected void methodThree() {
System.out.println("Display methodThree");
}
public void methodFour() {
System.out.println("Display methodFour");
}
}

B.java

java
package com.pack1;
public class B extends A {
public static void main(String[] args) {
B obj = new B();
obj.methodOne(); //LINE 1
obj.methodTwo(); //LINE 2
obj.methodThree(); //LINE 3
obj.methodFour(); //LINE 4
}
}

Choose the correct option.

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

Correct answer

  • D

Question 8

+7 marksOne correct option

Consider the Java code given below.

java
public class AssertTest {
public static double computeTax(double salary, double investments) {
double taxableIncome = 0;
double taxRate = 0.2;
double tax = 0;
assert salary > 0 ; // assert-1
assert investments > 0; // assert-2
taxableIncome = salary - investments;
assert taxableIncome >= 0 ; // assert-3
tax = taxableIncome * taxRate;
return tax;
}
public static void main(String[] args) {
double salary = 120000;
double investments = 0;
assert salary > 0 ; // assert-4
double tax = computeTax(salary, investments);
}
}

Identify the first assert statement that throws the AssertionError when the class is executed as:

bash
java -ea AssertTest
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 9

+7 marksOne correct option

Consider the Java code given below.

java
interface Portable {
void showportable();
}
class Laptop implements Portable {
public void showportable() {
System.out.println("Laptop is portable");
}
}
class Tablet implements Portable {
public void showportable() {
System.out.println("Tablet is portable");
}
}
class DeviceList {
private Object[] pArr = {new Laptop(), new Tablet()};
public void testPortability() {
for (int i = 0; i < pArr.length; i++) {
// LINE 1
}
}
}
public class Test {
public static void main(String[] args) {
DeviceList dList = new DeviceList();
dList.testPortability();
}
}

Identify the appropriate option to fill in place of LINE 1 such that the output is:

text
Laptop is portable
Tablet is portable
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 10

+7 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
class Book {
String title;
int availableCopies;
//contructor to initialize title and availableCopies
public String toString() {
return title;
}
}
public class Test {
public static boolean isAvailable(int x) {
if(x < 3)
return false;
return true;
}
public static void getFinalList(List<Book> bookList) {
Iterator<Book> it = bookList.iterator();
while (it.hasNext()) {
Book b = it.next();
if (!isAvailable(b.availableCopies))
------------------------------ //LINE 1
}
}
public static void main(String[] args) {
ArrayList<Book> books = new ArrayList<Book>();
books.add(new Book("Alchemist", 3));
books.add(new Book("Wings", 0));
books.add(new Book("Fire", 5));
books.add(new Book("Truth", 1));
getFinalList(books);
System.out.println(books);
}
}

Choose the correct option to be filled in place of LINE 1 so that the output is:

text
[Alchemist, Fire]
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 11

+7 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
import java.util.stream.*;
public class MyClass {
public static void main(String args[]) {
List<Integer> numbers = new ArrayList<>();
numbers.add(2);
numbers.add(8);
numbers.add(5);
numbers.add(12);
numbers.add(15);
Stream<Integer> filteredNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * 2);
filteredNumbers.forEach(System.out::println);
}
}

What will the output be?

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

Correct answer

  • D

Question 12

+7 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Double> expenses1 = new ArrayList<>();
expenses1.add(120.50);
expenses1.add(300.75);
expenses1.add(45.20);
List<Double> expenses2 = new ArrayList<>();
expenses2.add(200.00);
expenses2.add(50.25);
expenses2.add(75.30);
Map<String, Double> avgExpenses = new HashMap<>();
Map<String, List<Double>> expenseMap = new HashMap<>();
expenseMap.put("Amit", expenses1);
expenseMap.put("Priya", expenses2);
Set<String> names = expenseMap.keySet();
for(String name : names){
List<Double> temp = expenseMap.get(name);
int count = 0;
double sum = 0;
***----------***
CODE BLOCK
***----------***
double avg = sum / count;
avgExpenses.put(name, avg);
}
System.out.println(avgExpenses);
}
}

Choose the correct option to fill in the CODE BLOCK to add the name and average expense for each person as map entries in Map<String, Double> avgExpenses.

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

Correct answer

  • D

Question 13

+7 marksOne correct option

Consider the Java code given below.

java
import java.util.*;
public class SetIteratorTest {
public static void main(String[] args) {
var set1 = new HashSet<String>();
set1.add("Bottle");
set1.add("Bag");
set1.add("Paper");
set1.add("Cover");
var set2 = new TreeSet<String>(set1);
Iterator<String> it1 = set1.iterator();
Iterator<String> it2 = set2.iterator();
while (it1.hasNext()) {
System.out.print(it1.next()+ " ");
}
System.out.println();
while (it2.hasNext()) {
System.out.print(it2.next()+ " ");
}
}
}

Choose the correct option.

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

Correct answer

  • C

Question 14

+7 marksOne correct option

Consider the following Java code.

java
class Task implements Cloneable {
String taskName;
public Task(String n) {
taskName = n;
}
public Task clone() throws CloneNotSupportedException {
return (Task) super.clone();
}
}
class Project implements Cloneable {
String projectName;
Task task1;
Task task2;
public Project(String pN, Task t1, Task t2) {
projectName = pN;
task1 = t1;
task2 = t2;
}
public Project clone() throws CloneNotSupportedException {
Project p = (Project) super.clone();
p.task1 = p.task1.clone();
p.task2 = p.task2.clone();
return p;
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Task t1 = new Task("Design");
Task t2 = new Task("Development");
Project proj1 = new Project("ProjectX", t1, t2);
Project proj2 = proj1.clone();
proj2.task1.taskName = "Research";
proj2.projectName = "ProjectY";
System.out.println(proj1.projectName + " : " + proj1.task1.taskName);
System.out.println(proj2.projectName + " : " + proj2.task1.taskName);
}
}

What will the output be?

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

Correct answer

  • C

Question 15

+7 marksOne or more correct options

Consider the Java code given below that should print the names of vehicles whose mileage is between 15.0 and 25.0 (both inclusive).

java
import java.util.*;
class Vehicle {
String name;
double mileage;
public Vehicle(String n, double m) {
name = n;
mileage = m;
}
}
public class Test {
public static void main(String[] args) {
List<Vehicle> vehicles = new ArrayList<>();
vehicles.add(new Vehicle("Toyota", 18.5));
vehicles.add(new Vehicle("Honda", 20.2));
vehicles.add(new Vehicle("Ford", 14.8));
vehicles.add(new Vehicle("Chevrolet", 25.0));
vehicles.add(new Vehicle("BMW", 30.0));
//CODE BLOCK
}
}

Choose the correct option(s) to fill in place of CODE BLOCK to obtain the right answer.

Select all that apply.

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

Correct answers

  • B
  • C