Java Multiple Choice Questions & Answers on String Handling Basics for Freshers

1. Which of these class is superclass of String and StringBuffer class?

a) java.util
b) java.lang
c) ArrayList
d) None of the mentioned
Answer: b
2. Which of these operators can be used to concatenate two or more String objects?

a) +
b) +=
c) &
d) ||
Answer: a

Explanation: operator + is used to concatenate strings, Example String s = “i ” + “like ” + “java”; String s contains “I like java”.
3. Which of these method of class String is used to obtain length of String object?

a) get()
b) Sizeof()
c) lengthof()
d) length()
Answer: d

Explanation: Method length() of string class is used to get the length of the object which invoked method length().
4. Which of these method of class String is used to extract a single character from a String object?

a) CHARAT()
b) chatat()
c) charAt()
d) ChatAt()
Answer: c
5. Which of these constructors is used to create an empty String object?

a) String()
b) String(void)
c) String(0)
d) None of the mentioned
Answer: a
6. Which of these is an oncorrect statement?

a) String objects are immutable, they cannot be changed.
b) String object can point to some other reference of String variable.
c) StringBuffer class is used to store string in a buffer for later use.
d) None of the mentioned
Answer: c

Explanation: StringBuffer class is used to create strings that can be modified after they are created.
7. What is the output of this program?
  1.     class String_demo {
  2.         public static void main(String args[])
  3.         {
  4.             char chars[] = {'a', 'b', 'c'};
  5.             String s = new String(chars);
  6.             System.out.println(s);
  7.         }
  8.    }
a) a
b) b
c) c
d) abc
Answer: d

Explanation: String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “abc”.
8. What is the output of this program?
  1.     class String_demo {
  2.         public static void main(String args[])
  3.         {
  4.             int ascii[] = { 65, 66, 67, 68};
  5.             String s = new String(ascii, 1, 3);
  6.             System.out.println(s);
  7.         }
  8.    }
a) ABC
b) BCD
c) CDA
d) ABCD
Answer: b

Explanation: ascii is an array of integers which contains ascii codes of Characters A, B, C, D. String(ascii, 1, 3) is an constructor which initializes s with Characters corresponding to ascii codes stored in array ascii, starting position being given by 1 & ending position by 3, Thus s stores BCD.
9. What is the output of this program?
  1.     class String_demo {
  2.         public static void main(String args[])
  3.         {
  4.             char chars[] = {'a', 'b', 'c'};
  5.             String s = new String(chars);
  6.             String s1 = "abcd";
  7.             int len1 = s1.length();
  8.             int len2 = s.length();
  9.             System.out.println(len1 + " " + len2);
  10.         }
  11.    }
a) 3 0
b) 0 3
c) 3 4
d) 4 3
Answer: d
10. What is the output of this program?
  1.    class A {
  2.         int i;
  3. 	int j;
  4.         A() {
  5.             i = 1;
  6.             j = 2;
  7.         }
  8.    }
  9.    class Output {
  10.         public static void main(String args[])
  11.         {
  12.              A obj1 = new A();
  13. 	     System.out.print(obj1.toString());
  14.         }
  15.    }
a) true
b) false
c) String associated with obj1
d) Compilation Error
Answer: c

Explanation: toString() is method of class Object, since it is superclass of every class, every object has this method. toString() returns the string associated with the calling object.

Related

C Programming Multiple Choice Questions and Answers on Formatted Output Function printf() for Freshers

1. What is the output of this C code? #include int main() { int i = 10, j = 2; printf("%d\n", printf("%d %d ", i, j)); } a) Compile time error...

C# Multiple Choice Questions & Answers on Integer Data Types for Freshers

1. How many Bytes are stored by ‘Long’ Datatype in C# .net? a) 8b) 4c) 2d) 1 Answer: a Explanation : ‘Long’ is the datatype keyword used for storing data of unlimited length so by definition its ...

CSS Multiple Choice Questions & Answers on CSS3 Animation with Responsive Web Design for Freshers

1. Which of the following css property should be used to make a responsive image? a) floatb) max-widthc) margin-rightd) all of the mentioned Answer: d 2. What should be written in the blank of th...

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