Java MCQs on String class of Java Programming Language.
1. String in Java is a? Answer: a 2. Which of these method of String class is used to obtain character at specified index? Answer: d 3. Which of these keywords is used to refer to member of base class from a subclass? Answer: b 4. Which of these method of String class can be used to test to strings for equality? Answer: d 5. Which of the following statements are incorrect? Answer: b 6. What will be the output of the following Java program? a) I 7. What will be the output of the following Java program? a) I 8. What will be the output of the following Java program? a) 9 9. What will be the output of the following Java program? a) hello hello 10. What will be the output of the following Java program? a) false false
a) class
b) object
c) variable
d) character array
Clarification: None.
a) char()
b) Charat()
c) charat()
d) charAt()
Clarification: None.
a) upper
b) super
c) this
d) none of the mentioned
Clarification: Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
a) isequal()
b) isequals()
c) equal()
d) equals()
Clarification: None.
a) String is a class
b) Strings in java are mutable
c) Every string is an object of class String
d) Java defines a peer class of String, called StringBuffer, which allows string to be altered
Clarification: Strings in Java are immutable that is they can not be modified.
class string_demo
{
public static void main(String args[])
{
String obj = "I" + "like" + "Java";
System.out.println(obj);
}
}
b) like
c) Java
d) IlikeJava
Clarification: Java defines an operator +, it is used to concatenate strings.
output:
$ javac string_demo.java
$ java string_demo
IlikeJava
class string_class
{
public static void main(String args[])
{
String obj = "I LIKE JAVA";
System.out.println(obj.charAt(3));
}
}
b) L
c) K
d) E
Clarification: charAt() is a method of class String which gives the character specified by the index. obj.charAt(3) gives 4th character i:e I.
output:
$ javac string_class.java
$ java string_class
I
class string_class
{
public static void main(String args[])
{
String obj = "I LIKE JAVA";
System.out.println(obj.length());
}
}
b) 10
c) 11
d) 12
Clarification: None.
output:
$ javac string_class.java
$ java string_class
11
class string_class
{
public static void main(String args[])
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
obj2 = " world";
System.out.println(obj + " " + obj2);
}
}
b) world world
c) hello world
d) world hello
Clarification: None.
output:
$ javac string_class.java
$ java string_class
hello world
class string_class
{
public static void main(String args[])
{
String obj = "hello";
String obj1 = "world";
String obj2 = "hello";
System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
}
}
b) true true
c) true false
d) false true
Clarification: equals() is method of class String, it is used to check equality of two String objects, if they are equal, true is retuned else false.
output:
$ javac string_class.java
$ java string_class
false true