0% found this document useful (0 votes)
4 views6 pages

Understanding String Operations in Java

The document contains a series of programming questions related to string manipulation in Java, focusing on string concatenation, comparison, and methods like indexOf and substring. Each question provides multiple-choice answers, testing the reader's understanding of string behavior and operations. The questions cover various aspects of string handling, including equality checks, output results, and method usage.

Uploaded by

See Wen Hui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Understanding String Operations in Java

The document contains a series of programming questions related to string manipulation in Java, focusing on string concatenation, comparison, and methods like indexOf and substring. Each question provides multiple-choice answers, testing the reader's understanding of string behavior and operations. The questions cover various aspects of string handling, including equality checks, output results, and method usage.

Uploaded by

See Wen Hui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

String Worksheet

1. What is the output for the following:


String s1 = “Bob”;
String s2 = “Linda”;
String s3 = s1+s2;
[Link](s3);
a. Bob
b. Linda
c. BobLinda
d. Bob Linda
e. error

2. What is the output for the following:


String s1 = "Bob";
String s2 = s1;
s2 = "Linda";
String s3 = s1+s2;
[Link](s3);
a. BobBob
b. LindaLinda
c. BobLinda
d. Bob
e. error

3. The statement s3=s1+s2; is equivalent to:


a. s3=s2+s1;
b. [Link](s1,s2);
c. s3 = [Link](s1);
d. s3 = [Link](s2);
e. none of these

4. What is the output for the following:


String s1 = "Bob";
String s2 = "Linda";
String s3 = new String("Bob");
if(s1==s3)
[Link]("They are equal");
else
[Link]("Not equal");
a. They are equal
b. Not equal
c. Error
5. What is the output for the following: ****This problem may be omitted for A students*****
String s1 = "Bob";
String s2 = "Linda";
String s3 = "Bob";
if(s1==s3)
[Link]("They are equal");
else
[Link]("Not equal");
a. They are equal
b. Not equal
c. Error

6. What is the output for the following:


String s1 = "Bob";
String s2 = "Linda";
String s3 = new String("Bob");
if([Link](s3))
[Link]("They are equal");
else
[Link]("Not equal");
a. They are equal
b. Not equal
c. Error

7. What is the output for the following:


String s1 = "Bob";
String s2 = "Linda";

if([Link](s2)==0)
[Link]("They are equal");
else if([Link](s2)<0)
[Link](s1+" is smaller");
else
[Link](s2+" is smaller");
a. They are equal
b. Bob is smaller
c. Linda is smaller
d. Error

8. Which of the following would assign s3 to “in” using the declarations in #7?
a. String s3 = [Link](2,2);
b. String s3 = [Link](1,2);
c. String s3 = [Link](1,3);
d. String s3 = [Link](1,2);
9. What is the output for the following:
String s1 = "bob";
String s2 = "Linda";

if([Link](s2)==0)
[Link]("They are equal");
else if([Link](s2)>0)
[Link](s1+" is bigger");
else
[Link](s2+" is bigger");
a. They are equal
b. bob is bigger
c. Linda is bigger
d. Error

10. What is the output for the following:


String s1 = "bob";
String s2 = "Linda";

if([Link] (s2)==0)
[Link]("They are equal");
else if([Link](s2)>0)
[Link](s1+" is bigger");
else
[Link](s2+" is bigger");
a. They are equal
b. bob is bigger
c. Linda is bigger
d. Error

11. What is the output of the following code if banana is entered?


s1 = // a String read from the user

for(int i = 0; i < [Link](); i++)


s2+=[Link]([Link]()-i-1);
[Link](s2);

a. banana
b. bananaananab
c. ananab
d. error

12. What is the value of x:


String s2="fast car";
int x = [Link](‘a’);

a. 1
b. 2
c. 7
d. 6
e. –1
13. What is the output of the following?
String s1 = "Result";
s1 = s1 + 1 + 2;
[Link]( s1 );

a. Result12
b. Result3
c. Syntax error
d. Result 1 2
e. R12

14. What is the output of the following?


String s1 = "Result";
s1 = 1 + 2 + s1;
[Link]( s1 );

a. 12Result
b. 3Result
c. Syntax error
d. 1 2 Result
e. 12R

15. What is the output of the following?


String s1 = "Result";
s1 = s1 + (1 + 2);
[Link]( s1 );

a. Result12
b. Result3
c. Syntax error
d. Result 1 2
e. R12

16. What is the output of the following?


String s1 = "Result";
s1 = s1 + 3 * 4;
[Link]( s1 );

a. Result34
b. Result12
c. Syntax error
d. Result 3 4
e. R34
17. What is the output of the following?
String s1 = "Result";
s1 = 3 * 4 + s1;
[Link]( s1 );

a. 34Result
b. 12Result
c. Syntax error
d. 3 4 Result
e. 34R

18. What is the output of the following?


String s1 = "mississippi";
s2 = "is";
int result = [Link](s2);
[Link]( result );

a. 0
b. -1
c. 11
d. 1
e. 4

19. <Fill in the blank>


What is the output of the following?
String s1 = "ohio";
String s2;
for(int i = 0; i < [Link]( ); i++)
{ s2 = [Link](i);
[Link](s2);
}

_________________________________________________________

20. What is the output of the following?


String s1 = "missouri";
String s2 = "mississippi";
[Link]( [Link]( [Link](1,4) );

a. 0
b. 1
c. 2
d. 3
e. -1
21. (From apcentral) Assume the following declarations have been made

private String s;
private int n;

public void changer(String x, int y)


{
x = x + "peace";
y = y * 2;
}

Assume s has the value "world" and n has the value 6. What are the values of s and n after the call
changer(s, n) ?

s n
a. world 6
b. worldpeace 6
c. world 12
d. worldpeace 12
e. peace 12

Common questions

Powered by AI

The comparison is based on lexicographical order. compareTo() returns 0 if the strings are equal, a negative integer if s1 precedes s2, and a positive integer if s1 follows s2 lexicographically. Case sensitivity is considered unless using compareToIgnoreCase().

Using '==' compares reference equality, meaning it checks if s1 and s3 point to the same memory location. '.equals()' compares the actual content of the strings. Therefore, if(s1==s3) may return false for strings with the same content but different memory references, while if(s1.equals(s3)) correctly checks for content equality and would return true .

The output is "Result3". The expression inside the parentheses is evaluated first, resulting in the integer 3, which is then concatenated to the string "Result" .

The output is "BobLinda". When concatenating strings using the '+' operator, the two string literals "Bob" and "Linda" are combined without any spaces or additional characters .

The values remain unchanged as "world" and 6. Java uses pass-by-value, meaning changes to the parameters x and y inside the method do not affect the original variables s and n outside the method. The variables are local to the method .

The output is "Linda is bigger". The compareTo() method is case-sensitive, and uppercase letters precede lowercase letters lexicographically. Thus, "Linda" comes before "bob" in a lexicographical comparison .

The output will be "BobLinda". s2 is reassigned to "Linda", leaving the original s1 as "Bob". Concatenating s1 and s2 results in "BobLinda" .

The potential outputs are: - "ohio" (from index 0) - "hio" (from index 1) - "io" (from index 2) - "o" (from index 3) Each iteration generates a substring starting from index i to the end of the string .

The output is "3Result". The operators in Java give precedence to addition before concatenation when numbers are involved. Hence, 1 + 2 is calculated first to get 3, which is then concatenated with the string "Result" .

The indexOf method is used to find the starting position of the substring "is" within the string "mississippi". It returns 1, as 'is' begins at index 1 in 'mississippi'. This method helps in substring searching and manipulation .

You might also like