uiz Space

January 2025 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java Quiz 1: 23 February 2025 (January 2025 term)

The IIT Madras BS Programming Concepts using Java (Java) Quiz 1 paper sat on 23 Feb 2025, in the January 2025 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 23 Feb 2025 · No negative marking.

Question 1

+6 marksOne correct option

Consider the code given below. How many activation records are created while executing this code?

java
class ClassOne{
public void methodOne(){
// ...
}
public void methodTwo(){
// ...
methodOne();
// ...
}
public void methodThree(){
// ...
methodTwo();
// ...
}
}
class ClassTwo{
public static void main(String[] args){
// ...
ClassOne c = new ClassOne();
c.methodThree();
}
}
  1. A

    4

  2. B

    3

  3. C

    5

  4. D

    2

Show answer

Correct answer

  • A

    4

Question 2

+6 marksOne correct option
  1. A

    SeniorProgrammer is a valid subtype of Programmer because it has all methods of Programmer and additional functionality.

  2. B

    Programmer is not a valid subtype of SeniorProgrammer because it lacks the method reviewCode()

  3. C

    Subtyping allows SeniorProgrammer to be used wherever a Programmer is expected.

  4. D

    Subtyping allows Programmer to be used wherever a SeniorProgrammer is expected.

Show answer

Correct answer

  • D

    Subtyping allows Programmer to be used wherever a SeniorProgrammer is expected.

Question 3

+6 marksOne correct option

Consider the Java code given below.

java
public class NumberEvaluation {
public static void main(String[] args) {
int number = 10;
do {
number--;
} while (number > 5);
switch (number) {
case 5:
System.out.println("Low");
break;
case 6:
System.out.println("Medium");
break;
default:
if (number > 6) {
System.out.println("High");
} else {
System.out.println("Unknown");
}
break;
}
}
}

What will the output be?

  1. A

    Low

  2. B

    Medium

  3. C

    High

  4. D

    Unknown

Show answer

Correct answer

  • A

    Low

Question 4

+6 marksOne correct option

Consider the code given below.

java
class Fruits {
public Fruits() {
System.out.println("Fruit created!");
}
public void display() {
System.out.println("Fruits!");
}
}
class Grapes extends Fruits {
public Grapes() {
super();
System.out.println("Grape created!");
}
public void display() {
System.out.println("Grapes!");
}
}
public class Example {
public static void main(String[] args) {
Fruits a = new Grapes();
a.display();
}
}

Choose the correct option.

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

Correct answer

  • B

Question 5

+6 marksOne correct option

Consider the Java code given below.

java
interface Animal {
default void sound() {
System.out.println("Animal making sound");
}
void eat();
}
interface Mammal extends Animal {
default void sound() {
System.out.println("Mammal making sound");
}
}
class Dog implements Mammal {
public void eat() {
System.out.println("Dog eating");
}
}
public class Test {
public static void main(String[] args) {
Animal obj = new Dog(); // LINE 1
obj.sound(); // LINE 2
obj.eat();
}
}

Choose the correct option.

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

Correct answer

  • C

Question 6

+7 marksOne correct option

Consider the code given below that checks whether two players are the same. Method equals is overridden to compare two Player objects as follows.
If two players have the same playerID and teamName, then they are said to be equal.

java
class Player {
private String playerID;
private String teamName;
// Constructor to initialize instance variables
public boolean equals(Object obj) {
//CODE BLOCK
}
}
public class Test {
public static void main(String[] args) {
Player p1 = new Player("004", "Red");
Player p2 = new Car("004", "Red");
if (p1.equals(p2))
System.out.println("p1 and p2 are same");
else
System.out.println("p1 and p2 are different");
}
}

Choose the correct option to fill in place of CODE BLOCK so that the output is:
p1 and p2 are same

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

Correct answer

  • B

Question 7

+7 marksOne correct option

Consider the Java code given below.

java
interface Vehicle {
default void drive() {
System.out.println("Vehicle is driving");
}
}
interface Fuel {
default void refuel() {
System.out.println("Vehicle is refueling");
}
}
class Car implements Vehicle, Fuel {
public void drive() {
System.out.println("Car is driving smoothly");
}
}
public class Test {
public static void main(String[] args) {
Vehicle v1 = new Car();
v1.drive();
v1.refuel(); // LINE 1
}
}

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
class Book{
String name;
String[] authors;
public Book(String nm, String[] a){
name = nm;
authors = a;
}
public Book(Book b){
this.name = b.name;
this.authors = b.authors;
}
}
public class Test{
public static void main(String[] args){
String[] authors = {"Tagore", "Narayan", "Vikram"};
Book b1 = new Book("Gita", authors );
Book b2 = new Book(b1);
Book b3 = b1;
b2.name = "Nirmala";
b2.authors[0] = "Laxmana";
System.out.println(b1.name + ", " + b1.authors[0]);
System.out.println(b2.name + ", " + b2.authors[0]);
System.out.println(b3.name + ", " + b3.authors[0]);
}
}

What will the output be?

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

Correct answer

  • A

Question 9

+7 marksOne correct option

Consider the Java code given below.

java
class Appliance {
public void turnOn() {
System.out.println("Turn on appliance");
}
}
class Fan extends Appliance {
public void turnOn() {
System.out.println("Turn on fan");
}
public void turnOn(int speed) {
System.out.println("Set fan speed to: " + speed);
}
}
class SmartFan extends Fan {
public void turnOn() {
super.turnOn();
System.out.println("Turn on smart fan with default settings");
}
}
public class Test {
public static void main(String[] args) {
Fan obj = new SmartFan(); // LINE 1
obj.turnOn(); // LINE 2
obj.turnOn(3); // LINE 3
}
}

Choose the correct option.

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

Correct answer

  • A

Question 10

+7 marksOne correct option

Consider the Java code given below.

java
class BankAccount {
private int accountId;
private static double interestRate = 3.5;
public BankAccount(int accId) {
accountId = accId;
}
public final double calculateInterest() {
return interestRate * 0.01;
}
}
class SavingsAccount extends BankAccount {
public SavingsAccount(int accountId) {
super(accountId);
}
public final double calculateInterest() { // LINE 1
return (interestRate + 0.5) * 0.01; // LINE 2
}
}
public class BankTest {
public static void main(String[] args) {
SavingsAccount s1 = new BankAccount(505); // LINE 3
BankAccount b1 = new SavingsAccount(501); // LINE 4
System.out.println(b1.calculateInterest());
System.out.println(s1.calculateInterest());
}
}

Which of the following statements is FALSE?

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

Correct answer

  • D

Question 11

+7 marksOne correct option

Consider the Java code given below.

java
interface Flyable {
default void fly() {
System.out.println("Flyable: Flying");
}
void takeOff();
}
-------- CODE BLOCK ------------
class Sparrow extends Bird {
public void takeOff() {
System.out.println("Sparrow taking off");
}
}
public class Test {
public static void main(String[] args) {
Flyable obj = new Sparrow();
obj.fly();
obj.takeOff();
}
}

Identify the correct option to fill in place of CODE BLOCK, such that the output is:

Bird: Flying high
Sparrow taking off

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

Correct answer

  • B

Question 12

+7 marksOne correct option

Consider the Java code given below.

java
interface OrderCallback {
void onOrderReady(String dish);
}
class Waitstaff implements OrderCallback {
public void orderDish(String dish) {
Chef c = new Chef(this); // LINE 1
c.prepareDish(dish);
}
public void onOrderReady(String dish) {
System.out.println("Waitstaff: " + dish + " served!")
;
}
}
class Chef {
private OrderCallback callback;
public Chef(OrderCallback cb) {
callback = cb;
}
public void prepareDish(String dish) {
System.out.println("Chef: Preparing " + dish + "...")
;
System.out.println("Chef: " + dish + " is ready.");
callback.onOrderReady(dish); // LINE 2
}
}
public class Restaurant {
public static void main(String[] args) {
Waitstaff s1 = new Waitstaff();
s1.orderDish("pasta");
}
}

Choose the correct option.

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

Correct answer

  • D

Question 13

+7 marksOne correct option

Consider the code given below.

java
interface PlaylistIterator {
public boolean hasNextSong();
public Object getNextSong();
}
abstract class Song {
public abstract void play();
}
class SongPlaylist {
public SongPlaylist() {
private final int count = 3;
private Track[] songs = {new Track("Song A"), new Track("Song B"),
new Track("Song C")};
}
private class Track extends Song {
private String title;
public Track(String title) {
this.title = title;
}
public void play() {
System.out.println("Playing: " + title);
}
}
private class PlaylistIter implements PlaylistIterator {
private int currentIndex;
public PlaylistIter() {
// Constructor
}
public boolean hasNextSong() {
// Check if there is a next song
}
public Object getNextSong() {
// Return the next song
}
}
public PlaylistIterator getIterator() {
return new PlaylistIter();
}
}
public class PlaylistTest {
public static void main(String[] args) {
SongPlaylist playlist = new SongPlaylist();
PlaylistIterator iter = playlist.getIterator();
while (iter.hasnext()){
-----------------------; //LINE 1
}
}
}
  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • A

Question 14

+7 marksOne or more correct options

Consider the Java program below.

java
class Appliance {
public void turnOn() {
System.out.println("Appliance is turned on");
}
}
class WashingMachine extends Appliance {
public void turnOn() {
System.out.println("Washing machine is turned on");
}
public void startWashCycle() {
System.out.println("Wash cycle starts");
}
}
public class TestAppliance {
public static void main(String[] args) {
Appliance obj = new WashingMachine();
obj.turnOn();
--------------------// LINE 1
}
}

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

Washing machine is turned on
Wash cycle starts

Select all that apply.

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

Correct answers

  • C
  • D

Question 15

+7 marksOne or more correct options

Consider the Java code given below. Identify the correct option(s) to fill in the blank at LINE 1, such that the output is: Not eligible for Insurance Claim

java
interface ClaimEligibility {
void printEligibility();
}
class Insurance {
private int age;
public Insurance(int a) {
age = a;
}
public ClaimEligibility checkEligibility() {
if (age > 25)
return new EligibleForClaim();
return new NotEligibleForClaim();
}
private class EligibleForClaim implements ClaimEligibility {
public void printEligibility() {
System.out.println("Eligible for Insurance Claim");
}
}
private class NotEligibleForClaim implements ClaimEligibility {
public void printEligibility() {
System.out.println("Not eligible for Insurance Claim");
}
}
}
public class Test {
public static void main(String[] args) {
Insurance ins1 = new Insurance(20);
---------------------------- // LINE 1
}
}

Select all that apply.

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

Correct answers

  • A
  • B