Menu
Question Index
...

What will be the output of the following program?

public class PlayFair {
    public static void main(String[] args) {
        StringIterator si = new StringIterator("PlayFair");
        p(si.next());         p(si.next());
        p(si.previous());     p(si.next());
        p(si.next());         p(si.previous());
        p(si.previous());
    }
    private static void p(Character c) { System.out.print(c); }
}
interface IIterator<T> {
    T next();
    T previous();
}
class StringIterator implements IIterator<Character> {
    String s;
    int position = 0;
    public StringIterator(String s) { this.s = s; }
    public Character next() {
        return position++ < s.length() ? s.charAt(position++) : null;
    }
    public Character previous() {
        return position-- >= 0 ? s.charAt(position--) : null;
    }
}


Plalaya
lalayal
lyyyaay
Some other output or Compilation Error
Runtime Error

Doubts

Problems

Topic: Java Iterator

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App