Streams and Functional Interfaces: past questions

17 questions · 97 marks · 8 papers. Try each one before checking.

Solve them and check answers →Read the notes first

2023 Aug

Question 13 MSQ · 4 marks

Consider the Java code given below that should print the names of teachers whose experience is between 2 and 4 (both inclusive).

import java.util.*;
class Teacher{
    String name;
    double experience;
    public Teacher(String name, double exp) {
        this.name = name;
        this.experience = exp;
    }
}
public class Stream {
    public static void main(String[] args) {
        List<Teacher> tList = new ArrayList<Teacher>();
        tList.add(new Teacher("T1", 3.5));
        tList.add(new Teacher("T2", 4.2));
        tList.add(new Teacher("T3", 2.6));
        tList.add(new Teacher("T4", 3.4));
        tList.add(new Teacher("T5", 1.5));
        //CODE BLOCK
    }
}

Choose the correct option(s) to fill in place of CODE BLOCK to obtain the right answer.

  1. tList.stream()
         .map(i -> i.experience >= 2 && i.experience <= 4)
         .forEach(t -> System.out.println(t.name));
  2. tList.stream()
         .filter(i -> i.experience >= 2 && i.experience <= 4)
         .forEach(t -> System.out.println(t.name));
  3. tList.stream()
         .filter(i -> i.experience >= 2)
         .filter(i -> i.experience <= 4)
         .forEach(t -> System.out.println(t.name));
  4. tList.stream()
         .filter(i -> i.experience >= 2)
         .map(i -> i.experience <= 4)
         .forEach(t -> System.out.println(t.name));

Question 15 MSQ · 4 marks

From among the options, choose the code segment(s) that give(s) the same output as is given by the Java code inside the CODE BLOCK.

import java.util.*;
import java.util.stream.*;
public class Test{
    public static void main(String[] args) {
        //CODE BLOCK begins here
        Stream.iterate(1, n -> n + 1)
              .map(n -> n * n)
              .limit(4)
              .forEach(i -> System.out.println(i));
        //CODE BLOCK ends here
    }
}
  1. Stream.iterate(1, n -> n + 1)
          .map(n -> n * n)
          .filter(n -> n <= 4)
          .forEach(i -> System.out.println(i));
  2. Stream.iterate(1, n -> n <= 4, n -> n + 1)
          .map(n -> n * n)
          .forEach(i -> System.out.println(i));
  3. Stream.iterate(1, n -> n + 1)
          .takeWhile(n -> n <= 4)
          .map(n -> n * n)
          .forEach(i -> System.out.println(i));
  4. Stream.iterate(1, n -> n + 1)
          .map(n -> n * n)
          .dropWhile(n -> n <= 16)
          .forEach(i -> System.out.println(i));

2023 Dec

Question 4 MCQ · 3 marks

Consider the following Java code.

import java.util.stream.*;
public class Test {
    public static void main(String[] args) {
        String[] colors = {"red", "green", "blue", "yellow"};
        Stream.of(colors)
              .map((color) -> color.length())
              .filter((length) -> length % 2 == 0)
              .forEach((x) -> System.out.println(x));
    }
}

What will the output be?

  1. 3
    5
  2. blue
    yellow
  3. 4
    6
  4. red
    green

Question 16 MSQ · 4 marks

Consider the following code that computes the sum of the even numbers from array iArr.

import java.util.*;
//CODE BLOCK: Define Filter
public class LTest{
    public static <T extends Number> double sum(T[] x, Filter<T> pred) {
        double s = 0.0;
        for(var i : x){
            if(pred.isValid(i))
                s += i.doubleValue();
        }
        return s;
    }
    public static void main(String[] args) {
        Integer[] iArr = new Integer[]{1, 2, 3, 4, 5};
        System.out.print(sum(iArr, i -> i % 2 == 0));
    }
}

Identify the correct option(s) to fill in place of CODE BLOCK such that the output is 6.0.

  1. interface Filter<T>{
        public boolean isValid(T a);
    }
  2. abstract class Filter<T extends Number>{
        public abstract boolean isValid(T a);
    }
  3. interface Filter<T extends Number>{
        public boolean isValid(T a);
    }
  4. interface Filter<T extends Number>{
        public abstract boolean isValid(T a);
        public abstract boolean predicate(T a);
    }

2024 Mar 24

Question 9 MCQ · 6 marks

Consider the Java code given below.

import java.util.*;
import java.util.stream.*;
public class MyClass {
    public static void main(String args[]) {
        List<String> wordlist = new ArrayList<>();
        wordlist.add("reluctant");
        wordlist.add("test");
        wordlist.add("unpleasant");
        wordlist.add("delicious");
        wordlist.add("away");
        Stream<String> startLongWords = wordlist.stream()
            .filter(w -> w.length() > 5)
            .map(s -> s.substring(0, 2));
        startLongWords.forEach(System.out::println);
    }
}

What will the output be?

  1. re
    te
    un
    de
    aw
  2. te
    aw
  3. re
    un
    de
  4. reluctant
    unpleasant
    delicious

Question 16 MSQ · 8 marks

Consider the Java code given below that should print the names of students whose gpa is between 3.0 and 3.8 (both inclusive).

import java.util.*;
class Student {
    String name;
    double gpa;
    public Student(String n, double g) {
        name = n;
        gpa = g;
    }
}
public class StreamExample {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 3.5));
        students.add(new Student("Bob", 3.2));
        students.add(new Student("Charlie", 3.8));
        students.add(new Student("David", 3.0));
        students.add(new Student("Eva", 4.0));
        //CODE BLOCK
    }
}

Choose the correct option(s) to fill in place of CODE BLOCK to obtain the right answer.

  1. students.stream()
            .map(s -> s.gpa >= 3.0 && s.gpa <= 3.8)
            .forEach(s -> System.out.println(s.name));
  2. students.stream()
            .filter(s -> s.gpa >= 3.0 && s.gpa <= 3.8)
            .forEach(s -> System.out.println(s.name));
  3. students.stream()
            .filter(s -> s.gpa >= 3.0)
            .filter(s -> s.gpa <= 3.8)
            .forEach(s -> System.out.println(s.name));
  4. students.stream()
            .filter(s -> s.gpa >= 3.0)
            .map(s -> s.gpa <= 3.8)
            .forEach(s -> System.out.println(s.name));

2024 Aug 04

Question 8 MCQ · 6 marks

Consider the Java code given below.

import java.util.*;
import java.util.stream.*;
class Transaction {
    String id;
    double amount;
    public Transaction(String i, double a) {
        this.id = i;
        this.amount = a;
    }
    public String toString() {
        return id + ": $" + amount;
    }
}
public class TransactionProcessor {
    public static void main(String[] args) {
        List<Transaction> transactions = new ArrayList<Transaction>();
        transactions.add(new Transaction("TXN1", 999.99));
        transactions.add(new Transaction("TXN2", 5000.00));
        transactions.add(new Transaction("TXN3", 1234.56));
        transactions.add(new Transaction("TXN4", 2500.00));
        transactions.add(new Transaction("TXN5", 330.40));
        transactions.stream()
                    .filter(t -> t.amount > 1000)
                    .forEach(t -> System.out.print(t.id + " "));
    }
}

What will the output be?

  1. TXN1 TXN2 TXN3 TXN4 TXN5

  2. TXN2 TXN3 TXN4

  3. TXN3 TXN4 TXN2

  4. TXN1 TXN3 TXN4 TXN2

Question 13 MCQ · 7 marks

From among the options, choose the code segment that gives the same output as is given by the Java code inside the CODE BLOCK.

import java.util.stream.*;
public class Example {
    public static void main(String[] args) {
        //CODE BLOCK starts here
        long count = Stream.iterate(1, n -> n + 2)
                           .map(n -> n * n)
                           .limit(4)
                           .count();
        //CODE BLOCK ends here
        System.out.println(count);
    }
}
  1. long count = Stream.iterate(1, n -> n + 2)
                       .map(n -> n * n)
                       .filter(n -> n < 81)
                       .count();
  2. long count = Stream.iterate(1, n -> n + 2)
                       .map(n -> n * n)
                       .takeWhile(n -> n < 81)
                       .count();
  3. long count = Stream.iterate(1, n -> n + 2)
                       .map(n -> n * n)
                       .skipWhile(n -> n < 81)
                       .count();
  4. long count = Stream.iterate(1, n -> n + 2)
                       .map(n -> n * n)
                       .dropWhile(n -> n < 81)
                       .count();

2025 Mar 16

Question 12 MCQ · 7 marks

Method Stream.iterate(e, f) returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element e, producing a Stream consisting of e, f(e), f(f(e)), ...

Based on the above information, consider the code given below, and answer the question that follows.

import java.util.stream.*;
public class Test {
    public static void main(String[] args) {
        Stream.iterate(50, p -> p - 5)
              .map(p -> p * 2)
              .filter(p -> p % 4 == 0)
              .limit(4)
              .forEach(p -> System.out.print(p + " "));
    }
}

What will the output be?

  1. 40 20 0 -20

  2. 100 90 80 70

  3. 100 80 60 40

  4. 100 120 140 160

Question 15 MSQ · 7 marks

The Java code given below should print the names of water filters whose flow rate falls within the range of 5.0 to 15.0 (inclusive).

import java.util.*;
import java.util.stream.*;
class WaterFilter {
    String name;
    double flowRate;
    public WaterFilter(String n, double f) {
        this.name = n;
        this.flowRate = f;
    }
}
public class Test {
    public static void main(String[] args) {
        List<WaterFilter> filters = new ArrayList<>();
        filters.add(new WaterFilter("FilterA", 4.5));
        filters.add(new WaterFilter("FilterB", 10.0));
        filters.add(new WaterFilter("FilterC", 12.0));
        filters.add(new WaterFilter("FilterD", 15.0));
        filters.add(new WaterFilter("FilterE", 16.0));
        // CODE BLOCK
    }
}

Choose the correct option(s) to fill in place of CODE BLOCK to obtain the right answer.

  1. filters.stream()
           .map(f -> f.flowRate >= 5.0 && f.flowRate <= 15.0)
           .forEach(f -> System.out.println(f.name));
  2. filters.stream()
           .filter(f -> f.flowRate >= 5.0 && f.flowRate <= 15.0)
           .forEach(f -> System.out.println(f.name));
  3. filters.stream()
           .filter(f -> f.flowRate >= 5.0)
           .filter(f -> f.flowRate <= 15.0)
           .forEach(f -> System.out.println(f.name));
  4. filters.stream()
           .filter(f -> f.flowRate >= 5.0)
           .map(f -> f.flowRate <= 15.0)
           .forEach(f -> System.out.println(f.name));

2026 Apr 06

Question 10 MCQ · 7 marks

Consider the following Java code.

import java.util.*;
import java.util.stream.*;

public class StreamTest {
    public static void main(String[] args) {
        List<String> authors = Arrays.asList("premchand", "narayan", "tagore",
                                             "desai", "bond");

        authors.stream()
               .filter(w -> w.length() > 6)
               .map(w -> w.toUpperCase())
               .skip(1)
               .forEach(System.out::println);
    }
}

What will the output be?

  1. PREMCHAND NARAYAN

  2. NARAYAN

  3. PREMCHAND

  4. TAGORE DESAI

Question 14 MSQ · 8 marks

Consider the Java code given below that should print the names of individuals whose BMI is between 18 and 25 (both inclusive).

import java.util.*;
class Person {
    String name;
    double bmi;
    public Person(String n, double b) {
        name = n;
        bmi = b;
    }
}
public class TestBMI {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        people.add(new Person("Arjun", 22.5));
        people.add(new Person("Meera", 18.0));
        people.add(new Person("Rahul", 27.3));
        people.add(new Person("Sara", 24.8));
        people.add(new Person("Imran", 30.2));
        people.add(new Person("David", 17.5));
        //CODE BLOCK
    }
}

Choose the correct option(s) to fill in place of CODE BLOCK to obtain the right answer.

  1. people.stream()
          .map(p -> p.bmi >= 18 && p.bmi <= 25)
          .forEach(p -> System.out.println(p.name));
  2. people.stream()
          .filter(p -> p.bmi >= 18)
          .map(p -> p.bmi <= 25)
          .forEach(p -> System.out.println(p.name));
  3. people.stream()
          .filter(p -> p.bmi >= 18 && p.bmi <= 25)
          .forEach(p -> System.out.println(p.name));
  4. people.stream()
          .filter(p -> p.bmi >= 18)
          .filter(p -> p.bmi <= 25)
          .forEach(p -> System.out.println(p.name));

2025 Aug 03

Question 6 MCQ · 6 marks

Method Stream.iterate(e, f) returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element e, producing a Stream consisting of e, f(e), f(f(e)), etc.

Based on the above information, consider the code given below, and answer the question that follows.

import java.util.stream.*;
public class StreamTest {
    public static void main(String[] args) {
        Stream.iterate(5, n -> n + 3)
              .filter(n -> n % 4 == 0)
              .map(n -> n / 2)
              .limit(4)
              .forEach((x) -> System.out.print(x + " "));
    }
}

What will the output be?

  1. 2 4 6 8

  2. 4 6 8 10

  3. 4 10 16 22

  4. 8 16 24 32

Question 15 MSQ · 7 marks

From among the options, choose the code segment(s) that produce(s) the same output as the Java code given in the CODE BLOCK.

import java.util.stream.*;
import java.util.*;
public class Test {
    public static void main(String[] args) {
        //CODE BLOCK begins here
        Stream.of("a", "bb", "ccc", "dddd")
              .map(s -> s.length())
              .forEach(n -> System.out.println(n));
        //CODE BLOCK ends here
    }
}
  1. List<String> list = List.of("a", "bb", "ccc", "dddd");
    list.stream()
        .map(s -> s.length())
        .forEach(n -> System.out.println(n));
  2. Stream.iterate(1, n -> n + 1)
          .limit(4)
          .forEach(n -> System.out.println(n));
  3. String[] arr = {"a", "bb", "ccc", "dddd"};
    Arrays.stream(arr)
          .map(String::length)
          .forEach(System.out::println);
  4. Stream.of("a", "bb", "ccc", "dddd")
          .filter(s -> s.length() > 2)
          .forEach(s -> System.out.println(s.length()));

Question 7 MCQ · 6 marks

Consider the Java code given below.

import java.util.*;
public class Sample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(10, 20, 25, 15, 5, 30, 35);
        numbers.stream()
               .takeWhile(n -> n % 2 == 0)
               .forEach(n -> System.out.print(n + " "));
        System.out.println();
        numbers.stream()
               .dropWhile(n -> n % 2 == 0)
               .forEach(n -> System.out.print(n + " "));
    }
}

What will the output be?

  1. 10 20
    25 15 5 30 35
  2. 10 20 25
    15 5 30 35
  3. 10 20 30
    25 15 5 35
  4. 10 20
    25 15 5 35

2022 July

Question 5 MCQ · 3 marks

Consider the following program.

import java.util.stream.*;
import java.util.*;
class Order{
    private String Id;
    private double amount;
    public Order(String id, double amt){
        this.Id = id;
        this.amount = amt;
    }
    public double getAmount(){
        return amount;
    }
}
public class Test{
    public static void main(String[] args){
        var oList = new ArrayList<Order>();        //LINE 1
        oList.add(new Order("A0000", 1000.0));
        oList.add(new Order("A0001", 600.0));
        oList.add(new Order("A0002", 1200.0));
        oList.add(new Order("A0003", 900.0));
        var elements = oList.stream()              //LINE 2
            .filter((x) -> x.getAmount() > 900)
            .count();
        System.out.println(elements);              //LINE 3
    }
}

Choose the correct option.

  1. It generates compiler error at LINE 1 and LINE 2

  2. It compiles without any error but generates NullPointerException at LINE 1 and LINE 2

  3. It compiles without any error but generates NullPointerException at LINE 2 and LINE 3

  4. It generates the output: 2

  5. It generates the output: 3

Question 13 MSQ · 4 marks

Which of the following statements can count the number of integers between 50 and 100 that are not divisible by 2?

  1. Stream.iterate(50, n -> n + 1).limit(50).filter(n -> n % 2 != 0).count();

  2. Stream.iterate(50, n -> n + 1).filter(n -> n % 2 != 0).count();

  3. Stream.iterate(50, n -> n < 100, n -> n + 1).filter(n -> n % 2 != 0).count();

  4. Stream.filter(50, n -> n < 100, n -> n + 1).iterate(n -> n % 2 == 0).count();

Check your answers →

Weeks covering this topic

Other Java topics