Sample questions
Question 1Medium
What is the value returned by function compareTo() if the invoking string is less than the string compared?
- zero
- value less than zero
- value greater than zero
- None of the mentioned
Answer: B. value less than zero
Explanation
compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.
Step-by-step solution
- compareTo() returns negative number if invoking string is less than compared string.
- Negative values are less than zero, so result is value less than zero.
- This happens because lexicographical order determines the return value.
- The correct answer matches this behavior of compareTo().
- Final answer: value less than zero.
Why the other options are wrong
- A. A zero result means the strings are equal, but the question asks about the invoking string being less than the other.
- C. A value greater than zero is returned when the invoking string is greater, not when it is less.
- D. None is wrong because compareTo() returns a value less than zero in this case.
Tip: Remember: compareTo() returns negative if first string is smaller.
Question 2Medium
What is the output of this program?
class output {
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
StringBuffer c1 = new StringBuffer(" World");
c.append(c1);
System.out.println(c);
}
}
- Hello
- World
- Helloworld
- Hello World
Answer: D. Hello World
Explanation
append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Output:
$ javac output.java
$ java output
Hello World
Step-by-step solution
- ```java
StringBuffer c = new StringBuffer("Hello");
```
creates a buffer with "Hello".
- ```java
StringBuffer c1 = new StringBuffer(" World");
```
creates a buffer with " World".
- ```java
c.append(c1);
```
adds " World" to the end of "Hello" making it "Hello World".
- ```java
System.out.println(c);
```
prints the concatenated string "Hello World".
- The output is Hello World.
Why the other options are wrong
- A. Hello alone is wrong because append added the text from c1 to the end of c.
- B. World alone is wrong because c1 is only appended to c, and c itself is printed.
- C. Helloworld has no space, but c1 starts with a space, so the output is Hello World.
Tip: Remember that append() adds content as-is, including spaces and special characters.
Question 3Medium
What is the output of this program?
class string_demo {
public static void main(String args[])
{
String obj = "I" + "like" + "Java";
System.out.println(obj);
}
}
- I
- like
- Java
- IlikeJava
Answer: D. IlikeJava
Explanation
The + operator joins the string pieces I, like and Java, giving IlikeJava.
Step-by-step solution
- The + operator joins strings, so I + like + Java is joined in order.
- No spaces are added, so the result is IlikeJava.
- Println prints IlikeJava, which is the correct answer, option D.
Why the other options are wrong
- A. Only the first string is printed, but + joins all three.
- B. Only the second string would be printed, but + joins all three.
- C. Only the third string would be printed, but + joins all three.
Tip: String + joins the text in order, and no spaces appear unless you add them.
Question 4Medium
What is the output of this program?
class output {
public static void main(String args[])
{ String s = "Hello World";
int i = s.indexOf('o');
int j = s.lastIndexOf('l');
System.out.print(i + " " + j);
}
}
- 4 8
- 5 9
- 4 9
- 5 8
Answer: C. 4 9
Explanation
indexOf() method returns the index of first occurrence of the character where as lastIndexOf() returns the index of last occurrence of the character.
output:
$ javac output.java
$ java output
4 9
Step-by-step solution
- indexOf('o') finds the first o in Hello World at index 4 (H is 0, e is 1, l is 2, l is 3, o is 4).
- lastIndexOf('l') finds the last l at index 9 (W is 6, o is 7, r is 8, l is 9).
- The program prints 4, then a space, then 9.
- The output is 4 9, which is option C.
Why the other options are wrong
- A. The last l is at index 9, not 8; index 8 is the letter r.
- B. The first o is at index 4, not 5; index 5 is the space.
- D. Both numbers are wrong: the first o is at index 4 and the last l is at index 9.
Tip: Indexes start at 0 and count spaces; indexOf finds the first match and lastIndexOf the last.
Question 5Medium
What is the output of this program?
class output {
public static void main(String args[])
{
String s1 = "Hello i love java";
String s2 = new String(s1);
System.out.println((s1 == s2) + " " + s1.equals(s2));
}
}
- true true
- false false
- true false
- false true
Answer: D. false true
Explanation
The == operator compares two object references to see whether they refer to the same instance, where as equals() compares the content of the two objects.
Output:
$ javac output.java
$ java output false true
Step-by-step solution
S1 is a string literal stored in the string pool.S2 is created using new String(s1), so it's a separate object in heap memory.s1 == s2 compares references, which are different, so result is false.s1.equals(s2) compares content, which is same, so result is true.- Final output is false true.
Why the other options are wrong
- A. The == check is false here, since new String creates a separate object.
- B. equals compares content, which matches, so it is true, not false.
- C. The == result is false, not true, because the strings are separate objects.
Tip: Remember: == checks references, equals() checks content for Strings.
Question 6Medium
What will s2 contain after following lines of code?
String s1 = "one";
String s2 = s1.concat("two")
- one
- two
- onetwo
- twoone
Answer: C. onetwo
Explanation
The concat() method combines the string 'two' to the end of s1, resulting in 'onetwo'.
Step-by-step solution
- S1 is initialized as 'one'.
s2 = s1.concat('two') appends 'two' to s1.- Concatenation of 'one' and 'two' gives 'onetwo'.
- So s2 contains the value 'onetwo'.
- Final answer: onetwo.
Why the other options are wrong
- A. s1 is still one, but s2 is set to s1.concat(two), which adds two to the end, so it is not just one.
- B. s2 is not just two, because concat adds two to the end of s1, giving onetwo.
- D. concat adds text to the end of s1, so it gives onetwo, not twoone.
Tip: Remember that concat() adds to the end of the string.
Question 7Medium
What is the output of this program?
class String_demo {
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68};
String s = new String(ascii, 1, 3);
System.out.println(s);
}
}
- ABC
- BCD
- CDA
- ABCD
Answer: B. BCD
Explanation
This constructor reads 3 values from the array, starting at index 1: 66, 67 and 68. These are the letters B, C and D.
Step-by-step solution
- The
ascii array contains values 65, 66, 67, 68 which are ASCII codes for A, B, C, D. String constructor String(ascii, 1, 3) starts from index 1 and takes 3 characters: B, C, D.- So the string
s is formed as 'BCD'. - ```java
System.out.println(s)
```
prints the value of
s which is BCD. - Final answer: BCD.
Why the other options are wrong
- A. ABC starts at index 0, but the constructor starts at index 1 with offset 1, so A is skipped.
- C. CDA wraps around past the end of the array, but the constructor only reads three values in order from index 1.
- D. ABCD uses all four values, but the count of 3 only takes three characters starting at index 1.
Tip: Remember the String(byte[], int, int) constructor takes start index and length.
Question 8Medium
What is the string contained in s after following lines of code?
StringBuffer s = new StringBuffer("Hello");
s.deleteCharAt(0);
- Hell
- ello
- Hel
- llo
Answer: B. ello
Explanation
deleteCharAt() method deletes the character at the specified index location and returns the resulting StringBuffer object.
Step-by-step solution
- The first line is missing the = sign; read it as s = new StringBuffer("Hello").
- deleteCharAt(0) removes the character at index 0, which is H.
- The string left is "ello". Option B is the answer.
Why the other options are wrong
- A. Hell would need the last character removed, not the first.
- C. Hel would need the last two characters removed.
- D. Llo would need the first two characters removed, but only one is removed.
Tip: deleteCharAt(index) removes one character at that index, and indexes start at 0.
Question 9Medium
Which of these keywords is used to refer to member of base class from a sub class?
- upper
- super
- this
- None of the mentioned
Answer: B. super
Explanation
Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
Step-by-step solution
- In Java, 'super' is used to access members of the parent class from the child class.
- It helps in calling the parent class constructor or accessing its methods and variables.
- Other keywords like 'this' refer to the current object, not the base class.
- None of the mentioned is incorrect as 'super' is the correct keyword.
- Thus, the answer is super.
Why the other options are wrong
- A. upper is not a Java keyword, so it cannot refer to members of the base class.
- C. this refers to the current class object, not to members inherited from the base class.
- D. None is wrong because super is the keyword used to reach base class members.
Tip: Remember 'super' for accessing base class members in inheritance.
Question 10Medium
What is the output of this program?
class output {
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
System.out.println(c.length());
}
}
- 4
- 5
- 6
- 7
Answer: B. 5
Explanation
The length() method of StringBuffer returns the number of characters in Hello, which is 5.
Step-by-step solution
- ```java
StringBuffer c = new StringBuffer("Hello");
```
creates a buffer with "Hello".
c.length() returns the number of characters in "Hello", which is 5.- ```java
System.out.println(5);
```
prints the value 5 to the console.
Why the other options are wrong
- A. Hello has five letters, so length() returns 5, not 4.
- C. Hello has five characters and no extra one is counted, so the answer is not 6.
- D. 7 would need two more characters; the string Hello only has five.
Tip: Remember, length() gives character count, not byte size.
Practise all 31 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →