MCQs
java.io provides support for input and output operations.
What is the output of this program?
class output {
public static void main(String args[])
{
char c[]={'a','1','b',' ','A','0'];
for (int i = 0; i < 5; ++i) {
if(Character.isDigit(c[i]))
System.out.println(c[i]" is a digit");
if(Character.isWhitespace(c[i]))
System.out.println(c[i]" is a Whitespace character");
if(Character.isUpperCase(c[i]))
System.out.println(c[i]" is an Upper case Letter");
if(Character.isUpperCase(c[i]))
System.out.println(c[i]" is a lower case Letter");
i = i + 3;
}
}
}
a) a is a lower case Letter
is White space character
b) b is a lower case Letter
is White space characte
c) a is a lower case Letter
A is a upper case Letter
d) a is a lower case Letter
0 is a digit
Answer: Option AExplanation:
What is the output of this program?
import java.io.*;
class Chararrayinput {
public static void main(String[] args) {
String obj = "abcdefgh";
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, 1, 4);
int i;
int j;
try {
while((i = input1.read()) == (j = input2.read())) {
System.out.print((char)i);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
No output is printed. CharArrayReader object input1 contains string "abcdefgh" whereas
object input2 contains string "bcde", when while((i=input1.read())==(j=input2.read())) is
executed the starting character of each object is compared since they are unequal
control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
What is the output of this program?
import java.io.*;
class Chararrayinput {
public static void main(String[] args) {
String obj = "abcdefgh";
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, 1, 4);
int i;
int j;
try {
while ((i = input1.read()) == (j = input2.read())) {
System.out.print((char)i);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
No output is printed. CharArrayReader object input1 contains string “abcdefgh†whereas object
input2 contains string “bcdeâ€, when while((i=input1.read())==(j=input2.read())) is executed the
starting character of each object is compared since they are unequal control comes out of loop
and nothing is printed on the screen.
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 = input2.read()) != -1) {
System.out.print((char)i);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
None.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
abc
The insert() method inserts one string into another. It is overloaded to accept values of all
simple types, plus String and Objects. Sting is inserted into invoking object at specified
position. "Good " is inserted in "Hello World" T index 6 giving "Hello Good World".
output:
$ javac output.java
$ java output
Hello Good World
What is the output of this program if input given is "abc'def/'egh"?
class Input_Output {
public static void main(String args[]) throws IOException {
char c;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
do {
c = (char) obj.read();
System.out.print(c);
} while(c != '\'');
}
}
`setminus`' is used for single quotes that is for representing ' .
Output:
$ javac Input_Output.java
$ java Input_Output
abc'
None.
Output:
$ javac output.java
$ java output
Hxllo
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 = input2.read()) != -1) {
System.out.print((char)i);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
reverse() method reverses all characters. It returns the reversed object on which it was called.
Output:
$ javac output.java
$ java output
olleH