Bird
Raised Fist0

Consider this snippet monitoring Kafka broker metrics in Java:

medium📝 Debug Q14 of Q15
Kafka - Monitoring and Operations

Consider this snippet monitoring Kafka broker metrics in Java:

double bytesIn = jmxClient.getAttribute("kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec");
double bytesOut = jmxClient.getAttribute("kafka.server:type=BrokerTopicMetrics,name=BytesOutPerSec");
if (bytesIn > bytesOut) {
System.out.println("More incoming data");
} else {
System.out.println("More outgoing data");
}

What is the likely bug in this code?

AThe code does not handle the case when bytesIn equals bytesOut
BThe comparison should use >= to handle equal values
CThe metric names are incorrect and will cause an error
DThe JMX attribute values should be cast to int, not double
Step-by-Step Solution
Solution:
  1. Step 1: Review the if-else logic

    The code checks if bytesIn is greater than bytesOut, else prints "More outgoing data".
  2. Step 2: Identify missing case handling

    If bytesIn equals bytesOut, the else block runs, printing "More outgoing data" which is misleading.
  3. Final Answer:

    The code does not handle the case when bytesIn equals bytesOut -> Option A
  4. Quick Check:

    Equal values need explicit handling [OK]
Quick Trick: Check if equal values are handled in if-else [OK]
Common Mistakes:
MISTAKES
  • Assuming metric names are wrong
  • Ignoring equal value scenario
  • Casting to wrong data type without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes