Java Multiple Choice Questions & Answers on Writing Console Output for Freshers

https://www.computersprofessor.com/2018/01/java-multiple-choice-questions-answers_8.html
1. Which of these class contains the methods print() & println()?
a) System
b) System.out
c) BUfferedOutputStream
d) PrintStream
Answer: d
Explanation: print() and println() are defined under the class PrintStream, System.out is the byte stream used by these methods .
2. Which of these methods can be used to writing console output?
a) print()
b) println()
c) write()
d) All of the mentioned
Answer: d
3. Which of these class is used to create an object whose character sequence is mutable?
a) String()
b) StringBuffer()
c) Both of the mentioned
d) None of the mentioned
Answer: b
Explanation: StringBuffer represents growable and writeable character sequence.
4. Which of these method of class StringBuffer is used to reverse sequence of characters?
a) reverse()
b) reverseall()
c) Reverse()
d) reverseAll()
Answer: a
Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
5. Which of these classes are used by character streams output operations?
a) InputStream
b) Writer
c) ReadStream
d) InputOutputStream
Answer: b
Explanation: Character streams uses Writer and Reader classes for input & output operations.
6. Which of the following statement is correct?
a) reverse() method reverses all characters.
b) reverseall() method reverses all characters.
c) replace() method replaces first occurrence of a character in invoking string with another character.
d) replace() method replaces last occurrence of a character in invoking string with another character.
Answer: a
Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
7. 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') ));
}
}
a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9
Answer:a
Explantion: indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
8. What is the output of this program?
class output {
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
StringBuffer s2 = s1.reverse();
System.out.println(s2);
}
}
a) Hello
b) olleH
c) HelloolleH
d) olleHHello
b) olleH
c) HelloolleH
d) olleHHello
Answer: b
Explanation: reverse() method reverses all characters. It returns the reversed object on which it was called.
9. What is the output of this program?
class output {
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello World");
s1.insert(6 , "Good ");
System.out.println(s1);
}
}
a) HelloGoodWorld
b) HellGoodoWorld
c) HellGood oWorld
d) Hello Good World
b) HellGoodoWorld
c) HellGood oWorld
d) Hello Good World
Answer: d
Explanation: 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”.
10. 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
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:a
Explanation:Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i]) are the function of library java.lang
they are used to find weather the given character is of specified type or not. They return true or false i:e Boolean variable.