Quiz Space

Programming Concepts using Java · End Term · 13 Apr 2025 · January 2025 term · Set QDD3

Question 12: Method Optional.ofNullable(T value) returns an Optional …

Question 12

+4 marksOne correct option

Method Optional.ofNullable(T value) returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional. Based on this description, consider the code given below, and answer the question that follows.

java
import java.util.*;
class HearingAid {
String brand;
String batteryType;
public HearingAid(String b, String bt) {
this.brand = b;
this.batteryType = bt;
}
}
public class Test {
public static void main(String[] args) {
var aidList = new ArrayList<HearingAid>();
aidList.add(new HearingAid("Phonak", "Rechargeable"));
aidList.add(new HearingAid("Widex", null));
for (HearingAid obj : aidList) {
Optional<String> op1 = Optional.ofNullable(obj.batteryType);
op1.ifPresent(type -> System.out.println(type));
}
}
}

Choose the correct option.

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

Correct answer

  • B

Question 12 of 23 in the IIT Madras BS Programming Concepts using Java (Java) End Term paper sat on 13 Apr 2025, in the January 2025 term (IIT M DIPLOMA AN EXAM QDD3 13 Apr 2025). It carries 4 marks.

More questions from this paper

  1. Q1Consider the code given below. Choose the correct option.
  2. Q2Consider the following Java code that uses chained exceptions. Which of these could be the output of this code?
  3. Q3Consider the two Java files given below. Animal.java: Test.java: Choose the correct option.
  4. Q4Consider the Java code given below. Choose the correct option.
  5. Q5Consider the code given below. Choose the correct option.
  6. Q6Consider the Java code given below. What will the output be?
  7. Q7Consider the Java code given below. Choose the correct option.
  8. Q8Consider the Java code given below. What will the output be?
  9. Q9Consider the Java code given below. Choose the correct option.
  10. Q10The method Stream.iterate(e, f) returns an infinite sequential ordered Stream produced by iterative application of a fu…
  11. Q11Consider the code given below. What will the output be?
  12. Q13Consider the code given below. What will the output be?
  13. Q14Consider the following Java code. Choose the correct option when the class is executed as:
  14. Q15Consider the Java code given below. Assume the file "data.txt" already contains the following text: All the best for ex…
  15. Q16Method Collectors.partitioningBy(predicate) returns a Collector which partitions the input elements according to a pred…
  16. Q17Figure question
  17. Q18Which of the following statements is/are correct regarding garbage collection?
  18. Q19Consider the Java code given below. Which of the following is true about the given code.
  19. Q20Consider the code given below. Which of the following options is/are possible result/s of the above code?
  20. Q21Consider the Java code given below that processes Displayable objects in a shop. From among the options, identify the a…
  21. Q22Consider the following Java code. Identify the line/s which has/have errors.
  22. Q23Consider the Java code given below.