Programming MCQs

Top 20 MCQ Questions On Arrays And Strings In Java

Pinterest LinkedIn Tumblr
Here are the collections of top 20 MCQ questions on Arrays and Strings in Java, which includes MCQ questions on different types of arrays like one dimensional arrays and two dimensional arrays, declaring the array, creating memory locations and putting values into the memory locations. It also includes MCQs on different types of string methods like toSting(), ValueOf(), CharAt(), getChars(), getBytes(), toCharArray(), indexOf() and lastindexOf().

1. Which of the following declaration of the array contains the error?

A) int x[ ]= int[10];
B) int [ ] y=new int[5];
C) float d[ ]= {1,2,3};
D) int a[ ] = {1, 2,3; int b[ ]; b=a;

2. What will be the content of array variable table after executing the following code?

A) 0    0    0                          B)  1    0    0
     0    0    0                                1    1    0
     0    0    0                                1    1    1

C) 0    0    1                         D)  1    0    0
     0    1    0                               0    1    0
     1    0    0                               0    0    1

3. Consider the following code.
 int number[ ]=new int[5];
After execution of this statement which of the following are true.
i) number [0] is undefined       ii) number[5] is undefined
iii) number [4] is null              iv) number.length() is 5
A) i and ii only
B) ii and iii only
C) ii and iv only
D) i and iv only

4. There is a companion class to string called ………………….., whose objects contain strings that can be modified after they are created.
A) stringBuffer
B) stringClass
C) toString( )
D) stringChar

5. Which of the following classes are available in the java.lang package?
i) Stack                                 ii) Object
iii) Math                              iv) Random
A) i and ii only
B) ii and iii only
C) ii and iv only
D) i and iv only

6. Consider the following code,
string s=new String(); will create an instance of string with
A) at least one character
B) a default character
C) no characters in it
D) number of characters in it

7. …………………. returns a sting that contains the human-readable equivalent of the value with which it is called.
A) toValue()
B) value()
C) valueOf()
D) getValue()

8. …………………… method simply return a string that appropriately describes an object of your class.
A) getChars()
B) toString()
C) toCharArray()
D) getString()

9. To extract a single character from a sting, you can refer directly to an individual character via the …………………… method.
A) charAt()
B) getChars()
C) getCharAt()
D) getAt()

10. If you need to extract more than one character at a time, you can use the …………………………. .
A) getBytes()
B) charAt()
C) getChars()
D) getString()

11. If S1 and S2 are two strings, which of the following statements or expressions are correct.
i) String S3=S1+S2;                         ii) String S3=S1-S2;
iii) S1<=S2;                                     iv) S1.compareTo(S2);
A) i and ii only
B) ii and iii only
C) ii and iv only
D) i and iv only

12. For the following expression,
      String S=new String(“abc”);
      Which of the following calls are valid?
i) S.trim()                                    ii) S.replace(‘a’, ‘A’)
iii) S.Substring(3)                       iv)  S.toUpperCase()
A) i and ii only
B) i, ii and iii only
C) ii, iii and iv only
D) All i, ii, iii and iv

13. What will be the output of the following code?
String S=new String();
System.Out.Println (“S = ” +S);
A) null
B) error
C) =S
D) S=

14. …………………………….. is most useful when you are exporting a string value into an environment that does not support 16-bit unicode characters.
A) getChars()
B) getString()
C) getValue()
D) getBytes()

15. You can use …………………… method, if you want to convert all the characters in a string object into a character array.
A) toCharArray()
B) toString()
C) toChar()
D) toArray()

16. Which of the following method call gives the position of ‘X’ that occurs after n^th position in the string S1.
A) S1.indexOf(‘X’)
B) S1.indexOf(‘X’ , n)
C) S1.index(‘X’, 1)
D) S1. index(‘X’ , n)

17. Which of the following method call gives the position of the first occurrence of ‘X’ in the string S1.
A) S1.indexOf(‘X’)
B) S1.indexOf(‘X’ , n)
C) S1.index(‘X’, 1)
D) S1. index(‘X’ , n)

18. The ……………………….. method compares a specific region inside a string with another specific region in another string.
A) getRegion()
B) getMatches()
C) regionMatches()
D) insideRegion()

19. The …………………… compares the characters inside a sting object whereas ……………….. compares two objects references to see whether they refer to the same instance.
A) = = operator , equals( ) method
B) equals( ) method , = = operator
C) equals( ) method, = = = operator
D) = = operator, compare( ) method

20. ………………….. method return the index at which the character or sub-string was found or -1 on failure.
i) indexOf()                       ii) lastindexOf()
iii) index()                         iv) firstindexOf()
A) i and ii only
B) ii and iii only
C) ii and iv only
D) i and iv only

Answers

1. A) int x[ ]= int[10];
2. D)  1    0    0
          0    1    0
          0    0    1
3. C) ii and iv only
4. A) stringBuffer
5. B) ii and iii only
6. C) no characters in it
7. C) valueOf()
8. B) toString()
9. A) charAt()
10. C) getChars()
11. D) i and iv only
12. D) All i, ii, iii and iv
13. D) S=
14. D) getBytes()
15. A) toCharArray()
16. B) S1.indexOf(‘X’ , n)
17. A) S1.indexOf(‘X’)
18. C) regionMatches()
19. B) equals( ) method , = = operator
20. A) i and ii only

Read Next: Top 20 MCQ Questions On Strings And Vectors In Java
Read More:  Java MCQ Questions

Please follow and like us:
RSS
Follow by Email
X (Twitter)
Visit Us
Follow Me
YouTube
YouTube
LinkedIn
linkedIn | Top 20 MCQ Questions On Arrays And Strings In Java
Share

Author

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Comments are closed.

RSS
Follow by Email
X (Twitter)
Visit Us
Follow Me
YouTube
YouTube
LinkedIn
Share