Java Multiple Choice Questions & Answers on String Class for Freshers

1. String in Java is a?

a) class
b) object
c) variable
d) character array
Answer: a
2. Which of these method of String class is used to obtain character at specified index?

a) char()
b) Charat()
c) charat()
d) charAt()
Answer: d
3. Which of these keywords is used to refer to member of base class from a sub class?

a) upper
b) super
c) this
d) None of the mentioned
Answer: b

Explanation: Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
4. Which of these method of String class can be used to test to strings for equality?

a) isequal()
b) isequals()
c) equal()
d) equals()
Answer: d
5. Which of the following statements are incorrect?

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.
Answer: b

Explanation: Strings in Java are immutable that is they can not be modified.
6. What is the output of this program?
  1.     class string_demo {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "I" + "like" + "Java";   
  5.             System.out.println(obj);     
  6.         }
  7.    }
a) I
b) like
c) Java
d) IlikeJava
Answer: d

Explanation: Java defines an operator +, it is used to concatenate strings.
7. What is the output of this program?
  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "I LIKE JAVA";   
  5.             System.out.println(obj.charAt(3));
  6.         } 
  7.     }
a) I
b) L
c) K
d) E
Answer: a

Explanation: charAt() is a method of class String which gives the character specified by the index. obj.charAt(3) gives 4th character i:e I.
8. What is the output of this program?
  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "I LIKE JAVA";   
  5.             System.out.println(obj.length());
  6.         }
  7.     }
a) 9
b) 10
c) 11
d) 12
Answer: c
9. What is the output of this program?
  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "hello";
  5.             String obj1 = "world";   
  6.             String obj2 = obj;
  7.             obj2 = " world";
  8.             System.out.println(obj + " " + obj2);
  9.         }
  10.     }
a) hello hello
b) world world
c) hello world
d) world hello
Answer: c
10. What is the output of this program?
  1.     class string_class {
  2.         public static void main(String args[])
  3.         {
  4.             String obj = "hello";
  5.             String obj1 = "world";   
  6.             String obj2 = "hello";
  7.             System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
  8.         }
  9.     }
a) false false
b) true true
c) true false
d) false true
Answer: d

Explanation: 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.

Related

C# Questions & Answers on String Formatting for Freshers

1. What will be the output of the given code snippet? static void Main(string[] args) { string s1 = "olleH"; string s2 = "olleh"; if (s1 == s2) Console.WriteLine("...

C# Questions & Answers on Introduction of String Formatting for Freshers

1. What are strings in C#? a) a sequence of charactersb) array of charactersc) objects of built in data typed) a reference type Answer: c Explanation: Generally, a string is defined as a sequence...

C# Questions & Answers on Accessor controls of class for Freshers

1. Which among these access specifiers should be used for main() method? a) privateb) publicc) protectedd) none of the mentioned Answer: b Explanation: main() method must be specified public as i...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item