MCQs
What is the output of this program?
import java.io.*;
public class filesinputoutput {
public static void main(String[] args) {
String obj = "abc";
byte b[] = obj.getBytes();
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
for (int i = 0; i < 2; ++ i) {
int c;
while((c = obj1.read()) != -1) {
if(i == 0) {
System.out.print(Character.toUpperCase((char)c));
obj2.write(1);
}
}
System.out.print(obj2);
}
}
}
None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa
append() method of class StringBuffer is used to concatenate the string representation
to the end of invoking string.
Output:
$ javac output.java
$ java output
Hello World
indexof('c')and lastIndexof('c')are defined function which are used to get the index
of first and last occurrence of
the character pointed by c in the given array.
Output:
$ javac output.java
$ java output
6 4 6 9
What is the output of this program?
import java.io.*;
class Chararrayinput {
public static void main(String[] args) {
String obj = "abcdef";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0,length,c,0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 3);
int i;
try {
while ((i = input1.read()) != -1) {
System.out.print((char)i);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
(Output will be different in your case)
What is the output of this program if input given is "Hello stop World"?
class Input_Output {
public static void main(String args[]) throws IOException {
string str;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
do {
str = (char) obj.readLine();
System.out.print(str);
} while(!str.equals("strong"));
}
}
"stop" will be able to terminate the do-while loop only when it occurs singly in a line.
"Hello stop World" does not terminate the loop.
Output:
$ javac Input_Output.java
$ java Input_Output
Hello stop World
reverse() method reverses all characters. It returns the reversed object on which
it was called.
obj.available() returns the number of bytes.
None.
PushbackReader class allows one or more characters to be returned to the input stream.
This allows looking ahead in input stream and performing action accordingly.