Quiz Space

Programming Concepts using Java · Quiz 2 · 23 Nov 2025 · September 2025 term

Question 15: Consider the Java code given below.\ class NegativeBalan…

Question 15

+7 marksOne correct option

Consider the Java code given below.
class NegativeBalanceException extends Exception {
public NegativeBalanceException() {
super("Transaction failed: negative balance");
}
}
class BankAccount {
private double balance;
public BankAccount(double b) {
balance = b;
}
public void withdraw(double amount) throws NegativeBalanceException {
if(balance - amount < 0)
throw new NegativeBalanceException();
balance -= amount;
System.out.println("Withdrawal successful: " + balance);
}
}
public class TestBank {
public static void main(String[] args) {
try {
BankAccount acc1 = new BankAccount(500);
BankAccount acc2 = new BankAccount(200);
acc1.withdraw(600);
acc2.withdraw(150);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Choose the correct option.

  1. A

    This program generates output:
    Transaction failed: negative balance

  2. B

    This program generates output:
    Transaction failed: negative balance
    Withdrawal successful: 50.0

  3. C

    This program generates output:
    Withdrawal successful: -100.0
    Withdrawal successful: 50.0

  4. D

    The program crashes due to the uncaught exception: NegativeBalanceException

Show answer

Correct answer

  • A

    This program generates output:
    Transaction failed: negative balance

Question 15 of 15 in the IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 23 Nov 2025, in the September 2025 term (IIT M DIPLOMA AN EXAM QDD2 23 Nov 2025 NEW). It carries 7 marks.

More questions from this paper

  1. Q1Consider the Java code given below that should print the names of students whose grades are between 70.0 and 90.0 (both…
  2. Q2Consider the code given below.\ interface Vehicle {\ public abstract double getFuelEfficiency();\ }\ class Car implemen…
  3. Q3Consider the Java code given below. Choose the correct option to fill in place of CODE BLOCK so that the output is: Ple…
  4. Q4Consider the following Java code. What will the output be?
  5. Q5Consider the Java code given below. How does class MathUtils look after type erasure?
  6. Q6Consider the Java code given below.\ interface Discount{\ public void apply(int price);\ }\ class ShoppingCart{\ public…
  7. Q7Consider two Java files located in two different packages as shown below.\ Account.java:\ package com.bank;\ public cla…
  8. Q8Consider the Java code given below.\ interface Shape {\ void draw();\ }\ class ShapeFactory {\ private String type;\ pu…
  9. Q9The merge(K key, V value, remappingFunction) function is defined as: If the specified key is not already associated wit…
  10. Q10Consider the Java code given below that prints the average rating of a set of Rateable objects. From among the options,…
  11. Q11Consider the code given below. Identify the appropriate option(s) to fill in the blank at LINE 1 such that the output o…
  12. Q12Consider the Java code given below.\ class Student {\ private int marks;\ public Student(int marks){\ assert marks >= 0…
  13. Q13Consider the Java code given below. Choose the correct option.
  14. Q14Consider the Java code given below.\ interface Chargeable {\ void showChargeStatus();\ }\ class Laptop implements Charg…