Evaluating String Expressions

Practice Questions

Computer Science › Evaluating String Expressions

Questions
9
1

What is the output of the following code?

System.out.println((2*2) + " foo " + (3-1) + new Integer(5) + "3-2");

2

Consider the code below:

String s = "Logic!";

String s2 = "";

for(int i = 0; i < s.length(); i++) {

if(i != 0) {

s2 += " ";

}

s2 += s;

}

What is the value of s2 at the end of the code's execution?

3

Suppose you have the following lines of code:

Object integerObj = new Integer(2);

System.out.println((String) integerObj);

What will be the output resulting from running the lines of code?

4

What is the best way to print out a String variable into a sentence in Swift (iOS)?

5

string str = "Hello"

char newvar = str\[4\];

What is the value of newvar?

6

What is the output of the following code?

System.out.println( (1+2) + (3-1) + new Integer(5));

7

String s = "abcdefghijklmnop";

String s2 = "";

for(int i = 0; i < s.length(); i += 2) {

s2 += s.charAt(i+1);

s2 += s.charAt(i);

}

What is the value of s2 at the end of the code above?

8

Which of the following will cause the Java compiler to throw an error?

9

Consider the following code:

Q2

What is the output of the method call mystery("Green eggs and ham")?

Return to subject