Quiz Space

Java Quiz 2: 1 December 2024 (September 2024 term)

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

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

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

12 more questions in this paper

Sign in with Google — it is free — to see every question with its answer and explanation, practise it in learning mode, or take it as a timed mock test.

More on the Java Quiz 2 1 Dec 2024 paper

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. The first 3 questions are below. Sign in with Google — it is free — to see the whole paper with its answers and explanations, in learning mode or as a timed mock test.

FeatureJava Quiz 2 1 Dec 2024 at a glance
TermSeptember 2024 term
SubjectProgramming Concepts using Java
Course codeBSCS2005
Questions15
Marks100
Duration120 min
MCQ13
MSQ2
Official paperIIT M DIPLOMA AN EXAM QDD2 01 Dec 2024
Negative markingNo negative marking.
Updated

Same Quiz 2, other subjects

More Java