Java String Handling Test | Core Java Topical Test (Series 2)
Finish Quiz
0 of 19 questions completed
Questions:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Information
Java String Handling Test Series 2 | Core Java Topical Test. Java String Handling Test Quiz 2, Core java Topical Tests have the best questions to make you understand the topic well. … This Test will cover String Handling from basic to advanced level..Free Java String Handling Quiz, Online Java, online Test Quiz 2. Java String Handling Test Quiz 2 Question and Answers 2019 is available for all users in Free . Now take Java online Test Quiz 2 in free from below…. Java String Handling Quiz 2 Free Mock Test 2019. Java String Handling Test Quiz 2 Question and Answers in PDF. The Java online mock test paper is free for all students. Java String Handling Test is very useful for exam preparation and getting for Rank. Java String Handling Test Quiz 2 Question and Answers in English. Java String Handling Mock test for topic via String Handling Mode. Here we are providing Java String Handling Mock Test in English Now Test your self for “Java String Handling Test in English” Exam by using below quiz…
This paper has 20 questions.
Time allowed is 25 minutes.
The Java String Handling Mock Test is Very helpful for all students. Now Scroll down below n click on “Start Quiz” or “Start Test” and Test yourself.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
0 of 19 questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 points, (0)
Average score |
|
Your score |
|
Categories
- Not categorized 0%
Pos. | Name | Entered on | Points | Result |
---|---|---|---|---|
Table is loading | ||||
No data available | ||||
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- Answered
- Review
-
Question 1 of 19
1. Question
What is output?
- public class Test15{
- public static void method(StringBuffer newSb){
- newSb.append(” Added”); //Line 3
- newSb= new StringBuffer(“Hai”); //Line 4
- }
- public static void main(String a[]){
- StringBuffer sb = new StringBuffer(“String Buffer”);
- method(sb);
- System.out.println(sb);
- }
- }
Choose the one below:
Correct
As StringBuffer is mutable class when newSb.append(” Added”) is executed, it appends the string value to actual object which is referred by newSb and sb reference variables. At Line 4 new StringBuffer object is created and refrenced by newSb but sb still refer to the actual object and the above code snippet results to “String Buffer Added“.
Incorrect
As StringBuffer is mutable class when newSb.append(” Added”) is executed, it appends the string value to actual object which is referred by newSb and sb reference variables. At Line 4 new StringBuffer object is created and refrenced by newSb but sb still refer to the actual object and the above code snippet results to “String Buffer Added“.
-
Question 2 of 19
2. Question
What will be the output?
- public class Test2{
- static void method(String str){
- System.out.println(“String”);
- }
- public static void main(String args[]){
- method(null);
- }
- }
Choose the one below:
Correct
static method is associate with class not with object. Hence the above code snippet results “String” as output.
Incorrect
static method is associate with class not with object. Hence the above code snippet results “String” as output.
-
Question 3 of 19
3. Question
Which of the following methods are methods of the String class?
Correct
replace() method is available in String class and it is returns a new string resulting from replacing all occurrences of old character in this string with new character.
Incorrect
replace() method is available in String class and it is returns a new string resulting from replacing all occurrences of old character in this string with new character.
-
Question 4 of 19
4. Question
Is String a wrapper class?
Correct
String is not wrapper class. In Java, Wrapper class is defined as class which converts and store equivalet primitive type. For example :- Integer, Character
Incorrect
String is not wrapper class. In Java, Wrapper class is defined as class which converts and store equivalet primitive type. For example :- Integer, Character
-
Question 5 of 19
5. Question
What will be the output?
System.out.println(“john” == new String(“john”));
System.out.println(“john”.equals(“john”));
System.out.println(“john” == “john”);
System.out.println(new String(“john”)==new String(“john”));Correct
“john” == new String(“john”) produces false as “john” is created in string pool and new String(“john”) at heap and == operator compares object reference.
“john”.equals(“john”) produces true as equals() method compares content of object.
“john” == “john” produces true as “john” is created in String pool and refer to same object.
new String(“john”)==new String(“john”) creates two string in heap and == checks reference of both objects and returns false.
Incorrect
“john” == new String(“john”) produces false as “john” is created in string pool and new String(“john”) at heap and == operator compares object reference.
“john”.equals(“john”) produces true as equals() method compares content of object.
“john” == “john” produces true as “john” is created in String pool and refer to same object.
new String(“john”)==new String(“john”) creates two string in heap and == checks reference of both objects and returns false.
-
Question 6 of 19
6. Question
Examine the following code
String str = null;
boolean bValue = str instanceof String;What value is placed in bValue?
Correct
instanceof is used to check if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface. In the above code snippet reference variable str is null. Hence the output is FALSE.
Incorrect
instanceof is used to check if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface. In the above code snippet reference variable str is null. Hence the output is FALSE.
-
Question 7 of 19
7. Question
What is the output of below code snippet?
if( new String(“ModernPathshala”).trim() == new String(“ModernPathshala”) )
System.out.println(“Equal”);
else
System.out.println(“Not Equal”);Correct
The above code snippet results as Not Equal as an output because reference of both string are not same.
Incorrect
The above code snippet results as Not Equal as an output because reference of both string are not same.
-
Question 8 of 19
8. Question
What will the following program print when run?
- public class Search {
- public static void main(String[] args) {
- String s = “Contentment!”;
- int middle = s.length()/2;
- String nt = s.substring(middle-1, middle+1);
- System.out.println(s.lastIndexOf(nt, middle));
- }
- }
Correct
s.length()/2 returns 6
s.substring(middle-1, middle+1) returns nt.
s.lastIndexOf(nt, middle) searches String value “nt” from middle i.e 5th index of String s.
Output:- 5Incorrect
s.length()/2 returns 6
s.substring(middle-1, middle+1) returns nt.
s.lastIndexOf(nt, middle) searches String value “nt” from middle i.e 5th index of String s.
Output:- 5 -
Question 9 of 19
9. Question
Which one of the following is not legal?
Correct
Option System.out.println(“st” + new String(’e’ + ’p’)); is invalid as ‘e’+’p’ results to an integer value and String class does not have any constructor to create String using integer value.
Incorrect
Option System.out.println(“st” + new String(’e’ + ’p’)); is invalid as ‘e’+’p’ results to an integer value and String class does not have any constructor to create String using integer value.
-
Question 10 of 19
10. Question
What will be the result of attempting to compile and run the following code?
- public class TestStringOperation {
- public static void main(String[] args) {
- String str1 = new String(“java”);
- str1.concat(” world”);
- StringBuffer strBuf1 = new StringBuffer(” magazine”);
- strBuf1.append(” article”);
- System.out.println(str1 + strBuf1);
- }
- }
Correct
concat() method returns a new String with the value of the String passed into the methodand append it to the end of the String used to invoke this method. So value of str1 remains “java” after executing str1.concat(” world”); line. Value of strBuf1 becomes “magazine article” after executing strBuf1.append(” article”); line. Hence System.out.println(str1 + strBuf1); results “java magazine article” as an output.
Incorrect
concat() method returns a new String with the value of the String passed into the methodand append it to the end of the String used to invoke this method. So value of str1 remains “java” after executing str1.concat(” world”); line. Value of strBuf1 becomes “magazine article” after executing strBuf1.append(” article”); line. Hence System.out.println(str1 + strBuf1); results “java magazine article” as an output.
-
Question 11 of 19
11. Question
What results would print from the following code snippet:
System.out.println(“ABCDE “.valueOf(98765));?Correct
valueOf() is static method defined in String class. Hence the above code compiles and runs successfully and prints 98765 as an output.
Incorrect
valueOf() is static method defined in String class. Hence the above code compiles and runs successfully and prints 98765 as an output.
-
Question 12 of 19
12. Question
Which one of the expressions will evaluate to true if preceded by the following code?
String a = “hello”;
String b = new String(a);
String c = a;
char[] d = { ‘h’, ‘e’, ‘l’, ‘l’, ‘o’ };Correct
a.equals(b) returns true as equals() method compares content of two string if same or not.
Incorrect
a.equals(b) returns true as equals() method compares content of two string if same or not.
-
Question 13 of 19
13. Question
What will be the result of attempting to compile and run the following program?
- public class MyClass {
- public static void main(String[] args) {
- StringBuffer sb = new StringBuffer(“have a nice day“);
- sb.setLength(6);
- System.out.println(sb);
- }
- }
Correct
The program will print “have a” when run.
Incorrect
The program will print “have a” when run.
-
Question 14 of 19
14. Question
What will be written to the standard output when the following program is run?
- import static java.lang.System.out;
- public class TestOutput {
- public static void main(String[] args) {
- String space = ” “;
- String composite = space + “windows” + space + space;
- composite.concat(“server”);
- String trimmed = composite.trim();
- out.println(trimmed.length());
- }
- }
Correct
concat() method returns a String with the value of the String passed into the methodand append it to the end of the String used to invoke this method. Hence the value of composite does not change.
Incorrect
concat() method returns a String with the value of the String passed into the methodand append it to the end of the String used to invoke this method. Hence the value of composite does not change.
-
Question 15 of 19
15. Question
What is the output for following code snippet?
- String string = “null”+null+“null”;
- System.out.println(string);
Correct
In java String class if String reference is null, it is converted to the string “null”.
Hence the above code returns nullnullnull as an output.
Incorrect
In java String class if String reference is null, it is converted to the string “null”.
Hence the above code returns nullnullnull as an output.
-
Question 16 of 19
16. Question
Given code snippet :-
String str1 = "Modern Pathshala"; String str2 = new String("Modern Pathshala"); String str3 = "Modern Pathshala";
How many objects are created?
Correct
In the above code snippet str1 is created in String Pool whereas String created using new operator i.e str2 is created in Heap. Further str3 refers to String Pool object which is already created.
Hence total 2 objects are created.
The pictorial representation of above object creation can be dsescribed as :-
Incorrect
In the above code snippet str1 is created in String Pool whereas String created using new operator i.e str2 is created in Heap. Further str3 refers to String Pool object which is already created.
Hence total 2 objects are created.
The pictorial representation of above object creation can be dsescribed as :-
-
Question 17 of 19
17. Question
What is the output of below code snippet?
System.out.println((“a” + “b” + “c”).intern() == “abc”); Correct
The Java programming language requires that identical string literals (that is, literals that contain the same sequence of characters) must refer to the same instance of class String. In addition, if the method String.intern is called on any string, the result is a reference to the same class instance that would be returned if that string appeared as a literal. Thus,
(“a” + “b” + “c”).intern() == “abc” must have the value true.
To derive a string literal, the Java virtual machine examines the sequence of characters given by the CONSTANT_String_info structure.
- If the method String.intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that given by the CONSTANT_String_info structure, then the result of string literal derivation is a reference to that same instance of class String.
- Otherwise, a new instance of class String is created containing the sequence of Unicode characters given by the CONSTANT_String_info structure; that class instance is the result of string literal derivation. Finally, the intern method of the new String instance is invoked.
Incorrect
The Java programming language requires that identical string literals (that is, literals that contain the same sequence of characters) must refer to the same instance of class String. In addition, if the method String.intern is called on any string, the result is a reference to the same class instance that would be returned if that string appeared as a literal. Thus,
(“a” + “b” + “c”).intern() == “abc” must have the value true.
To derive a string literal, the Java virtual machine examines the sequence of characters given by the CONSTANT_String_info structure.
- If the method String.intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that given by the CONSTANT_String_info structure, then the result of string literal derivation is a reference to that same instance of class String.
- Otherwise, a new instance of class String is created containing the sequence of Unicode characters given by the CONSTANT_String_info structure; that class instance is the result of string literal derivation. Finally, the intern method of the new String instance is invoked.
-
Question 18 of 19
18. Question
Given code snippet :-
String s;
int a;
s = “Foolish boy.”;
a = s.indexOf(“fool”);What is the output?
Correct
Incorrect
-
Question 19 of 19
19. Question
Identify error in below source code –
- public class JavaStringIsEmpty
- {
- public static void main(String args[])
- {
- String strl = “”;
- String str2 = null;
- String str2 = “Hello Java”;
- System.out.println(“Is String 1 empty? :” + strl.isEmpty());
- System.out.println(“Is String 3 empty? :” + str3.isEmpty());
- }
- }
Correct
Same variable str2 is declared twice which is not allowed.
Incorrect
Same variable str2 is declared twice which is not allowed.