Sail E0 Webinar

MCQs

Total Questions : 51 | Page 3 of 6 pages
Question 21.


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);
}
}
}
  1.    AaBaCa
  2.    ABCaaa
  3.    AaaBaaCaa
  4.    AaBaaCaaa
 Discuss Question
Answer: Option D. -> AaBaaCaaa

None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa


Question 22.


What is the output of this program?


class output {
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
StringBuffer c1 = new StringBuffer(" World");
c.append(c1);
System.out.println(c);
}
}
  1.    Hello
  2.    World
  3.    Helloworld
  4.    Hello World
 Discuss Question
Answer: Option D. -> Hello World

 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


Question 23.


What is the output of this program?


class output {
public static void main(String args[])
{
String a="hello i love java";
System.out.println(indexof('i')+" "+indexof('o')+" "+lastIndexof('i')+" "+lastIndexof('o') ));
}
}
  1.    6 4 6 9
  2.    5 4 5 9
  3.    7 8 8 9
  4.    4 3 6 9
 Discuss Question
Answer: Option A. -> 6 4 6 9

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



Question 24.


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();
}
}
}
  1.    abc
  2.    abcd
  3.    abcde
  4.    abcdef
 Discuss Question
Answer: Option D. -> abcdef


Question 25.


What is the output of this program?


import java.io.*;
class filesinputoutput {
public static void main(String args[]) {
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}


Note: inputoutput.java is stored in the disk.


  1.    true
  2.    false
  3.    prints number of bytes in file
  4.    prints number of characters in the file
 Discuss Question
Answer: Option C. -> prints number of bytes in file

obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
(Output will be different in your case)


Question 26.


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"));
}
}
  1.    Hello
  2.    Hello stop
  3.    World
  4.    Hello stop World
 Discuss Question
Answer: Option D. -> Hello stop World

"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


Question 27.

Which of the following statement is correct?


  1.    reverse() method reverses all characters.
  2.    reverseall() method reverses all characters.
  3.    replace() method replaces first occurrence of a character in invoking string with another character.
  4.    replace() method replaces last occurrence of a character in invoking string with another character.
 Discuss Question
Answer: Option A. -> reverse() method reverses all characters.

reverse() method reverses all characters. It returns the reversed object on which 

it was called.


Question 28.


What is the output of this program?


import java.io.*;
class filesinputoutput {
public static void main(String args[]) {
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}


Note: inputoutput.java is stored in the disk.


  1.    true
  2.    false
  3.    prints number of bytes in file
  4.    prints number of characters in the file
 Discuss Question
Answer: Option C. -> prints number of bytes in file

obj.available() returns the number of bytes.


Question 29.

Which of these methods is used to write() into a file?


  1.    put()
  2.    putFile()
  3.    write()
  4.    writeFile()
 Discuss Question
Answer: Option C. -> write()

None.


Question 30.

Which of these classes can return more than one character to be returned to input stream?


  1.    BufferedReader
  2.    Bufferedwriter
  3.    PushbachReader
  4.    CharArrayReader
 Discuss Question
Answer: Option C. -> PushbachReader

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.


Latest Videos

Latest Test Papers